Skip to content

Commit 0111439

Browse files
authored
Semantic Version : ensure Major.Minor.Patch (#1154)
Config files: use always "0.0.0" for PLM if version is empty
1 parent c14e6f5 commit 0111439

File tree

12 files changed

+100
-24
lines changed

12 files changed

+100
-24
lines changed

libs/rtemodel/include/RteItem.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,18 @@ class RteItem : public XmlTreeItem<RteItem>
249249
virtual std::string GetVendorName() const;
250250

251251
/**
252-
* @brief determine value of attribute related to version
253-
* @return value of attribute related to version
254-
*/
252+
* @brief determine value of attribute related to version
253+
* @return value of attribute related to version
254+
*/
255255
virtual const std::string& GetVersionString() const;
256256

257+
/**
258+
* @brief semantic version string of attribute related to version
259+
* @param returnZeroStringIfEmpty returns "0.0.0" if version is empty
260+
* @return value of attribute related to version in semantic version form with meta data removed
261+
*/
262+
virtual const std::string GetSemVer(bool returnZeroStringIfEmpty = false) const;
263+
257264
/**
258265
* @brief determine value of attribute related to API version
259266
* @return value of attribute related to API version

libs/rtemodel/src/RteInstance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -939,10 +939,10 @@ string RteFileInstance::Backup(bool bDeleteExisting)
939939
string thisFile = GetAbsolutePath();
940940
string backupFile = RteFsUtils::BackupFile(thisFile, bDeleteExisting);
941941
// backup .base file if exists
942-
string baseFile = RteUtils::AppendFileBaseVersion(thisFile, GetVersionString());
942+
string baseFile = RteUtils::AppendFileBaseVersion(thisFile, GetSemVer(true));
943943
if (RteFsUtils::Exists(baseFile)) {
944944
// ensure the same backup number as this file
945-
string baseBackupFile = RteUtils::AppendFileBaseVersion(backupFile, GetVersionString());
945+
string baseBackupFile = RteUtils::AppendFileBaseVersion(backupFile, GetSemVer(true));
946946
RteFsUtils::CopyCheckFile(baseFile, baseBackupFile, false);
947947
}
948948
return backupFile;
@@ -972,7 +972,7 @@ bool RteFileInstance::Copy(RteItem* f, bool bMerge)
972972
RteProject* project = GetProject();
973973
if (project) {
974974
// we can use base file backup here, it should already exist
975-
string baseFile = RteUtils::AppendFileBaseVersion(bak, GetVersionString());
975+
string baseFile = RteUtils::AppendFileBaseVersion(bak, GetSemVer(true));
976976
project->MergeFiles(bak, dst, baseFile);
977977
}
978978
}

libs/rtemodel/src/RteItem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "RteConstants.h"
2323

2424
#include "RteFsUtils.h"
25+
#include "VersionCmp.h"
2526
#include "XMLTree.h"
2627
#include "CrossPlatformUtils.h"
2728

@@ -437,6 +438,12 @@ const string& RteItem::GetVersionString() const
437438
}
438439

439440

441+
const std::string RteItem::GetSemVer(bool returnZeroStringIfEmpty) const
442+
{
443+
return VersionCmp::ToSemVer(GetVersionString(), returnZeroStringIfEmpty);
444+
}
445+
446+
440447
void RteItem::RemoveItem(RteItem* item)
441448
{
442449
RemoveChild(item, false);

libs/rtemodel/src/RtePackage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ string RtePackage::DisplayNameFromId(const string& id)
160160

161161
string RtePackage::VersionFromId(const string& id)
162162
{
163-
return VersionCmp::RemoveVersionMeta(RteUtils::GetSuffix(id, RteConstants::PREFIX_PACK_VERSION_CHAR));
163+
return VersionCmp::ToSemVer(RteUtils::GetSuffix(id, RteConstants::PREFIX_PACK_VERSION_CHAR));
164164
}
165165

166166
string RtePackage::ReleaseVersionFromId(const string& id)
@@ -754,7 +754,7 @@ string RtePackage::GetPackageIDfromAttributes(const XmlItem& attr, bool withVers
754754
{
755755
const auto& vendor = attr.GetAttribute("vendor");
756756
const string version = withVersion ?
757-
VersionCmp::RemoveVersionMeta(attr.GetAttribute("version")) : EMPTY_STRING;
757+
VersionCmp::ToSemVer(attr.GetAttribute("version")) : EMPTY_STRING;
758758

759759
return ComposePackageID(vendor, attr.GetAttribute("name"), version, useDots);
760760
}
@@ -777,7 +777,7 @@ string RtePackage::GetPackagePath(bool withVersion) const
777777
path += GetName();
778778
path += "/";
779779
if (withVersion && !GetVersionString().empty()) {
780-
path += VersionCmp::RemoveVersionMeta(GetVersionString());
780+
path += GetSemVer();
781781
path += "/";
782782
}
783783
return path;
@@ -848,7 +848,7 @@ string RtePackageInfo::GetPackagePath(bool withVersion) const
848848
return path;
849849
}
850850
if (withVersion && !GetVersionString().empty()) {
851-
path += VersionCmp::RemoveVersionMeta(GetVersionString()) + "/";
851+
path += GetSemVer() + "/";
852852
}
853853
return path;
854854
}

libs/rtemodel/src/RteProject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ void RteProject::UpdateConfigFileBackups(RteFileInstance* fi, RteItem* f)
681681
string absPath = RteFsUtils::AbsolutePath(fi->GetAbsolutePath()).generic_string();
682682
string dir = RteUtils::ExtractFilePath(absPath, false);
683683
string name = RteUtils::ExtractFileName(absPath);
684-
const string& baseVersion = fi->GetAttribute("version"); // explicitly check the file instance version
685-
const string& updateVersion = f->GetVersionString();
684+
const string baseVersion = VersionCmp::ToSemVer(fi->GetAttribute("version"), true); // explicitly check the file instance version
685+
const string updateVersion = f->GetSemVer(true);
686686
string baseFile = RteUtils::AppendFileBaseVersion(absPath, baseVersion);
687687
if (!RteFsUtils::Exists(baseFile)) {
688688
// create base file if possible

libs/rtemodel/test/src/RteItemTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ TEST(RteItemTest, GetComponentID_all_attributes) {
3131
RteItem item(attributes);
3232
item.SetTag("require");
3333

34+
EXPECT_EQ("9.9.9", item.GetSemVer());
35+
3436
EXPECT_EQ("Class:Group(API)@1.1.1", item.GetApiID(true));
3537
EXPECT_EQ("Class:Group(API)", item.GetApiID(false));
3638

@@ -59,6 +61,8 @@ TEST(RteItemTest, GetComponentID_reduced_attributes) {
5961
};
6062
RteItem item(attributes);
6163
item.SetTag("accept");
64+
65+
EXPECT_EQ("", item.GetSemVer());
6266
EXPECT_EQ("accept Vendor::Class:Group", item.GetDependencyExpressionID());
6367

6468
EXPECT_EQ("Vendor::Class:Group", item.GetComponentID(true));
@@ -94,6 +98,14 @@ TEST(RteItemTest, ComponentAttributesFromId) {
9498
EXPECT_EQ("Class:Group&Variant", item.GetComponentID(true));
9599
}
96100

101+
TEST(RteItemTest, SemVer) {
102+
RteItem item;
103+
EXPECT_EQ("", item.GetSemVer());
104+
EXPECT_EQ("0.0.0", item.GetSemVer(true));
105+
item.SetAttribute("version", "1.0-b+m");
106+
EXPECT_EQ("1.0.0-b", item.GetSemVer());
107+
}
108+
97109
TEST(RteItemTest, PackageID) {
98110

99111
RteItem packInfo;

libs/rteutils/include/VersionCmp.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ class VersionCmp
8787
*/
8888
static std::string Floor(const std::string& v);
8989

90+
/**
91+
* @brief ensure string is in semver format with 0 patch if missing (1.1.1->1.1.1; 1.2 -> 1.1.0; 1.3-b -> 1.3.0-b ; 1 -> 1.0.0)
92+
* @param v version string
93+
* @param returnZeroStringIfEmpty returns "0.0.0" if version is empty
94+
* @return string with minor and patch set to 0 if missing and optionally remove meta data
95+
*/
96+
static std::string ToSemVer(const std::string& v, bool returnZeroStringIfEmpty = false);
9097

9198
/**
9299
* @brief compare string to return mode constant

libs/rteutils/src/VersionCmp.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "AlnumCmp.h"
1818
#include "RteUtils.h"
19+
#include "RteConstants.h"
1920

2021
#include <cstring>
2122

@@ -212,6 +213,28 @@ std::string VersionCmp::Floor(const std::string& v)
212213
}
213214

214215

216+
std::string VersionCmp::ToSemVer(const std::string& v, bool returnZeroStringIfEmpty)
217+
{
218+
if(v.empty()) {
219+
return returnZeroStringIfEmpty? RteConstants::NULL_VERSION : v;
220+
}
221+
string version = RemoveVersionMeta(RteUtils::GetPrefix(v, '-'));
222+
auto dot_count = std::count(version.begin(), version.end(), '.');
223+
if(dot_count >= 2) {
224+
return RemoveVersionMeta(v); // patch and minor is there
225+
}
226+
227+
for(auto i = dot_count; i < 2; ++i) {
228+
version += ".0";
229+
}
230+
string revision = RteUtils::GetSuffix(v, '-');
231+
if(!revision.empty()) {
232+
version += "-" + RemoveVersionMeta(revision);
233+
}
234+
return version;
235+
}
236+
237+
215238
VersionCmp::MatchMode VersionCmp::MatchModeFromString(const std::string& mode)
216239
{
217240
if (mode == "fixed")

libs/rteutils/test/src/RteUtilsTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ TEST(RteUtils, AppendFileVersion)
343343
{
344344
EXPECT_EQ("./foo/[email protected]", RteUtils::AppendFileBaseVersion("./foo/bar.ext", "1.2.0"));
345345
EXPECT_EQ("./foo/[email protected]", RteUtils::AppendFileUpdateVersion("./foo/bar.ext", "1.2.3"));
346-
EXPECT_EQ("./foo/[email protected]", RteUtils::AppendFileBaseVersion("./foo/bar.ext", ""));
347-
EXPECT_EQ("./foo/[email protected]", RteUtils::AppendFileUpdateVersion("./foo/bar.ext", ""));
346+
EXPECT_EQ("./foo/[email protected]", RteUtils::AppendFileBaseVersion("./foo/bar.ext", "0.0.0"));
348347
}
349348

350349
TEST(RteUtilsTest, RemoveVectorDuplicates)

libs/rteutils/test/src/VersionCmpTests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,28 @@ TEST(VersionCmpTest, CeilFloor) {
3434
}
3535

3636

37+
TEST(VersionCmpTest, ToSemVer) {
38+
EXPECT_EQ(VersionCmp::ToSemVer(""), "");
39+
EXPECT_EQ(VersionCmp::ToSemVer("", true), "0.0.0");
40+
41+
EXPECT_EQ(VersionCmp::ToSemVer("2.3"), "2.3.0");
42+
EXPECT_EQ(VersionCmp::ToSemVer("2.3-b"), "2.3.0-b");
43+
EXPECT_EQ(VersionCmp::ToSemVer("2.3+m"), "2.3.0");
44+
EXPECT_EQ(VersionCmp::ToSemVer("2.3-b+m"), "2.3.0-b");
45+
46+
EXPECT_EQ(VersionCmp::ToSemVer("2"), "2.0.0");
47+
EXPECT_EQ(VersionCmp::ToSemVer("2.3.0-b"), "2.3.0-b");
48+
EXPECT_EQ(VersionCmp::ToSemVer("2.3-b"), "2.3.0-b");
49+
EXPECT_EQ(VersionCmp::ToSemVer("2-b"), "2.0.0-b");
50+
EXPECT_EQ(VersionCmp::ToSemVer("2-b"), "2.0.0-b");
51+
}
52+
53+
3754
TEST(VersionCmpTest, VersionCompare) {
55+
EXPECT_EQ( 0, VersionCmp::Compare("1", "1.0.0"));
56+
EXPECT_EQ( 0, VersionCmp::Compare("1.0", "1.0.0"));
57+
EXPECT_EQ( 0, VersionCmp::Compare("1.0.", "1.0.0"));
58+
3859
EXPECT_EQ( -1, VersionCmp::Compare("6.5.0-a", "6.5.0", true));
3960
EXPECT_EQ( 0, VersionCmp::Compare("6.5.0-a", "6.5.0-A", true));
4061
EXPECT_EQ( 0, VersionCmp::Compare("6.5.0+b", "6.5.0+A", true));

0 commit comments

Comments
 (0)