Skip to content

Commit

Permalink
Merge pull request #853 from AlbertoCuadra/develop
Browse files Browse the repository at this point in the history
Update: rewrite INSTALL function #852
  • Loading branch information
AlbertoCuadra committed Apr 7, 2023
2 parents ac21760 + 44dd356 commit cf17d5b
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 48 deletions.
205 changes: 192 additions & 13 deletions INSTALL.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,193 @@
function INSTALL()
% Function to install Combustion Toolbox as a package that can be used
% using the desktop environment (plain code) or the GUI.
% Install the MATLAB toolbox from the *.mlappinstall file

% Get the directory path
dir_name = fileparts( mfilename('fullpath') );
% Find *.mlappinstall file in the installer folder
toolbox_filename = dir(fullfile(dir_name, 'installer', '*.mltbx'));
% Install the toolbox
toolbox = matlab.addons.toolbox.installToolbox(fullfile(toolbox_filename.folder, toolbox_filename.name));
% Print installed addons to the screen to verify the installation
matlab.addons.installedAddons()
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')
%
% 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
%
% Notes:
% The code is available in:
% * GitHub - https://github.com/AlbertoCuadra/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
% PhD Candidate - Group Fluid Mechanics
% Universidad Carlos III de Madrid

% Default
action = 'install';
type = 'all';

% Definitions
name = 'Combustion Toolbox';
app_name = 'combustion_toolbox_app';
dir_code = get_dir_code();
dir_app = fullfile(dir_code, 'installer', [app_name, '.mlappinstall']);

% Unpack
if nargin
action = varargin{1};
end

if nargin > 1
type = varargin{2};
end

% Get type of installation/uninstallation
[FLAG_PATH, FLAG_GUI] = get_type(type);

% Install/Uninstall Combustion Toolbox
action_code(action);

% NESTED FUNCTIONS
function action_code(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;
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
action_path(f_path, message);

% Install/Uninstall the Combustion Toolbox app
action_app(f_app, message);

end

function action_path(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);
genpath_subfolders = genpath(dir_subfolders);
f_path(genpath_subfolders);
end

fprintf('OK!\n')
end

function action_app(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);
fprintf('OK!\n')

end

end

% SUB-PASS FUNCTIONS
function dir_code = get_dir_code()
% 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] = get_type(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
4 changes: 2 additions & 2 deletions combustion_toolbox_contents.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@
% PhD Candidate - Group Fluid Mechanics
% Universidad Carlos III de Madrid
%
% Last update Mar 14 2023
% Last update Apr 07 2023
% -------------------------------------------------------------------------
help combustion_toolbox_contents.m

% Set path
Combustion_Toolbox_setPath;
INSTALL();
% Display splash
gui_display_splash('pause', 2);
% Check for updates
Expand Down
31 changes: 0 additions & 31 deletions combustion_toolbox_set_path.m

This file was deleted.

Binary file removed installer/combustion_toolbox_package.mltbx
Binary file not shown.
4 changes: 2 additions & 2 deletions run_test.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
% Note:
% Stop the build if any tests failed

% Set the path for the combustion toolbox
combustion_toolbox_set_path()
% Set the path for the Combustion Toolbox
INSTALL('install', 'path');

% Run the tests and store the results
results = unit_test().run;
Expand Down

0 comments on commit cf17d5b

Please sign in to comment.