Skip to content

Commit

Permalink
Merge pull request cocos#8885 from cocos-creator/v3.2.0
Browse files Browse the repository at this point in the history
support engine version validation (cocos#8874)
  • Loading branch information
SantyWang authored Jun 25, 2021
2 parents 176e23e + ae23df6 commit c31bdbf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"build-api-json": "gulp build-api-json -i index.ts -o ./3d-api-docs/tempJson",
"build-api": "npm run build-api-json && gulp build-3d-api -i index.ts -o ./3d-api-docs -j ./3d-api-docs/tempJson",
"checkout-engine-version": "node ./scripts/checkout-engine-version.js",
"varify-engine-version": "node ./scripts/varify-engine-version.js",
"test": "gulp test"
},
"repository": {
Expand Down
13 changes: 9 additions & 4 deletions scripts/checkout-engine-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const fs = require('fs');
const targetEngineVersion = process.argv[2];
const versionRegExp = /^\d+\.\d+\.\d+$/
if (!targetEngineVersion) {
throw new Error('please specify a target engine version');
console.error('please specify a target engine version');
process.exit(1);
} else if (!versionRegExp.test(targetEngineVersion)) {
throw new Error('please specify a valid engine version');
console.error('please specify a valid engine version');
process.exit(1);
}

/**
Expand All @@ -38,7 +40,10 @@ for (filePath in fileHandlers) {
let content = fs.readFileSync(filePath, 'utf8');
content = handler(content);
if (!content) {
throw new Error('file handler need to return the handle result');
console.error('file handler need to return the handle result');
process.exit(1);
}
fs.writeFileSync(filePath, content, 'utf8');
}
}
console.log('Checkout engine version complete!')
process.exit(0);
30 changes: 30 additions & 0 deletions scripts/varify-engine-version.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require('fs');
const ps = require('path');
const vGit = require('v-git');

const repo = vGit.init(ps.join(__dirname, '../'));
const branchName = repo.branch;
const branchRegExp = /^v?(\d+\.\d+\.\d+)(?:-.*)?$/
const matchResult = branchName.match(branchRegExp);
if (!(matchResult && matchResult[1])) {
console.warn(`Invalid branch name: "${branchName}", skip engine version validation.`)
process.exit(0);
}
const branchVersion = matchResult[1];

const versionRegExpMap = {
'./package.json': /"version": "(.*)"/,
'./cocos/core/global-exports.ts': /engineVersion = '(.*)'/,
};
for (filePath in versionRegExpMap) {
const regExp = versionRegExpMap[filePath];
const content = fs.readFileSync(filePath, 'utf8');
const version = content.match(regExp)[1];
if (version !== branchVersion) {
console.error(`Wrong engine version "${version}" in file "${filePath}", need to match branch name "${branchName}".`);
process.exit(1);
}
}

console.log('Engine version validation complete!');
process.exit(0);

0 comments on commit c31bdbf

Please sign in to comment.