Skip to content

Commit

Permalink
fix: reject package installation if current editor version is not qua…
Browse files Browse the repository at this point in the history
…lified (refs: #16)
  • Loading branch information
favoyang committed Nov 15, 2020
1 parent 76855b9 commit d4b15ac
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 52 deletions.
38 changes: 37 additions & 1 deletion lib/cmd-add.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { log } = require("./logger");
const url = require("url");
const {
compareEditorVersion,
env,
fetchPackageInfo,
fetchPackageDependencies,
fetchPackageInfo,
getLatestVersion,
loadManifest,
parseEditorVersion,
parseEnv,
parseName,
saveManifest
Expand Down Expand Up @@ -68,6 +70,40 @@ const _add = async function({ pkg, testables }) {
);
return { code: 1, dirty };
}
const versionInfo = pkgInfo.versions[version];
// verify editor version
if (versionInfo.unity) {
const requiredEditorVersion = versionInfo.unityRelease
? versionInfo.unity + "." + versionInfo.unityRelease
: versionInfo.unity;
if (env.editorVersion) {
const editorVersionResult = parseEditorVersion(env.editorVersion);
const requiredEditorVersionResult = parseEditorVersion(
requiredEditorVersion
);
if (!editorVersionResult) {
log.warn(
"editor.version",
`${env.editorVersion} is unknown. The editor version check is disabled.`
);
}
if (!requiredEditorVersionResult) {
log.error("package.unity", `${requiredEditorVersion} is not valid.`);
return { code: 1, dirty };
}
if (
editorVersionResult &&
requiredEditorVersionResult &&
compareEditorVersion(env.editorVersion, requiredEditorVersion) < 0
) {
log.warn(
"editor.version",
`requires ${requiredEditorVersion} but found ${env.editorVersion}`
);
return { code: 1, dirty };
}
}
}
// pkgsInScope
if (!useUpstream) {
const pkgs = await fetchPackageDependencies({
Expand Down
65 changes: 63 additions & 2 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const isIp = require("is-ip");
const isWsl = require("is-wsl");
const keyFileStorage = require("key-file-storage").default;
const TOML = require("@iarna/toml");
const yaml = require("yaml");

const { execute } = require("./utils/process");
const { getNpmClient } = require("./client");
Expand All @@ -29,6 +30,7 @@ const parseEnv = async function(options, { checkPath }) {
env.upstreamRegistry = "https://packages.unity.com";
env.systemUser = false;
env.wsl = false;
env.editorVersion = null;
// the npmAuth field of .upmconfig.toml
env.npmAuth = {};
// the dict of auth param for npm registry API
Expand Down Expand Up @@ -100,22 +102,37 @@ const parseEnv = async function(options, { checkPath }) {
if (!checkPath) return true;
// cwd
if (options.parent.chdir) {
let cwd = path.resolve(options.parent.chdir);
const cwd = path.resolve(options.parent.chdir);
if (!fs.existsSync(cwd)) {
log.error("env", `can not resolve path ${cwd}`);
return false;
}
env.cwd = cwd;
} else env.cwd = process.cwd();
// manifest path
let manifestPath = path.join(env.cwd, "Packages/manifest.json");
const manifestPath = path.join(env.cwd, "Packages/manifest.json");
if (!fs.existsSync(manifestPath)) {
log.error(
"manifest",
`can not locate manifest.json at path ${manifestPath}`
);
return false;
} else env.manifestPath = manifestPath;
// editor version
const projectVersionPath = path.join(
env.cwd,
"ProjectSettings/ProjectVersion.txt"
);
if (!fs.existsSync(projectVersionPath)) {
log.warn(
"ProjectVersion",
`can not locate ProjectVersion.text at path ${projectVersionPath}`
);
} else {
const projectVersionData = fs.readFileSync(projectVersionPath, "utf8");
const projectVersionContent = yaml.parse(projectVersionData);
env.editorVersion = projectVersionContent.m_EditorVersion;
}
// return
return true;
};
Expand Down Expand Up @@ -375,8 +392,51 @@ const saveUpmConfig = async function(config, configDir) {
log.notice("config", "saved unity config at " + configPath);
};

// Compare unity editor version and return -1, 0, or 1.
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];
}
for (let i = 0; i < arrA.length; i++) {
const valA = arrA[i];
const valB = arrB[i];
if (valA > valB) return 1;
else if (valA < valB) return -1;
}
return 0;
};

// Prase editor version string to groups.
const parseEditorVersion = function(version) {
if (!version) return null;
const regex = /^(?<major>\d+)\.(?<minor>\d+)(\.(?<patch>\d+)((?<flag>a|b|f|c)(?<build>\d+))?)?/;
const match = regex.exec(version);
if (!match) return null;
const groups = match.groups;
const result = {
major: parseInt(groups.major),
minor: parseInt(groups.minor)
};
if (groups.patch) result.patch = parseInt(groups.patch);
if (groups.flag) {
result.flag = groups.flag.toLowerCase();
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);
return result;
};

module.exports = {
cleanCache,
compareEditorVersion,
env,
fetchPackageDependencies,
fetchPackageInfo,
Expand All @@ -388,6 +448,7 @@ module.exports = {
loadUpmConfig,
parseEnv,
parseName,
parseEditorVersion,
saveManifest,
saveUpmConfig
};
59 changes: 19 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"npmlog": "^4.1.2",
"pkginfo": "^0.4.1",
"promptly": "^3.0.3",
"update-notifier": "^4.1.0"
"update-notifier": "^4.1.0",
"yaml": "^1.10.0"
}
}
Loading

0 comments on commit d4b15ac

Please sign in to comment.