-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppDesignerProDev.m
353 lines (322 loc) · 12.5 KB
/
AppDesignerProDev.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
classdef AppDesignerProDev < handle
% Helps you to build toolbox and deploy it to GitHub
% By Pavel Roslovets, ETMC Exponenta
% https://github.com/ETMC-Exponenta/ToolboxExtender
properties
ext % Toolbox Extender
vp % project version
vp_fcn % get version callback function
end
methods
function obj = AppDesignerProDev(extender)
% Init
if nargin < 1
obj.ext = AppDesignerProExtender;
else
if ischar(extender) || isStringScalar(extender)
obj.ext = AppDesignerProExtender(extender);
else
obj.ext = extender;
end
end
if ~strcmp(obj.ext.root, pwd)
warning("Project root folder does not math with current folder." +...
newline + "Consider to change folder, delete installed toolbox or restart MATLAB")
end
obj.gvp();
end
function vp = gvp(obj)
% Get project version
if ~isempty(obj.vp_fcn)
vp = obj.vp_fcn();
else
ppath = obj.ext.getppath();
if isfile(ppath)
if obj.ext.type == "toolbox"
vp = matlab.addons.toolbox.toolboxVersion(ppath);
else
txt = obj.ext.readtxt(ppath);
vp = char(regexp(txt, '(?<=(<param.version>))(.*?)(?=(</param.version>))', 'match'));
end
else
vp = '';
end
end
obj.vp = vp;
end
function build(obj, vp, gendoc)
% Build toolbox for specified version
ppath = obj.ext.getppath();
if nargin < 3 || gendoc
obj.gendoc();
end
if nargin > 1 && ~isempty(vp)
obj.setver(vp);
else
vp = obj.vp;
end
[~, bname] = fileparts(obj.ext.pname);
bpath = fullfile(obj.ext.root, bname);
if obj.ext.type == "toolbox"
obj.updateroot();
obj.seticons();
matlab.addons.toolbox.packageToolbox(ppath, bname);
else
matlab.apputil.package(ppath);
movefile(fullfile(obj.ext.root, obj.ext.name + ".mlappinstall"), bpath + ".mlappinstall",'f');
end
obj.ext.echo("v" + vp + " has been built");
end
function test(obj, varargin)
% Build and install
obj.build(varargin{:});
obj.ext.install();
end
function untag(obj, v)
% Delete tag from local and remote
untagcmd1 = sprintf('git push --delete origin v%s', v);
untagcmd2 = sprintf('git tag -d v%s', v);
system(untagcmd1);
system(untagcmd2);
system('git push --tags');
obj.ext.echo('has been untagged');
end
function release(obj, vp)
% Build toolbox, push and tag version
if nargin > 1
obj.vp = vp;
else
vp = '';
end
if ~isempty(obj.ext.pname)
obj.build(vp, true);
end
obj.push();
obj.tag();
obj.ext.echo('has been deployed');
if ~isempty(obj.ext.pname)
clipboard('copy', ['"' char(obj.ext.getbinpath) '"'])
disp("Binary path was copied to clipboard")
end
disp("* Now create release on GitHub *");
chapters = ["Summary" "Upgrade Steps" "Breaking Changes"...
"New Features" "Bug Fixes" "Improvements" "Other Changes"];
chapters = join("# " + chapters, newline);
fprintf("Release notes hint: fill\n%s\n", chapters);
disp("! Don't forget to attach binary from clipboard !");
pause(1)
web(obj.ext.remote + "/releases/edit/v" + obj.vp, '-browser')
end
function gendoc(obj, format, docdir, options)
% Generate html, pdf or md (beta) from mlx
arguments
obj
format = "html"
docdir = fullfile(obj.ext.root, 'doc')
options.TargetDir = docdir
options.AddCredentials = false
end
format = string(format);
docdir = strip(docdir, '/');
[fs, ~] = obj.ext.dir(fullfile(docdir, '*.mlx'));
for i = 1 : height(fs)
[~, fname] = fileparts(fs.name(i));
respath = char(fullfile(options.TargetDir, fname + "." + format));
resinfo = obj.ext.dir(respath);
convert = isempty(resinfo);
if ~convert
convert = fs.date(i) >= resinfo.date(1);
end
if convert
fprintf('Converting %s.mlx...\n', fname);
if format == "md"
obj.mlx2md(char(fs.path(i)), char(respath), options.AddCredentials);
else
matlab.internal.liveeditor.openAndConvert(char(fs.path(i)), char(respath));
end
end
end
disp('Docs have been generated');
end
function setver(obj, vp)
% Set version
ppath = obj.ext.getppath();
if obj.ext.type == "toolbox"
matlab.addons.toolbox.toolboxVersion(ppath, vp);
else
txt = obj.ext.readtxt(ppath);
txt = regexprep(txt, '(?<=(<param.version>))(.*?)(?=(</param.version>))', vp);
txt = strrep(txt, '<param.version />', '');
obj.ext.writetxt(txt, ppath);
end
obj.gvp();
end
function webrel(obj)
% Open GitHub releases webpage
obj.ext.webrel();
end
function check(obj)
% Check deployed release
upd = AppDesignerProUpdater(obj.ext);
upd.fetch();
disp("Latest release: v" + upd.vr);
if isempty(upd.rel)
fprintf('[X] Release notes\n');
end
if isempty(upd.relsum)
fprintf('[X] Release notes summary\n');
end
if isempty(upd.bin)
fprintf('[X] Toolbox binary\n');
end
end
function [com, url] = webinstaller(obj)
% Get web installer command
url = obj.shorturl(obj.ext.getrawurl('installweb.m'));
com = sprintf("eval(webread('%s'))", url);
end
end
methods (Hidden)
function updateroot(obj)
% Update project root
service = com.mathworks.toolbox_packaging.services.ToolboxPackagingService;
pr = service.openProject(obj.ext.getppath());
service.removeToolboxRoot(pr);
service.setToolboxRoot(pr, obj.ext.root);
service.closeProject(pr);
end
function seticons(obj)
% Set icons of app in toolbox
xmlfile = 'DesktopToolset.xml';
oldtxt = '<icon filename="matlab_app_generic_icon_' + string([16; 24]) + '"/>';
newtxt = '<icon path="./" filename="icon_' + string([16; 24]) + '.png"/>';
if isfile(xmlfile) && isfolder('resources')
if all(isfile("resources/icon_" + [16 24] + ".png"))
txt = obj.ext.readtxt(xmlfile);
if contains(txt, oldtxt)
txt = replace(txt, oldtxt, newtxt);
obj.ext.writetxt(txt, xmlfile);
end
end
end
end
function push(obj)
% Commit and push project to GitHub
commitcmd = sprintf('git commit -m v%s', obj.vp);
system('git add .');
system(commitcmd);
system('git push');
obj.ext.echo('has been pushed');
end
function tag(obj, vp)
% Tag git project and push tag
if nargin < 2
vp = obj.vp;
end
tagcmd = sprintf('git tag -a v%s -m v%s', vp, vp);
system(tagcmd);
system('git push --tags');
obj.ext.echo('has been tagged');
end
function url = shorturl(obj, url)
% Shorten URL by git.io
host = "https://git.io/";
url = webwrite(host + "create", 'url', url);
url = host + url;
end
end
methods (Hidden = true)
function mlx2md(obj, fpath, htmlpath, addcred)
% Convert mlx-script to markdown md-file (beta)
if nargin < 4
addcred = false;
end
[~, fname] = fileparts(fpath);
tempf = "_temp_" + fname + ".m";
matlab.internal.liveeditor.openAndConvert(fpath, char(tempf));
txt = string(split(fileread(tempf), newline));
delete(tempf);
txt = erase(txt, char(13));
% Convert code
code = find(~startsWith(txt, '%') & txt ~= "");
txt2 = strings();
for i = 1 : length(txt)
if ismember(i, code)
if ~ismember(i-1, code)
txt2 = txt2 + "``` MATLAB" + newline;
end
txt2 = txt2 + txt(i) + newline;
if ~ismember(i+1, code)
txt2 = txt2 + "```" + newline;
end
else
txt2 = txt2 + txt(i) + newline;
end
end
txt = string(split(txt2, newline));
% Convert first title
if startsWith(txt(1), '%% ')
txt(1) = replace(txt(1), '%% ', '# ');
end
% Convert other titles
titles = startsWith(txt, '%% ') & txt ~= "%% ";
txt(titles) = replace(txt(titles), '%% ', '## ');
% Convert lists
lists = find(startsWith(txt, '% * '));
txt(lists) = extractAfter(txt(lists), '% ');
lists = find(startsWith(txt, '% # '));
txt(lists) = replace(txt(lists), '% # ', '* ');
% Convert text
text = find(startsWith(txt, '% '));
txt2 = strings();
for i = 1 : length(txt)
if ismember(i, text)
str = char(txt(i));
str = replace(str, '|', '`');
%str = replace(str, '*', '**');
txt2 = txt2 + str(3:end);
if ~ismember(i+1, text)
txt2 = txt2 + newline;
end
else
txt2 = txt2 + txt(i) + newline;
end
end
txt = string(split(txt2, newline));
br = txt == "%% ";
txt(br) = "";
% Convert links
links = extractBetween(join(txt, newline), '<', '>');
urls = extractBefore(links, ' ');
names = extractAfter(links, ' ');
txt = replace(txt, "<" + links + ">", "[" + names + "](" + urls + ")");
if addcred
txt(end+1) = sprintf("***\n*Generated from %s.mlx with [Toolbox Extender](%s)*",...
fname, 'https://github.com/ETMC-Exponenta/ToolboxExtender');
end
obj.ext.writetxt(join(txt, newline), htmlpath, 'utf-8');
end
end
methods (Static)
function exclude(ppath, fmask)
if isfile(ppath)
try
% Exclude file or folder from Toolbox Project
service = com.mathworks.toolbox_packaging.services.ToolboxPackagingService;
pr = service.openProject(ppath);
ex = service.getExcludeFilter(pr);
ex = split(string(ex), newline);
fmask = string(fmask);
for i = 1 : length(fmask)
if ~ismember(fmask(i), ex)
ex = [ex; fmask(i)];
service.setExcludeFilter(pr, join(ex, newline));
end
end
service.closeProject(pr);
catch
end
end
end
end
end