Permalink
Browse files

Started rebuild of grunt-rigger plugin to work with rigger 0.4.x

  • Loading branch information...
1 parent 77a6744 commit 3d10bd0d513c9d42addde1c8ec1eb925e4caa218 @DamonOehlman committed Oct 12, 2012
View
1 .npmignore
@@ -1 +0,0 @@
-/node_modules/
View
2 bin/grunt-rigger
@@ -1,2 +0,0 @@
-#!/usr/bin/env node
-require('grunt').npmTasks('grunt-rigger').cli();
View
40 grunt.js
@@ -1,40 +0,0 @@
-module.exports = function(grunt) {
-
- // Project configuration.
- grunt.initConfig({
- test: {
- files: ['test/**/*.js']
- },
- lint: {
- files: ['grunt.js', 'tasks/**/*.js', 'test/**/*.js']
- },
- watch: {
- files: '<config:lint.files>',
- tasks: 'default'
- },
- jshint: {
- options: {
- curly: true,
- eqeqeq: true,
- immed: true,
- latedef: true,
- newcap: true,
- noarg: true,
- sub: true,
- undef: true,
- boss: true,
- eqnull: true,
- node: true,
- es5: true
- },
- globals: {}
- }
- });
-
- // Load local tasks.
- grunt.loadTasks('tasks');
-
- // Default task.
- grunt.registerTask('default', 'lint test');
-
-};
View
12 package.json
@@ -20,21 +20,19 @@
"url": "https://github.com/DamonOehlman/grunt-rigger/blob/master/LICENSE-MIT"
}
],
- "main": "grunt.js",
- "bin": "bin/grunt-rigger",
"engines": {
"node": "*"
},
"scripts": {
- "test": "grunt test"
+ "test": "./node_modules/.bin/mocha --reporter spec -t 30s"
},
"dependencies": {
- "async": "0.1.x",
- "grunt": "0.3.x",
- "rigger": "0.3.x"
+ "rigger": "0.4.x",
+ "grunt": "0.3.x"
},
"devDependencies": {
- "grunt": "0.3.x"
+ "coffee-script": "1.3.x",
+ "mocha": "1.5.x"
},
"keywords": [
"gruntplugin"
View
128 tasks/rig.js
@@ -8,96 +8,52 @@
var reFileDirective = /^file\_/i,
path = require('path'),
- async = require('async'),
- rigger = require('rigger');
-
-module.exports = function(grunt) {
+ fs = require('fs'),
+ rigger = require('rigger'),
+ grunt = require('grunt'),
+ async = grunt.utils.async,
+ _ = grunt.utils._,
+ riggerOpts = {},
+ helpers = {};
- // Please see the grunt documentation for more information regarding task and
- // helper creation: https://github.com/cowboy/grunt/blob/master/docs/toc.md
-
- // ==========================================================================
- // TASKS
- // ==========================================================================
-
- grunt.registerMultiTask('rig', 'Your task description goes here.', function() {
- // get the files
- var files = grunt.file.expandFiles(this.file.src),
- callback = this.async(),
- rigTask = this;
-
- grunt.helper('rig', files, function(err, data) {
- // write the output file
- grunt.file.write(rigTask.file.dest, data);
- grunt.log.writeln('File "' + rigTask.file.dest + '" created.');
-
- // TODO: error handling for async
-
- // trigger the callback
- callback();
+// initialise the compile helper
+helpers.compile = function() {
+ var files = [],
+ done = this.async();
+
+ // iterate through the files
+ _.forEach(this.data, function(src, dst) {
+ files.push({ src: src, dst: dst });
});
- });
-
- // ==========================================================================
- // HELPERS
- // ==========================================================================
-
- grunt.registerHelper('rig', function(files, options, callback) {
- // expand the directives
- var fileOpts = files.map(function(filepath) {
- var directive = grunt.task.getDirectiveParts(filepath);
-
- // if the directive is a file directive, then extract the basepath
- if (directive && reFileDirective.test(directive[0])) {
- filepath = directive[1];
- }
- // otherwise if we are dealing with a directive, then reset the filepath
- else if (directive) {
- filepath = '';
- }
-
- // return the directory for the path
- return filepath ? {
- cwd: path.resolve(path.dirname(filepath)),
- filetype: path.extname(filepath).slice(1)
- } : null;
- }),
-
- // get the file contents
- fileContents = files.map(function(filepath) {
- return grunt.task.directive(filepath, grunt.file.read);
- }),
-
- // define a file index to sync the contents map with basePath
- // it's a bit hacky but async doesn't tell us
- fileIndex = 0;
-
- // remap options if required
- if (typeof options === 'function') {
- callback = options;
- options = {};
- }
-
- // ensure we have options
- options = options || {};
- // default the separate to linefeed as per concat
- options = grunt.utils._.defaults(options || {}, {
- separator: grunt.utils.linefeed
- });
-
- // process each of the files that need to be rigged
- async.map(
- fileContents,
-
- function(data, itemCallback) {
- rigger.process(data, fileOpts[fileIndex++], itemCallback);
+ async.forEach(
+ files,
+ function(file, itemCallback) {
+ var destFile = path.resolve(file.dst);
+
+ rigger(path.resolve(file.src), riggerOpts, function(err, output) {
+ if (err) return itemCallback(err);
+
+ grunt.file.mkdir(path.dirname(destFile));
+ fs.writeFile(destFile, output, 'utf8', itemCallback);
+ });
},
-
- function(err, results) {
- callback(err, results.join(grunt.utils.normalizelf(options.separator)));
- }
+ done
);
- });
+};
+
+function rigFiles() {
+ if (this.target == 'opts') {
+ riggerOpts = _.clone(this.data);
+ }
+
+ // call the appropriate helper
+ (helpers[this.target] || helpers.compile).call(this);
+}
+module.exports = function(grunt) {
+
+ // register the rig and rigger tasks
+ grunt.registerMultiTask('rig', 'Rig files using targetting include patterns', rigFiles);
+ grunt.registerMultiTask('rigger', 'Rig files using targetting include patterns', rigFiles);
};
View
2 test/basic-rigging/dist/simple.js
@@ -0,0 +1,2 @@
+var a = 5;
+var b = 10;
View
11 test/basic-rigging/grunt.js
@@ -0,0 +1,11 @@
+module.exports = function(grunt) {
+ grunt.initConfig({
+ rig: {
+ compile: {
+ 'dist/simple.js': 'src/simple.js'
+ }
+ }
+ });
+
+ grunt.loadTasks('../../tasks');
+};
View
1 test/basic-rigging/src/a.js
@@ -0,0 +1 @@
+var a = 5;
View
1 test/basic-rigging/src/b.js
@@ -0,0 +1 @@
+var b = 10;
View
2 test/basic-rigging/src/simple.js
@@ -0,0 +1,2 @@
+//= a.js
+//= b.js
View
13 test/helpers/runner.js
@@ -0,0 +1,13 @@
+var assert = require('assert'),
+ grunt = require('grunt'),
+ path = require('path');
+
+module.exports = function(target) {
+ var opts = {
+ config: path.resolve(__dirname, '..', target, 'grunt.js')
+ }
+
+ return function(done) {
+ grunt.tasks('rig', opts, done);
+ };
+};
View
3 test/mocha.opts
@@ -0,0 +1,3 @@
+--timeout 30s
+--reporter spec
+--globals TAP_Global_Harness,AssertionError,events
View
36 test/rigger_test.js
@@ -1,36 +0,0 @@
-var grunt = require('grunt');
-
-/*
- ======== A Handy Little Nodeunit Reference ========
- https://github.com/caolan/nodeunit
-
- Test methods:
- test.expect(numAssertions)
- test.done()
- Test assertions:
- test.ok(value, [message])
- test.equal(actual, expected, [message])
- test.notEqual(actual, expected, [message])
- test.deepEqual(actual, expected, [message])
- test.notDeepEqual(actual, expected, [message])
- test.strictEqual(actual, expected, [message])
- test.notStrictEqual(actual, expected, [message])
- test.throws(block, [error], [message])
- test.doesNotThrow(block, [error], [message])
- test.ifError(value)
-*/
-
-exports['rigger'] = {
- setUp: function(done) {
- // setup here
- done();
- },
- 'helper': function(test) {
- // test.expect(1);
- // tests here
- // test.equal(grunt.helper('rigger'), 'rigger!!!', 'should return the correct value.');
- // TODO: tests
-
- test.done();
- }
-};
View
5 test/simple.js
@@ -0,0 +1,5 @@
+var runner = require('./helpers/runner');
+
+describe('simple include tests', function() {
+ it('should be able to perform a simple rig', runner('basic-rigging'));
+});

0 comments on commit 3d10bd0

Please sign in to comment.