From 6bd84d18bf1f7deac1cbf0eaed3f748ef3caf8bc Mon Sep 17 00:00:00 2001 From: Hugo Oliveira Date: Wed, 12 Aug 2020 12:29:36 +1000 Subject: [PATCH] feat(refact): startup path handling & speed This increase speed and reduce the amount of path entries in non-deployments toolbox sessions by more than 10x, since now we ignore git folders and any other symlink/data folder within the root path. --- Util/CellUtils/inCellPartialMatchString.m | 93 +++++++++++++++++++++++ imosToolbox.m | 25 ++++-- 2 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 Util/CellUtils/inCellPartialMatchString.m diff --git a/Util/CellUtils/inCellPartialMatchString.m b/Util/CellUtils/inCellPartialMatchString.m new file mode 100644 index 000000000..489abca29 --- /dev/null +++ b/Util/CellUtils/inCellPartialMatchString.m @@ -0,0 +1,93 @@ +function [is_complete, indexes] = inCellPartialMatchString(xcell,ycell,first_match_only) +% function [indexes,is_complete] = inCellPartialMatchString(xcell, ycell,first_match_only) +% +% Check if each string entry in ycell is within any string entry in xcell. +% +% If all the content of a ycell{n} string +% is found within the string of xcell{m} +% the m index is stored. +% +% For simple partial string matching, use the contains function +% +% See examples for usage. +% +% Inputs: +% +% xcell - a cell with items as strings. +% ycell - a string or another cell with items as strings. +% first_match_only - a boolean to skip subsequent matches of ycell in xcell. +% - Default: false +% +% Output: +% +% is_complete - a boolean to indicate that all ycell items +% were matched. +% indexes - a cell of indexes where matches occur in xcell. +% +% +% Example: +% % complete match +% xnames = {'a','ab','abc','123'}; +% ynames = {'a','3'}; +% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames); +% assert(is_complete) +% assert(isequal({1,2,3,4},indexes)) +% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames,true); +% assert(is_complete) +% assert(isequal({1,4},indexes)) +% +% % incomplete match +% xnames = {'a','b','c','123'}; +% ynames = {'x','y','z','3'}; +% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames); +% assert(~is_complete) +% assert(isequal({4},indexes)) +% +% % no match +% xnames = {'x','y','z'}; +% ynames = {'a','b','c'}; +% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames); +% assert(~is_complete) +% assert(isequal({},indexes)) + +% +% author: hugo.oliveira@utas.edu.au +% +narginchk(2, 3) +if nargin<3 + first_match_only = false; +end + +if ~iscell(ycell) + ycell = {ycell}; +end + +indexes = cell(1,numel(ycell)*numel(xcell)); +ydetection = zeros(1,numel(ycell)); + +c = 0; +for k = 1:numel(ycell) + if ~ischar(ycell{k}) + error('The second argument index `ycell{%d}` is not a string', k); + end + for kk = 1:length(xcell) + if ~ischar(xcell{kk}) + error('The first argument index `xcell{%d}` is not a string', k); + end + if contains(xcell{kk},ycell{k}) + c = c + 1; + indexes{c} = kk; + ydetection(k) = true; + if first_match_only + break + end + end + end +end + +indexes = indexes(1:c); +if isempty(indexes) + indexes = {}; +end +is_complete = all(ydetection); +end diff --git a/imosToolbox.m b/imosToolbox.m index 8f6817868..40120ea96 100644 --- a/imosToolbox.m +++ b/imosToolbox.m @@ -49,16 +49,25 @@ function imosToolbox(auto, varargin) path = ''; if ~isdeployed [path, ~, ~] = fileparts(which('imosToolbox.m')); - % set Matlab path for this session (add all recursive directories to Matlab % path) - searchPath = textscan(genpath(path), '%s', 'Delimiter', pathsep); - searchPath = searchPath{1}; - iPathToRemove = ~cellfun(@isempty, strfind(searchPath, [filesep '.'])); - searchPath(iPathToRemove) = []; - searchPath = cellfun(@(x)([x pathsep]), searchPath, 'UniformOutput', false); - searchPath = [searchPath{:}]; - addpath(searchPath); + addpath(fullfile(path,'Util/Path')); + addpath(fullfile(path,'Util/CellUtils')); + addpath(fullfile(path,'Util/Schema')); + + [~,subfolders] = FilesInFolder(path); + ignored_subfolders = {'.git','.mypy_cache','imos-toolbox/snapshot','imos-toolbox/data','imos-toolbox/dist'}; + [~,ignore_indexes] = inCellPartialMatchString(subfolders,ignored_subfolders); + valid_subfolders = popFromCell(subfolders,subfolders([ignore_indexes{:}])); + + rmpath(fullfile(path,'Util/Path')); + rmpath(fullfile(path,'Util/CellUtils')); + rmpath(fullfile(path,'Util/Schema')); + + cell_of_folders_strings = cellfun(@genpath,valid_subfolders,'UniformOutput',false); + all_folders_as_string= cat(2,cell_of_folders_strings{:}); + addpath(path); + addpath(all_folders_as_string); end if isempty(path), path = pwd; end