Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: clean up editor-version #126

Merged
merged 7 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 80 additions & 37 deletions src/types/editor-version.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,77 @@
/**
* Describes a version of a Unity editor. Mostly this follows calendar-versioning,
* with some extra rules for chinese releases.
* @see https://calver.org/
*/
export type EditorVersion = {
type LocaleCode = "c";

type ReleaseFlag = "a" | "b" | "f" | "c";

type RegularVersion = {
/**
* The major version. This is the release year.
*/
major: number;
/**
* The minor version. This is usually a number from 1 to 3.
*/
minor: number;
minor: 1 | 2 | 3 | 4;
ComradeVanti marked this conversation as resolved.
Show resolved Hide resolved
};

type PatchVersion = RegularVersion & {
/**
* An optional patch.
* A patch.
*/
patch?: number;
patch: number;
};

type ReleaseVersion = PatchVersion & {
/**
* A flag describing a specific release.
*/
flag?: "a" | "b" | "f" | "c";
flagValue?: 0 | 1 | 2;
flag: ReleaseFlag;
/**
* A specific build.
*/
build?: number;
build: number;
};

type LocalVersion = ReleaseVersion & {
/**
* A flag describing a specific locale build.
*/
loc?: string;
locValue?: number;
loc: LocaleCode;
/**
* The specific build for a locale.
*/
locBuild?: number;
locBuild: number;
};

/**
* Describes a version of a Unity editor. Mostly this follows calendar-versioning,
* with some extra rules for chinese releases.
* @see https://calver.org/
*/
export type EditorVersion = RegularVersion | PatchVersion | LocalVersion;

function localeValue(loc: LocaleCode) {
if (loc === "c") return 1;
throw new Error("Unknown locale");
}

function releaseValue(flag: ReleaseFlag): number {
if (flag === "b") return 1;
if (flag === "f") return 2;
return 0;
}

function isPatch(version: EditorVersion): version is PatchVersion {
return "patch" in version;
}

function isRelease(version: EditorVersion): version is ReleaseVersion {
return isPatch(version) && "flag" in version;
}

function isLocal(version: EditorVersion): version is LocalVersion {
return isRelease(version) && "loc" in version;
}

/**
* Compares two editor versions for ordering.
* @param verA The first version.
Expand All @@ -48,11 +85,11 @@ export const compareEditorVersion = function (
const editorVersionToArray = (ver: EditorVersion) => [
ver.major,
ver.minor,
ver.patch || 0,
ver.flagValue || 0,
ver.build || 0,
ver.locValue || 0,
ver.locBuild || 0,
isPatch(ver) ? ver.patch : 0,
...(isRelease(ver) ? [releaseValue(ver.flag), ver.build] : [0, 0]),
...(isLocal(ver)
? [ver.build, localeValue(ver.loc), ver.locBuild]
: [0, 0, 0]),
];
const arrA = editorVersionToArray(verA);
const arrB = editorVersionToArray(verB);
Expand Down Expand Up @@ -97,27 +134,33 @@ export const tryParseEditorVersion = function (
locBuild?: `${number}`;
};
const regex =
/^(?<major>\d+)\.(?<minor>\d+)(\.(?<patch>\d+)((?<flag>a|b|f|c)(?<build>\d+)((?<loc>c)(?<locBuild>\d+))?)?)?/;
/^(?<major>\d+)\.(?<minor>[1234])(\.(?<patch>\d+)((?<flag>a|b|f|c)(?<build>\d+)((?<loc>c)(?<locBuild>\d+))?)?)?/;
const match = regex.exec(version);
if (!match) return null;
const groups = <RegexMatchGroups>match.groups;
const result: EditorVersion = {

const regular: RegularVersion = {
major: parseInt(groups.major),
minor: parseInt(groups.minor),
minor: parseInt(groups.minor) as ReleaseVersion["minor"],
};
if (groups.patch) result.patch = parseInt(groups.patch);
if (groups.flag) {
result.flag = groups.flag;
if (result.flag == "a") result.flagValue = 0;
if (result.flag == "b") result.flagValue = 1;
if (result.flag == "f") result.flagValue = 2;
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);
}
return result;
if (!groups.patch) return regular;
const patch: PatchVersion = {
...regular,
patch: parseInt(groups.patch),
};

if (!(groups.flag && groups.build)) return patch;
const release: ReleaseVersion = {
...patch,
flag: groups.flag,
build: parseInt(groups.build),
};

if (!(groups.loc && groups.locBuild)) return release;
return {
...release,
loc: groups.loc,
locBuild: parseInt(groups.locBuild),
} satisfies LocalVersion;
};
5 changes: 0 additions & 5 deletions test/test-editor-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe("editor-version", function () {
minor: 2,
patch: 1,
flag: "a",
flagValue: 0,
build: 5,
});
});
Expand All @@ -43,7 +42,6 @@ describe("editor-version", function () {
minor: 2,
patch: 1,
flag: "b",
flagValue: 1,
build: 5,
});
});
Expand All @@ -55,7 +53,6 @@ describe("editor-version", function () {
minor: 2,
patch: 1,
flag: "f",
flagValue: 2,
build: 5,
});
});
Expand All @@ -67,10 +64,8 @@ describe("editor-version", function () {
minor: 2,
patch: 1,
flag: "f",
flagValue: 2,
build: 1,
loc: "c",
locValue: 1,
locBuild: 5,
});
});
Expand Down