<commit_before>fs             = require 'fs'
path           = require 'path'
child_process  = require 'child_process'

debug          = require('debug') 'client:scripts'
build          = require 'bant-build'
through        = require 'through2'
coffeeify      = require 'coffeeify'
rewritify      = require 'rewritify'
pistachioify   = require 'pistachioify'
browserify     = require 'browserify'
watchify       = require 'watchify'
uglifyify      = require 'uglifyify'

glob           = require 'glob'
xtend          = require 'xtend'
gitrev         = require 'git-rev'
mkdirp         = require 'mkdirp'
pretty         = require 'pretty-bytes'

del            = require 'del'
merge          = require 'merge-stream'
argv           = require('minimist') process.argv

gulp           = require 'gulp'
gutil          = require 'gulp-util'
gdebug         = require 'gulp-debug'
rename         = require 'gulp-rename'
watch          = require 'gulp-watch'
styleHelper    = require './gulptasks/style'
concat         = require 'gulp-concat'

devMode        = argv.devMode?
watchMode      = argv.watchMode?
version        = argv.ver? or 1

log            = (color, message) -> gutil.log gutil.colors[color] message

modules = glob.sync('*/bant.json').map (row) -> return row.split('/')[0]


opts =
  outdir: path.resolve __dirname, '../website/a/p/p'
  baseurl: '/a/p/p'
  modules: modules
  rev: null
  browserify:
    extensions: ['.coffee', '.js', '.json']
    # debug: true
  globals:
    config: {}
    appClasses: {}
    navItems: []
    navItemIndex: []
    REMOTE_API: {}


files = []
folders = opts.modules
BUILD_PATH = opts.outdir


gulp.task 'clean', (callback) ->
  del [
    "#{BUILD_PATH}/css/*.*.css"
    "#{BUILD_PATH}/sprites/*"
    "#{__dirname}/.sprites/*"]
  , force: yes, callback


gulp.task 'default', ['styles', 'scripts'], (callback) ->
  mkdirp "#{opts.outdir}/thirdparty", (err) ->
    throw err  if err
    callback()


gulp.task 'create-dirs', (callback) ->

  mkdirp "#{opts.outdir}/thirdparty", (err) ->
    throw err  if err
    callback()


gulp.task 'set-remote-api', (callback) ->

  child_process.exec 'node get-bongo-schema.js', (err, res) ->
    debug 'could not gt bongo-schema'  if err
    opts.globals.REMOTE_API = xtend opts.globals.REMOTE_API, JSON.parse res
    callback()


gulp.task 'set-config-apps', ['set-revid'], (callback) ->
  apps = {}

  {baseurl, rev} = opts

  modules.forEach (id) ->
    name = switch id
      when 'ide' then 'IDE'
      else id.charAt(0).toUpperCase() + id.slice(1)

    apps[name] =
      identifier: id
      name: name
      style: "#{baseurl}/#{id}.css?v=#{opts.rev}"
      script: "#{baseurl}/#{id}.js?v=#{opts.rev}"

  opts.globals.config = xtend opts.globals.config, {apps}
  callback()


gulp.task 'set-revid', (callback) ->

  gitrev.short (rev) ->
    opts.rev = rev
    callback()


gulp.task 'copy-thirdparty', ['create-dirs'], (callback) ->

  i = 0

  glob 'thirdparty/**/*', nodir: yes, (err, files) ->
    i = files.length - 1

    throw err  if err

    files.forEach (file, j) ->
      return  if file is 'thirdparty/Readme.md'

      components = file.split('/').slice(0, -1).join '/'

      mkdirp "#{opts.outdir}/#{components}", (err) ->
        throw err  if err
        stream = fs.createReadStream "#{__dirname}/#{file}"
        stream.pipe fs.createWriteStream "#{opts.outdir}/#{file}"
        callback()  if i - j is 0


gulp.task 'copy-assets', ['create-dirs'], (callback) ->

  i = 0

  glob 'assets/**/*', nodir: yes, (err, files) ->
    i = files.length - 1

    throw err  if err

    files.forEach (file, j) ->
      return  if file is 'assets/Readme.md'

      components = file.split('/').slice(0, -1).join '/'

      mkdirp "#{opts.outdir}/#{components}", (err) ->
        throw err  if err
        stream = fs.createReadStream "#{__dirname}/#{file}"
        stream.pipe fs.createWriteStream "#{opts.outdir}/#{file}"
        callback()  if i - j is 0


gulp.task 'scripts', ['set-remote-api', 'set-config-apps', 'copy-thirdparty', 'copy-assets'], (callback) ->

  debug "modules:\t#{modules.join ', '}"
  debug "outdir:\t#{opts.outdir}"
  debug "rev:\t#{opts.rev}"

  mapping = {}
  modules.forEach (name) -> mapping[name] = "../#{name}/lib"

  if watchMode
    b = watchify(browserify(xtend(opts.browserify, watchify.args)))
  else
    b = browserify opts.browserify

  b.transform coffeeify, global: yes
   .transform pistachioify, global: yes
   .transform rewritify,
     global: yes
     extensions: ['coffee']
     basedir: __dirname
     mapping: mapping
  
  unless devMode
    b.transform uglifyify,
      global: yes
      mangle: yes


  bant = build b, globals: opts.globals
    .on 'bundle', (bundle) ->
      outfile = path.join opts.outdir, "#{bundle.name}.js"
      fs.writeFile outfile, bundle.source, (err, res) ->
        debug "could not write #{outfile}"  if err
        debug "#{pretty bundle.source.length} written to #{path.basename outfile}"

  b.require(require.resolve('kd.js'), { expose: 'kd' })  unless watchMode

  gulp.src(['*/bant.json']).pipe bant<commit_msg>build: uglify screw-ie8 option added<commit_after>fs             = require 'fs'
path           = require 'path'
child_process  = require 'child_process'

debug          = require('debug') 'client:scripts'
build          = require 'bant-build'
through        = require 'through2'
coffeeify      = require 'coffeeify'
rewritify      = require 'rewritify'
pistachioify   = require 'pistachioify'
browserify     = require 'browserify'
watchify       = require 'watchify'
uglifyify      = require 'uglifyify'

glob           = require 'glob'
xtend          = require 'xtend'
gitrev         = require 'git-rev'
mkdirp         = require 'mkdirp'
pretty         = require 'pretty-bytes'

del            = require 'del'
merge          = require 'merge-stream'
argv           = require('minimist') process.argv

gulp           = require 'gulp'
gutil          = require 'gulp-util'
gdebug         = require 'gulp-debug'
rename         = require 'gulp-rename'
watch          = require 'gulp-watch'
styleHelper    = require './gulptasks/style'
concat         = require 'gulp-concat'

devMode        = argv.devMode?
watchMode      = argv.watchMode?
version        = argv.ver? or 1

log            = (color, message) -> gutil.log gutil.colors[color] message

modules = glob.sync('*/bant.json').map (row) -> return row.split('/')[0]


opts =
  outdir: path.resolve __dirname, '../website/a/p/p'
  baseurl: '/a/p/p'
  modules: modules
  rev: null
  browserify:
    extensions: ['.coffee', '.js', '.json']
    # debug: true
  globals:
    config: {}
    appClasses: {}
    navItems: []
    navItemIndex: []
    REMOTE_API: {}


files = []
folders = opts.modules
BUILD_PATH = opts.outdir


gulp.task 'clean', (callback) ->
  del [
    "#{BUILD_PATH}/css/*.*.css"
    "#{BUILD_PATH}/sprites/*"
    "#{__dirname}/.sprites/*"]
  , force: yes, callback


gulp.task 'default', ['styles', 'scripts'], (callback) ->
  mkdirp "#{opts.outdir}/thirdparty", (err) ->
    throw err  if err
    callback()


gulp.task 'create-dirs', (callback) ->

  mkdirp "#{opts.outdir}/thirdparty", (err) ->
    throw err  if err
    callback()


gulp.task 'set-remote-api', (callback) ->

  child_process.exec 'node get-bongo-schema.js', (err, res) ->
    debug 'could not gt bongo-schema'  if err
    opts.globals.REMOTE_API = xtend opts.globals.REMOTE_API, JSON.parse res
    callback()


gulp.task 'set-config-apps', ['set-revid'], (callback) ->
  apps = {}

  {baseurl, rev} = opts

  modules.forEach (id) ->
    name = switch id
      when 'ide' then 'IDE'
      else id.charAt(0).toUpperCase() + id.slice(1)

    apps[name] =
      identifier: id
      name: name
      style: "#{baseurl}/#{id}.css?v=#{opts.rev}"
      script: "#{baseurl}/#{id}.js?v=#{opts.rev}"

  opts.globals.config = xtend opts.globals.config, {apps}
  callback()


gulp.task 'set-revid', (callback) ->

  gitrev.short (rev) ->
    opts.rev = rev
    callback()


gulp.task 'copy-thirdparty', ['create-dirs'], (callback) ->

  i = 0

  glob 'thirdparty/**/*', nodir: yes, (err, files) ->
    i = files.length - 1

    throw err  if err

    files.forEach (file, j) ->
      return  if file is 'thirdparty/Readme.md'

      components = file.split('/').slice(0, -1).join '/'

      mkdirp "#{opts.outdir}/#{components}", (err) ->
        throw err  if err
        stream = fs.createReadStream "#{__dirname}/#{file}"
        stream.pipe fs.createWriteStream "#{opts.outdir}/#{file}"
        callback()  if i - j is 0


gulp.task 'copy-assets', ['create-dirs'], (callback) ->

  i = 0

  glob 'assets/**/*', nodir: yes, (err, files) ->
    i = files.length - 1

    throw err  if err

    files.forEach (file, j) ->
      return  if file is 'assets/Readme.md'

      components = file.split('/').slice(0, -1).join '/'

      mkdirp "#{opts.outdir}/#{components}", (err) ->
        throw err  if err
        stream = fs.createReadStream "#{__dirname}/#{file}"
        stream.pipe fs.createWriteStream "#{opts.outdir}/#{file}"
        callback()  if i - j is 0


gulp.task 'scripts', ['set-remote-api', 'set-config-apps', 'copy-thirdparty', 'copy-assets'], (callback) ->

  debug "modules:\t#{modules.join ', '}"
  debug "outdir:\t#{opts.outdir}"
  debug "rev:\t#{opts.rev}"

  mapping = {}
  modules.forEach (name) -> mapping[name] = "../#{name}/lib"

  if watchMode
    b = watchify(browserify(xtend(opts.browserify, watchify.args)))
  else
    b = browserify opts.browserify

  b.transform coffeeify, global: yes
   .transform pistachioify, global: yes
   .transform rewritify,
     global: yes
     extensions: ['coffee']
     basedir: __dirname
     mapping: mapping

  unless devMode
    b.transform uglifyify,
      global      : yes
      mangle      : yes
      'screw-ie8' : yes


  bant = build b, globals: opts.globals
    .on 'bundle', (bundle) ->
      outfile = path.join opts.outdir, "#{bundle.name}.js"
      fs.writeFile outfile, bundle.source, (err, res) ->
        debug "could not write #{outfile}"  if err
        debug "#{pretty bundle.source.length} written to #{path.basename outfile}"

  b.require(require.resolve('kd.js'), { expose: 'kd' })  unless watchMode

  gulp.src(['*/bant.json']).pipe bant
{'fraction_non_alphanumeric': 0.16177188018343253, 'fraction_numerical': 0.003122255829837057, 'mean_word_length': 2.5664578983994435, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6574832', 'n_tokens_mistral': 3565, 'n_tokens_neox': 3465, 'n_words': 835}