-
Notifications
You must be signed in to change notification settings - Fork 28
/
mlrPath.m
284 lines (260 loc) · 7.73 KB
/
mlrPath.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
% mlrPath.m
%
% $Id:$
% usage: mlrPath
% by: justin gardner
% date: 09/09/14 (from nott.m)
% purpose: allows happy disambiguation of paths for people
% with mrTools and vista installed. Examines what
% paths you have installed and will shut down
% conflicting paths from vista so that you can run
% mrTools - also, will bring back those paths when
% you quit and go back
%
% run without any arguments to switch between mrTools/vistasoft
%
% mlrPath
%
% Or set which one you want to use:
%
% mlrPath mrTools
% % -or-
% mlrPath vistasoft
%
% figure out which mrTools path you are using
%
% mlrPath which
%
% Or switch back to the one you started with
%
% mlrPath revert
%
% You can get the paths for mlr and vista if they exist
% vistaPath = mrGetPref('vistaPath');
% mlrPath = mrGetPref('mlrPath');
%
%
function mlrPath(switchTo)
verbose = true;
% try to get paths for vista and for mlr
mlrRoot = getRootPath('mlr');
vistaRoot = getRootPath('vista');
% try to guess vista from mlr if we didn't get it
if isempty(vistaRoot) && ~isempty(mlrRoot)
vistaRoot = fullfile(fileparts(mlrRoot),'vistasoft');
if ~isdir(vistaRoot), vistaRoot = []; end
end
% try to guess mlr from vista if we didn't get it
if isempty(mlrRoot) && ~isempty(vistaRoot)
mlrRoot = fullfile(fileparts(vistaRoot),'mrTools');
if ~isdir(mlrRoot), mlrRoot = []; end
end
% add matlab utilities if we have it
matlabUtilitiesPath = fullfile(mlrRoot,'mrUtilities');
if ~isempty(mlrRoot) && isempty(findstr(matlabUtilitiesPath,path))
addpath(genpath(matlabUtilitiesPath));
end
% save as prefs
if ~exist('mrSetPref')==2
if ~isempty(mlrRoot), mrSetPref('mlrPath',mlrRoot);end
if ~isempty(vistaRoot), mrSetPref('vistaPath',vistaRoot);end
end
% get which one we are using
whichMLR = fileparts(fileparts(which('mrAlign')));
[dump whichMLRName] = fileparts(whichMLR);
whichMLRName = lower(whichMLRName);
% check for secondary path
hasBothPaths = false;
otherPath = '';
if strcmp(whichMLR,mlrRoot)
if ~isempty(which('mrVista')),hasBothPaths = true;end
otherPath = vistaRoot;
else
if ~isempty(which('mrLoadRet')),hasBothPaths = true;end
otherPath = mlrRoot;
end
% decide which one to switch to.
if (nargin <= 0) || isempty(switchTo)
if strcmp(whichMLR,vistaRoot)
switchTo = mlrRoot;
elseif strcmp(whichMLR,mlrRoot)
switchTo = vistaRoot;
end
end
% get the top level names of mlrRoot
if ~isempty(mlrRoot)
[dump mlrName] = fileparts(mlrRoot);
if isempty(mlrName),mlrName = mlrRoot;end
mlrName = lower(mlrName);
else
% set root to empty string (so it works in fileparts)
mlrName = '';mlrRoot = '';
end
% get the top level names of vistaRoot
if ~isempty(vistaRoot)
[dump vistaName] = fileparts(vistaRoot);
if isempty(vistaName),vistaName = vistaRoot;end
vistaName = lower(vistaName);
else
vistaName = '';vistaRoot = '';
end
if ~isempty(switchTo)
[dump switchToName] = fileparts(switchTo);
if isempty(switchToName),switchToName = switchTo;end
switchToName = lower(switchToName);
else
switchToName = '';switchTo = '';
end
% a few names that the user can put for switchTo
% make these the same as the actual directory name
if any(strcmp(switchToName,{'mlr','mrtools'}))
switchToName = mlrName;
switchTo = mlrRoot;
end
if any(strcmp(switchToName,{'vista','vistasoft'}))
switchToName = vistaName;
switchTo = vistaRoot;
end
% see if we are switching
vistasoftSelectPaths = {};
if any(strcmp(lower(switchToName),{'mrtools+vista'}))
% vistasoft paths to add
vistasoftSelectPaths = {'mrDiffusion','mrMesh','utilities','mrBOLD/Utilities','fileFilters','external/pyrTools'};
switchToName = mlrName;
switchTo = mlrRoot;
end
% user wants to revert to original path that was set
% this happens when mrTools quits
addOtherPath = [];
if any(strcmp(switchToName,{'revert'}))
global mlrOriginalPath;
if isempty(mlrOriginalPath)
% not set in global, so grab from lastPath preference
revertPath = mrGetPref('lastPath');
else
revertPath = mlrOriginalPath;
end
if isempty(revertPath)
return
end
% get the path parameters
[switchTo switchToName] = fileparts(revertPath);
switchTo = fullfile(switchTo,switchToName);
switchToName = lower(switchToName);
% check whether we have to install the other path as well
global mlrOriginalHasBothPaths;
if isequal(mlrOriginalHasBothPaths,true)
if isequal(switchToName,mlrName)
addOtherPath = vistaRoot;
else
addOtherPath = mlrRoot;
end
end
end
% unknown switchTo or missing path
if ~any(strcmp(switchToName,{mlrName,vistaName}))
disp(sprintf('(mlrPath) Currently using: %s',whichMLR));
% check both paths
if hasBothPaths
disp(sprintf('(mlrPath) Secondary path is: %s',otherPath));
end
return
end
% no vista path
if (strcmp(switchToName,vistaName) && isempty(vistaRoot))
disp(sprintf('(mlrPath) Could not find vistasoft path'));
return
end
% already have correct path (and there is only one path)
% so nothing to do
if isequal(whichMLRName,switchToName) && ~hasBothPaths && isempty(addOtherPath)
% check that the vista paths needed are also loaded
missingPath = false;
for i = 1:length(vistasoftSelectPaths)
if exist(vistasoftSelectPaths{i}) == 0
missingPath = true;
end
end
% if all paths are there, then just return
if ~missingPath
return
end
end
% display what we are going to do
if verbose
% check both paths
if hasBothPaths
disp(sprintf('(mlrPath) Had both %s and %s leaving only %s',whichMLR,otherPath,switchTo));
else
if ~isempty(vistasoftSelectPaths)
disp(sprintf('(mlrPath) Switching from %s to %s with select vistasoft paths',whichMLR,switchTo));
else
disp(sprintf('(mlrPath) Switching from %s to %s',whichMLR,switchTo));
end
end
end
% now save what path we are using
global mlrOriginalPath
global mlrOriginalHasBothPaths
if isempty(mlrOriginalPath)
mlrOriginalPath = whichMLR;
mlrOriginalHasBothPaths = hasBothPaths;
end
mrSetPref('lastPath',whichMLR);
% first remove everyone
warning('off','MATLAB:rmpath:DirNotFound')
rmpath(genpath(mlrRoot));
rmpath(genpath(vistaRoot));
warning('on','MATLAB:rmpath:DirNotFound')
% if adding the other path (this happens when we are adding
% back both paths since this is the way they used to be - i.e.
% a revert call. Note this comes first so that it will not have
% precedence in the path
if ~isempty(addOtherPath)
addpath(genpath(addOtherPath));
if verbose
disp(sprintf('(mlrPath) Adding secondary path: %s',addOtherPath));
end
end
% switch path
if strcmp(switchToName,mlrName)
% add mrTools
addpath(genpath(mlrRoot));
addpath(vistaRoot);
% add vistasoft paths that do not conflict
for i = 1:length(vistasoftSelectPaths)
thisPath = fullfile(vistaRoot,vistasoftSelectPaths{i});
if isdir(thisPath)
addpath(genpath(thisPath));
end
end
elseif strcmp(switchToName,vistaName)
% add vista
addpath(genpath(vistaRoot));
% add just the top level directory for mlr (so that we can have this
% function)
addpath(mlrRoot);
else
% add back both
addpath(genpath(vistaRoot));
addpath(genpath(mlrRoot));
end
%%%%%%%%%%%%%%%%%%%%%
% getRootPath %
%%%%%%%%%%%%%%%%%%%%%
function rootPath = getRootPath(packageName)
% get the function that returns the path of the package and pref name
functionName = sprintf('%sRootPath',packageName);
prefName = sprintf('%sPath',packageName);
% see if that function is on the path
if exist(functionName)
rootPath = eval(functionName);
else
rootPath = [];
% try to get the path
if exist('mrGetPref')==2
rootPath = mrGetPref(prefName);
end
end
% make sure it exists
if ~isdir(rootPath),rootPath = [];end