TrueReality  v0.1.1912
VersionUtil.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
22 #include <trUtil/VersionUtil.h>
23 
24 #include <trUtil/DateTime.h>
25 #include <trUtil/FileUtils.h>
26 #include <trUtil/PathUtils.h>
27 #include <trUtil/StringUtils.h>
29 #include <trUtil/Logging/Log.h>
30 
31 #include <ctime>
32 #include <iomanip>
33 #include <iostream>
34 #include <sstream>
35 
36 namespace trUtil
37 {
38  const std::string VersionUtil::VERSION_FILE_NAME = std::string("Version.trver");
39  const std::string VersionUtil::MAJ_VERSION = std::string("Major");
40  const std::string VersionUtil::MIN_VERSION = std::string("Minor");
41  const std::string VersionUtil::YYMM_VERSION = std::string("YYMM");
42  const std::string VersionUtil::BUILD_VERSION = std::string("Build");
43 
45  VersionUtil::VersionUtil() : VersionUtil(VERSION_FILE_NAME, trUtil::PathUtils::GetConfigPath(), PathUtils::GetRootPath())
46  {}
47 
49  VersionUtil::VersionUtil(std::string fileName) : VersionUtil(fileName, trUtil::PathUtils::GetConfigPath(), PathUtils::GetRootPath())
50  {}
51 
53  VersionUtil::VersionUtil(std::string fileName, std::string filePath) : VersionUtil(fileName, filePath, PathUtils::GetRootPath())
54  {}
55 
57  VersionUtil::VersionUtil(std::string fileName, std::string filePath, std::string repoPath)
58  {
59  mRepoPath = repoPath;
60 
61  mVersion.SetFileName(fileName);
62  mVersion.SetFilePath(filePath);
63 
64  //If there is no version file, create one
65  if (mVersion.FileExists())
66  {
67  mVersion.ReadFromFile(fileName);
68  }
69  else
70  {
71  std::string errMsg = "Version File does not exist, generating a new one...";
72  LOG_W("Looking for file in : " + mVersion.GetFilePath())
73  LOG_W(errMsg)
74  std::cerr << errMsg << std::endl;
77  }
78  }
79 
82  {
83  }
84 
87  {
89  }
90 
92  void VersionUtil::SetVersion(int maj, int min, std::string yymm, int build)
93  {
94  SetMajorVersion(maj);
95  SetMinorVersion(min);
96  SetYYMMVersion(yymm);
97  SetBuildVersion(build);
98  }
99 
101  void VersionUtil::SetVersion(int maj, int min, int yymm, int build)
102  {
103  SetMajorVersion(maj);
104  SetMinorVersion(min);
105  SetYYMMVersion(yymm);
106  SetBuildVersion(build);
107  }
108 
111  {
113 
114  }
115 
118  {
121  }
122 
125  {
127  }
128 
131  {
132  return mVersion.GetInt(MAJ_VERSION);
133  }
134 
137  {
139  }
140 
143  {
144  return mVersion.GetInt(MIN_VERSION);
145  }
146 
148  void VersionUtil::SetYYMMVersion(std::string yymm)
149  {
151  }
152 
155  {
157  }
158 
161  {
163  }
164 
167  {
168  mVersion.SetInt(BUILD_VERSION, build);
169  }
170 
173  {
174  return mVersion.GetInt(BUILD_VERSION);
175  }
178  {
179  std::stringstream ver;
180 
181  ver << GetMajorVersion() << "." << GetMinorVersion() << "." << GetYYMMVersion() << "." << GetBuildVersion();
182 
183  if (GetMajorVersion() == 0 && GetMinorVersion() == 0)
184  {
185  LOG_E("Version should not be 0.0, something is wrong...")
186  }
187  return ver.str();
188  }
189 
192  {
193  DateTime date(time(0)); //Gets todays date
194  std::stringstream ver;
195  std::string temp;
196 
197  //Convert the year to a string
198  ver << date.GetYear();
199  temp = ver.str();
200 
201  //Check if we have the full year format of 4 characters ex: "1999" and grab the last two
202  if (temp.size() == 4)
203  {
204  ver.str(std::string()); //Clears the stream
205  ver << temp[2] << temp[3];
206  }
207  else
208  {
209  //If the format is wrong, return nothing
210  return "";
211  }
212 
213  //Appends the month value, making the month always have 2 digits. Ex 6 would be 06
214  ver << std::setfill('0') << std::setw(2) << date.GetMonth();
215 
216  return ver.str();
217  }
218 
221  {
222  try
223  {
224  //Try to get the version number from HG or GIT repo. If HG repo is not present, try GIT, if GIT is not present, fail.
225 
226  //Get the revision number string from the HG repo
227  std::cerr << "\nChecking for an HG repo..." << std::endl;
228  std::string rev = trUtil::FileUtils::GetInstance().RunCommand("hg -R " + mRepoPath + " identify --num");
229 
231  {
232  //Get the revision number string from the GIT repo
233  std::cerr << "\nChecking for a GIT repo..." << std::endl;
234  rev = trUtil::FileUtils::GetInstance().RunCommand("git -C " + mRepoPath + " rev-list --count HEAD");
235 
237  {
238  LOG_E("No .hg or .git folders found at the repo path. \nRepo Path: " + mRepoPath);
239 
241  std::cerr << ".hg (HG Repo) or .git (Git Repo) is needed for this to work. Check that an HG or Git command line client is present." << std::endl;
243  }
244 
245  }
246 
247  //Extract the revision number from the string
250 
251  //Convert the string to an integer
252  return trUtil::StringUtils::FromString<int>(rev);
253  }
254  catch (trUtil::Exception &ex)
255  {
256  ex.LogException();
257  }
258  return -1;
259  }
260 
263  {
264  //Adds +1 to the current commit number to encompass the version commit.
266  }
267 }
static const std::string BUILD_VERSION
Definition: VersionUtil.h:53
void SetBuildVersion(int build)
Sets the Build part of the version string.
void SetMinorVersion(int min)
Sets the Minor part of the version string.
This is the exception class used throughout the engine.
Definition: Exception.h:48
void IncrementVersion()
Increments the version number of the build by one and sets the YYMM to current date.
void GenerateVersionStructure()
Generates a copy of the version file in RAM.
virtual bool ReadFromFile(std::string fileName)
Reads from a file and parses the JSON into a Root node.
Definition: File.cpp:69
void UpdateVersion()
Updates the version file to the most recent HG/Git Revision and YTMM.
std::string GetYYMMVersion()
Gets the Year/Month part of the version string.
virtual void SetString(const std::string &key, const std::string &value) override
Sets the String to be stored at the given key.
Definition: File.cpp:398
void SetVersion(int maj, int min, std::string yymm, int build)
Sets the version numbers.
Definition: VersionUtil.cpp:92
virtual int GetInt(const std::string &key) const override
Returns the Integer value stored at the given key.
Definition: File.cpp:284
TR_UTIL_EXPORT std::string GetRootPath()
Get the root path to the engine (equivalent to the TR_ROOT environment) If the TR_ROOT environment is...
Definition: PathUtils.cpp:182
std::string RunCommand(const char *cmd)
Runs a command on the console, and returns the console printout in a form of a string.
Definition: FileUtils.cpp:195
static const std::string MIN_VERSION
Definition: VersionUtil.h:51
int GetBuildVersion()
Gets the Build part of the version string.
static const std::string MAJ_VERSION
Definition: VersionUtil.h:50
static const std::string YYMM_VERSION
Definition: VersionUtil.h:52
void SaveVersionFile()
Saves the version file currently in RAM to default location.
Definition: VersionUtil.cpp:86
static const std::string STR_BLANK("")
Constant value for a blank String.
#define LOG_W(msg)
Log a WARNING message.
Definition: Log.h:156
int GetCurrentCommitNum()
Returns the current system HG/Git revision number.
virtual bool FileExists()
Returns true if the file name and path exist on the HD.
Definition: File.cpp:178
virtual void SetFilePath(std::string newPath)
Sets the path of where the file will be read from or written to.
Definition: File.cpp:166
void LogException(trUtil::Logging::LogLevel level=trUtil::Logging::LogLevel::LOG_ERROR) const
Logs the exception to the default logger.
Definition: Exception.cpp:60
int GetMajorVersion()
Gets the Major part of the version string.
virtual std::string GetFilePath() const
Returns the path of where the file is read or written to.
Definition: File.cpp:172
void TR_UTIL_EXPORT FindAndReplace(std::string &modifiedString, const std::string &findWhat, const std::string &replaceWith)
Finds all instances of the findWhat string in the string modifiedString and replaces them with the re...
void SetMajorVersion(int maj)
Sets the Major part of the version string.
static const std::string VERSION_FILE_NAME
Definition: VersionUtil.h:49
virtual const std::string GetString(const std::string &key) const override
Returns the String value stored at the given key.
Definition: File.cpp:392
void TR_UTIL_EXPORT TextColor(TXT_COLOR TextColor)
Changes Text color in the console window....
Definition: TextColor.cpp:34
TR_UTIL_EXPORT std::string GetConfigPath()
Gets configuration path.
Definition: PathUtils.cpp:536
virtual void SetInt(const std::string &key, const int &value) override
Sets the Integer value to be stored at the given key.
Definition: File.cpp:290
A utility to read and control the engines version numbers.
Definition: VersionUtil.h:45
#define LOG_E(msg)
Log an ERROR message.
Definition: Log.h:165
void SetYYMMVersion(std::string yymm)
Sets the Year/Month part of the version string.
std::string GetVersionString()
Returns the full version string.
JSON::File mVersion
Definition: VersionUtil.h:242
virtual bool WriteToFile(std::string fileName)
Writes the internal Root Node to a JSON file.
Definition: File.cpp:123
static FileUtils & GetInstance()
Character separating the parts of a file path.
Definition: FileUtils.h:137
Namespace that holds various utility classes for the engine.
Definition: SmrtPtr.h:208
DEFAULT
Definition: TextColor.h:41
unsigned GetMonth() const
Gets the month.
Definition: DateTime.cpp:501
int GetMinorVersion()
Gets the Minor part of the version string.
unsigned GetYear() const
Gets the year.
Definition: DateTime.cpp:514
virtual void SetFileName(std::string fileName)
Sets a new file name.
Definition: File.cpp:154
std::string mRepoPath
Definition: VersionUtil.h:244
std::string GetTodaysVersionDate()
Returns todays date in the YYMM format as string.
BRIGHT_RED
Definition: TextColor.h:41