Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Commit

Permalink
Bug #62, Integrate bin/git-version and bin/replace directly in Gr…
Browse files Browse the repository at this point in the history
  • Loading branch information
nfreear committed Sep 7, 2018
1 parent 085caf2 commit 0de2713
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 71 deletions.
14 changes: 13 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,21 @@ module.exports = function (grunt) {
]);

grunt.registerTask('default', [
'gettext', 'sass', 'jshint', 'uglify', 'nice-package'
'gettext', 'sass', 'jshint', 'uglify', 'nice-package', 'git:version.json', 'replace:config+html'
]);

grunt.registerTask('git:version.json', 'Output version.JSON containing Git commit & other version info.', function () {
var done = this.async();

require('./bin/git-version.js')(grunt, done);
});

grunt.registerTask(
'replace:config+html',
'Update a "random" parameter for all <script> and CSS includes in "index.html". Plus, add version, add Google Map API key ...',
function () {
require('./bin/replace.js')(grunt);
});

/* ================================== */

Expand Down
68 changes: 38 additions & 30 deletions bin/git-version.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env node

/*!
Output version.JSON containing Git commit & other version info.
Expand All @@ -8,6 +6,8 @@
@see https://github.com/IET-OU/open-media-player-core/blob/master/src/Gitlib.php
*/

module.exports = git_version;

// USAGE: npm install simple-git --save-dev && node bin/git-version.js --all

var directory = __dirname + '/..'
Expand All @@ -24,35 +24,43 @@ var directory = __dirname + '/..'
lib: 'git-version.js'
};

if (matchArgv('--all')) {
version.extend = {
node_version: process.version,
npm_version: exec('npm --version', null, carryon),
maven_version: exec('mvn --version', split, carryon),
tomcat_version: exec(tomcat, split, carryon),
git_version: exec('git --version').replace(/(git )?(version )?/, '')
};
}
version.describe = exec('git describe --tags --long', null, carryon);
version.branch = exec('git rev-parse --abbrev-ref HEAD');
version.origin = exec('git config --get remote.origin.url');
version.url = version.origin.replace(/git@/, 'https://').replace('.com:', '.com/');

git.log([ '-1' ], function (err, data) {
handleError(err, 'git.log');

var log = data.latest;

version.commit = log.hash;
version.date = log.date;
version.message = log.message;
version.author = '%s <%m>'.replace(/%s/, log.author_name).replace(/%m/, log.author_email);
})
.then(function () {
fs.writeFileSync(json_file, JSON.stringify(version, null, '\t'));
console.error('File written: version.json');
});
function git_version (grunt, done) {

if (matchArgv('--all')) {
version.extend = {
node_version: process.version,
npm_version: exec('npm --version', null, carryon),
maven_version: exec('mvn --version', split, carryon),
tomcat_version: exec(tomcat, split, carryon),
git_version: exec('git --version').replace(/(git )?(version )?/, '')
};
}

version.describe = exec('git describe --tags --long', null, carryon);
version.branch = exec('git rev-parse --abbrev-ref HEAD');
version.origin = exec('git config --get remote.origin.url');
version.url = version.origin.replace(/git@/, 'https://').replace('.com:', '.com/');

git.log([ '-1' ], function (err, data) {
handleError(err, 'git.log');

var log = data.latest;

version.commit = log.hash;
version.date = log.date;
version.message = log.message;
version.author = '%s <%m>'.replace(/%s/, log.author_name).replace(/%m/, log.author_email);
})
.exec(function () { // Was '.then';
fs.writeFileSync(json_file, JSON.stringify(version, null, '\t'));

// console.error('File written: version.json');

grunt.log.ok('File written: version.json');

done();
});
}

// === Utilities ===

Expand Down
81 changes: 43 additions & 38 deletions bin/replace.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/env node

/*!
Update a "random" parameter for all <script> and CSS includes in "index.html". Plus, add version.
@copyright Nick Freear, 15-November-2017.
@link http://www.nquire-it.org/version.json
*/

module.exports = replace_config_js_html;

const replace = require('replace');
const version = require('./../static/src/version.json');
const INDEX_HTML = path('/../static/src/index.html');
Expand All @@ -16,47 +16,52 @@ const RAND = getRandomInt(11, 1000);
const PKG = require('./../package.json');
const ENV = require('./../.env.json');

console.warn('Version.json ~ describe, rand:', version.describe, RAND);
function replace_config_js_html (grunt) {

grunt.log.ok('Version.json ~ describe, rand:', version.describe, RAND);

replace({
paths: [ INDEX_HTML ],
regex: /\.(css|js)\?r=(_RAND_|[^\"]+)/g,
replacement: '.$1?r=' + RAND,
count: true,
recursive: false
});
// console.warn('Version.json ~ describe, rand:', version.describe, RAND);

replace({
paths: [ INDEX_HTML ],
regex: /content="nQuire-it\/(_VERSION_|[^\"]+)"/,
replacement: 'content="nQuire-it/%s"'.replace(/%s/, version.describe),
count: true,
recursive: false
});
replace({
paths: [ INDEX_HTML ],
regex: /\.(css|js)\?r=(_RAND_|[^\"]+)/g,
replacement: '.$1?r=' + RAND,
count: true,
recursive: false
});

replace({
paths: [ INDEX_HTML ],
regex: /\/maps\/api\/js\?key=(_GOOGLE_MAP_KEY_|[\w_\-]+)/,
replacement: '/maps/api/js?key=%s'.replace(/%s/, ENV._GOOGLE_MAP_KEY_),
count: true,
recursive: false
});
replace({
paths: [ INDEX_HTML ],
regex: /content="nQuire-it\/(_VERSION_|[^\"]+)"/,
replacement: 'content="nQuire-it/%s"'.replace(/%s/, version.describe),
count: true,
recursive: false
});

replace({
paths: [ CONFIG_JS ],
regex: /version: ['"][\w_\.\-]+['"],/,
replacement: 'version: "%s",'.replace(/%s/, version.describe),
count: true,
recursive: false
});
replace({
paths: [ INDEX_HTML ],
regex: /\/maps\/api\/js\?key=(_GOOGLE_MAP_KEY_|[\w_\-]+)/,
replacement: '/maps/api/js?key=%s'.replace(/%s/, ENV._GOOGLE_MAP_KEY_),
count: true,
recursive: false
});

replace({
paths: [ CONFIG_JS ],
regex: /build_time: ['"][\w_\.\-:]+['"],/,
replacement: 'build_time: "%s",'.replace(/%s/, new Date().toISOString()),
count: true,
recursive: false
});
replace({
paths: [ CONFIG_JS ],
regex: /version: ['"][\w_\.\-]+['"],/,
replacement: 'version: "%s",'.replace(/%s/, version.describe),
count: true,
recursive: false
});

replace({
paths: [ CONFIG_JS ],
regex: /build_time: ['"][\w_\.\-:]+['"],/,
replacement: 'build_time: "%s",'.replace(/%s/, new Date().toISOString()),
count: true,
recursive: false
});
}

function getRandomInt(min, max) {
min = Math.ceil(min);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@
"type": "git",
"url": "https://github.com/IET-OU/nquire-web-source.git"
},
"X_env_file": "./.env.json",
"scripts": {
"build": "grunt && npm run version",
"build": "grunt",
"bugs": "ghi | cat",
"java": "mvn compile -f app/pom.xml",
"cp-compiled": "cp -r app/target/classes/org/greengin/nquireit/ ../nquire-web-compiled/classes/org/greengin/nquireit",
Expand All @@ -79,7 +80,7 @@
"test-all": "npm run version || grunt test",
"tr-compile-java": "mvn compile -f app/pom.xml | tee app/mvn.log | egrep '(ERROR|WARN|INFO)'",
"tr-exit-java": "bash bin/travis-exit-java.sh",
"version": "bin/git-version.js && bin/replace.js; # --all",
"version": "grunt git:version.json && grunt replace:config+html; # --all",
"weblate-stats": "wget -q -O - http://weblate.iet.open.ac.uk/widgets/nquire-it/-/shields-badge.svg | egrep -o '\\d+%' | tail -n1"
}
}

0 comments on commit 0de2713

Please sign in to comment.