-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppDesignerProExtender.m
460 lines (417 loc) · 15.3 KB
/
AppDesignerProExtender.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
classdef AppDesignerProExtender < handle
% Contains core functions. Required for other classes and functionality
% By Pavel Roslovets, ETMC Exponenta
% https://github.com/ETMC-Exponenta/ToolboxExtender
properties
name % project name
pname % name of project file
type % type of project
root % root dir
remote % GitHub link
vc % current installed version
extv % Toolbox Extender version
end
properties (Hidden)
config = 'ToolboxConfig.xml' % configuration file name
project % MATLAB Project handle
end
methods
function obj = AppDesignerProExtender(root)
% Init
if nargin < 1
obj.root = fileparts(mfilename('fullpath'));
else
obj.root = root;
end
end
function set.root(obj, root)
% Set root path
obj.root = root;
if ~obj.readconfig()
obj.getpname();
obj.gettype();
obj.getname();
obj.getremote();
end
obj.gvc();
end
function [vc, guid] = gvc(obj)
% Get current installed version
switch obj.type
case "toolbox"
tbx = matlab.addons.toolbox.installedToolboxes;
if ~isempty(tbx)
tbx = struct2table(tbx, 'AsArray', true);
idx = strcmp(tbx.Name, obj.name);
vcs = string(tbx.Version(idx));
guid = tbx.Guid(idx);
vc = '';
for i = 1 : length(vcs)
if matlab.addons.isAddonEnabled(guid{i}, vcs(i))
vc = char(vcs(i));
break
end
end
else
vc = '';
guid = '';
end
case "app"
apps = matlab.apputil.getInstalledAppInfo;
vc = '';
guid = '';
otherwise
vc = '';
end
obj.vc = vc;
end
function res = install(obj, fpath)
% Install toolbox or app
if nargin < 2
fpath = obj.getbinpath();
end
switch obj.type
case "toolbox"
res = matlab.addons.install(fpath);
case "app"
res = matlab.apputil.install(fpath);
otherwise
error('Unsupported for %s\n', obj.type);
end
obj.gvc();
obj.echo('has been installed');
end
function uninstall(obj)
% Uninstall toolbox or app
[~, guid] = obj.gvc();
if isempty(guid)
disp('Nothing to uninstall');
else
guid = string(guid);
for i = 1 : length(guid)
switch obj.type
case "toolbox"
matlab.addons.uninstall(char(guid(i)));
case "app"
matlab.apputil.uninstall(char(guid(i)));
otherwise
error('Unsupported for %s\n', obj.type);
end
end
disp(obj.name + " was uninstalled");
try
obj.gvc();
end
end
end
function doc(obj, name)
% Open page from documentation
docdir = fullfile(obj.root, 'doc');
if isfolder(docdir)
if (nargin < 2) || isempty(name)
name = 'GettingStarted';
end
if ~any(endsWith(name, {'.mlx' '.html'}))
if computer == "GLNXA64" %% Linux and MATLAB Online
name = name + ".mlx";
else
name = name + ".html";
end
end
docpath = fullfile(docdir, name);
if endsWith(name, '.html')
web(char(docpath));
else
open(char(docpath));
end
end
end
function examples(obj)
% cd to Examples dir
expath = fullfile(obj.root, 'examples');
cd(expath);
end
function web(obj)
% Open GitHub page
web(obj.remote, '-browser');
end
function addfav(obj, label, code, icon)
% Add favorite
favs = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
nfav = com.mathworks.mlwidgets.favoritecommands.FavoriteCommandProperties();
nfav.setLabel(label);
nfav.setCategoryLabel(obj.name);
nfav.setCode(code);
if nargin > 3
[ipath, iname, iext] = fileparts(icon);
nfav.setIconPath(fullfile(obj.root, ipath));
nfav.setIconName(iname + string(iext));
end
nfav.setIsOnQuickToolBar(true);
favs.addCommand(nfav);
end
function yes = isfav(obj, label)
% Does favorite exist
favs = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
yes = favs.hasCommand(label, obj.name);
end
function yes = isfavs(obj)
% Does favorites category exist
favs = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
yes = favs.hasCategory(obj.name);
end
function yes = rmfav(obj, label)
% Remove favorite
favs = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
yes = favs.removeCommand(label, obj.name);
end
function rmfavs(obj)
% Remove all favorites
favs = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
favs.removeCategory(obj.name)
end
end
methods (Hidden)
function echo(obj, msg)
% Display service message
fprintf('%s %s\n', obj.name, msg);
end
function name = getname(obj)
% Get project name from project file
name = '';
ppath = obj.getppath();
if isfile(ppath)
switch obj.type
case "toolbox"
txt = obj.readtxt(ppath);
name = char(extractBetween(txt, '<param.appname>', '</param.appname>'));
case "project"
name = obj.project.Name;
end
end
obj.name = name;
end
function pname = getpname(obj)
% Get project file name
fs = obj.dir(fullfile(obj.root, '*.prj'));
if ~isempty(fs)
isproj = false(1, length(fs.name));
for i = 1 : length(fs.name)
txt = obj.readtxt(fs.path(i));
isproj(i) = ~contains(txt, '<MATLABProject111111');
end
if any(isproj)
names = fs.name(isproj);
pname = names(1);
obj.pname = pname;
else
%warning('Project file was not found in a current folder');
end
else
%warning('Project file was not found in a current folder');
end
end
function ppath = getppath(obj)
% Get project file full path
if ~isempty(obj.pname)
ppath = fullfile(obj.root, obj.pname);
else
ppath = '';
end
end
function type = gettype(obj)
% Get project type (Toolbox/App)
ppath = obj.getppath();
txt = obj.readtxt(ppath);
if contains(txt, 'plugin.toolbox')
type = 'toolbox';
elseif contains(txt, 'plugin.apptool')
type = 'app';
elseif contains(txt, '<MATLABProject')
type = 'project';
p = [];
try
p = currentProject;
catch
p = openProject(obj.pname);
end
if isempty(p)
error('Corrupted project file: %s\n', ppath);
else
obj.project = p;
end
else
type = 'package';
end
obj.type = type;
end
function remote = getremote(obj)
% Get remote (GitHub) address via Git
[~, cmdout] = system('git remote -v');
remote = extractBetween(cmdout, 'https://', '.git', 'Boundaries', 'inclusive');
if isempty(remote)
remote = extractBetween(cmdout, 'https://', '(', 'Boundaries', 'inclusive');
remote = strtrim(erase(remote, '('));
end
if ~isempty(remote)
remote = remote(end);
end
remote = obj.cleargit(remote);
obj.remote = remote;
end
function name = getvalidname(obj, cname)
% Get valid variable name
name = char(obj.name);
if nargin > 1
name = char(name + string(cname));
end
name = matlab.lang.makeValidName(name);
end
function txt = readtxt(~, fpath)
% Read text from file
if isfile(fpath)
f = fopen(fpath, 'r', 'n', 'windows-1251');
txt = fread(f, '*char')';
fclose(f);
else
txt = '';
end
end
function writetxt(~, txt, fpath, encoding)
% Wtite text to file
if nargin < 4
encoding = 'windows-1251';
end
fid = fopen(fpath, 'w', 'n', encoding);
fwrite(fid, unicode2native(txt, encoding));
fclose(fid);
end
function txt = txtrep(obj, fpath, old, new)
% Replace in txt file
txt = obj.readtxt(fpath);
txt = replace(txt, old, new);
obj.writetxt(txt, fpath);
end
function [bpath, bname] = getbinpath(obj)
% Get generated binary file path
[~, name] = fileparts(obj.pname);
switch obj.type
case "toolbox"
ext = ".mltbx";
case "app"
ext = ".mlappinstall";
otherwise
error('Unsupported for %s\n', obj.type);
end
bname = name + ext;
bpath = fullfile(obj.root, bname);
end
function ok = readconfig(obj)
% Read config from xml file
confpath = fullfile(obj.root, obj.config);
ok = isfile(confpath);
if ok
xml = xmlread(confpath);
conf = obj.getxmlitem(xml, 'config', 0);
obj.name = obj.getxmlitem(conf, 'name');
obj.pname = obj.getxmlitem(conf, 'pname');
obj.type = obj.getxmlitem(conf, 'type');
obj.remote = obj.cleargit(obj.getxmlitem(conf, 'remote'));
obj.extv = obj.getxmlitem(conf, 'extv');
end
end
function i = getxmlitem(~, xml, name, getData)
% Get item from XML
if nargin < 4
getData = true;
end
i = xml.getElementsByTagName(name);
i = i.item(0);
if getData
i = i.getFirstChild;
if ~isempty(i)
i = i.getData;
end
i = char(i);
end
end
function [nname, npath] = cloneclass(obj, classname, sourcedir, prename)
% Clone Toolbox Extander class to current Project folder
if nargin < 4
prename = "Toolbox";
end
if nargin < 3
sourcedir = pwd;
end
if nargin < 2
classname = "Extender";
else
classname = lower(char(classname));
classname(1) = upper(classname(1));
end
pname = obj.getvalidname;
if isempty(pname)
pname = 'Toolbox';
end
oname = string(prename) + classname;
nname = pname + string(classname);
npath = fullfile(obj.root, nname + ".m");
opath = fullfile(sourcedir, oname + ".m");
copyfile(opath, npath);
obj.txtrep(npath, "obj = " + oname, "obj = " + nname);
obj.txtrep(npath, "classdef " + oname, "classdef " + nname);
obj.txtrep(npath, "obj.ext = AppDesignerProExtender", "obj.ext = " + obj.getvalidname + "Extender");
obj.txtrep(npath, "upd = AppDesignerProUpdater", "upd = " + obj.getvalidname + "Updater");
end
function name = getselfname(~)
% Get self class name
name = mfilename('class');
end
function webrel(obj)
% Open GitHub releases webpage
web(obj.remote + "/releases", '-browser');
end
function repo = getrepo(obj)
% Get repo string from remote URL
repo = extractAfter(obj.remote, 'https://github.com/');
end
function url = getlatesturl(obj)
% Get latest release URL
url = obj.getapiurl() + "/releases/latest";
end
function url = getapiurl(obj)
% Get GitHub API URL
url = "https://api.github.com/repos/" + obj.getrepo();
end
function url = getrawurl(obj, fname)
% Get GitHub raw source URL
url = sprintf("https://raw.githubusercontent.com/%s/master/%s", obj.getrepo(), fname);
end
end
methods (Hidden, Static)
function [fs, ds] = dir(dpath)
% Get directory content in convenient format
if nargin < 1
dpath = pwd;
end
fs = struct2table(dir(dpath), 'AsArray', true);
fs = convertvars(fs, 1:2, 'string');
fs = fs(~ismember(fs.name, ["." ".."]), :);
fs.date = datetime(fs.datenum, 'ConvertFrom', 'datenum');
fs.datenum = [];
fs.path = fullfile(fs.folder, fs.name);
fs = movevars(fs, "path", 'After', 'folder');
if nargout > 1
ds = fs(fs.isdir, :);
fs = fs(~fs.isdir, :);
end
end
function remote = cleargit(remote)
% Delete .git
remote = char(remote);
if endsWith(remote, '.git')
remote = remote(1:end-4);
end
end
end
end