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

chore: Update release automation #2230

Merged
merged 1 commit into from
Feb 5, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- run: npm ci
- run: npm run build
- run: npm run build:esm
- run: echo "alpha_version=$(node -e "console.log(require('./version').getCiVersion(null, false));")" >> $GITHUB_ENV
- run: echo "alpha_version=$(node -e "console.log(require('./version').getAlphaVersion());")" >> $GITHUB_ENV
- run: echo $alpha_version
- run: npm --no-git-tag-version version $alpha_version
- run: npm publish --tag next
Expand Down
130 changes: 130 additions & 0 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 @@ -56,7 +56,7 @@
"coveralls": "echo 'Temporarily Remove Coveralls Due To Outage'",
"apidocs": "node scripts/apidocs.js",
"pretty": "prettier --write \"{src,sandbox/src,sandbox/tests}/**/*.{ts,js,json,css,md}\" --config prettier.config.js",
"release": "node scripts/release.js"
"release": "cross-env release=true npm run all"
},
"devDependencies": {
"@babel/core": "7.16.12",
Expand All @@ -78,6 +78,7 @@
"copy-webpack-plugin": "10.2.0",
"copyfiles": "2.4.1",
"coveralls": "3.1.1",
"cross-env": "7.0.3",
"css-loader": "6.5.1",
"eslint": "8.8.0",
"eslint-config-prettier": "8.3.0",
Expand Down
7 changes: 6 additions & 1 deletion scripts/excalibur-version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const replace = require('replace-in-file');
const version = require('../version').getCiVersion();
const versioner = require('../version');
let version = versioner.getAlphaVersion();
if (process.env.release) {
version = versioner.getReleaseVersion();
}
console.log('Replacing __EX_VERSION with:', version);
const options = {
files: 'build/dist/index.js',
from: /process\.env\.__EX_VERSION/g,
Expand Down
6 changes: 3 additions & 3 deletions scripts/release.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const { execSync } = require('child_process');
const readline = require('readline');
const semver = require('semver');
const { getLatestVersion } = require('./version');
const { getReleaseVersion } = require('./version');

function generatePatchVersion() {
let version = getLatestVersion();
let version = getReleaseVersion();
version = semver.inc(version, 'patch');
return version;
}
const currentVersion = getLatestVersion();
const currentVersion = getReleaseVersion();
const version = generatePatchVersion();

const rl = readline.createInterface({
Expand Down
130 changes: 10 additions & 120 deletions version.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
const { execSync } = require('child_process');
const semver = require('semver');
const package = require('./package.json');

function getCiOptions() {
return {
ghToken: process.env.GH_TOKEN || undefined,
appveyorBuild: process.env.APPVEYOR_BUILD_NUMBER || '',
buildNumber: process.env.GITHUB_RUN_NUMBER || '',
commit: process.env.TRAVIS_COMMIT || '',
isPr: (process.env.GITHUB_HEAD_REF || 'false').replace('/', '-'),
travisTag: process.env.TRAVIS_TAG || '',
fork: process.env.TRAVIS_REPO_SLUG
};
}

function getCurrentCommit() {
const commit = execSync('git rev-parse HEAD').toString().trim();
return commit;
Expand All @@ -23,118 +10,21 @@ function getNextVersion() {
return package.exNextVersion;
}

function isTaggedRelease(options) {
return !!options.travisTag;
}

function isLocal(options) {
return !options.appveyorBuild && !options.buildNumber;
}

function isPr(options) {
return !options.ghToken && options.isPr !== 'false';
}

function isFork(options) {
return !options.ghToken && options.fork !== 'excaliburjs/Excalibur';
}

function generateLocalVersion() {
let version = 'local';
try {
execSync('git fetch');
const commit = getCurrentCommit();
version = getNextVersion();
version = version + '-' + commit.substring(0, 7);
} catch (err) {
console.error(err);
}
return version;
}

function generateTaggedVersion(options) {
const version = options.travisTag.match(/^v?([0-9\.]+)$/)[1];
return version;
}

function generateCommunityVersion(options) {
if (isPr(options)) {
return 'pr-' + options.isPr;
}
if (isFork(options)) {
return 'fork-' + options.fork;
}
throw Error('Invalid community version');
}

function generateAlphaVersion(options) {
function getAlphaVersion() {
let commit = getCurrentCommit();
let version = getNextVersion();

// Nuget doesn't yet support the + suffix in versions
const appveyVersion = version + '.' + options.appveyorBuild + '-alpha';
const travisVersion = version + '-alpha.' + options.buildNumber + '+' + commit.substring(0, 7);

if (options.appveyorBuild) {
return appveyVersion;
} else {
return travisVersion;
}
}

function getCiVersion(ciOptions, log = true) {
if (!ciOptions) {
ciOptions = getCiOptions();
}
let version = 'unknown';
if (isLocal(ciOptions)) {
version = generateLocalVersion(ciOptions);
log ? console.log('[local]: ' + version) : null;
} else if (isPr(ciOptions) || isFork(ciOptions)) {
version = generateCommunityVersion(ciOptions);
log ? console.log('[community]: ' + version) : null;
} else if (isTaggedRelease(ciOptions)) {
version = generateTaggedVersion(ciOptions);
log ? console.log('[release]: ' + version) : null;
if (process.env.GITHUB_RUN_NUMBER) {
return version + '-alpha.' + process.env.GITHUB_RUN_NUMBER + '+' + commit.substring(0, 7);
} else {
// Else alpha version
version = generateAlphaVersion(ciOptions);
log ? console.log('[alpha]: ' + version) : null;
return version + '-alpha.0' + '+' + commit.substring(0, 7);
}
return version;
}

exports.getCiOptions = getCiOptions;
exports.getCurrentCommit = getCurrentCommit;
exports.getNextVersion = getNextVersion;
exports.isTaggedRelease = isTaggedRelease;
exports.isLocal = isLocal;
exports.isFork = isFork;
exports.isPr = isPr;
exports.generateLocalVersion = generateLocalVersion;
exports.generateTaggedVersion = generateTaggedVersion;
exports.generateAlphaVersion = generateAlphaVersion;
exports.generateCommunityVersion = generateCommunityVersion;
exports.getCiVersion = getCiVersion;

// good enough assertions
function assertContains(actual, value, message) {
if (!actual.includes(value)) {
throw Error(`Assertion failed for ${message}`);
}
function getReleaseVersion() {
return package.version;
}

const local = getCiVersion({}, false);
assertContains(local, '-', 'local version');

const pr = getCiVersion({ isPr: 'somepr', buildNumber: 'somebuild' }, false);
assertContains(pr, 'pr-', 'pr version');

const fork = getCiVersion({ fork: 'somefork', isPr: 'false', buildNumber: 'somebuild' }, false);
assertContains(fork, 'fork-', 'fork version');

const tagged = getCiVersion({ travisTag: 'v0.0.1', ghToken: 'sometoken', buildNumber: 'somebuild' }, false);
assertContains(tagged, '0.0.1', 'tagged version');

const alpha = getCiVersion({ buildNumber: 'somebuild', ghToken: 'sometoken' }, false);
assertContains(alpha, '-alpha.', 'alpha version');
module.exports = {
getAlphaVersion: getAlphaVersion,
getReleaseVersion: getReleaseVersion
}
Loading