-
Notifications
You must be signed in to change notification settings - Fork 58
/
PTKUpdate.m
111 lines (98 loc) · 4.35 KB
/
PTKUpdate.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
function updated = PTKUpdate(varargin)
% PTKUpdate. A script to update the PTK codebase via git
%
%
%
% Licence
% -------
% Part of the TD Pulmonary Toolkit. https://github.com/tomdoel/pulmonarytoolkit
% Author: Tom Doel, 2015. www.tomdoel.com
% Distributed under the GNU GPL v3 licence. Please see website for details.
%
persistent PTK_LastUpdated
force_update = nargin > 0 && strcmp(varargin{1}, 'force');
current_date = date();
updated = false;
% We check for updates at most once per day, to avoid unnecessary
% startup delays
if force_update || isempty(PTK_LastUpdated) || ~strcmp(PTK_LastUpdated, current_date)
PTK_LastUpdated = current_date;
if checkDoNotUpdate() && ~force_update
disp('Note: automatic update checks have been turned off.');
elseif ~isWebsiteFound()
disp('Note: cannot check for updates as cannot connect to repository.');
elseif ~isMasterBranch()
disp('Note: PTK only checks for updates when master branch is checked out.');
else
clearDoNotUpdate();
full_path = mfilename('fullpath');
[rootSourceDir, ~, ~] = fileparts(full_path);
[rootSourceDir, ~, ~] = fileparts(rootSourceDir);
repoList = DepMatRepo('pulmonarytoolkit', 'master', 'https://github.com/tomdoel/pulmonarytoolkit.git', 'pulmonarytoolkit');
depMat = DepMat(repoList, rootSourceDir);
status = depMat.getAllStatus();
switch status
case DepMatStatus.GitNotFound
disp('! Cannot check for updates as git could not be found');
case DepMatStatus.DirectoryNotFound
disp('! Cannot check for updates as the repository could not be found');
case DepMatStatus.NotUnderSourceControl
disp('! Cannot check for updates as this repository does not appear to be under git source control');
case DepMatStatus.FetchFailure
disp('! Cannot check for updates because a failure occurred previously during fetch. Please fix the repository and delete the depmat_fetch_failure file.');
case DepMatStatus.UpToDate
case {DepMatStatus.UpdateAvailable, DepMatStatus.LocalChanges}
answer = questdlg('A new version of PTK is available. Do you wish to update PTK?','Pulmonary Toolkit','Later','Do not ask me again', 'Update','Update');
if strcmp(answer, 'Do not ask me again')
setDoNotUpdateFlag();
elseif strcmp(answer, 'Update')
if depMat.updateAll()
updated = true;
end
end
case DepMatStatus.Conflict
disp('! An update is available but this would cause a conflict. Please update and merge manually.');
case DepMatStatus.GitFailure
disp('! Cannot check for updates because a git command failed to execute.');
end
end
end
end
function setDoNotUpdateFlag
full_path = mfilename('fullpath');
[path_root, ~, ~] = fileparts(full_path);
filename = fullfile(path_root, 'do-not-update-git');
fileHandle = fopen(filename, 'w');
fclose(fileHandle);
end
function doNotUpdate = checkDoNotUpdate
full_path = mfilename('fullpath');
[path_root, ~, ~] = fileparts(full_path);
filename = fullfile(path_root, 'do-not-update-git');
doNotUpdate = (2 == exist(filename, 'file'));
end
function found = isWebsiteFound()
% Checks for basic website connectivity
url = 'https://github.com/tomdoel/pulmonarytoolkit';
% read the URL
try
options = weboptions('Timeout', 1);
webread(url, options);
found = true;
catch
found = false;
end
end
function clearDoNotUpdate()
full_path = mfilename('fullpath');
[path_root, ~, ~] = fileparts(full_path);
filename = fullfile(path_root, 'do-not-update-git');
if (2 == exist(filename, 'file'))
delete(filename);
end
end
function repoExists = isMasterBranch()
[success, branch] = DepMat.execute('git rev-parse --symbolic-full-name --abbrev-ref HEAD');
branch = strtrim(branch);
repoExists = success && strcmp(branch, 'master');
end