grunt-monty-python.js 6.15 KB
/*
 * grunt-monty-python
 *
 */

'use strict';

module.exports = function (grunt) {
    var _ = require('lodash');
    function createConfig(mpConfig) {

        var holyGrailTmp = '.grunt/holygrail';
        var initConfig = {
            clean: {
                dist: {
                    files: [{
                        dot: true,
                        src: [
                            holyGrailTmp,
                            'dist/*',
                            '!dist/.git*'
                        ]
                    }]
                },
            },
        };
        var checkList = [];
        var buildList = [
            'check',
            'clean:dist',
        ];

        function externalMultiTask(name, desc, command, updateOptions) {
            grunt.log.writeln('registering multiTask', name);
            grunt.registerMultiTask(name, desc, function() {
                grunt.log.writeln('foobar', name);
                grunt.log.writeln('foobar', name);
                var done = this.async();
                var options = this.options();
                var target = this.target;
                var configFile = holyGrailTmp + '/configs/' + name + '-' + target + '.json';
                var commandArgs = updateOptions.apply(this, [target, options, configFile]);
                grunt.file.write(configFile, JSON.stringify(options) + '\n');
                grunt.log.writeln(JSON.stringify(commandArgs) + '\n');
                require('child_process').execFile(command, commandArgs, function (error, stdout, stderr) {
                    stdout = stdout.split('\n').map(function(v) {
                        return name + ':' + target + ': ' + v;
                    }).join('\n');
                    grunt.log.writeln(stdout);
                    if (error) {
                        stderr = stderr.split('\n').map(function(v) {
                            return name + ':' + target + ': ' + v;
                        }).join('\n');
                        grunt.log.writeln(stderr);
                    }
                    done(error);
                });
            });
        }

        grunt.loadNpmTasks('grunt-monty-python');
        if (mpConfig.jshint) {
            initConfig.jshint = {
                options: mpConfig.jshint.options,
                all: [
                    'Gruntfile.js',
                ],
                main: {
                    options: _.extend({}, mpConfig.jshint.browserOptions, {
                        globals: {
                            define: false,
                            require: false,
                        },
                    }),
                    files: {
                        src: [
                            'src/scripts/main.js',
                        ],
                    },
                }
            };
            checkList.push('jshint');
        }

        if (mpConfig.csslint) {
            initConfig.csslint = {
            };
            checkList.push('csslint');
        }

        if (mpConfig.htmlmin) {
            initConfig.htmlmin = {
            };
            checkList.push('htmlmin');
        }

        if (common.punch) {
            grunt.registerMultiTask('punch', 'run punch externally to create html files', function() {
                var done = this.async();
                var options = this.options();
                var target = this.target;

                //var PunchConfigHandler = require('punch/lib/config_handler');
                var PunchSiteGenerator = require('punch/lib/site_generator');
                var punchConfig = _.clone(require('punch/lib/default_config'));
                _.merge(punchConfig, options);
                punchConfig.output_dir = holyGrailTmp + '/punch/output-' + target;
                punchConfig.generator.blank = false;

                PunchSiteGenerator.setup(punchConfig);
                grunt.log.write('foo', punchConfig);
                try {
                    PunchSiteGenerator.generate(function() {
                        grunt.log.writeln('done generator', arguments);
                        done(true);
                    });
                } catch (e) {
                    grunt.log.writeln('fail', e);
                    done(false);
                }
            });

            initConfig.punch = {
                options: {
                    'template_dir': 'src/punch/templates',
                    'content_dir': 'src/punch/contents',
                    plugins: {
                        parsers: {
                            '.html': 'punch-html-fragment-content-parser',
                        },
                    },
                    bundles: {
                    },
                },
                generate: {
                },
            };
            buildList.push('punch:generate');
        }


        buildList.push('useminPrepare');    // html
        //buildList.push('usemin:views'
        buildList.push('requirejs');        // script
        buildList.push('copy:css');         // css
        buildList.push('imagemin');         // image
        buildList.push('svgmin');           // image
        buildList.push('rev:img');          // image
        buildList.push('autoprefixer');     // css
/*
        'concat:requirejs',         // montyPython
        'concat:generated',         // montyPython
        'cssmin',                   // montyPython
        //'uglify:dist',
        'copy:dist',
        //'modernizr',
        'uglify:generated',         // montyPython
        //'rev:dist',                 // montyPython
        'rev:css',                  // montyPython
        'revconfig',                // montyPython
        'uglify:bower',             // montyPython
        'uglify:requireconfig',     // montyPython
        'rev:requireconfig',        // montyPython
        'usemin:html',              // montyPython
        'usemin:css',               // montyPython
        'htmlmin',                  // montyPython
*/

        grunt.initConfig(initConfig);

        grunt.registerTask('check', checkList);

        grunt.registerTask('default', [
            'check',
        ]);

        grunt.registerTask('build', buildList);
    }

    return {
        createConfig: createConfig,
    };
};