<commit_before>gulp = require 'gulp'
gulp_concat = require 'gulp-concat'

{ kit } = require 'nobone'

{
    _,
    log,
    copy,
    glob,
    spawn,
    remove,
    Promise,
    path: { join }
} = kit

class Builder
    constructor: ->
        @src_path = 'src'
        @dist_path = 'dist'
        @lib_path = 'lib'
        @doc_path = 'doc'
        @build_temp_path = 'build_temp'
        @require_temp_path = 'require_temp'

    start: ->
        self = @
        @update_build_dir()
        .then ->
            self.compile_all_coffee()
        .then ->
            self.combine_js()
        .then ->
            self.compress_js()
        .then ->
            self.add_license()
        .then ->
            self.complie_as()
        .then ->
            self.clean()
        .done ->
            console.log '>> Build done.'.yellow

    copy_to_dist: (from, to) ->
        to = join @dist_path, to
        copy(from, to).then ->
            log '>> Copy: '.cyan + from + ' -> '.green + to

    update_build_dir: ->
        self = @
        from = join 'src', 'js'

        glob join(@dist_path, '**', '*')
        .then (paths) ->
            Promise.all(
                # swf文件默认不处理，由complie_as时自己决定是否重编
                _.reject(paths, (path) ->
                    /\.(swf|cache)$/.test(path)
                ).map (path) ->
                    remove (path)
                    log '>> Clean: '.cyan + path
            )
        .then ->
            copy(from, join(self.build_temp_path, 'js')).then ->
                kit.log '>> Copy: '.cyan + from + ' -> '.green + self.build_temp_path
        .then ->
            Promise.all([
                self.copy_to_dist join(self.lib_path, 'expressInstall.swf'), 'expressInstall.swf'
                self.copy_to_dist join(self.doc_path, 'mp3', 'empty.mp3'), 'empty.mp3'
            ])

    compile_all_coffee: ->
        self = @
        coffeescript = kit.require 'coffee-script'

        glob join(self.build_temp_path, '**', '*.coffee')
        .then (coffee_list) ->
            Promise.all coffee_list.map (path) ->
                js_path = path.replace(/(\.coffee)$/, '') + '.js'

                kit.readFile(path, 'utf8').then (str) ->
                    try
                        return coffeescript.compile(str, { bare: true })
                    catch e
                        log ">> Error: #{path} \n#{e}".red
                .then (code) ->
                    kit.outputFile(js_path, code).then ->
                        remove(path)
                    .then ->
                        log '>> Compiled: '.cyan + path

    combine_js: (options = {}) ->
        self = @
        { dist_path, require_temp_path } = @

        log '>> Compile client js with requirejs ...'.cyan

        requirejs = kit.require 'requirejs'

        opts_pc =
            appDir: @build_temp_path
            baseUrl: 'js'
            dir: require_temp_path

            optimize: 'none'
            optimizeCss: 'standard'
            modules: [
                {
                    name: 'muplayer/player'
                },
                {
                    name: 'muplayer/plugin/equalizer'
                },
                {
                    name: 'muplayer/plugin/lrc'
                }
            ]
            fileExclusionRegExp: /^\./
            removeCombined: true
            pragmas:
                FlashCoreExclude: false
            # 为映射muplayer这个namespace
            paths:
                'muplayer': '.'

        opts_pc = _.extend(opts_pc, options.pc)

        new Promise (resolve) ->
            # PC
            requirejs.optimize opts_pc, (buildResponse) ->
                log '>> r.js for PC'.cyan
                log buildResponse

                Promise.all(
                    opts_pc.modules.map (mod) ->
                        file = mod.name.replace(/^muplayer/, 'js') + '.js'
                        from = join(require_temp_path, file)
                        to = file.split('/').slice(-1)[0]
                        self.copy_to_dist from, to
                ).then ->
                    opts_webapp = _.cloneDeep opts_pc
                    opts_webapp.pragmas.FlashCoreExclude = true
                    opts_webapp.modules = [
                        {
                            name: 'muplayer/player'
                        }
                    ]

                    opts_webapp =_.extend(opts_webapp, options.webapp)

                    # Webapp
                    requirejs.optimize opts_webapp, (buildResponse) ->
                        log '>> r.js for WebApp'.cyan
                        log buildResponse

                        glob join(require_temp_path, 'js', 'lib', 'zepto', '**', '*.js')
                        .then (file_list) ->
                            Promise.all([
                                opts_webapp.modules.map (mod) ->
                                    file = join require_temp_path, (mod.name.replace(/^muplayer/, 'js') + '.js')
                                    fname = 'zepto-' + file.split('/').slice(-1)[0]<commit_msg>fix builder<commit_after>gulp = require 'gulp'
gulp_concat = require 'gulp-concat'

{ kit } = require 'nobone'

{
    _,
    log,
    copy,
    glob,
    spawn,
    remove,
    Promise,
    path: { join }
} = kit

class Builder
    constructor: ->
        @src_path = 'src'
        @dist_path = 'dist'
        @lib_path = 'lib'
        @doc_path = 'doc'
        @build_temp_path = 'build_temp'
        @require_temp_path = 'require_temp'

    start: ->
        self = @
        @update_build_dir()
        .then ->
            self.compile_all_coffee()
        .then ->
            self.combine_js()
        .then ->
            self.compress_js()
        .then ->
            self.add_license()
        .then ->
            self.complie_as()
        .then ->
            self.clean()
        .done ->
            console.log '>> Build done.'.yellow

    copy_to_dist: (from, to) ->
        to = join @dist_path, to
        copy(from, to).then ->
            log '>> Copy: '.cyan + from + ' -> '.green + to

    update_build_dir: ->
        self = @
        from = join 'src', 'js'

        glob join(@dist_path, '**', '*')
        .then (paths) ->
            Promise.all(
                # swf文件默认不处理，由complie_as时自己决定是否重编
                _.reject(paths, (path) ->
                    /\.(swf|cache)$/.test(path)
                ).map (path) ->
                    remove (path)
                    log '>> Clean: '.cyan + path
            )
        .then ->
            copy(from, join(self.build_temp_path, 'js')).then ->
                kit.log '>> Copy: '.cyan + from + ' -> '.green + self.build_temp_path
        .then ->
            Promise.all([
                self.copy_to_dist join(self.lib_path, 'expressInstall.swf'), 'expressInstall.swf'
                self.copy_to_dist join(self.doc_path, 'mp3', 'empty.mp3'), 'empty.mp3'
            ])

    compile_all_coffee: ->
        self = @
        coffeescript = kit.require 'coffee-script'

        glob join(self.build_temp_path, '**', '*.coffee')
        .then (coffee_list) ->
            Promise.all coffee_list.map (path) ->
                js_path = path.replace(/(\.coffee)$/, '') + '.js'

                kit.readFile(path, 'utf8').then (str) ->
                    try
                        return coffeescript.compile(str, { bare: true })
                    catch e
                        log ">> Error: #{path} \n#{e}".red
                .then (code) ->
                    kit.outputFile(js_path, code).then ->
                        remove(path)
                    .then ->
                        log '>> Compiled: '.cyan + path

    combine_js: (options = {}) ->
        self = @
        { dist_path, require_temp_path } = @

        log '>> Compile client js with requirejs ...'.cyan

        requirejs = kit.require 'requirejs'

        opts_pc =
            appDir: @build_temp_path
            baseUrl: 'js'
            dir: require_temp_path

            optimize: 'none'
            optimizeCss: 'standard'
            modules: [
                {
                    name: 'muplayer/player'
                },
                {
                    name: 'muplayer/plugin/equalizer'
                },
                {
                    name: 'muplayer/plugin/lrc'
                }
            ]
            fileExclusionRegExp: /^\./
            removeCombined: true
            pragmas:
                FlashCoreExclude: false
            # 为映射muplayer这个namespace
            paths:
                'muplayer': '.'

        opts_pc = _.extend(opts_pc, options.pc)

        new Promise (resolve) ->
            # PC
            requirejs.optimize opts_pc, (buildResponse) ->
                log '>> r.js for PC'.cyan
                log buildResponse

                Promise.all(
                    opts_pc.modules.map (mod) ->
                        file = mod.name.replace(/^muplayer/, 'js') + '.js'
                        from = join(require_temp_path, file)
                        to = file.split('/').slice(-1)[0]
                        self.copy_to_dist from, to
                ).then ->
                    opts_webapp = _.cloneDeep opts_pc
                    opts_webapp.modules = [
                        {
                            name: 'muplayer/player'
                        }
                    ]

                    opts_webapp =_.extend(opts_webapp, options.webapp)
                    opts_webapp.pragmas.FlashCoreExclude = true

                    # Webapp
                    requirejs.optimize opts_webapp, (buildResponse) ->
                        log '>> r.js for WebApp'.cyan
                        log buildResponse

                        glob join(require_temp_path, 'js', 'lib', 'zepto', '**', '*.js')
                        .then (file_list) ->
                            Promise.all([
                                opts_webapp.modules.map (mod) ->
                                    file = join require_temp_path, (mod.name.replace(/^muplayer/, 'js') + '.js')
                                    fname = 'zepto-' + file.split('/').slice(-1)[0]
{'fraction_non_alphanumeric': 0.12416502946954813, 'fraction_numerical': 0.0015717092337917485, 'mean_word_length': 1.0879819524200165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 109, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5351393', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2844, 'n_words': 532}