grunt-monty-python.js 11.9 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',
                        ]
                    }]
                },
            },
        };
        if (common.useRev) {
            initConfig.rev = {
                dist: {
                    files: [],
                },
            };
        }
        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');
        }

        var htmlSourceDir;
        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');
            htmlSourceDir = '.grunt/holygrail/punch/output-generate';
            initConfig.copy.dist.files.push({
                expand: true,
                dot: true,
                cwd: htmlSourceDir,
                dest: 'dist',
                src: [
                    '{,*/}*.html',
                ]
            });
        } else {
            htmlSourceDir = 'src';
        }

        if (true) {
            initConfig.useminPrepare = {
                options: {
                    dest: 'dist'
                },
                html: {
                    options: {
                        staging: '.grunt/usemin',
                    },
                    src: htmlSourceDir + '/!(scripts){,**/}*.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'
                    }]
                }
            };
        }

        if (common.css) {
            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');
            initConfig.usemin.css =  _.map(common.css.options.dirs, function (value) {
                return 'dist/' + value + '/{,**/}*.css';
            });
            if (common.useRev) {
                initConfig.rev.css = {
                    files: {
                        src: _.map(common.css.options.dirs, function (value) {
                            return 'dist/' + value + '/{,**/}*.css';
                        }),
                    }
                };
            }
        }

        if (common.images) {
            initConfig.imagemin = {
                dist: {
                    files: _.map(common.images.options.dirs, function (value) {
                        return {
                            expand: true,
                            cwd: 'src/' + value,
                            src: '{,**/}*.{gif,jpeg,jpg,png}',
                            dest: 'dist/' + value,
                        };
                    }),
                }
            };
            initConfig.svgmin = {
                dist: {
                    files: _.map(common.images.options.dirs, function (value) {
                        return {
                            expand: true,
                            cwd: 'src/' + value,
                            src: '{,**/}*.svg',
                            dest: 'dist/' + value,
                        };
                    }),
                }
            };
            initConfig.copy.img = {
                files: [{
                    expand: true,
                    dot: true,
                    cwd: 'src',
                    dest: 'dist',
                    src: _.map(common.images.options.dirs, function (value) {
                        return value + '/{,**/}*.*';
                    }),
                }]
            };
            if (common.useRev) {
                initConfig.rev.img = {
                    files: {
                        src: _.map(common.images.options.dirs, function (value) {
                            return 'dist/' + value + '/{,**/}*.{gif,jpeg,jpg,png,webp}';
                        }),
                    }
                };
            }
        }

        if (common.bower) {
            externalMultiTask('bower', 'run bower externally', 'bower', function(target, options, configFile) {
                return ['--config.directory=' + options.directory, target];
            });
            initConfig.bower = {
                options: {
                    directory: 'src/' + common.bower.directory,
                },
                install: {},
                update: {},
            };
            if (common.useRev) {
                initConfig.rev.dist.files.push({src: 'dist/' + common.bower.directory});
            }
        }

        buildList.push('useminPrepare');    // html
        //buildList.push('usemin:views'
        //buildList.push('requirejs');        // script
        buildList.push('copy:css');         // css
        if (common.images) {
            buildList.push('imagemin');         // image
            buildList.push('svgmin');           // image
            if (common.useRev) {
                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
        if (common.useRev) {
            buildList.push('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,
    };
};