Skip to content

Commit

Permalink
fix: handle editor version with a location build (2020.1.1f1c1)
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Dec 15, 2020
1 parent aca7917 commit fc3e5de
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 36 deletions.
42 changes: 32 additions & 10 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,12 +413,17 @@ const saveUpmConfig = async function(config, configDir) {
const compareEditorVersion = function(a, b) {
const verA = parseEditorVersion(a);
const verB = parseEditorVersion(b);
let arrA = [verA.major, verA.minor];
let arrB = [verB.major, verB.minor];
if (verA.patch && verB.patch) {
arrA = [verA.major, verA.minor, verA.patch, verA.flagValue, verA.build];
arrB = [verB.major, verB.minor, verB.patch, verB.flagValue, verB.build];
}
const editorVersionToArray = ver => [
ver.major,
ver.minor,
ver.patch || 0,
ver.flagValue || 0,
ver.build || 0,
ver.locValue || 0,
ver.locBuild || 0
];
const arrA = editorVersionToArray(verA);
const arrB = editorVersionToArray(verB);
for (let i = 0; i < arrA.length; i++) {
const valA = arrA[i];
const valB = arrB[i];
Expand All @@ -428,10 +433,23 @@ const compareEditorVersion = function(a, b) {
return 0;
};

// Prase editor version string to groups.
/**
* Prase editor version string to groups.
*
* E.g. 2020.2.0f2c4
* major: 2020
* minor: 2
* patch: 0
* flag: 'f'
* flagValue: 2
* build: 2
* loc: 'c'
* locValue: 1
* locBuild: 4
*/
const parseEditorVersion = function(version) {
if (!version) return null;
const regex = /^(?<major>\d+)\.(?<minor>\d+)(\.(?<patch>\d+)((?<flag>a|b|f|c)(?<build>\d+))?)?/;
const regex = /^(?<major>\d+)\.(?<minor>\d+)(\.(?<patch>\d+)((?<flag>a|b|f|c)(?<build>\d+)((?<loc>c)(?<locBuild>\d+))?)?)?/;
const match = regex.exec(version);
if (!match) return null;
const groups = match.groups;
Expand All @@ -445,9 +463,13 @@ const parseEditorVersion = function(version) {
if (result.flag == "a") result.flagValue = 0;
if (result.flag == "b") result.flagValue = 1;
if (result.flag == "f") result.flagValue = 2;
if (result.flag == "c") result.flagValue = 3;
if (groups.build) result.build = parseInt(groups.build);
}
if (groups.loc) {
result.loc = groups.loc.toLowerCase();
if (result.loc == "c") result.locValue = 1;
if (groups.locBuild) result.locBuild = parseInt(groups.locBuild);
}
if (groups.build) result.build = parseInt(groups.build);
return result;
};

Expand Down
63 changes: 37 additions & 26 deletions test/test-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,16 @@ describe("cmd-core.js", function() {
});
});
it("test x.y.zcn", function() {
parseEditorVersion("2019.2.1c5").should.deepEqual({
parseEditorVersion("2019.2.1f1c5").should.deepEqual({
major: 2019,
minor: 2,
patch: 1,
flag: "c",
flagValue: 3,
build: 5
flag: "f",
flagValue: 2,
build: 1,
loc: "c",
locValue: 1,
locBuild: 5
});
});
it("test invalid version", function() {
Expand All @@ -454,36 +457,44 @@ describe("cmd-core.js", function() {
});

describe("compareEditorVersion", function() {
it("test x1.y1 == x2.y2", function() {
it("test 2019.1 == 2019.1", function() {
compareEditorVersion("2019.1", "2019.1").should.equal(0);
});
it("test x1.y1 > x2.y2 equals", function() {
it("test 2019.1.1 == 2019.1.1", function() {
compareEditorVersion("2019.1.1", "2019.1.1").should.equal(0);
});
it("test 2019.1.1f1 == 2019.1.1f1", function() {
compareEditorVersion("2019.1.1f1", "2019.1.1f1").should.equal(0);
});
it("test 2019.1.1f1c1 == 2019.1.1f1c1", function() {
compareEditorVersion("2019.1.1f1c1", "2019.1.1f1c1").should.equal(0);
});
it("test 2019.2 > 2019.1", function() {
compareEditorVersion("2019.2", "2019.1").should.equal(1);
});
it("test 2020.2 > 2019.1", function() {
compareEditorVersion("2020.1", "2019.1").should.equal(1);
});
it("test x1.y1 < x2.y2 equals", function() {
it("test 2019.1 < 2019.2", function() {
compareEditorVersion("2019.1", "2019.2").should.equal(-1);
});
it("test 2019.1 < 2020.1", function() {
compareEditorVersion("2019.1", "2020.1").should.equal(-1);
});
it("test x1.y1.z1[flag1]n1 == x2.y2.z2[flag1]n2", function() {
compareEditorVersion("2019.1.1a1", "2019.1.1a1").should.equal(0);
compareEditorVersion("2019.1.1b1", "2019.1.1b1").should.equal(0);
compareEditorVersion("2019.1.1f1", "2019.1.1f1").should.equal(0);
compareEditorVersion("2019.1.1c1", "2019.1.1c1").should.equal(0);
});
it("test x1.y1.z1[flag1]n1 > x2.y2.z2[flag1]n21", function() {
compareEditorVersion("2019.1.1a2", "2019.1.1a1").should.equal(1);
compareEditorVersion("2019.1.2a1", "2019.1.1a1").should.equal(1);
compareEditorVersion("2019.1.1f1", "2019.1.1b1").should.equal(1);
compareEditorVersion("2019.1.1b1", "2019.1.1a1").should.equal(1);
compareEditorVersion("2019.1.1c1", "2019.1.1f1").should.equal(1);
});
it("test x1.y1.z1[flag1]n1 < x2.y2.z2[flag1]n21", function() {
compareEditorVersion("2019.1.1a1", "2019.1.1a2").should.equal(-1);
compareEditorVersion("2019.1.1a1", "2019.1.2a1").should.equal(-1);
compareEditorVersion("2019.1.1b1", "2019.1.1f1").should.equal(-1);
compareEditorVersion("2019.1.1a1", "2019.1.1b1").should.equal(-1);
compareEditorVersion("2019.1.1f1", "2019.1.1c1").should.equal(-1);
it("test 2019.1 < 2019.1.1", function() {
compareEditorVersion("2019.1", "2019.1.1").should.equal(-1);
});
it("test 2019.1.1 < 2019.1.1f1", function() {
compareEditorVersion("2019.1.1", "2019.1.1f1").should.equal(-1);
});
it("test 2019.1.1a1 < 2020.1.1b1", function() {
compareEditorVersion("2019.1.1a1", "2020.1.1b1").should.equal(-1);
});
it("test 2019.1.1b1 < 2020.1.1f1", function() {
compareEditorVersion("2019.1.1b1", "2020.1.1f1").should.equal(-1);
});
it("test 2019.1.1f1 < 2020.1.1f1c1", function() {
compareEditorVersion("2019.1.1f1", "2020.1.1f1c1").should.equal(-1);
});
});

Expand Down

0 comments on commit fc3e5de

Please sign in to comment.