function [fileDepends] = checkProjectDependencies(filelist, projectDir, bQuiet)
%CHECKPROJECTDEPENDENCIES check project files for dependency functions
%
%Usage: fileDepends = checkProjectDependencies(filelist, projectDir)
%
%fileDepends: list of files missing from the main project
%filelist: list of project files
%projectDir: root folder of the project
%bQuiet: report whether there are non-matlab dependencies
%default as empty
fileDepends = [];
%check inputs
if nargin < 2
return;
elseif nargin == 2
bQuiet = true;
end
%select only the m-files for depndency check
funFiles = ~cellfun('isempty', regexpi(filelist, '\.m$', 'once'));
funFiles = filelist(funFiles);
%check dependencies for each file
for f = 1:numel(funFiles)
%get the function file
funFile = funFiles{f};
%check possible dependencies on java functions
extdep = mlint('-calls', funFile);
extdep = {extdep.message};
extfltr = ~cellfun('isempty', regexp(extdep, '(javaaddpath)|(import)', 'once'));
if any(extfltr)
%external java dependencies are used
bExtraDepends(f) = true;
extraDepends(f) = {funFile};
else
bExtraDepends(f) = false;
extraDepends(f) = {''};
end
list = depfun(funFile, '-toponly', '-quiet');
%remove matlab toolbox functions
toolpath = fullfile(matlabroot, 'toolbox');
if ispc()
toolpath_ = regexprep(toolpath, '\\','/');
list_ = regexprep(list, '\\', '/');
toolfltr = cellfun('isempty', regexpi(list_, ['^' toolpath_], 'once'));
else
toolfltr = cellfun('isempty', regexpi(list, ['^' toolpath], 'once'));
end
newlist = list(toolfltr);
%remove the current function under investigation
if ispc()
newlist_ = regexprep(newlist, '\\', '/');
funFile_ = regexprep(funFile, '\\', '/');
selffltr = cellfun('isempty', regexpi(newlist_, [funFile_ '$'], 'once'));
else
selffltr = cellfun('isempty', regexpi(newlist, [funFile '$'], 'once'));
end
newlist = newlist(selffltr);
%remove files from the project folder
if ispc()
newlist_ = regexprep(newlist, '\\', '/');
projectDir_ = regexprep(projectDir, '\\', '/');
rootfltr = cellfun('isempty', regexpi(newlist_, ['^' projectDir_], 'once'));
else
rootfltr = cellfun('isempty', regexpi(newlist, ['^' projectDir], 'once'));
end
newlist = newlist(rootfltr);
if isempty(newlist)
%no dependencies in the form of matlab functions
continue;
end
if isempty(fileDepends)
fileDepends = newlist';
else
%check if items on the new list already exist in fileDepends
doublefltr = cellfun(@(c) strcmpi(newlist, c), fileDepends, 'UniformOutput', false);
doublefltr = cellfun(@any, doublefltr);
if sum(doublefltr) == numel(newlist)
continue;
end
fileDepends = [fileDepends, newlist'];
end
end
%check if there are any extra dependencies not found by depfun
if ~bQuiet && any(bExtraDepends)
msg = {'the following functions had extra dependencies that need to be manually resolved:'};
%check which files have extra dependencies
fltr = ~cellfun('isempty', extraDepends);
extraDepends = extraDepends(fltr);
msg = [msg; extraDepends'];
helpdlg(msg, 'Unresolved dependencies');
end
end