aboutsummaryrefslogtreecommitdiffstats
path: root/lib/grunt/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/grunt/utils.js')
-rw-r--r--lib/grunt/utils.js103
1 files changed, 96 insertions, 7 deletions
diff --git a/lib/grunt/utils.js b/lib/grunt/utils.js
index a3a7e7b2..1c356c18 100644
--- a/lib/grunt/utils.js
+++ b/lib/grunt/utils.js
@@ -1,9 +1,11 @@
var fs = require('fs');
+var path = require('path');
var shell = require('shelljs');
var grunt = require('grunt');
var spawn = require('child_process').spawn;
var semver = require('semver');
-var version;
+var _ = require('lodash');
+var version, pkg;
var CSP_CSS_HEADER = '/* Include this file in your html if you are using the CSP mode. */\n\n';
var PORT_MIN = 8000;
@@ -21,6 +23,23 @@ var getRandomPorts = function() {
];
};
+var getPackage = function() {
+ if ( !pkg ) {
+
+ // Search up the folder hierarchy for the first package.json
+ var packageFolder = path.resolve('.');
+ while ( !fs.existsSync(path.join(packageFolder, 'package.json')) ) {
+ var parent = path.dirname(packageFolder);
+ if ( parent === packageFolder) { break; }
+ packageFolder = parent;
+ }
+ pkg = JSON.parse(fs.readFileSync(path.join(packageFolder,'package.json'), 'UTF-8'));
+
+ }
+
+ return pkg;
+};
+
module.exports = {
@@ -31,9 +50,20 @@ module.exports = {
},
+ getGitRepoInfo: function() {
+ var GITURL_REGEX = /^https:\/\/github.com\/([^\/]+)\/(.+).git$/;
+ var match = GITURL_REGEX.exec(getPackage().repository.url);
+ var git = {
+ owner: match[1],
+ repo: match[2]
+ };
+ return git;
+ },
+
+
getVersion: function(){
if (version) return version;
- var package = JSON.parse(fs.readFileSync('package.json', 'UTF-8'));
+
try {
var gitTag = getTagOfCurrentCommit();
@@ -46,7 +76,7 @@ module.exports = {
// snapshot release
semVerVersion = getSnapshotVersion();
fullVersion = semVerVersion + '-' + getSnapshotSuffix();
- codeName = 'snapshot'
+ codeName = 'snapshot';
}
var versionParts = semVerVersion.match(/(\d+)\.(\d+)\.(\d+)/);
@@ -57,9 +87,12 @@ module.exports = {
minor: versionParts[2],
dot: versionParts[3],
codename: codeName,
- cdn: package.cdnVersion
+ cdn: getPackage().cdnVersion
};
+ // Stable versions have an even minor version
+ version.isStable = version.minor%2 === 0;
+
return version;
} catch (e) {
@@ -69,7 +102,7 @@ module.exports = {
function getTagOfCurrentCommit() {
var gitTagResult = shell.exec('git describe --exact-match', {silent:true});
var gitTagOutput = gitTagResult.output.trim();
- var branchVersionPattern = new RegExp(package.branchVersion.replace('.', '\\.').replace('*', '\\d+'));
+ var branchVersionPattern = new RegExp(getPackage().branchVersion.replace('.', '\\.').replace('*', '\\d+'));
if (gitTagResult.code === 0 && gitTagOutput.match(branchVersionPattern)) {
return gitTagOutput;
} else {
@@ -88,7 +121,7 @@ module.exports = {
}
function getSnapshotVersion() {
- var oldTags = shell.exec('git tag -l v'+package.branchVersion, {silent:true}).output.trim().split('\n');
+ var oldTags = shell.exec('git tag -l v'+getPackage().branchVersion, {silent:true}).output.trim().split('\n');
// ignore non semver versions.
oldTags = oldTags.filter(function(version) {
return version && semver.valid(version);
@@ -102,7 +135,7 @@ module.exports = {
semVerVersion = semver.inc(semVerVersion, 'patch');
}
} else {
- semVerVersion = semver.valid(package.branchVersion.replace(/\*/g, '0'));
+ semVerVersion = semver.valid(getPackage().branchVersion.replace(/\*/g, '0'));
}
return semVerVersion;
}
@@ -114,6 +147,62 @@ module.exports = {
}
},
+ getPreviousVersions: function() {
+ var VERSION_REGEX = /([1-9]\d*)\.(\d+)\.(\d+)(?:-?rc\.?(\d+)|-(snapshot))?/;
+
+ // Pad out a number with zeros at the front to make it `digits` characters long
+ function pad(num, digits) {
+ var zeros = Array(digits+1).join('0');
+ return (zeros+num).slice(-digits);
+ }
+
+ function padVersion(version) {
+ // We pad out the version numbers with 0s so they sort nicely
+ // - Non-Release Candidates get 9999 for their release candidate section to make them appear earlier
+ // - Snapshots get 9 added to the front to move them to the top of the list
+ var maxLength = 4;
+ var padded = (version.snapshot ? '9' : '0') + pad(version.major, maxLength) +
+ pad(version.minor, maxLength) + pad(version.dot, maxLength) +
+ pad(version.rc || 9999, maxLength);
+ return padded;
+ }
+
+ function getVersionFromTag(tag) {
+ var match = VERSION_REGEX.exec(tag);
+ if ( match ) {
+ var version = {
+ tag: tag,
+ major: match[1], minor: match[2], dot: match[3], rc: match[4],
+ snapshot: !!match[5] && getSnapshotSuffix()
+ };
+
+ if(version.snapshot) {
+ version.full = version.major + '.' + version.minor + '.x (edge)';
+ } else {
+ version.full = version.major + '.' + version.minor + '.' + version.dot +
+ (version.rc ? '-rc.' + version.rc : '');
+ }
+
+ // Stable versions have an even minor version and are not a release candidate
+ version.isStable = !(version.minor%2 || version.rc);
+
+ // Versions before 1.0.2 had a different docs folder name
+ version.docsUrl = 'http://code.angularjs.org/' + version.full + '/docs';
+ if ( version.major < 1 || (version.major === 1 && version.minor === 0 && version.dot < 2 ) ) {
+ version.docsUrl += '-' + version.full;
+ }
+
+ return version;
+ }
+ }
+
+ var tags = shell.exec('git tag', {silent: true}).output.split(/\s*\n\s*/);
+ return _(tags)
+ .map(getVersionFromTag)
+ .filter() // getVersion can map to undefined - this clears those out
+ .sortBy(padVersion)
+ .value();
+ },
startKarma: function(config, singleRun, done){
var browsers = grunt.option('browsers');