2014-06-23 23:17:31 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2014-03-11 06:34:09 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
|
var path = require('path');
|
|
|
|
|
var shell = require('shelljs');
|
|
|
|
|
var semver = require('semver');
|
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
|
2016-06-14 18:30:38 +01:00
|
|
|
var process = require('process');
|
|
|
|
|
// We are only interested in whether this environment variable exists, hence the !!
|
|
|
|
|
var NO_REMOTE_REQUESTS = !!process.env['NG1_BUILD_NO_REMOTE_VERSION_REQUESTS'];
|
2016-06-15 15:12:30 +02:00
|
|
|
var versionSource = NO_REMOTE_REQUESTS ? 'local' : 'remote';
|
2016-06-14 18:30:38 +01:00
|
|
|
|
2016-07-20 15:45:04 +02:00
|
|
|
var currentPackage, previousVersions, cdnVersion;
|
2016-06-13 20:44:51 +03:00
|
|
|
|
2014-03-11 06:34:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load information about this project from the package.json
|
|
|
|
|
* @return {Object} The package information
|
|
|
|
|
*/
|
|
|
|
|
var getPackage = function() {
|
|
|
|
|
// Search up the folder hierarchy for the first package.json
|
|
|
|
|
var packageFolder = path.resolve('.');
|
2015-01-30 21:24:50 +00:00
|
|
|
while (!fs.existsSync(path.join(packageFolder, 'package.json'))) {
|
2014-03-11 06:34:09 +00:00
|
|
|
var parent = path.dirname(packageFolder);
|
2015-01-30 21:24:50 +00:00
|
|
|
if (parent === packageFolder) { break; }
|
2014-03-11 06:34:09 +00:00
|
|
|
packageFolder = parent;
|
|
|
|
|
}
|
|
|
|
|
return JSON.parse(fs.readFileSync(path.join(packageFolder,'package.json'), 'UTF-8'));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parse the github URL for useful information
|
|
|
|
|
* @return {Object} An object containing the github owner and repository name
|
|
|
|
|
*/
|
|
|
|
|
var getGitRepoInfo = function() {
|
2016-11-25 14:54:49 +00:00
|
|
|
var GITURL_REGEX = /^https:\/\/github.com\/([^/]+)\/(.+).git$/;
|
2014-03-11 06:34:09 +00:00
|
|
|
var match = GITURL_REGEX.exec(currentPackage.repository.url);
|
|
|
|
|
var git = {
|
|
|
|
|
owner: match[1],
|
|
|
|
|
repo: match[2]
|
|
|
|
|
};
|
|
|
|
|
return git;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract the code name from the tagged commit's message - it should contain the text of the form:
|
|
|
|
|
* "codename(some-code-name)"
|
|
|
|
|
* @param {String} tagName Name of the tag to look in for the codename
|
|
|
|
|
* @return {String} The codename if found, otherwise null/undefined
|
|
|
|
|
*/
|
|
|
|
|
var getCodeName = function(tagName) {
|
2016-11-11 19:47:28 +00:00
|
|
|
var gitCatOutput = shell.exec('git cat-file -p ' + tagName, {silent:true}).stdout;
|
2014-03-30 08:57:17 +02:00
|
|
|
var tagMessage = gitCatOutput.match(/^.*codename.*$/mg)[0];
|
2014-03-11 06:34:09 +00:00
|
|
|
var codeName = tagMessage && tagMessage.match(/codename\((.*)\)/)[1];
|
|
|
|
|
if (!codeName) {
|
2016-08-10 12:13:14 +02:00
|
|
|
throw new Error('Could not extract release code name. The message of tag ' + tagName +
|
|
|
|
|
' must match \'*codename(some release name)*\'');
|
2014-03-11 06:34:09 +00:00
|
|
|
}
|
|
|
|
|
return codeName;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-05-21 09:13:46 +01:00
|
|
|
* Compute a build segment for the version, from the CI build number and current commit SHA
|
2014-03-11 06:34:09 +00:00
|
|
|
* @return {String} The build segment of the version
|
|
|
|
|
*/
|
|
|
|
|
function getBuild() {
|
2016-11-11 19:47:28 +00:00
|
|
|
var hash = shell.exec('git rev-parse --short HEAD', {silent: true}).stdout.replace('\n', '');
|
2015-01-30 21:24:50 +00:00
|
|
|
return 'sha.' + hash;
|
2014-03-11 06:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-27 08:19:05 +01:00
|
|
|
function checkBranchPattern(version, branchPattern) {
|
|
|
|
|
// check that the version starts with the branch pattern minus its asterisk
|
|
|
|
|
// e.g. branchPattern = '1.6.*'; version = '1.6.0-rc.0' => '1.6.' === '1.6.'
|
|
|
|
|
return version.slice(0, branchPattern.length - 1) === branchPattern.replace('*', '');
|
|
|
|
|
}
|
2014-03-11 06:34:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If the current commit is tagged as a version get that version
|
|
|
|
|
* @return {SemVer} The version or null
|
|
|
|
|
*/
|
|
|
|
|
var getTaggedVersion = function() {
|
|
|
|
|
var gitTagResult = shell.exec('git describe --exact-match', {silent:true});
|
|
|
|
|
|
2015-01-30 21:24:50 +00:00
|
|
|
if (gitTagResult.code === 0) {
|
2016-11-11 19:47:28 +00:00
|
|
|
var tag = gitTagResult.stdout.trim();
|
2014-03-11 06:34:09 +00:00
|
|
|
var version = semver.parse(tag);
|
2014-03-14 15:59:23 -07:00
|
|
|
|
2016-10-27 20:27:41 +01:00
|
|
|
if (version && checkBranchPattern(version.version, currentPackage.branchPattern)) {
|
2014-03-14 15:59:23 -07:00
|
|
|
version.codeName = getCodeName(tag);
|
2014-03-14 16:26:40 -07:00
|
|
|
version.full = version.version;
|
2015-01-14 20:44:32 +00:00
|
|
|
version.branch = 'v' + currentPackage.branchPattern.replace('*', 'x');
|
2014-03-11 06:34:09 +00:00
|
|
|
return version;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-03-14 15:59:23 -07:00
|
|
|
|
|
|
|
|
return null;
|
2014-03-11 06:34:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a collection of all the previous versions sorted by semantic version
|
|
|
|
|
* @return {Array.<SemVer>} The collection of previous versions
|
|
|
|
|
*/
|
|
|
|
|
var getPreviousVersions = function() {
|
2016-06-14 18:30:38 +01:00
|
|
|
// If we are allowing remote requests then use the remote tags as the local clone might
|
2014-03-12 15:19:48 -07:00
|
|
|
// not contain all commits when cloned with git clone --depth=...
|
2016-06-14 18:30:38 +01:00
|
|
|
// Otherwise just use the tags in the local repository
|
2014-03-13 17:42:43 -07:00
|
|
|
var repo_url = currentPackage.repository.url;
|
2016-06-14 18:30:38 +01:00
|
|
|
var query = NO_REMOTE_REQUESTS ? 'git tag' : 'git ls-remote --tags ' + repo_url;
|
|
|
|
|
var tagResults = shell.exec(query, {silent: true});
|
2016-06-13 20:44:51 +03:00
|
|
|
if (tagResults.code === 0) {
|
2016-11-11 19:47:28 +00:00
|
|
|
return _(tagResults.stdout.match(/v[0-9].*[0-9]$/mg))
|
2016-06-13 20:44:51 +03:00
|
|
|
.map(function(tag) {
|
|
|
|
|
var version = semver.parse(tag);
|
|
|
|
|
return version;
|
|
|
|
|
})
|
|
|
|
|
.filter()
|
|
|
|
|
.map(function(version) {
|
|
|
|
|
// angular.js didn't follow semantic version until 1.20rc1
|
|
|
|
|
if ((version.major === 1 && version.minor === 0 && version.prerelease.length > 0) || (version.major === 1 && version.minor === 2 && version.prerelease[0] === 'rc1')) {
|
|
|
|
|
version.version = [version.major, version.minor, version.patch].join('.') + version.prerelease.join('');
|
|
|
|
|
version.raw = 'v' + version.version;
|
|
|
|
|
}
|
|
|
|
|
version.docsUrl = 'http://code.angularjs.org/' + version.version + '/docs';
|
|
|
|
|
// Versions before 1.0.2 had a different docs folder name
|
|
|
|
|
if (version.major < 1 || (version.major === 1 && version.minor === 0 && version.patch < 2)) {
|
|
|
|
|
version.docsUrl += '-' + version.version;
|
|
|
|
|
version.isOldDocsUrl = true;
|
|
|
|
|
}
|
|
|
|
|
return version;
|
|
|
|
|
})
|
|
|
|
|
.sort(semver.compare)
|
|
|
|
|
.value();
|
2014-03-18 07:11:59 +00:00
|
|
|
} else {
|
2016-06-13 20:44:51 +03:00
|
|
|
return [];
|
2014-03-11 06:34:09 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2014-03-26 15:51:11 -07:00
|
|
|
var getCdnVersion = function() {
|
2016-06-13 20:44:51 +03:00
|
|
|
return _(previousVersions)
|
|
|
|
|
.filter(function(tag) {
|
|
|
|
|
return semver.satisfies(tag, currentPackage.branchVersion);
|
|
|
|
|
})
|
|
|
|
|
.reverse()
|
|
|
|
|
.reduce(function(cdnVersion, version) {
|
|
|
|
|
if (!cdnVersion) {
|
2016-06-14 18:30:38 +01:00
|
|
|
if (NO_REMOTE_REQUESTS) {
|
|
|
|
|
// We do not want to make any remote calls to the CDN so just use the most recent version
|
|
|
|
|
cdnVersion = version;
|
|
|
|
|
} else {
|
|
|
|
|
// Note: need to use shell.exec and curl here
|
|
|
|
|
// as version-infos returns its result synchronously...
|
|
|
|
|
var cdnResult = shell.exec('curl http://ajax.googleapis.com/ajax/libs/angularjs/' + version + '/angular.min.js ' +
|
2016-06-15 15:12:30 +02:00
|
|
|
'--head --write-out "%{http_code}" -silent',
|
2016-06-14 18:30:38 +01:00
|
|
|
{silent: true});
|
|
|
|
|
if (cdnResult.code === 0) {
|
2016-06-15 15:12:30 +02:00
|
|
|
// --write-out appends its content to the general request response, so extract it
|
2016-11-11 19:47:28 +00:00
|
|
|
var statusCode = cdnResult.stdout.split('\n').pop().trim();
|
2016-06-14 18:30:38 +01:00
|
|
|
if (statusCode === '200') {
|
|
|
|
|
cdnVersion = version;
|
|
|
|
|
}
|
2014-03-26 15:51:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
2016-06-13 20:44:51 +03:00
|
|
|
}
|
|
|
|
|
return cdnVersion;
|
|
|
|
|
}, null);
|
2014-06-23 23:17:31 +02:00
|
|
|
};
|
2014-03-11 06:34:09 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the unstable snapshot version
|
|
|
|
|
* @return {SemVer} The snapshot version
|
|
|
|
|
*/
|
|
|
|
|
var getSnapshotVersion = function() {
|
2014-06-23 23:17:31 +02:00
|
|
|
var version = _(previousVersions)
|
2014-03-11 06:34:09 +00:00
|
|
|
.filter(function(tag) {
|
|
|
|
|
return semver.satisfies(tag, currentPackage.branchVersion);
|
|
|
|
|
})
|
|
|
|
|
.last();
|
|
|
|
|
|
2015-01-30 21:24:50 +00:00
|
|
|
if (!version) {
|
2014-03-12 15:19:48 -07:00
|
|
|
// a snapshot version before the first tag on the branch
|
2015-01-14 20:44:32 +00:00
|
|
|
version = semver(currentPackage.branchPattern.replace('*','0-beta.1'));
|
2014-03-12 19:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-11 06:34:09 +00:00
|
|
|
// We need to clone to ensure that we are not modifying another version
|
|
|
|
|
version = semver(version.raw);
|
|
|
|
|
|
2020-05-21 09:13:46 +01:00
|
|
|
var ciBuild = process.env.CIRCLE_BUILD_NUM || process.env.BUILD_NUMBER;
|
2014-03-12 15:19:48 -07:00
|
|
|
if (!version.prerelease || !version.prerelease.length) {
|
|
|
|
|
// last release was a non beta release. Increment the patch level to
|
|
|
|
|
// indicate the next release that we will be doing.
|
|
|
|
|
// E.g. last release was 1.3.0, then the snapshot will be
|
2015-12-11 11:29:39 +02:00
|
|
|
// 1.3.1-build.1, which is lesser than 1.3.1 according to the semver!
|
2014-03-12 15:19:48 -07:00
|
|
|
|
|
|
|
|
// If the last release was a beta release we don't update the
|
|
|
|
|
// beta number by purpose, as otherwise the semver comparison
|
|
|
|
|
// does not work any more when the next beta is released.
|
|
|
|
|
// E.g. don't generate 1.3.0-beta.2.build.1
|
|
|
|
|
// as this is bigger than 1.3.0-beta.2 according to semver
|
|
|
|
|
version.patch++;
|
|
|
|
|
}
|
2020-05-21 09:13:46 +01:00
|
|
|
version.prerelease = ciBuild ? ['build', ciBuild] : ['local'];
|
2014-03-11 06:34:09 +00:00
|
|
|
version.build = getBuild();
|
|
|
|
|
version.codeName = 'snapshot';
|
|
|
|
|
version.isSnapshot = true;
|
|
|
|
|
version.format();
|
|
|
|
|
version.full = version.version + '+' + version.build;
|
2014-09-26 20:51:55 +01:00
|
|
|
version.branch = 'master';
|
2017-10-16 18:55:12 +02:00
|
|
|
version.distTag = currentPackage.distTag;
|
2014-03-11 06:34:09 +00:00
|
|
|
|
|
|
|
|
return version;
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-13 20:44:51 +03:00
|
|
|
|
2014-03-11 06:34:09 +00:00
|
|
|
exports.currentPackage = currentPackage = getPackage();
|
2016-07-20 15:45:04 +02:00
|
|
|
exports.gitRepoInfo = getGitRepoInfo();
|
2014-03-11 06:34:09 +00:00
|
|
|
exports.previousVersions = previousVersions = getPreviousVersions();
|
2014-03-26 15:51:11 -07:00
|
|
|
exports.cdnVersion = cdnVersion = getCdnVersion();
|
2014-03-11 06:34:09 +00:00
|
|
|
exports.currentVersion = getTaggedVersion() || getSnapshotVersion();
|
2016-06-14 18:30:38 +01:00
|
|
|
|
|
|
|
|
if (NO_REMOTE_REQUESTS) {
|
|
|
|
|
console.log('==============================================================================================');
|
|
|
|
|
console.log('Running with no remote requests for version data:');
|
|
|
|
|
console.log(' - this is due to the "NG1_BUILD_NO_REMOTE_VERSION_REQUESTS" environment variable being defined.');
|
|
|
|
|
console.log(' - be aware that the generated docs may not have valid or the most recent version information.');
|
|
|
|
|
console.log('==============================================================================================');
|
|
|
|
|
}
|
2016-06-15 15:12:30 +02:00
|
|
|
|
|
|
|
|
console.log('CDN version (' + versionSource + '):', cdnVersion ? cdnVersion.raw : 'No version found.');
|
|
|
|
|
console.log('Current version (' + versionSource + '):', exports.currentVersion.raw);
|