Robochameleon  v1.0
increaseClassVersion.m
Go to the documentation of this file.
1 
2 function increaseClassVersion(classInputText, varargin)
3  forceEnabled = 0;
4  if length(varargin) > 0
5  if strcmp(varargin{1}, 'force');
6  forceEnabled = 1;
7  end
8  end
9  % Identify if the user specified the name with the extension
10  matched = regexp(classInputText, '^.*\.m', 'start');
11 
12  %Try to strip the path from file name if any
13  [~, classInputTextStripped] = fileparts(classInputText);
14 
15  % Retrieve the root class name and the version as regexp tokens
16  if matched
17  tkns = regexp(classInputTextStripped, '^([a-zA-z0-9_-]+)_v([0-9]+)\.m', 'tokens');
18  else
19  tkns = regexp(classInputTextStripped, '^([a-zA-z0-9_-]+)_v([0-9]+)', 'tokens');
20  end
21 
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');
25  end
26 
27  % Retrieve the root class name and the version from the tokens
28  tokens = tkns{1};
29  classRootName = cell2mat(tokens(1));
30  try
31  currentVersion = str2num(cell2mat(tokens(2)));
32  catch e
33  robolog('The file name format must be ClassName_vX.m, where X is an integer number.', 'ERR');
34  end
35 
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);
41 
42  % Check if input class file exists
43  if exist(classFileName, 'file') ~= 2
44  robolog('The specified class file doesn''t exist.', 'ERR');
45  end
46 
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
50  if isunix
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');
54  end
55  if ~isempty(isModified) && ~forceEnabled
56  robolog(['The class file is not git-clean.\n' ...
57  'You should:\n' ...
58  ' 1. Stash your files.\n' ...
59  ' 2. Call increaseClassVersion.\n' ...
60  ' 3. Add the newly created class file to git and commit.\n' ...
61  ' 4. Call increaseClassVersion with the ''force'' option.\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.' ...
64  ], 'ERR');
65  end
66  elseif ispc
67  robolog(['The class file should be git-clean before calling this function.\n' ...
68  'You should:\n' ...
69  ' 1. Stash your files.\n' ...
70  ' 2. Call increaseClassVersion.\n' ...
71  ' 3. Add the newly created class file to git and commit.\n' ...
72  ' 4. Call increaseClassVersion with the ''force'' option.\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.' ...
75  ], 'WRN');
76  else
77  robolog('Can''t determine system platform.','ERR');
78  end
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');
82  end
83 
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');
93  fprintf(fid, s);
94  fclose(fid);
95  robolog('New file created: %s', newClassFullName);
96 end
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.