From ca01120227d062597f033060e7e26fcb1f7c4067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Mon, 18 Apr 2016 10:10:27 +0200 Subject: [PATCH] Use git rev as semver metadata --- Jakefile.js | 28 ++++++++++++++++++---------- build/build.js | 14 ++++++++++++-- build/publish.sh | 2 +- package.json | 1 + src/Leaflet.js | 2 +- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Jakefile.js b/Jakefile.js index b027ad3f..7633f2d4 100644 --- a/Jakefile.js +++ b/Jakefile.js @@ -14,7 +14,7 @@ For a custom build, open build/build.html in the browser and follow the instruct var build = require('./build/build.js'), buildDocs = require('./build/docs'), - version = require('./src/Leaflet.js').version; + git = require('git-rev'); function hint(msg, args) { return function () { @@ -27,6 +27,21 @@ function hint(msg, args) { }; } +// Returns the version string in package.json, plus a semver build metadata if +// this is not an official release +function calculateVersion(officialRelease, callback) { + + var version = require('./package.json').version; + + if (officialRelease) { + callback(version); + } else { + git.short(function(str) { + callback (version + '+' + str); + }); + } +} + desc('Check Leaflet source for errors with ESLint'); task('lint', {async: true}, hint('Checking for JS errors...', 'src --config .eslintrc')); @@ -34,16 +49,9 @@ desc('Check Leaflet specs source for errors with ESLint'); task('lintspec', {async: true}, hint('Checking for specs JS errors...', 'spec/suites --config spec/.eslintrc')); desc('Combine and compress Leaflet source files'); -task('build', {async: true}, function (compsBase32, buildName) { - var v; - - jake.exec('git log -1 --pretty=format:"%h"', {breakOnError: false}, function () { +task('build', {async: true}, function (compsBase32, buildName, officialRelease) { + calculateVersion(officialRelease, function(v){ build.build(complete, v, compsBase32, buildName); - - }).on('stdout', function (data) { - v = version + ' (' + data.toString() + ')'; - }).on('error', function () { - v = version; }); }); diff --git a/build/build.js b/build/build.js index 2bbb2257..b3fa0c4f 100644 --- a/build/build.js +++ b/build/build.js @@ -70,13 +70,23 @@ function loadSilently(path) { } } -function bundleFiles(files, copy) { +// Concatenate the files while building up a sourcemap for the concatenation, +// and replace the line defining L.version with the string prepared in the jakefile +function bundleFiles(files, copy, version) { var node = new SourceNode(null, null, null, ''); node.add(new SourceNode(null, null, null, copy + '(function (window, document, undefined) {')); for (var i = 0, len = files.length; i < len; i++) { var contents = fs.readFileSync(files[i], 'utf8'); + + if (files[i] === 'src/Leaflet.js') { + contents = contents.replace( + new RegExp('version: \'.*\''), + 'version: ' + JSON.stringify(version) + ); + } + var lines = contents.split('\n'); var lineCount = lines.length; var fileNode = new SourceNode(null, null, null, ''); @@ -119,7 +129,7 @@ exports.build = function (callback, version, compsBase32, buildName) { srcFilename = filenamePart + '-src.js', mapFilename = filenamePart + '-src.map', - bundle = bundleFiles(files, copy), + bundle = bundleFiles(files, copy, version), newSrc = bundle.src + '\n//# sourceMappingURL=' + mapFilename, oldSrc = loadSilently(srcPath), diff --git a/build/publish.sh b/build/publish.sh index 619fbcfa..b77e32b4 100755 --- a/build/publish.sh +++ b/build/publish.sh @@ -8,7 +8,7 @@ npm test || exit 1 git checkout -b build -npm run build +jake build[,,true] git add dist/leaflet-src.js dist/leaflet.js dist/leaflet-src.map -f git commit -m "v$VERSION" diff --git a/package.json b/package.json index b8d8ef13..4550c59a 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "devDependencies": { "eslint": "^2.8.0", "eslint-config-mourner": "^2.0.1", + "git-rev": "^0.2.1", "happen": "~0.3.1", "jake": "~8.0.12", "karma": "~0.13.22", diff --git a/src/Leaflet.js b/src/Leaflet.js index 55e607ed..1c3f0935 100644 --- a/src/Leaflet.js +++ b/src/Leaflet.js @@ -1,6 +1,6 @@ var L = { - version: '1.0.0-rc.1' + version: 'dev' }; function expose() {