Home > io > getToolboxVersion.m

getToolboxVersion

PURPOSE ^

getToolboxVersion

SYNOPSIS ^

function version = getToolboxVersion(toolbox,fileID,mainBranchFlag)

DESCRIPTION ^

 getToolboxVersion
   Returns the version of a given toolbox, or if not available the latest
   commit hash (7 characters).

   toolbox         string with the toolbox name (e.g. "RAVEN")
   fileID          string with the name of a file that is only found in
                   the corresponding toolbox (e.g. "ravenCobraWrapper.m").
   mainBranchFlag  logical, if true, function will error if the toolbox is
                   not on the main branch (opt, default false).

   version         string containing either the toolbox version or latest
                   commit hash (7 characters).

   Usage: version = getToolboxVersion(toolbox,fileID,mainBranchFlag)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function version = getToolboxVersion(toolbox,fileID,mainBranchFlag)
0002 % getToolboxVersion
0003 %   Returns the version of a given toolbox, or if not available the latest
0004 %   commit hash (7 characters).
0005 %
0006 %   toolbox         string with the toolbox name (e.g. "RAVEN")
0007 %   fileID          string with the name of a file that is only found in
0008 %                   the corresponding toolbox (e.g. "ravenCobraWrapper.m").
0009 %   mainBranchFlag  logical, if true, function will error if the toolbox is
0010 %                   not on the main branch (opt, default false).
0011 %
0012 %   version         string containing either the toolbox version or latest
0013 %                   commit hash (7 characters).
0014 %
0015 %   Usage: version = getToolboxVersion(toolbox,fileID,mainBranchFlag)
0016 toolbox=char(toolbox);
0017 fileID=char(fileID);
0018 
0019 if nargin<3
0020     mainBranchFlag = false;
0021 end
0022 
0023 currentPath = pwd;
0024 version     = '';
0025 
0026 %Try to find root of toolbox:
0027 try
0028     toolboxPath = which(fileID);                %full file path
0029     slashPos    = getSlashPos(toolboxPath);
0030     toolboxPath = toolboxPath(1:slashPos(end)); %folder path
0031     %Go up until the root is found:
0032     D = dir(toolboxPath);
0033     while ~ismember({'.git'},{D.name})
0034         slashPos    = getSlashPos(toolboxPath);
0035         toolboxPath = toolboxPath(1:slashPos(end-1));
0036         D           = dir(toolboxPath);
0037     end
0038     cd(toolboxPath);
0039 catch
0040     disp([toolbox ' toolbox cannot be found'])
0041     version = 'unknown';
0042 end
0043 %Check if in main:
0044 if mainBranchFlag
0045     [~,currentBranch] = system('git rev-parse --abbrev-ref HEAD');
0046     currentBranch = strtrim(currentBranch);
0047     if any([strcmp(currentBranch, "main"), strcmp(currentBranch, "master")])
0048         cd(currentPath);
0049         error(['ERROR: ' toolbox ' not in main (or master) branch. Check-out this branch of ' toolbox ' before submitting model for Git.'])
0050     end
0051 end
0052 %Try to find version file of the toolbox:
0053 if isempty(version)
0054     try
0055         fid     = fopen([toolboxPath 'version.txt'],'r');
0056         version = fscanf(fid,'%s');
0057         fclose(fid);
0058     catch
0059         %If no file available, look up the tag:
0060         try
0061             [~,version] = system('git describe --tags');
0062             version = strtrim(version);
0063             [~,commit] = system('git log -n 1 --format=%H');
0064             commit = commit(1:7);
0065             %If no tag available or commit is part of tag, get commit instead:
0066             if ~isempty(strfind(version,'fatal')) || ~isempty(strfind(version,commit))
0067                 version = ['commit ' commit];
0068             else
0069                 version = strrep(version,'v','');
0070             end
0071         catch
0072             version = 'unknown';
0073         end
0074     end
0075 end
0076 cd(currentPath);
0077 end
0078 
0079 function slashPos = getSlashPos(path)
0080 slashPos = strfind(path,'\');       %Windows
0081 if isempty(slashPos)
0082     slashPos = strfind(path,'/');   %MAC/Linux
0083 end
0084 end

Generated by m2html © 2005