Skip to content
This repository has been archived by the owner on Jan 12, 2020. It is now read-only.

Commit

Permalink
Cleaner code might be easier to read?
Browse files Browse the repository at this point in the history
  • Loading branch information
paazmaya committed Jul 23, 2013
1 parent 0d98800 commit a107bf4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 39 deletions.
17 changes: 9 additions & 8 deletions README.md
Expand Up @@ -3,8 +3,9 @@
Provides a Grunt task that runs the command 'hg activity' with the given options.
This task can be configured to split the activity graphic based on the given time span.

The project that this task is used, should use Mercurial as its version control system and the current user
should have [the Activity Extension](http://mercurial.selenic.com/wiki/ActivityExtension) installed.
The project that this task is used, should use [Mercurial](http://mercurial.selenic.com/) as
its version control system and the current user should have
[the Activity Extension](http://mercurial.selenic.com/wiki/ActivityExtension) installed.


## Getting Started
Expand Down Expand Up @@ -42,13 +43,13 @@ Current values shown are the defaults.
main: {
options: {
split: ['none'], // 'none', 'authors', 'files', 'branches', 'directories',
filenamePrefix: 'activity-',
filenamePrefix: 'activity',
width: 800,
height: 600,
datemin: '', // yyyy-mm-dd, if left empty, will use all available time
datemax: '', // yyyy-mm-dd
interval: '1w', // int followed by: m = months (unsupported: w = weeks, d = days, h = hours)
iterations: 10, // number of iterations the interval should be useds
interval: '3m', // int followed by: y = years, m = months, w = weeks, d = days, h = hours
iterations: 4, // number of iterations the interval should be useds
uselines: true,
showtags: false,
imagetitle: '', // prefix which will be followed by the split if not none and time span
Expand All @@ -68,14 +69,14 @@ grunt hgactivity

## Dependencies

* Mercurial version control system
* Activity extension for Mercurial
* [Mercurial version control system](http://mercurial.selenic.com/)
* [Activity extension for Mercurial](http://mercurial.selenic.com/wiki/ActivityExtension)
* [moment for internal date handling](http://momentjs.com/)


## Changelog

* 2013-07-15 v0.1.0 Initial release
* 2013-07-23 v0.1.0 Initial release


## License
Expand Down
69 changes: 38 additions & 31 deletions tasks/hgactivity.js
Expand Up @@ -11,14 +11,17 @@ module.exports = function(grunt) {

grunt.registerMultiTask('hgactivity', 'Repository activity', function() {
var moment = require('moment'),
dateFormat = 'YYYY-MM-DD';
dateFormat = 'YYYY-MM-DD', // The format expected by 'hg activity'
args = ['activity'], // Command line arguments for 'hg activity'
dates = [], // Collection of dates used as delimiters of each time span, if interval used
commands = []; // List of commands that are finally called

var done = this.async();

// Default options which will be extended with user defined
var options = this.options({
split: ['none'], // 'none', 'authors', 'files', 'branches', 'directories',
filenamePrefix: 'activity-',
filenamePrefix: 'activity',
width: 800,
height: 600,
datemin: '', // yyyy-mm-dd, if left empty, will use all available time
Expand All @@ -30,10 +33,15 @@ module.exports = function(grunt) {
imagetitle: '', // prefix which will be followed by the split if not none and time span
cwindow: 2
});
console.dir(options);

// Command line arguments for 'hg activity'
var args = ['activity'];
// moment.js keywords
var timeWords = {
y: 'years',
m: 'months', // default
w: 'weeks',
d: 'days',
h: 'hours'
};

// Options that can be used as such and prepended with --
['width', 'height', 'cwindow'].forEach(function (key) {
Expand All @@ -50,35 +58,23 @@ module.exports = function(grunt) {
}
});

var dates = [];

// moment.js keywords
var timeWords = {
y: 'years',
m: 'months', // default
w: 'weeks',
d: 'days',
h: 'hours'
};

// Time span for each picture. This is the only option that triggers multiple image generation
if (options.interval !== '') {
if (typeof options.interval === 'string' && options.interval !== '') {

var span = options.interval.substr(-1);
console.log('span: ' + span);
if (!timeWords.hasOwnProperty(span)) {
span = 'm';
}

var counter = parseInt(options.interval, 10);
console.log('counter: ' + counter);

// Start from today if date max is missing...
if (options.datemax === '') {
// Use today
options.datemax = moment().format(dateFormat);
}

// How about datemin?
// How about datemin? Currently there are no checks for it...

var max = moment(options.datemax, dateFormat);
dates.push(options.datemax);
Expand All @@ -97,20 +93,31 @@ module.exports = function(grunt) {
});
}

// Build the commands
var commands = [];

// The amount of split options defines the amount of outer loops.
// Inner loop count depends of the time span and interval.
options.split.forEach(function (split) {
var filename = options.filenamePrefix + (split !== 'none' ? split + '-' : '');

for (var i = 0; i < dates.length - 1; i++) {
var hgArgs = args.concat(
'--datemin', dates[i + 1], '--datemax', dates[i],
'--filename', (filename + i + '.png')
);
commands.push(hgArgs);
var filename = options.filenamePrefix + (split !== 'none' ? '_' + split : '');
var title = (options.imagetitle.length > 0 ? options.imagetitle : '') +
(split !== 'none' ? split : '');

// Need at least one pair of dates
if (dates.length > 1) {
for (var i = 0; i < dates.length - 1; i++) {
commands.push(args.concat(
'--split', split,
'--datemin', dates[i + 1], '--datemax', dates[i],
'--filename', (filename + '_' + dates[i + 1] + '_' + dates[i] + '.png'),
'--imagetitle', ('"' + title + ': ' + dates[i + 1] + ' - ' + dates[i] + '"')
));
}
}
else {
// Assume that there is no intervals needed, thus one per split.
commands.push(args.concat(
'--split', split,
'--filename', filename + '.png',
'--imagetitle', '"' + title + '"'
));
}
});

Expand Down

0 comments on commit a107bf4

Please sign in to comment.