Mercurial > repos > public > matlab_path_manager
view +mpm/MatlabPathManager.m @ 27:05a8b30ee4a7
Make loaded paths persistent across matlab restarts
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Tue, 18 Sep 2018 13:03:53 +0200 |
parents | e2c18217aee0 |
children | 0842a1b2ac7e |
line wrap: on
line source
classdef MatlabPathManager properties projectFileName = '.subpaths'; stateFileName = 'state'; end methods function obj = MatlabPathManager() end function p = installLocation(obj) nameCurrentFile = mfilename('fullpath'); pathParts = split(nameCurrentFile, filesep); p = join(pathParts(1:end-2), filesep); p = p{1}; end function p = stateFilePath(obj) p = fullfile(obj.installLocation(), obj.stateFileName); end % Load a given subpath into the state file and the matlab path and do savepath(), atomically function loadSubpath(obj, p) state = mpm.PersistentState(obj.stateFilePath); addpath(p); state.subpaths(p) = true; state.saveState(); savepath() % after save state to make sure saved paths are always present in the state end % Unload a given subpath from the state file and the matlab path and do savepath(), atomically function unloadSubpath(obj, p) state = mpm.PersistentState(obj.stateFilePath); rmpath(p); savepath() % before save state to make sure saved paths are always present in the state state.subpaths.remove(p); state.saveState(); end % Read project file in a folder and return cell array of all subpaths function sp = projectSubpaths(obj, projectFolder) try fstr = fileread(fullfile(projectFolder, obj.projectFileName)); catch error('Subpath definition file ''%s'' not found.', obj.projectFileName); end sp = splitlines(strtrim(fstr)); end function s = loadedSubpaths(obj) state = mpm.PersistentState(obj.stateFilePath); s = state.subpaths.keys(); % TODO: Make it respect order from the matlab path end function ps = matlabPath(obj) ps = split(path(), pathsep); end % Return all subpaths loaded into the matlab path, mimicing the order they appear there. function s = pathStatus(obj) end % Return all subpaths in the project and if they are active on the matlab path or not. function s = projectStatus(obj, projectFolder) end % Load all subpaths for a project function checkin(obj, projectFolder) sp = obj.projectSubpaths(projectFolder); for i = 1:length(sp) fullSubpath = fullfile(projectFolder, sp{i}); obj.loadSubpath(fullSubpath); end end % Unload all subpaths for a project function checkout(obj, projectFolder) sp = obj.projectSubpaths(projectFolder); for i = 1:length(sp) fullSubpath = fullfile(projectFolder, sp{i}); obj.unloadSubpath(fullSubpath); end end % Check if the projects settings are correct % Is the project file correct? % The right version is in the matlab path % The right version is at the top of the path function b = verify(obj, projectFolder) mpath = obj.matlabPath(); sp = obj.projectSubpaths(projectFolder); for i = 1:length(sp) if ~isAtTop(sp{i}, fullfile(projectFolder, sp{i}), mpath) b = false; return; end end b = true; function b = isAtTop(subpath, fullsubpath, paths) for i = 1:length(paths) if ~endsWith(paths{i}, subpath) continue end b = strcmp(fullsubpath, paths{i}); return end b = false; end % TODO: Refactor this into several methods? end % Unload all loaded subpaths. The matlab path should be returned to it's original state function clear(obj) sp = obj.loadedSubpaths(); for i = 1:length(sp) obj.unloadSubpath(sp{i}); end end end end % TODO: Organize order of methods % TODO: Add flag for being persistent or not? (savepath or not) % TODO: Make is at top a method?