forked from excaliburjs/Excalibur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.js
152 lines (130 loc) · 4.5 KB
/
version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const { execSync } = require('child_process');
const semver = require('semver');
function getCiOptions() {
return {
ghToken: process.env.GH_TOKEN || undefined,
appveyorBuild: process.env.APPVEYOR_BUILD_NUMBER || '',
travisBuild: process.env.TRAVIS_BUILD_NUMBER || '',
commit: process.env.TRAVIS_COMMIT || '',
travisPr: process.env.TRAVIS_PULL_REQUEST || 'false',
travisTag: process.env.TRAVIS_TAG || '',
fork: process.env.TRAVIS_REPO_SLUG
};
}
function getCurrentCommit() {
const commit = execSync('git rev-parse HEAD').toString().trim();
return commit;
}
function getLatestTag(commit) {
execSync('git fetch --depth=1000');
execSync('git fetch --all --tags');
const tag = execSync(`git describe --tags ${commit} --abbrev=0`).toString().trim();
return tag;
}
function getLatestVersion() {
const commit = getCurrentCommit();
const tag = getLatestTag(commit);
return tag.match(/^v?([0-9\.]+)$/)[1];
}
function isTaggedRelease(options) {
return !!options.travisTag;
}
function isLocal(options) {
return !options.appveyorBuild && !options.travisBuild;
}
function isPr(options) {
return !options.ghToken && options.travisPr !== 'false';
}
function isFork(options) {
return !options.ghToken && options.fork !== 'excaliburjs/Excalibur';
}
function generateLocalVersion() {
let version = 'local';
try {
execSync('git fetch');
const commit = getCurrentCommit();
version = getLatestVersion();
version = semver.inc(version, 'minor');
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.travisPr;
}
if (isFork(options)) {
return 'fork-' + options.fork;
}
throw Error('Invalid community version');
}
function generateAlphaVersion(options) {
let commit = getCurrentCommit();
let version = getLatestVersion();
// Alpha builds target next projected release pre 1.0
version = semver.inc(version, 'minor');
// Nuget doesn't yet support the + suffix in versions
const appveyVersion = version + '.' + options.appveyorBuild + '-alpha';
const travisVersion = version + '-alpha.' + options.travisBuild + '+' + 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;
} else {
// Else alpha version
version = generateAlphaVersion(ciOptions);
log ? console.log('[alpha]: ' + version) : null;
}
return version;
}
exports.getCiOptions = getCiOptions;
exports.getCurrentCommit = getCurrentCommit;
exports.getLatestTag = getLatestTag;
exports.getLatestVersion = getLatestVersion;
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}`);
}
}
const local = getCiVersion({}, false);
assertContains(local, '-', 'local version');
const pr = getCiVersion({ travisPr: 'somepr', travisBuild: 'somebuild' }, false);
assertContains(pr, 'pr-', 'pr version');
const fork = getCiVersion({ fork: 'somefork', travisPr: 'false', travisBuild: 'somebuild' }, false);
assertContains(fork, 'fork-', 'fork version');
const tagged = getCiVersion({ travisTag: 'v0.0.1', ghToken: 'sometoken', travisBuild: 'somebuild' }, false);
assertContains(tagged, '0.0.1', 'tagged version');
const alpha = getCiVersion({ travisBuild: 'somebuild', ghToken: 'sometoken' }, false);
assertContains(alpha, '-alpha.', 'alpha version');