4 if length(varargin) > 0
5 if strcmp(varargin{1},
'force');
9 % Identify
if the user specified the name with the extension
10 matched = regexp(classInputText,
'^.*\.m',
'start');
12 %Try to strip the path from file name
if any
13 [~, classInputTextStripped] = fileparts(classInputText);
15 % Retrieve the root
class name and the version as regexp tokens
17 tkns = regexp(classInputTextStripped,
'^([a-zA-z0-9_-]+)_v([0-9]+)\.m',
'tokens');
19 tkns = regexp(classInputTextStripped,
'^([a-zA-z0-9_-]+)_v([0-9]+)',
'tokens');
22 % Raise an error
if we fail to parse the input text
23 if sum(size(tkns)) < 2
24 robolog(
'It was not possible to parse either the class name or the version number from the given name',
'ERR');
27 % Retrieve the root
class name and the version from the tokens
29 classRootName = cell2mat(tokens(1));
31 currentVersion = str2num(cell2mat(tokens(2)));
33 robolog(
'The file name format must be ClassName_vX.m, where X is an integer number.',
'ERR');
36 className = sprintf(
'%s_v%d', classRootName, currentVersion);
37 classFileName = sprintf(
'%s_v%d.m', classRootName, currentVersion);
38 newVersion = currentVersion + 1;
39 newClassName = sprintf(
'%s_v%d', classRootName, newVersion);
40 newClassFileName = sprintf(
'%s_v%d.m', classRootName, newVersion);
42 % Check
if input
class file exists
43 if exist(classFileName, 'file') ~= 2
44 robolog(
'The specified class file doesn''t exist.',
'ERR');
47 % Check that the
class file is git-clean, so we avoid loosing modifications tracking.
48 % The check can be performed in Linux but I think it
's not possible in Windows because the git 49 % executable is not in the PATH, so let's just print a warning
51 [a, isModified] = system([
'git ls-files -m ' classFileName]);
52 if findstr(
'fatal: Not a git repository', isModified)
53 robolog('You need to be in a folder within the git repository before running this command.', 'ERR');
55 if ~isempty(isModified) && ~forceEnabled
56 robolog(['The class file is not git-clean.\n' ...
58 ' 1. Stash your files.\n' ...
60 ' 3. Add the newly created class file to git and commit.\n' ...
62 'In this way the newly created file will show the modifications from the ' ...
63 'previous version of the class and everything will be nicely tracked in git.' ...
67 robolog(['The class file should be git-clean before calling this function.\n' ...
69 ' 1. Stash your files.\n' ...
71 ' 3. Add the newly created class file to git and commit.\n' ...
73 'In this way the newly created file will show the modifications from the \n' ...
74 'previous version of the class and everything will be nicely tracked in git.' ...
77 robolog('Can''t determine system platform.','ERR');
79 % Check if output class file exists to avoid overwriting it
80 if exist(newClassFileName, 'file') == 2 && ~forceEnabled
81 robolog('The target class file name already exists!', 'ERR');
84 % Increase the version of the class and change all the internal reference to the
85 % class name (class name, constructor, ecc.)
86 s = fileread(classFileName);
87 s = strrep(s, className, newClassName);
88 s = strrep(s, '%', '%%'); %Escape percentage symbols
89 s = strrep(s, '\', '\\'); %Escape backslash symbols
90 absPath = fileparts(which(classFileName));
91 newClassFullName = [absPath filesep newClassFileName];
92 fid = fopen(newClassFullName, 'w');
95 robolog('New file created: %s', newClassFullName);
function robolog(in msg, in varargin)
This function allows the user to print log messages in a standard way.
function increaseClassVersion(in classInputText, in varargin)
Increase the class version safetly and creates a new class file.