generated from janklab/MatlabProjectTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_project_from_template.m
300 lines (259 loc) · 7.64 KB
/
init_project_from_template.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
function init_project_from_template(varargin)
% Initialize this project from the MatlabProjectTemplate template
%#ok<*STRNU>
%#ok<*AGROW>
%#ok<*DEFNU>
% Parse options
doDev = false;
for i = 1:numel(varargin)
switch varargin{i}
case {'-d', '--dev'}
doDev = true;
otherwise
error('Invalid option: %s', varargin{i});
end
end
% Init empty variables just so we can use tab-completion while working on this
% script
PROJECT = [];
PROJECT_MATLAB_VERSION = [];
PACKAGE = [];
GHUSER = [];
PROJECT_EMAIL = [];
DOCTOOL = [];
PROJECT_AUTHOR = [];
PROJECT_SUMMARY = [];
PROJECT_DESCRIPTION = [];
AUTHOR_HOMEPAGE = [];
% Get going
reporoot = string(fileparts(mfilename('fullpath')));
RAII.cd = withcd(reporoot);
info = project_settings;
f = fieldnames(info);
for i = 1:numel(f)
if isstring(info.(f{i})) || ischar(info.(f{i}))
info.(f{i}) = char(info.(f{i}));
else
error('Bad value for %s: Must be a string', f{i});
end
end
% Validate project options
validFields = {'PROJECT', 'PROJECT_MATLAB_VERSION', 'PACKAGE', 'GHUSER', ...
'PROJECT_EMAIL', 'DOCTOOL', 'PROJECT_AUTHOR', 'PROJECT_SUMMARY', ...
'PROJECT_DESCRIPTION', 'AUTHOR_HOMEPAGE'};
missingFields = setdiff(validFields, fieldnames(info));
if ~isempty(missingFields)
error(['Missing fields in project_settings.m: %s.\n' ...
'You didn''t delete stuff from that file, did you?'], strjoin(missingFields, ', '));
end
badFields = setdiff(fieldnames(info), validFields);
if ~isempty(badFields)
fprintf('WARNING: Ignoring unrecognized fields in project_settings.m: %s\n', ...
strjoin(badFields, ', '));
end
copyStructFieldsIntoWorkspace(info);
docSiteDir = "doc-src-" + DOCTOOL;
if ~isfolder(docSiteDir)
error(['Invalid choice for DOCTOOL: %s\nValid choices are: ' ...
'jekyll, mkdocs, gh-pages, gh-pages-raw'], DOCTOOL);
end
badHtmlChars = '<>&';
htmlFields = ["PROJECT_SUMMARY", "PROJECT_DESCRIPTION", "AUTHOR_HOMEPAGE"];
for i = 1:numel(htmlFields)
if any(ismember(info.(htmlFields{i}), badHtmlChars))
error("%s may not contain %s", htmlFields{i}, badHtmlChars);
end
end
PACKAGE_CAP = [upper(PACKAGE(1)) PACKAGE(2:end)];
% Do work!
echo
echo("Initializing project " + PROJECT);
echo
echo("Generating a GUID for your project's Toolbox...")
PROJECT_GUID = char(toString(java.util.UUID.randomUUID));
% Munge the source code and documentation
mv("Mcode/+mypackage/+internal/MypackageBase.m", "Mcode/+mypackage/+internal/"+PACKAGE_CAP+"Base.m");
mv("Mcode/+mypackage/+internal/MypackageBaseHandle.m", "Mcode/+mypackage/+internal/"+PACKAGE_CAP+"BaseHandle.m");
mv("Mcode/+mypackage", "Mcode/+"+PACKAGE)
mv("src/java/myproject-java/src/main/java/com/example/mypackage", "src/java/myproject-java/src/main/java/com/example/"+PACKAGE)
mv("src/java/myproject-java", "src/java/"+PROJECT+"-java")
fileGlobsToMunge = regexp(".gitignore Makefile *.md */*.md */*.adoc */*.yml " ...
+ "myproject.prj.in */*.m */*/*.m */*/*/*.m */*/*/*/*.m src/java/*/*.xml " ...
+ "src/java/*/*/*/*/*/*/*/*.java azure-pipelines.yml .travis.yml " ...
+ ".circleci/config.yml dev-kit/*.m dev-kit/*.sh mypackage_toolbox_info.m " ...
+ "dev-kit/make_release dev-kit/*.m CHANGES.txt info.xml doc-project/*.txt doc-project/*.md", ' +', 'split');
filesToMunge = unique(fileglob2abspath(fileGlobsToMunge));
replacements = {
"__myproject__" PROJECT
"__myprojectemail__" PROJECT_EMAIL
"__myprojectguid__" PROJECT_GUID
"__myproject_matlab_version__" PROJECT_MATLAB_VERSION
"__myghuser__" GHUSER
"__YOUR_NAME_HERE__" PROJECT_AUTHOR
"__myproject_summary__" PROJECT_SUMMARY
"__myproject_description__" PROJECT_DESCRIPTION
"__author_homepage__" AUTHOR_HOMEPAGE
"myproject" PROJECT
"mypackage" PACKAGE
"myghuser" GHUSER
"Mypackage" PACKAGE_CAP
"R2019b" PROJECT_MATLAB_VERSION
"command: ./MatlabProjectTemplate/test_project_initialization" "command: echo Hello world"
"" ""
};
for file = filesToMunge
mungefile(file, replacements);
end
for f = fileglob2abspath({'*mypackage*', 'dev-kit/*mypackage*'})
relFile = strrep(f, reporoot+filesep, '');
newName = strrep(relFile, 'mypackage', PACKAGE);
mv(relFile, newName);
end
if ~doDev
delete('rollback_init')
end
rmdir2('docs', 's');
copyfile2("doc-src-"+DOCTOOL, 'docs');
for f = ["doc-src-jekyll" "doc-src-gh-pages" "doc-src-gh-pages-raw" "doc-src-mkdocs"]
rmdir2(f, 's');
end
rmdir2('doc', 's')
mkdir('doc')
writetext(PROJECT_MATLAB_VERSION, '.matlab_version')
writetext("0.1.0", "VERSION")
movefile('myproject.prj.in', PROJECT+".prj.in")
echo
echo("Project "+PROJECT+" is initialized.")
echo("See MatlabProjectTemplate/README.md for more info.")
echo
echo("Happy hacking!")
echo
end
function mungefile(file, replacements, replacementType)
arguments
file (1,1) string
replacements cell
replacementType (1,1) string = "string"
end
origTxt = readtext(file);
txt = origTxt;
for i = 1:size(replacements, 1)
[old, new] = replacements{i,:};
if replacementType == "string"
txt = strrep(txt, old, new);
elseif replacementType == "regex"
txt = regexprep(txt, old, new);
else
error('Invalid replacementType: %s', replacementType);
end
end
writetext(txt, file);
end
function out = fileglob2abspath(pats)
arguments
pats string
end
names = string([]);
abspaths = string([]);
for i = 1:numel(pats)
d = dir(pats(i));
name = string({d.name});
abspath = fullfile(string({d.folder}), name);
names = [names name];
abspaths = [abspath abspaths];
end
out = abspaths;
end
function echo(fmt, varargin)
if nargin == 0
fprintf('\n');
return
end
fprintf([char(fmt) '\n'], varargin{:});
end
function copyStructFieldsIntoWorkspace(s)
fields = fieldnames(s);
for i = 1:numel(fields)
assignin('caller', fields{i}, s.(fields{i}));
end
end
function copyfile2(src, dest, varargin)
% A version of copyfile that raises an error on failure
[ok,msg] = copyfile(src, dest, varargin{:});
if ~ok
error('Failed copying file "%s" to "%s": %s', src, dest, msg);
end
end
function mkdir2(varargin)
% A version of mkdir that raises error on failure
[ok,msg] = mkdir(varargin{:});
if ~ok
if nargin == 1
target = varargin{1};
else
target = fullfile(varargin{:});
end
error('Failed creating directory "%s": %s', target, msg);
end
end
function rmdir2(dir, varargin)
% A version of rmdir that raises errors on failure
[ok,msg] = rmdir(dir, varargin{:});
if ~ok
error('rmdir of "%s" failed: %s', dir, msg);
end
end
function out = system2(cmd)
% A version of system that raises an error on failure
if nargout == 0
ok = system(cmd);
else
[ok,out] = system(cmd);
end
if ~ok
error('Command failed. Command: %s', cmd);
end
end
function out = withcd(dir)
% Temporarily change to a new directory
origDir = pwd;
cd(dir);
out.RAII = onCleanup(@() cd(origDir));
end
function mv(source, dest)
% A version of movefile that raises an error on failure
[ok,msg] = movefile(source, dest);
if ~ok
error('Failed moving "%s" to "%s": %s', source, dest, msg);
end
end
function out = readtext(file, encoding)
% Read the contents of a text file as a string
%
% This is analagous to Matlab's readcsv and readtable, and exists because Matlab
% doesn't provide a basic file-slurping mechanism.
arguments
file (1,1) string
encoding (1,1) string = 'UTF-8' % TODO: auto-detect file encoding via sniffing
end
[fid,msg] = fopen(file, 'r', 'n', encoding);
if fid < 1
error('Failed opening file %s: %s', file, msg);
end
RAII.fh = onCleanup(@() fclose(fid));
c = fread(fid, Inf, 'uint8=>char');
out = string(c');
end
function writetext(text, file, encoding)
arguments
text (1,1) string
file (1,1) string
encoding (1,1) string = 'UTF-8'
end
[fid,msg] = fopen(file, 'w', 'n', encoding);
if fid < 1
error('Failed opening file %s: %s', file, msg);
end
RAII.fh = onCleanup(@() fclose(fid));
fprintf(fid, '%s', text);
end