grunt-monty-python.js 8.75 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*'
                        ]
                    }]
                },
            },
            copy: {
                dist: {
                    files: [{
                        expand: true,
                        dot: true,
                        cwd: 'src',
                        dest: 'dist',
                        src: [
                            'WEB-INF/{,*/}*.*',
                            '*.{ico,png,txt}',
                            '.htaccess',
                            '{,*/}*.html',
                        ]
                    }]
                },
            },
        };
        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');
            initConfig.copy.dist.files.push({
                expand: true,
                dot: true,
                cwd: '.grunt/holygrail/punch/output-generate',
                dest: 'dist',
                src: [
                    '{,*/}*.html',
                ]
            });
        }

        if (true) {
            initConfig.useminPrepare = {
                options: {
                    dest: 'dist'
                },
                html: {
                    options: {
                        staging: '.grunt/usemin',
                    },
                    src: holyGrailTmp + '/punch/output-generate/{,**/}*.html',
                },
            };
            initConfig.usemin = {
                options: {
                    dirs: ['dist']
                },
                html: {
                    options: {
                        dirs: ['dist'],
                        assetsDirs: ['dist'],
                    },
                    src: ['dist/!(scripts){,**/}*.html'],
                },
            };
            initConfig.clean.dist.files[0].src.push('.grunt/usemin');
            initConfig.htmlmin = {
                dist: {
                    //options: _.extend({}, common.htmlmin.options),
                    files: [{
                        expand: true,
                        cwd: 'dist',
                        src: '{,*/}*.html',
                        dest: 'dist'
                    }]
                }
            };
        }

        initConfig.copy.css: {
            files: [{
                expand: true,
                dot: true,
                cwd: 'src',
                dest: '.grunt/usemin-css/',
                src:  _.map(common.css.options.dirs, function (value) {
                    return value + '/{,**/}*.css';
                }),
            }]
        };
        initConfig.clean.dist.files[0].src.push('.grunt/usemin-css');

        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
        //buildList.push('concat:requirejs');         // montyPython
        buildList.push('concat:generated');         // montyPython
        //buildList.push('cssmin');                   // montyPython
        //'uglify:dist');
        buildList.push('copy:dist');
        //'modernizr');
        //buildList.push('uglify:generated');         // montyPython
        //'rev:dist');                 // montyPython
        //buildList.push('rev:css');                  // montyPython
        //buildList.push('revconfig');                // montyPython
        //buildList.push('uglify:bower');             // montyPython
        //buildList.push('uglify:requireconfig');     // montyPython
        //buildList.push('rev:requireconfig');        // montyPython
        buildList.push('usemin:html');              // montyPython
        //buildList.push('usemin:css');               // montyPython
        buildList.push('htmlmin');                  // montyPython

        grunt.initConfig(initConfig);

        grunt.registerTask('check', checkList);

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

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

    return {
        createConfig: createConfig,
    };
};