/
startup.m
356 lines (298 loc) · 10.9 KB
/
startup.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
function startup
% Creates current's user path structure and updates svn/git repositories
%
% This function is launched by MATLAB at startup.
%
% SYNOPSIS
%
% startup
%
% INPUT
%
% none
%
% OUTPUT
%
% none
% This file is part of MATLABStarter
%
% MATLABStarter is released under the terms of the Lesser GPL license
% version 3.0: http://www.gnu.org/licenses/lgpl-3.0.txt
%
% Copyright Aaron Ponti 2011 - 2013
% Set environment variables
% =========================================================================
if exist('envvars.m', 'file')
envvars;
end
% Make sure to restore the default search path
% =========================================================================
try
% Change to a non UNC path (to prevent problems in Windows), and then
% restore and save the default path before we build the dynamic path
cd( matlabroot )
warning off
restoredefaultpath;
warning on
savepath;
catch %#ok<CTCH> For back-compatibility
end
% Get all environment variables
% =========================================================================
% MATLABHOME must be defined and point to a valid directory
home = getenv( 'MATLABHOME' );
if isempty(home)
disp( [ 'Error: the environment variable ''MATLABHOME'' ', ...
'was not set.' ] );
disp( [ 'Only MATLAB''s standard functions and toolboxes ', ...
'will be accessible for this session.' ] );
return
end
if exist( home, 'dir' ) == 0
disp( [ 'Error: the directory specified by the environment ', ...
'variable ''MATLABHOME'' does not exists!' ] );
disp( [ 'Only MATLAB''s standard functions and toolboxes ', ...
'will be accessible for this session.' ] );
return
end
% MATLABUSERFOLDER is optional but if set it must point to a valid
% directory
userFolder = getenv( 'MATLABUSERFOLDER' );
if ~isempty( userFolder )
if exist( userFolder, 'dir' ) == 0
disp( [ 'Error: the directory specified by the environment ', ...
'variable ''MATLABUSERFOLDER'' does not exists!' ] );
% Set to empty so that it is ignored later
userFolder = [ ];
end
end
% XTENSIONS is ignored in all platform but in Windows, where it is optional
% Remark: for historical reasons, the addition of ImarisXT extensions
% can be turned off explicitly by setting the environment variable
% XTENSIONS to 'DISABLE'.
if ispc
XTensions = getenv( 'XTENSIONS' );
if isempty(XTensions)
% Ignore
XTensions = 'DISABLE';
end
if strcmpi( XTensions, 'DISABLE' ) == 0
if exist( XTensions, 'dir' ) == 0
disp( ...
[ 'Error: the directory specified by the environment ', ...
'variable ''XTENSIONS'' does not exists!' ] );
disp( [ 'Calling MATLAB accessory functions from Imaris ', ...
'will not be possible.' ] );
end
end
else
XTensions = 'DISABLE';
end
% IMARISPATH is ignored in Linux, and otherwise optional
xtPath = '';
if ispc || ismac
ImarisPath = getenv( 'IMARISPATH' );
if ~isempty(ImarisPath)
if exist( ImarisPath, 'dir' ) == 0
disp( ...
[ 'Error: the directory specified by the environment ', ...
'variable ''IMARISPATH'' does not exists!' ] );
disp( [ 'Calling MATLAB accessory functions from Imaris ', ...
'will not be possible.' ] );
ImarisPath = '';
end
end
% is XTENSIONS defined and valid?
isXTENSIONSdefined = exist('XTensions', 'var' ) && ...
strcmp(XTensions, 'DISABLE') == 0;
% Set directory for automatic update of ImarisXT extension if needed
if isXTENSIONSdefined == 0 && ~isempty(ImarisPath)
if ispc
xtPath = fullfile( ImarisPath, 'XT', 'matlab' );
elseif ismac
xtPath = fullfile( ImarisPath, 'Contents', 'SharedSupport', ...
'XT', 'matlab' );
else
xtPath = '';
end
% Check the full path
if ~isempty( xtPath )
if exist( xtPath, 'dir' ) == 0
disp( ...
[ 'Error: could not find the XT/matlab Imaris ', ...
'subfolder. Expected path: ' ] );
disp( xtPath );
xtPath = '';
end
end
end
end
% =========================================================================
%
% Build the list of directories to be updated
%
% If the passed folder is an svn working copy we add it and do not recurse
% If it is not a working copy, we get the list of first-level subfolders
%
% =========================================================================
% Add MATLABHOME with all its level-one subdirs
% =========================================================================
allDirs = getSubfolderDir( home );
% Add XTENSIONS if needed
% =========================================================================
if strcmp( XTensions, 'DISABLE' ) == 0
allDirs = cat( 2, allDirs, { XTensions } );
end
% Add IMARISPATH/{platform_dependent}/XT/matlab (xtPath) if needed
% =========================================================================
if ~isempty(xtPath)
allDirs = cat( 2, allDirs, { xtPath } );
end
% Add MATLABUSERFOLDER if needed
% =========================================================================
if ~isempty( userFolder )
allDirs = cat( 2, allDirs, getSubfolderDir( userFolder ) );
end
% Add also the directory containing this function -- which might be
% read-only for normal users, so additional care should be taken below
% (this is also a full path)
% =========================================================================
% Add also the directory containing this function
startupDirName = fileparts( which( mfilename ) );
if ~isempty( startupDirName )
allDirs = cat( 2, allDirs, { startupDirName } );
end
% Update all svn and git repositories
% =========================================================================
if exist( 'updateRepositories.m', 'file' ) == 2
updateRepositories( allDirs );
end
% =========================================================================
%
% Now build dynamic path
%
% =========================================================================
fprintf( 1, [ 'Please wait while the dynamic MATLAB and JAVA ', ...
'paths are built...' ] );
% MATLABHOME
% =========================================================================
success = addToPath( home );
if success == 0
disp( [ 'Error: could not create the dynamic MATLAB path ', ...
'specified by the environment variable ''MATLABHOME''.' ] );
disp( [ 'Only MATLAB''s standard functions and toolboxes ', ...
'will be accessible for this session.' ] );
disp( [ 'Please <a href="mailto:aaron.ponti@fmi.ch">report</a> ', ...
'this problem.' ] );
return
end
% User path
% =========================================================================
if ~isempty( userFolder )
success = addToPath( userFolder );
if success == 0
disp( [ 'Error: could not create the dynamic MATLAB ', ...
'path specified by the environment variable ', ...
'''MATLABUSERFOLDER''.' ] );
disp( [ 'Please <a href="mailto:aaron.ponti@fmi.ch">', ...
'report</a> this problem.' ] );
return
end
end
% Imaris path
% =========================================================================
if ~isempty( xtPath )
success = addToPath( xtPath );
if success == 0
disp( [ 'Error: could not create the dynamic MATLAB ', ...
'path specified by the environment variable ', ...
'''IMARISPATH''.' ] );
disp( [ 'Please <a href="mailto:aaron.ponti@fmi.ch">', ...
'report</a> this problem.' ] );
return
end
end
% XTENSIONS
% =========================================================================
if strcmpi( XTensions , 'DISABLE' ) == 0
success = addToPath( XTensions );
if success == 0
disp( [ 'Error: could not create the dynamic MATLAB path ', ...
'specified by the environment variable ''XTENSIONS''.' ] );
disp( [ 'Calling MATLAB accessory functions from Imaris ', ...
'will not be possible.' ] );
disp( [ 'Please <a href="mailto:aaron.ponti@fmi.ch">', ...
'report</a> this problem.' ] );
end
end
% Inform
fprintf( 1, ' Done.\n' );
% Now enter MATLABUSERFOLDER if defined, or MATLABHOME
if ~isempty( userFolder )
cd( userFolder );
% If a file called startup_user.m exists in the MATLABUSERFOLDER,
% then launch it (since we changed the the MATLABUSERFOLDER, the
% function -- if it exists -- will run from here and shadow possible
% other functions with the same name somewhere else in the path).
startupUserFile = fullfile( userFolder, 'startup_user.m' );
if exist( startupUserFile, 'file' ) == 2
fprintf( 1, ...
'Please wait while the user''s statup file is executed...' );
startup_user;
fprintf( 1, ' Done.\n' );
end
else
cd( home );
end
% Inform about the MATLABUSERFOLDER option
fprintf( 1, [ '\n WARNING: Do NOT save the MATLAB path!\n', ...
' If you want to work with your code on shared machines, ', ...
'follow the user\n configuration instructions at ', ...
'http://www.scs2.net/next/index.php?id=130.\n\nReady.\n\n' ] );
% =========================================================================
% =========================================================================
function allDirs = getSubfolderDir( folder )
if exist( folder, 'dir' ) == 0
allDirs = {};
return;
end
% If folder is a subversion working copy, we just return it
if ( exist( [ folder, filesep, '.svn' ], 'dir' ) || ...
exist( [ folder, filesep, '_svn' ], 'dir' ) )
allDirs{ 1 } = folder;
return
end
% Get list of subdirectories
subdirs = dir( folder );
subdirs( [ subdirs.isdir ] == 0 ) = [];
nDirs = numel( subdirs );
% Allocate space for all directories to be updated (or at least checked)
allDirs = cell( 1, nDirs - 2 );
c = 0;
for i = 1 : nDirs
if strcmp( subdirs( i ).name, '..' ) == 0 && ...
strcmp( subdirs( i ).name, '.' ) == 0
% Add only if a a subfolder '.svn' or '_svn' exists
currDirName = [ folder, filesep, subdirs( i ).name ];
if ( exist( [ currDirName, filesep, '.svn' ], 'dir' ) || ...
exist( [ currDirName, filesep, '_svn' ], 'dir' ) || ...
exist( [ currDirName, filesep, '.git' ], 'dir' ) )
c = c + 1;
allDirs{ c } = currDirName;
end
end
end
allDirs = allDirs( 1 : c );
% =========================================================================
function success = addToPath( folder )
success = 1;
try
% This is the modified version of genpath
[ MATLABPATH, JAVAPATH ] = cgenpath( folder );
% Add dynamic MATLAB path
addpath( MATLABPATH );
% Add dynamic JAVA path
javaaddpath( JAVAPATH );
catch
success = 0;
end