Skip to content

Commit

Permalink
refactored checkDependencies()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Vincent committed Jul 3, 2016
1 parent 17840eb commit 198f585
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
94 changes: 45 additions & 49 deletions ddToolbox/functions/checkDependencies.m
Original file line number Diff line number Diff line change
@@ -1,82 +1,78 @@
function [sucess] = checkDependencies()
% Install dependencies
function checkDependencies()

% See if we have a userpath

if isempty(userpath)
userpath('reset')
end
dependencies={...
'https://github.com/drbenvincent/mcmc-utils-matlab',...
'https://github.com/altmany/export_fig',...
'https://github.com/drbenvincent/matjags',...
'https://github.com/brian-lau/MatlabProcessManager',...
'https://github.com/brian-lau/MatlabStan'};

installPath = userpath;
installPath = installPath(1:end-1);
originalPath = cd;

display('Installing, or updating, the following dependencies from GitHub')
% THE ALGORITHM
for url=dependencies
cloneOrUpdateDependency(url{:});
end

dependencies={...
'mcmc-utils-matlab', 'https://github.com/drbenvincent/mcmc-utils-matlab';...
'export_fig', 'https://github.com/altmany/export_fig';...
'matjags', 'https://github.com/drbenvincent/matjags';...
'MatlabProcessManager','https://github.com/brian-lau/MatlabProcessManager';...
'MatlabStan','https://github.com/brian-lau/MatlabStan'};
cd(originalPath)
end

display(dependencies);

try
sucess = ensureDependenciesExist(installPath, dependencies);
cd(originalPath)
sucess = true;
catch
cd(originalPath)
sucess = false;
function installPath = defineInstallPath()
if isempty(userpath)
userpath('reset')
end

display(sucess)
installPath = userpath;
% Fix the trailing ":" which only sometimes appears
installPath = removeTrailingColon(installPath);
end


function sucess = ensureDependenciesExist( installPath, dependencies )
addpath(installPath)
% check if dependencies exist on the matlab patch
for n=1:size(dependencies,1)
repoName = dependencies{n,1};
addpath(fullfile(installPath,repoName));
if ~isRepoOnPath(repoName)
address = dependencies{n,2};
sucess = cloneGitHubRepo(address, installPath);
addpath(fullfile(installPath,repoName));
else
sucess = updateGitHubRepo(installPath,repoName);
sucess = true;
end
function str = removeTrailingColon(str)
if str(end)==':'
str(end)='';
end
end

function cloneOrUpdateDependency(url)
displayDependencyToCommandWindow(url);
repoName = getRepoNameFromUrl(url);
%addpath(defineInstallPath()) % < --------do we need to do this??
addpath(fullfile(defineInstallPath(),repoName));
if ~isRepoFolderOnPath(repoName)
cloneGitHubRepo(url, defineInstallPath());
else
updateGitHubRepo(defineInstallPath(),repoName);
end
end

function displayDependencyToCommandWindow(url)
weblinkCode = makeWeblinkCode(url);
displayClickableLinkToCommandWindow(url, weblinkCode);
end

function sucess = cloneGitHubRepo(repoAddress, installPath)
function cloneGitHubRepo(repoAddress, installPath)
try
cd(installPath)
command = sprintf('git clone %s.git', repoAddress);
system(command);
sucess = true;
catch
sucess = false;
error('git clone failed')
end
end

function onPath = isRepoOnPath(repoName)
function onPath = isRepoFolderOnPath(repoName)
onPath = exist(repoName,'dir')==7;
end

function sucess = updateGitHubRepo(installPath,repoName)
function updateGitHubRepo(installPath,repoName)
try
cd(fullfile(installPath,repoName))
fprintf('\n\n%s\n', repoName)
system('git pull');
sucess=true;
catch
sucess = false;
warning('Unable to update GitHub repository')
end
end

function repoName = getRepoNameFromUrl(url)
[~,repoName] = fileparts(url);
end
7 changes: 7 additions & 0 deletions ddToolbox/utils/displayClickableLinkToCommandWindow.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function hyperlink = displayClickableLinkToCommandWindow(text, codeToRun)
% printLink('Do something', 'a=3')

codeToRun = ['matlab: ' codeToRun];
hyperlink = sprintf('<a href="%s">%s</a>', codeToRun, text);
disp(hyperlink)
return
3 changes: 3 additions & 0 deletions ddToolbox/utils/makeWeblinkCode.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function weblinkCode = makeWeblinkCode(url)
weblinkCode = sprintf('web(''%s'')', url);
return

0 comments on commit 198f585

Please sign in to comment.