-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathINSTALL.m
397 lines (333 loc) · 12.6 KB
/
INSTALL.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
function INSTALL(varargin)
% This function installs the Combustion Toolbox repository from local
% files. It adds all subfolders to the MATLAB path and installs the
% Combustion Toolbox app.
%
% Optional Args:
% * action (char): 'install' or 'uninstall' (default: 'install')
% * type (char): 'path', 'GUI', or 'all' (default: 'all')
% * FLAG_HOME (bool): true or false (default: false)
%
% Examples:
% * INSTALL(); % Installs the Combustion Toolbox
% * INSTALL('uninstall'); % Uninstalls the Combustion Toolbox
% * INSTALL('install', 'path'); % Installs the Combustion Toolbox (only MATLAB path)
% * INSTALL('install', 'GUI'); % Installs the Combustion Toolbox (only GUI)
% * INSTALL('install', 'all'); % Installs the Combustion Toolbox
% * INSTALL('install', 'all', true); % Installs the Combustion Toolbox with FLAG_HOME set to true
%
% Notes:
% The code is available in:
% * GitHub - https://github.com/CombustionToolbox/combustion_toolbox
% * MATLAB File Exchange - https://in.mathworks.com/matlabcentral/fileexchange/101088-combustion-toolbox
% * Zenodo - https://doi.org/10.5281/zenodo.5554911
%
% Website: https://combustion-toolbox-website.readthedocs.io/
% or type "run website_CT"
%
% @author: Alberto Cuadra Lara
% Postdoctoral researcher - Group Fluid Mechanics
% Universidad Carlos III de Madrid
% Default
action = 'install';
type = 'all';
FLAG_HOME = false;
% Unpack
if nargin
action = varargin{1};
end
if nargin > 1
type = varargin{2};
end
if nargin > 2
FLAG_HOME = varargin{3};
end
% Get package destination
packageDst = getPath(FLAG_HOME);
% Install Combustion Toolbox package and app
installPackage(action, type, packageDst);
end
% SUB-PASS FUNCTIONS
function packageDst = getPath(FLAG_HOME)
% Get path package destination
% Get path package destination based on current location
if ~FLAG_HOME
packageDst = pwd();
return
end
% Get path package destination based on home operating system location
switch computer('arch')
case {'mac', 'maci64', 'maca64', 'maci'}
% macOS
packageDst = '/Applications/CombustionToolbox/matlab';
case 'linux64'
% Linux
packageDst = '/usr/local/CombustionToolbox/matlab';
case 'win64'
% Windows
packageDst = 'C:\Program Files\CombustionToolbox\matlab';
otherwise
error('Unsupported operating system');
end
end
function bashScriptFile = generateBash(packageDst, action)
% Generate bash script
% Definitions
packageSrc = pwd();
startupFile = fullfile(getenv('HOME'), 'Documents', 'MATLAB', 'startup.m');
addPathLine = sprintf('addpath(genpath(''%s'')) %%%% added by Combustion toolbox installer', packageDst);
% Define the content of the Bash script
if strcmp(action, 'install')
bashScriptContent = sprintf([
'#!/bin/bash\n' ...
'\n' ...
'PACKAGE_SRC="%s"\n' ...
'PACKAGE_DST="%s"\n' ...
'STARTUP_FILE="%s"\n' ...
'ADDPATH_LINE="%s"\n' ...
'\n' ...
'printf "Copying Combustion Toolbox destination directory... "\n' ...
'rsync -aq --exclude=".git" "$PACKAGE_SRC/" "$PACKAGE_DST/"\n' ...
'printf "OK!\\n"\n' ...
'\n' ...
'printf "Adding Combustion Toolbox destination directory to startup.m... "\n' ...
'if grep -Fxq "$ADDPATH_LINE" "$STARTUP_FILE"; then\n' ...
' printf "OK!\\n"\n' ...
'else\n' ...
' echo "$ADDPATH_LINE" >> "$STARTUP_FILE"\n' ...
' printf "OK!\\n"\n' ...
'fi\n'], ...
packageSrc, packageDst, startupFile, addPathLine);
elseif strcmp(action, 'uninstall')
bashScriptContent = sprintf([
'#!/bin/bash\n' ...
'\n' ...
'STARTUP_FILE="%s"\n' ...
'\n' ...
'printf "Removing Combustion Toolbox data from startup.m... "\n' ...
'sed -i '''' "/added by Combustion toolbox installer/d" "$STARTUP_FILE"\n' ...
'printf "OK!\\n"\n'], ...
startupFile);
else
error('Invalid action specified. Please use ''install'' or ''uninstall''.');
end
% Write the Bash script content to a file
bashScriptFile = 'install.sh';
fid = fopen(bashScriptFile, 'wt');
fprintf(fid, bashScriptContent);
fclose(fid);
% Make the script executable
system(['chmod +x ' bashScriptFile]);
end
function installPackage(action, type, packageDst)
% This function installs the Combustion Toolbox repository from local
% files. It adds all subfolders to the MATLAB path and installs the
% Combustion Toolbox app.
%
% Args:
% action (char): 'install' or 'uninstall'
% type (char): 'path', 'GUI', or 'all'
% packageDst (char): package destination path
%
% Examples:
% * installPackage(); % Installs the Combustion Toolbox
% * installPackage('uninstall'); % Uninstalls the Combustion Toolbox
% * installPackage('install', 'path'); % Installs the Combustion Toolbox (only MATLAB path)
% * installPackage('install', 'GUI'); % Installs the Combustion Toolbox (only GUI)
% * installPackage('install', 'all'); % Installs the Combustion Toolbox
%
% Notes:
% The code is available in:
% * GitHub - https://github.com/CombustionToolbox/combustion_toolbox
% * MATLAB File Exchange - https://in.mathworks.com/matlabcentral/fileexchange/101088-combustion-toolbox
% * Zenodo - https://doi.org/10.5281/zenodo.5554911
%
% Website: https://combustion-toolbox-website.readthedocs.io/
% or type "run website_CT"
%
% @author: Alberto Cuadra Lara
% Postdoctoral researcher - Group Fluid Mechanics
% Universidad Carlos III de Madrid
% Definitions
name = 'Combustion Toolbox';
app_name = 'combustion_toolbox_app';
dir_code = getDirCode();
dir_database = fullfile(dir_code, 'databases');
dir_app = fullfile(dir_code, 'installer', [app_name, '.mlappinstall']);
% Get type of installation/uninstallation
[FLAG_PATH, FLAG_GUI] = getType(type);
% Add/remove Combustion Toolbox in MATLAB startup.m file
try
% Generate bash script
bashScriptFile = generateBash(packageDst, action);
% Execute bash script
system(['./' bashScriptFile]);
% Remove the bash script
delete(bashScriptFile);
catch
fprintf('Error including Combustion Toolbox path in MATLAB startup.m file');
end
% Move local path to package destination
cd(packageDst);
% Install/Uninstall Combustion Toolbox
actionCode(action);
% NESTED FUNCTIONS
function actionCode(action)
% Install or uninstall the Combustion Toolbox
%
% Args:
% action (char): 'install' or 'uninstall'
% Definitions
switch lower(action)
case 'install'
f_path = @addpath;
f_app = @matlab.apputil.install;
message = 'Installing';
case 'uninstall'
f_path = @rmpath;
f_app = @matlab.apputil.uninstall;
app_info = matlab.apputil.getInstalledAppInfo;
if isempty(app_info)
return
end
FLAG_ID = contains(struct2table(app_info).id, app_name);
if ~sum(FLAG_ID)
dir_app = [];
else
dir_app = app_info(FLAG_ID).id;
end
message = 'Uninstalling';
otherwise
error('Invalid action specified. Please use ''install'' or ''uninstall''.');
end
% Install/Uninstall path
actionPath(f_path, message);
% Install/Uninstall the Combustion Toolbox app
actionApp(f_app, message);
end
function actionDatabases(action, path)
% Install/Uninstall databases
%
% Args:
% action (char): 'install' or 'uninstall'
% Import packages
import combustiontoolbox.databases.*
if strcmpi(action, 'uninstall')
return
end
% Install databases
databases = {NasaDatabase()};
for i = 1:length(databases)
databases{i}.save('path', path);
end
end
function actionPath(f_path, message)
% Install/Uninstall path
%
% Args:
% f_path (function): Function to add or remove the path
% message (char): Message to display
if ~FLAG_PATH
return
end
% Add the code directory to the MATLAB path
fprintf('%s %s path... ', message, name);
f_path(dir_code);
% Get subfolders (except '.git' and '.github')
d = dir(dir_code);
subfolders = d([d(:).isdir]);
subfolders = subfolders(~ismember({subfolders(:).name}, {'.', '..', '.git', '.github'}));
% Add all subfolders to the MATLAB path
for i = length(subfolders):-1:1
dir_subfolders = fullfile(subfolders(i).folder, subfolders(i).name);
% Check if the subfolder is a package folder
if startsWith(subfolders(i).name, '+')
f_path(subfolders(i).folder); % Add parent directory
continue
end
genpath_subfolders = genpath(dir_subfolders);
f_path(genpath_subfolders);
end
% Save the path permanently
if strcmpi(action, 'install')
savepath;
% Install databases
actionDatabases(action, dir_database);
end
fprintf('OK!\n')
end
function actionApp(f_app, message)
% Install/Uninstall the Combustion Toolbox app
%
% Args:
% f_app (function): Function to install or uninstall the app
% message (char): Message to display
if ~FLAG_GUI
return
end
if isempty(dir_app)
fprintf('%s app is not currently installed.\n', name);
return
end
fprintf('%s %s app... ', message, name);
f_app(dir_app);
if isequal(f_app, @matlab.apputil.install)
% Get dir app
app_info = matlab.apputil.getInstalledAppInfo;
FLAG_ID = contains(struct2table(app_info).id, app_name);
dir_app = app_info(FLAG_ID).location;
dir_database_app = fullfile(dir_app, 'databases');
% Make directory databases
if ~exist(dir_database_app, 'dir')
mkdir(dir_database_app);
% Add directory to the MATLAB path
addpath(dir_database_app);
% Save the path permanently
savepath;
end
% Install databases
actionDatabases(action, dir_database_app);
end
fprintf('OK!\n')
end
end
function dir_code = getDirCode()
% Get the directory where the code is located
%
% Returns:
% dir_code (char): Directory where the code is located
% Check if user is running an executable in standalone mode
if isdeployed
[~, result] = system('set PATH');
dir_code = char(regexpi(result, 'Path=(.*?);', 'tokens', 'once'));
return
end
% Get current folder if the user is running an m-file from the
% MATLAB integrated development environment (regular MATLAB)
dir_code = pwd;
end
function [FLAG_PATH, FLAG_GUI] = getType(type)
% Get the type of installation/uninstallation
%
% Args:
% type (char): 'path', 'GUI', or 'all'
%
% Returns:
% Tuple containing
%
% * FLAG_PATH (bool): True if the path is installed/uninstalled
% * FLAG_GUI (bool): True if the GUI is installed/uninstalled
switch lower(type)
case 'all'
FLAG_PATH = true;
FLAG_GUI = true;
case 'path'
FLAG_PATH = true;
FLAG_GUI = false;
case 'gui'
FLAG_PATH = false;
FLAG_GUI = true;
otherwise
error('Invalid type specified. Please use ''path'', ''gui'', or ''all''.');
end
end