forked from mtex-toolbox/mtex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
startup_mtex.m
388 lines (312 loc) · 12.8 KB
/
startup_mtex.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
function startup_mtex(varargin)
% init MTEX session
%
% This is the startup file for MTEX. In general it is not necessary to edit
% this file. The startup options of MTEX can be edited in the file
% mtex_settings.m in this directory.
%
% this is a bugfix for MATLAB having very high cpu load on idle
if isunix && ~ismac
try
%com.mathworks.mlwidgets.html.HtmlComponentFactory.setDefaultType('HTMLRENDERER');
% to revert:
%com.mathworks.mlwidgets.html.HtmlComponentFactory.setDefaultType([]);
end
end
% Check MATLAB version
% --------------------
lasterr('') %#ok<LERR> %reset all errors
if MATLABverLessThan('7.6')
error(['MTEX can not be installed because your MATLAB version ',version,...
' is outdated and not longer supported by MTEX. The oldest Matlab ',...
'version MTEX has been tested on is Matlab 2012b (vers. 8.0).']);
elseif MATLABverLessThan('8.0')
warning(['MTEX may not be fully functional because your MATLAB version ',version,...
' is outdated and not longer supported by MTEX. The oldest Matlab ',...
'version MTEX has been tested on is Matlab 2012b (vers. 8.0).']);
end
% path to this function to be considered as the root of the MTEX
% installation
local_path = fileparts(mfilename('fullpath'));
% needs installation ?
do_install(local_path);
% initialize MTEX
fprintf('initialize');
%read MTEX version from version file
try
fid = fopen('VERSION','r');
MTEXversion = fgetl(fid);
fclose(fid);
fprintf([' ' MTEXversion ' ']);
catch
MTEXversion = 'MTEX';
end
p();
% setup search path
setMTEXPath(local_path);
p();
% set path to MTEX directories
setMTEXpref('mtexPath',local_path);
setMTEXpref('DataPath',fullfile(local_path,'data'));
setMTEXpref('architecture',computer('arch'));
setMTEXpref('version',MTEXversion);
setMTEXpref('generatingHelpMode',false);
p();
% init settings
mtex_settings;
p();
% check installation
check_installation;
p();
% make help searchable
if isempty(dir(fullfile(local_path,'doc','html','helpsearch*')))
disp('Creating search data base for MTEX documentation.')
builddocsearchdb(fullfile(local_path,'doc','html'));
end
% finish
if isempty(lasterr) % everything fine
fprintf(repmat('\b',1,length(MTEXversion)+18));
else
disp(' done!')
end
if isempty(javachk('desktop')) && ~check_option(varargin,'noMenu')
MTEXmenu;
end
end
% --------- private functions ----------------------
% mtex installation
% -----------------
function do_install(local_path)
% extract matlab search path
cellpath = regexp(path,['(.*?)\' pathsep],'tokens');
cellpath = [cellpath{:}]; %cellpath = regexp(path, pathsep,'split');
% if there is already an MTEX version running
% --> remove it
if isappdata(0,'mtex')
oldMTEX = getappdata(0,'mtex');
if ~strcmpi(oldMTEX.mtexPath,local_path)
rmappdata(0,'mtex');
disp('I found another version of MTEX and remove it from the current search path!');
close all
evalin('base','clear classes')
inst_dir = cellpath(~cellfun('isempty',strfind(cellpath,oldMTEX.mtexPath)));
if ~isempty(inst_dir), rmpath(inst_dir{:}); end
% update search path
cellpath = regexp(path,['(.*?)\' pathsep],'tokens');
cellpath = [cellpath{:}]; %cellpath = regexp(path, pathsep,'split');
local_path = fileparts(mfilename('fullpath'));
end
end
% MTEX is "installed" if the root directory is part of the search path
if any(strcmpi(local_path,cellpath))
setappdata(0,'MTEXInstalled',true);
else
setappdata(0,'MTEXInstalled',false);
end
% there should not be any other directory part of the search path execpt
% for the mtex root directory
% remove all previous mtex pathes from search path
if any(strcmpi(fullfile(local_path,'geometry'),cellpath))
inst_dir = cellpath(~cellfun('isempty',strfind(cellpath,local_path)));
if ~isempty(inst_dir), rmpath(inst_dir{:}); end
end
addpath(local_path);
end
% set MTEX search path
function setMTEXPath(local_path)
exclPath = {'c','data','help','templates'};
if ~MATLABverLessThan('8.1')
exclPath = [exclPath,'8.4'];
end
addpath_recurse(local_path,exclPath);
end
% check MATLAB version
% --------------------
function result = MATLABverLessThan(verstr)
MATLABver = ver('MATLAB');
toolboxParts = getParts(MATLABver(1).Version);
verParts = getParts(verstr);
result = (sign(toolboxParts - verParts) * [1; .1; .01]) < 0;
end
function parts = getParts(V)
parts = sscanf(V, '%d.%d.%d')';
if length(parts) < 3
parts(3) = 0; % zero-fills to 3 elements
end
end
function p()
if isempty(lasterr)
fprintf('.');
end
end
% -----------------------------------------------------------------------
function addpath_recurse(strStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs, blnDebug)
%ADDPATH_RECURSE Adds (or removes) the specified directory and its subfolders
% addpath_recurse(strStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs, blnDebug)
%
% By default, all hidden directories (preceded by '.'), overloaded method directories
% (preceded by '@'), and directories named 'private' or 'CVS' are ignored.
%
% Input Variables
% ===============
% strStartDir::
% Starting directory full path name. All subdirectories (except ignore list) will be added to the path.
% By default, uses current directory.
% caStrsIgnoreDirs::
% Cell array of strings specifying directories to ignore.
% Will also ignore all subdirectories beneath these directories.
% By default, empty list. i.e. {''}.
% strXorIntAddpathMode::
% Addpath mode, either 0/1, or 'begin','end'.
% By default, prepends.
% blnRemDirs::
% Boolean, when true will run function "in reverse", and
% recursively removes directories from starting path.
% By default, false.
% blnDebug::
% Boolean, when true prints debug info.
% By default, false.
%
% Output Variables
% ================
% None. If blnDebug is specified, diagnostic information will print to the screen.
%
% Example(s)
% ==========
% (1) addpath_recurse(); %Take all defaults.
% (2) addpath_recurse('strStartDir'); %Start at 'strStartDir', take other defaults. i.e. Do addpath().
% (3) addpath_recurse('strStartDir', '', 0, true); %Start at 'strStartDir', and undo example (2). i.e. Do rmpath().
% (4) addpath_recurse('strStartDir', '', 'end', false, true); %Do example (2) again, append to path, and display debug info.
% (5) addpath_recurse('strStartDir', '', 1, true, true); %Undo example (4), and display debug info.
%
% See Also
% ========
% addpath()
%
% Developers
% ===========
% Init Name Contact
% ---- --------------- ---------------------------------------------
% AK Anthony Kendall anthony [dot] kendall [at] gmail [dot] com
% JMcD Joe Mc Donnell
%
% Modifications
% =============
% Version Date Who What
% -------- -------- ----- --------------------------------------------------------------------
% 00.00.00 20080808 AK First created.
% 20090410 JMcD Redo input argument processing/checking.
% Only do processing/checking once. Do recursion in separate function.
% 20090411 JMcD Add debugging mode to display run info.
% 20090414 AK Modified variable names, small edits to code, ignoring CSV by default
% 20091104 AK Modified an optimization for Mac compatibility,
% recursive calls to just build the string for
% addpath/rmpath rather than call it each time
%--------------------------------------------------------------------------
%Error messages.
strErrStartDirNoExist = 'Start directory does not exist ???';
strErrIgnoreDirsType = 'Ignore directories must be a string or cell array. See HELP ???';
strErrIllAddpathMode = 'Illegal value for addpath() mode. See HELP ???';
strErrIllRevRecurseRemType = 'Illegal value for reverse recurse remove, must be a logical/boolean. See HELP ??';
strErrWrongNumArg = 'Wrong number of input arguments. See HELP ???';
strAddpathErrMessage = strErrIllAddpathMode;
%Set input args defaults and/or check them.
intNumInArgs = nargin();
assert(intNumInArgs <= 5, strErrWrongNumArg);
if intNumInArgs < 1
strStartDir = pwd();
end
if intNumInArgs < 2
caStrsIgnoreDirs = {''};
end
if intNumInArgs >= 2 && ischar(caStrsIgnoreDirs)
caStrsIgnoreDirs = { caStrsIgnoreDirs };
end
if intNumInArgs < 3 || (intNumInArgs >= 3 && isempty(strXorIntAddpathMode))
strXorIntAddpathMode = 0;
end
if intNumInArgs >= 3 && ischar(strXorIntAddpathMode) %Use 0/1 internally.
strAddpathErrMessage = sprintf('Input arg addpath() mode "%s" ???\n%s', strXorIntAddpathMode, strErrIllAddpathMode);
assert(any(strcmpi(strXorIntAddpathMode, {'begin', 'end'})), strAddpathErrMessage);
strXorIntAddpathMode = strcmpi(strXorIntAddpathMode, 'end'); %When 'end' 0 sets prepend, otherwise 1 sets append.
end
if intNumInArgs < 4
blnRemDirs = false;
end
if intNumInArgs < 5
blnDebug = false;
end
if size(caStrsIgnoreDirs, 1) > 1
caStrsIgnoreDirs = caStrsIgnoreDirs'; %Transpose from column to row vector, in theory.
end
%Check input args OK, before we do the thing.
strErrStartDirNoExist = sprintf('Input arg start directory "%s" ???\n%s', strStartDir, strErrStartDirNoExist);
assert(exist(strStartDir, 'dir') > 0, strErrStartDirNoExist);
assert(iscell(caStrsIgnoreDirs), strErrIgnoreDirsType);
assert(strXorIntAddpathMode == 0 || strXorIntAddpathMode == 1, strAddpathErrMessage);
assert(islogical(blnRemDirs), strErrIllRevRecurseRemType);
assert(islogical(blnDebug), 'Debug must be logical/boolean. See HELP.');
if blnDebug
intPrintWidth = 34;
rvAddpathModes = {'prepend', 'append'};
strAddpathMode = char(rvAddpathModes{ fix(strXorIntAddpathMode) + 1});
strRevRecurseDirModes = { 'false', 'true' };
strRevRecurseDirs = char(strRevRecurseDirModes{ fix(blnRemDirs) + 1 });
strIgnoreDirs = '';
for intD = 1 : length(caStrsIgnoreDirs)
if ~isempty(strIgnoreDirs)
strIgnoreDirs = sprintf('%s, ', strIgnoreDirs);
end
strIgnoreDirs = sprintf('%s%s', strIgnoreDirs, char(caStrsIgnoreDirs{intD}));
end
strTestModeResults = sprintf('... Debug mode, start recurse addpath arguments ...');
strTestModeResults = sprintf('%s\n%*s: "%s"', strTestModeResults, intPrintWidth, 'Start directory', strStartDir);
strTestModeResults = sprintf('%s\n%*s: "%s"', strTestModeResults, intPrintWidth, 'Ignore directories', strIgnoreDirs);
strTestModeResults = sprintf('%s\n%*s: "%s"', strTestModeResults, intPrintWidth, 'addpath() mode', strAddpathMode);
strTestModeResults = sprintf('%s\n%*s: "%s"', strTestModeResults, intPrintWidth, 'Reverse recurse remove directories', strRevRecurseDirs);
disp(strTestModeResults);
end
%Don't print the MATLAB warning if remove path string is not found
if blnRemDirs, warning('off', 'MATLAB:rmpath:DirNotFound'); end
%Build the list of directories
caAddRemDirs = {};
[caAddRemDirs] = addpath_recursively(caAddRemDirs, strStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs,blnDebug);
%Remove or add the directory from the search path
if blnRemDirs
if blnDebug, fprintf('"%s", removing from search path ...', strStartDir); end
rmpath(caAddRemDirs{:})
else
if blnDebug, fprintf('"%s", adding to search path ...', strStartDir); end
addpath(caAddRemDirs{:}, strXorIntAddpathMode);
end
%Restore the warning state for rmpath
if blnRemDirs, warning('on', 'MATLAB:rmpath:DirNotFound'); end
end % function addpath_recurse
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function [caAddRemDirs] = addpath_recursively(caAddRemDirs, strStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs, blnDebug)
%Note:Don't need to check input arguments, because caller already has.
%Add this directory to the add/remove path list
caAddRemDirs = [caAddRemDirs,strStartDir];
strFileSep = filesep();
%Get list of directories beneath the specified directory, this two-step process is faster.
if ispc
saSubDirs = dir(sprintf('%s%s', strStartDir, strFileSep));
else
saSubDirs = dir(strStartDir);
end
saSubDirs = saSubDirs([saSubDirs.isdir]); %Handles files without extensions that otherwise pass through previous filter
%Loop through the directory list and recursively call this function.
for intDirIndex = 1 : length(saSubDirs)
strThisDirName = saSubDirs(intDirIndex).name;
blnIgnoreDir = any(strcmpi(strThisDirName, { 'private', 'CVS', '.', '..', caStrsIgnoreDirs{:} }));
blnDirBegins = any(strncmp(strThisDirName, {'@', '.','+'}, 1));
if ~(blnIgnoreDir || blnDirBegins)
strThisStartDir = sprintf('%s%s%s', strStartDir, strFileSep, strThisDirName);
if blnDebug, fprintf('"%s", recursing ...', strThisStartDir); end
[caAddRemDirs] = addpath_recursively(caAddRemDirs, strThisStartDir, caStrsIgnoreDirs, strXorIntAddpathMode, blnRemDirs, blnDebug);
end
end % for each directory.
end % function addpath_recursively
%--------------------------------------------------------------------------
%__END__