<commit_before>path = require 'path'
fs = require 'fs-extra'
os = require 'os'
uuid = require 'uuid'

_ = require 'underscore'

Q = require 'q'
{github, gogs, gogsApi} = require '../utils/gitApi'
{execFile} = require 'child_process'

module.exports =
class GitCreatePackageManager

  activate: ->
    atom.workspaceView.command "atom-butterfly:gitCreatePackage", => @gitCreatePackage()
    console.log "GitCreatePackageManager activate"

  gitCreatePackage: ->
    GitCreatePackageWizardView = require './gitCreatePackage-wizard-view'
    gitCreatePackageWizardView = new GitCreatePackageWizardView().attach()
    info = null
    gitCreatePackageWizardView.finishPromise()
    .then (options) ->
      selectPath = atom.packages.getActivePackage('tree-view').mainModule.treeView.selectedPath
      if require('fs').statSync(selectPath).isFile()
        tmpfile = path.resolve os.tmpdir(), uuid.v1()
      else
        tmpfile = os.tmpdir()

      tmpDir = path.resolve tmpfile, path.basename(selectPath)
      if fs.existsSync tmpDir
        fs.removeSync tmpDir
      fs.copySync selectPath, tmpDir

      _.extend(options, gitPath: tmpDir)
    .then (options) ->
      info = options
      if options.repo is 'github'
        github().createRepos
          options:
            username: options.account
            password: <PASSWORD>
          name: options.packageName
          description: options.describe
          private: false
          auto_init:true
      else if options.repo is 'gogs'
        gogs().createRepos
          options:
            username: options.account
            password: <PASSWORD>
          Name: options.packageName
          Description: options.describe
          Private: false
          AutoInit: true
          License: 'MIT License'
    .then (obj) ->
      # 开始同步仓库资源
      if obj.type is 'gogs'
        repoUrl = "#{gogsApi}/#{info.account}/#{info.packageName}.git"
      else if obj.type is 'github'
        repoUrl = "https://github.com/#{info.account}/#{info.packageName}.git"

      Q.Promise (resolve, reject, notify) ->
        args = [info.gitPath
        repoUrl
        info.describe]
        options =
          maxBuffer: 1024*1024*10
        gitPath = atom.config.get('atom-butterfly.gitCloneEnvironmentPath') # 设置git环境变量
        options['env'] = path: gitPath if gitPath

        if obj.result # 创建仓库成功
          file = './lib/utils/gitApi_create.sh'
        else # 仓库已经存在
          file = './lib/utils/gitApi_update.sh'
        
        notify stdout: "execFile: #{file} #{args.join(' ') if args}"
        cp = execFile file, args, options, (error, stdout, stderr) ->
          if error then reject(error) else resolve()

        cp.on 'exit', (code, signal)->
          console.log "code:#{code}   signal： #{signal}"
          if signal is 'SIGTERM' and code == null
            reject new Error "SIGTERM"
          else
            resolve(repoUrl)

        cp.stdout.on 'data', (data) -> notify stdout: data.toString()
        cp.stderr.on 'data', (data) -> notify stderr: data.toString()
    .then (repoUrl)->
      # 开始发布到chameleon packagesManager
      server = atom.config.get('atom-butterfly.puzzleServerAddress')
      r = request.post {url:"#{server}/api/packages", timeout: 1000*60*10}, (err, httpResponse, body) ->
        reject(err) if err
        if httpResponse.statusCode is 201
          resolve
            result: true
            statusCode: 201
            body: body
        else if httpResponse.statusCode is 403
          resolve
            result: false
            statusCode: 403
            body: body
      form = r.form()
      form.append "access_token","#{atom.config.get('atom-butterfly.puzzleAccessToken')}"
      form.append "name", info.packageName
      form.append "repository_url", repoUrl
      form.append "description", info.describe || info.packageName
      form.append "previews", info.previews if info.previews
      # form.append "tags", info.tags if info.tags
    .then (obj) ->
      # TODO 是否更新此package
      if obj.statusCode is 403
        console.log 'update this package...'
    .progress (notify) ->
      console.log notify.stdout if notify.stdout
      console.error notify.stderr if notify.stderr
    .catch (error) ->
      console.trace error.stack
      done(error)
      alert("#{error}")
    .finally ->
      console.log "publish package finally."
<commit_msg>commit<commit_after>path = require 'path'
fs = require 'fs-extra'
os = require 'os'
uuid = require 'uuid'

_ = require 'underscore'

Q = require 'q'
{github, gogs, gogsApi} = require '../utils/gitApi'
{execFile} = require 'child_process'

module.exports =
class GitCreatePackageManager

  activate: ->
    atom.workspaceView.command "atom-butterfly:gitCreatePackage", => @gitCreatePackage()
    console.log "GitCreatePackageManager activate"

  gitCreatePackage: ->
    GitCreatePackageWizardView = require './gitCreatePackage-wizard-view'
    gitCreatePackageWizardView = new GitCreatePackageWizardView().attach()
    info = null
    gitCreatePackageWizardView.finishPromise()
    .then (options) ->
      gitCreatePackageWizardView.destroy()

      selectPath = atom.packages.getActivePackage('tree-view').mainModule.treeView.selectedPath
      if require('fs').statSync(selectPath).isFile()
        tmpfile = path.resolve os.tmpdir(), uuid.v1()
      else
        tmpfile = os.tmpdir()

      tmpDir = path.resolve tmpfile, path.basename(selectPath)
      if fs.existsSync tmpDir
        fs.removeSync tmpDir
      fs.copySync selectPath, tmpDir

      _.extend(options, gitPath: tmpDir)
    .then (options) ->
      info = options
      if options.repo is 'github'
        github().createRepos
          options:
            username: options.account
            password: <PASSWORD>
          name: options.packageName
          description: options.describe
          private: false
          auto_init:true
      else if options.repo is 'gogs'
        gogs().createRepos
          options:
            username: options.account
            password: <PASSWORD>
          Name: options.packageName
          Description: options.describe
          Private: false
          AutoInit: true
          License: 'MIT License'
    .then (obj) ->
      # 开始同步仓库资源
      if obj.type is 'gogs'
        repoUrl = "#{gogsApi}/#{info.account}/#{info.packageName}.git"
      else if obj.type is 'github'
        repoUrl = "https://github.com/#{info.account}/#{info.packageName}.git"

      Q.Promise (resolve, reject, notify) ->
        args = [info.gitPath
        repoUrl
        info.describe]
        options =
          maxBuffer: 1024*1024*10
        gitPath = atom.config.get('atom-butterfly.gitCloneEnvironmentPath') # 设置git环境变量
        options['env'] = path: gitPath if gitPath

        if obj.result # 创建仓库成功
          file = './lib/utils/gitApi_create.sh'
        else # 仓库已经存在
          file = './lib/utils/gitApi_update.sh'
        
        notify stdout: "execFile: #{file} #{args.join(' ') if args}"
        cp = execFile file, args, options, (error, stdout, stderr) ->
          if error then reject(error) else resolve()

        cp.on 'exit', (code, signal)->
          console.log "code:#{code}   signal： #{signal}"
          if signal is 'SIGTERM' and code == null
            reject new Error "SIGTERM"
          else
            resolve(repoUrl)

        cp.stdout.on 'data', (data) -> notify stdout: data.toString()
        cp.stderr.on 'data', (data) -> notify stderr: data.toString()
    .then (repoUrl)->
      # 开始发布到chameleon packagesManager
      server = atom.config.get('atom-butterfly.puzzleServerAddress')
      r = request.post {url:"#{server}/api/packages", timeout: 1000*60*10}, (err, httpResponse, body) ->
        reject(err) if err
        if httpResponse.statusCode is 201
          resolve
            result: true
            statusCode: 201
            body: body
        else if httpResponse.statusCode is 403
          resolve
            result: false
            statusCode: 403
            body: body
      form = r.form()
      form.append "access_token","#{atom.config.get('atom-butterfly.puzzleAccessToken')}"
      form.append "name", info.packageName
      form.append "repository_url", repoUrl
      form.append "description", info.describe || info.packageName
      form.append "previews", info.previews if info.previews
      # form.append "tags", info.tags if info.tags
    .then (obj) ->
      # TODO 是否更新此package
      if obj.statusCode is 403
        console.log 'update this package...'
    .progress (notify) ->
      console.log notify.stdout if notify.stdout
      console.error notify.stderr if notify.stderr
    .catch (error) ->
      console.trace error.stack
      done(error)
      alert("#{error}")
    .finally ->
      console.log "publish package finally."

{'fraction_non_alphanumeric': 0.11474293496765407, 'fraction_numerical': 0.007717625695153785, 'mean_word_length': 2.305326331582896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 41, 'https://': 2, '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': '2490366', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2597, 'n_words': 623}