Skip to content

Commit

Permalink
Preliminary new gruntplugin init template.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowboy committed Oct 1, 2012
1 parent 8501765 commit d8552cc
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 64 deletions.
8 changes: 5 additions & 3 deletions tasks/init/gruntplugin.js
Expand Up @@ -41,11 +41,13 @@ exports.template = function(grunt, init, done) {
init.prompt('node_version', grunt.package.engines.node)
], function(err, props) {
// Set a few grunt-plugin-specific properties.
props.short_name = props.name.replace(/^grunt[\-_]?/, '');
props.short_name = props.name.replace(/^grunt[\-_]?/, '').replace(/[\W_]+/g, '_').replace(/^(\d)/, '_$1');
props.main = 'Gruntfile.js';
props.npm_test = 'grunt nodeunit';
props.bin = 'bin/' + props.name;
props.npm_test = 'grunt test';
props.keywords = ['gruntplugin'];
props.devDependencies = {
'grunt-contrib-clean': '*'
};

// Files to copy (and process).
var files = init.filesToCopy(props);
Expand Down
1 change: 0 additions & 1 deletion tasks/init/gruntplugin/rename.json
@@ -1,5 +1,4 @@
{
"bin/name": "bin/{%= name %}",
"tasks/name.js": "tasks/{%= short_name %}.js",
"test/name_test.js": "test/{%= short_name %}_test.js"
}
15 changes: 0 additions & 15 deletions tasks/init/gruntplugin/root/.jshintrc

This file was deleted.

100 changes: 66 additions & 34 deletions tasks/init/gruntplugin/root/Gruntfile.js
@@ -1,53 +1,85 @@
/*
* {%= name %}
* {%= homepage %}
*
* Copyright (c) {%= grunt.template.today('yyyy') %} {%= author_name %}
* Licensed under the {%= licenses.join(', ') %} license{%= licenses.length === 1 ? '' : 's' %}.
*/

'use strict';

module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
nodeunit: {
files: ['test/**/*.js']
},
jshint: {
options: {
jshintrc: '.jshintrc'
},
gruntfile: {
src: 'Gruntfile.js'
},
bin: {
src: ['bin/{%= name %}']
},
lib: {
src: ['lib/**/*.js']
},
test: {
src: ['test/**/*.js']
curly: true,
eqeqeq: true,
immed: true,
latedef: true,
newcap: true,
noarg: true,
sub: true,
undef: true,
boss: true,
eqnull: true,
node: true,
es5: true,
},
all: [
'Gruntfile.js',
'tasks/*.js',
'<config:nodeunit.tests>'
],
},
watch: {
gruntfile: {
files: '<%= jshint.gruntfile.src %>',
tasks: ['jshint:gruntfile']
},
bin: {
files: '<%= jshint.bin.src %>',
tasks: ['jshint:bin']
},
lib: {
files: '<%= jshint.lib.src %>',
tasks: ['jshint:lib', 'nodeunit']

// Before generating any new files, remove any previously-created files.
clean: {
tests: ['tmp'],
},

// Configuration to be run (and then tested).
{%= short_name %}: {
default_options: {
options: {
},
files: {
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123'],
},
},
test: {
files: '<%= jshint.test.src %>',
tasks: ['jshint:test', 'nodeunit']
custom_options: {
options: {
separator: ': ',
punctuation: ' !!!',
},
files: {
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123'],
},
},
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js'],
},

});

// Load local tasks.
// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// Default task.
grunt.registerTask('default', ['jshint', 'nodeunit']);
// The clean plugin helps in testing.
//grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('clean', 'Clean up some shizzle.', function() {
if (grunt.file.exists('tmp')) { grunt.file.delete('tmp'); }
});

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', '{%= short_name %}', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);

};
2 changes: 0 additions & 2 deletions tasks/init/gruntplugin/root/bin/name

This file was deleted.

37 changes: 34 additions & 3 deletions tasks/init/gruntplugin/root/tasks/name.js
Expand Up @@ -11,10 +11,41 @@
module.exports = function(grunt) {

// Please see the grunt documentation for more information regarding task
// creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md
// creation: https://github.com/cowboy/grunt/blob/devel/docs/toc.md

grunt.registerTask('{%= short_name %}', 'Your task description goes here.', function() {
grunt.log.write('I am the ' + this.name + ' task...').ok();
grunt.registerMultiTask('{%= short_name %}', 'Your task description goes here.', function() {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
punctuation: '.',
separator: ', '
});

// Iterate over all specified file groups.
this.files.forEach(function(fileObj) {
// The source files to be concatenated. The "nonull" option is used
// to retain invalid files/patterns so they can be warned about.
var files = grunt.file.expand({nonull: true}, fileObj.src);

// Concat specified files.
var src = files.map(function(filepath) {
// Warn if a source file/pattern was invalid.
if (!grunt.file.exists(filepath)) {
grunt.log.error('Source file "' + filepath + '" not found.');
return '';
}
// Read file source.
return grunt.file.read(filepath);
}).join(options.separator);

// Handle options.
src += options.punctuation;

// Write the destination file.
grunt.file.write(fileObj.dest, src);

// Print a success message.
grunt.log.writeln('File "' + fileObj.dest + '" created.');
});
});

};
1 change: 1 addition & 0 deletions tasks/init/gruntplugin/root/test/expected/custom_options
@@ -0,0 +1 @@
Testing: 1 2 3 !!!
1 change: 1 addition & 0 deletions tasks/init/gruntplugin/root/test/expected/default_options
@@ -0,0 +1 @@
Testing, 1 2 3.
1 change: 1 addition & 0 deletions tasks/init/gruntplugin/root/test/fixtures/123
@@ -0,0 +1 @@
1 2 3
1 change: 1 addition & 0 deletions tasks/init/gruntplugin/root/test/fixtures/testing
@@ -0,0 +1 @@
Testing
24 changes: 18 additions & 6 deletions tasks/init/gruntplugin/root/test/name_test.js
Expand Up @@ -22,15 +22,27 @@ var grunt = require('grunt');
test.ifError(value)
*/

exports['{%= short_name %}'] = {
exports.{%= short_name %} = {
setUp: function(done) {
// setup here
// setup here if necessary
done();
},
'task': function(test) {
default_options: function(test) {
test.expect(1);
// tests here
test.ok(true, 'Should be true.');

var actual = grunt.file.read('tmp/default_options');
var expected = grunt.file.read('test/expected/default_options');
test.equal(actual, expected, 'should describe what the default behavior is.');

test.done();
},
custom_options: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/custom_options');
var expected = grunt.file.read('test/expected/custom_options');
test.equal(actual, expected, 'should describe what the custom option(s) behavior is.');

test.done();
}
},
};

0 comments on commit d8552cc

Please sign in to comment.