<commit_before>rimraf = require 'rimraf'
fs = require 'fs-plus'
path = require 'path'
mkdirp = require 'mkdirp'

remote = require 'remote'
dialog = remote.require 'dialog'

DebugServer = require './debug-server'
scaffolder = require './scaffold'
Downloader = require './utils/download'
Unzip = require './utils/unzip'

ProgressView = require './progress-view'
RunOnServerView = require './run-on-server-view'
ServerStatusView = require './server-status-view'

butterflyURL = "https://github.com/yezhiming/butterfly/archive/master.zip"

module.exports =

  activate: (state) ->
    atom.workspaceView.command "atom-butterfly:install", => @cmdInstall()
    atom.workspaceView.command "atom-butterfly:debug", => @cmdDebug()

    atom.workspaceView.command "atom-butterfly:create-project", => @cmdCreateProject()
    atom.workspaceView.command "atom-butterfly:run-on-server", => @cmdRunOnServer()

    @_setupDebugServer()

  _setupDebugServer: ->
    @serverStatusView = new ServerStatusView()
    @serverStatusView.on 'stopServer', => @debugServer.stop()

    @debugServer = new DebugServer()
    @debugServer.on 'start', => @serverStatusView.attach()
    @debugServer.on 'stop', => @serverStatusView.detach()

  deactivate: ->
    @runOnServerView.destroy()
    @serverStatusView.destroy()

    @debugServer.stop()

  serialize: ->

  cmdDebug: ->

  cmdCreateProject: ->
    dialog.showSaveDialog title: 'Create Project', defaultPath: atom.project.path, (destPath) =>

      return unless destPath
      console.log 'save to : %s', destPath

      mkdirp destPath, =>
        @cmdInstall destPath, ->
          atom.open {pathsToOpen: [destPath]}

  cmdRunOnServer: ->
    @runOnServerView = new RunOnServerView()
    @runOnServerView.attach()
    @runOnServerView.on 'createServer', (event, rootPath, destPath, httpPort, pushState)=>
      @debugServer.start(rootPath, destPath, httpPort, pushState)

  cmdInstall: (installToPath = atom.project.path, onFinish)->

    pv = new ProgressView(this)
    pv.attach()
    pv.setTitle("Download butterfly.js...")

    targetFolder = path.resolve(installToPath, 'butterfly')
    targetZipFile = path.resolve(installToPath, 'butterfly.zip')

    rimraf targetZipFile, =>

      Downloader.download(butterflyURL, targetZipFile)
      .on 'progress', ->
        pv.setProgress(progress)
      .on 'finish', =>
        rimraf targetFolder, =>

          pv.setTitle("Unzip...")
          Unzip.unzip targetZipFile, installToPath, =>

            fs.unlink targetZipFile, ->
              pv.destroy()
              onFinish()<commit_msg>using utils<commit_after>rimraf = require 'rimraf'
fs = require 'fs-plus'
path = require 'path'
mkdirp = require 'mkdirp'

remote = require 'remote'
dialog = remote.require 'dialog'

DebugServer = require './debug-server'
scaffolder = require './scaffold'
Downloader = require './utils/download'
Unzip = require './utils/unzip'

ProgressView = require './progress-view'
RunOnServerView = require './run-on-server-view'
ServerStatusView = require './server-status-view'

butterflyURL = "https://github.com/yezhiming/butterfly/archive/master.zip"

module.exports =

  activate: (state) ->
    atom.workspaceView.command "atom-butterfly:install", => @cmdInstall()
    atom.workspaceView.command "atom-butterfly:debug", => @cmdDebug()

    atom.workspaceView.command "atom-butterfly:create-project", => @cmdCreateProject()
    atom.workspaceView.command "atom-butterfly:run-on-server", => @cmdRunOnServer()

    @_setupDebugServer()

  _setupDebugServer: ->
    @serverStatusView = new ServerStatusView()
    @serverStatusView.on 'stopServer', => @debugServer.stop()

    @debugServer = new DebugServer()
    @debugServer.on 'start', => @serverStatusView.attach()
    @debugServer.on 'stop', => @serverStatusView.detach()

  deactivate: ->
    @runOnServerView.destroy()
    @serverStatusView.destroy()

    @debugServer.stop()

  serialize: ->

  cmdDebug: ->

  cmdCreateProject: ->
    dialog.showSaveDialog title: 'Create Project', defaultPath: atom.project.path, (destPath) =>

      return unless destPath
      console.log 'save to : %s', destPath

      mkdirp destPath, =>
        @cmdInstall destPath, ->
          atom.open {pathsToOpen: [destPath]}

  cmdRunOnServer: ->
    @runOnServerView = new RunOnServerView()
    @runOnServerView.attach()
    @runOnServerView.on 'createServer', (event, rootPath, destPath, httpPort, pushState)=>
      @debugServer.start(rootPath, destPath, httpPort, pushState)

  cmdInstall: (installToPath = atom.project.path, onFinish)->

    pv = new ProgressView(this)
    pv.attach()
    pv.setTitle("Download butterfly.js...")

    targetFolder = path.resolve(installToPath, 'butterfly')
    targetZipFile = path.resolve(installToPath, 'butterfly.zip')

    rimraf targetZipFile, =>

      Downloader.download(butterflyURL, targetZipFile)
      .on 'progress', (progress)->
        pv.setProgress(progress)
      .on 'finish', ->
        rimraf targetFolder, ->

          pv.setTitle("Unzip...")
          Unzip.unzip(targetZipFile, installToPath)
          .on 'finish', ->

            fs.unlink targetZipFile, ->
              pv.destroy()
              onFinish()
{'fraction_non_alphanumeric': 0.13908179012345678, 'fraction_numerical': 0.0, 'mean_word_length': 3.512619669277633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711353', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1578, 'n_words': 308}