<commit_before>_                                = require 'underscore'
path                             = require 'path'
fsPlus                           = require 'fs-plus'
fsExtra                          = require 'fs-extra'
fs                               = require 'fs'
{View, $}                        = require 'space-pen'
{SelectListView, TextEditorView} = require 'atom-space-pen-views'
{allowUnsafeEval, allowUnsafeNewFunction} = require 'loophole'


class ParamSelectView extends View

  initialize: (@targetPath, @template)->

    atom.commands.add @element,
      'core:confirm': => @onConfirm()
      'core:cancel': => @destroy()


    @createButton.on 'click', =>
      @onConfirm()

    @cancelButton.on 'click', =>
      @destroy()


  attach: ->
    @panel = atom.workspace.addModalPanel(item: this)
    # @miniEditor.focus()

  destroy: ->
    @panel.destroy()
    atom.workspace.getActivePane().activate()

  onConfirm: ->

    cfg = {}

    for param in (@template.params ? [])
      cfg[param] = @[param+"Editor"].getText()

    for rule in ((@template.rules?(cfg).items) ? [])
      continue unless rule.destinationFile

      if rule.sourceContentFile
        fullPathToTemplateFile = path.join @template.rootPath, rule.sourceContentFile
        fullPathToNewFile      = path.join @targetPath, rule.destinationFile
        try
          fsPlus.makeTreeSync( path.dirname(fullPathToNewFile) )
          fsExtra.copySync fullPathToTemplateFile, fullPathToNewFile
        catch error
          console.error  "Template processing error: #{error}"

      if rule.sourceTemplateFile
        fullPathToTemplateFile = path.join @template.rootPath, rule.sourceTemplateFile
        fullPathToNewFile      = path.join @targetPath, rule.destinationFile

        try
          fsPlus.makeTreeSync( path.dirname(fullPathToNewFile) )
          t = fs.readFileSync(fullPathToTemplateFile, "utf8")
          allowUnsafeEval ->
            allowUnsafeNewFunction ->
              compiledTemplate = _.template(t)<commit_msg>Add 'directory' prop parsing<commit_after>_                                = require 'underscore'
path                             = require 'path'
fsPlus                           = require 'fs-plus'
fsExtra                          = require 'fs-extra'
fs                               = require 'fs'
{View, $}                        = require 'space-pen'
{SelectListView, TextEditorView} = require 'atom-space-pen-views'
{allowUnsafeEval, allowUnsafeNewFunction} = require 'loophole'


class ParamSelectView extends View

  initialize: (@targetPath, @template)->

    atom.commands.add @element,
      'core:confirm': => @onConfirm()
      'core:cancel': => @destroy()


    @createButton.on 'click', =>
      @onConfirm()

    @cancelButton.on 'click', =>
      @destroy()


  attach: ->
    @panel = atom.workspace.addModalPanel(item: this)
    # @miniEditor.focus()

  destroy: ->
    @panel.destroy()
    atom.workspace.getActivePane().activate()

  onConfirm: ->

    cfg = {}
    nameParam = ''

    for param in (@template.params ? [])
      cfg[param] = @[param+"Editor"].getText()

      if param == 'Name' && @template.directory

        if cfg[param].length > 0
          nameParam = cfg[param].replace(/\s+/g, '-').toLowerCase()
        else
          nameParam = @template.name.replace(/\s+/g, '-').toLowerCase()

    for rule in ((@template.rules?(cfg).items) ? [])
      continue unless rule.destinationFile

      if rule.sourceContentFile
        fullPathToTemplateFile = path.join @template.rootPath, rule.sourceContentFile
        fullPathToNewFile      = path.join @targetPath, nameParam, rule.destinationFile
        try
          fsPlus.makeTreeSync( path.dirname(fullPathToNewFile) )
          fsExtra.copySync fullPathToTemplateFile, fullPathToNewFile
        catch error
          console.error  "Template processing error: #{error}"

      if rule.sourceTemplateFile
        fullPathToTemplateFile = path.join @template.rootPath, rule.sourceTemplateFile
        fullPathToNewFile      = path.join @targetPath, nameParam, rule.destinationFile

        try
          fsPlus.makeTreeSync( path.dirname(fullPathToNewFile) )
          t = fs.readFileSync(fullPathToTemplateFile, "utf8")
          allowUnsafeEval ->
            allowUnsafeNewFunction ->
              compiledTemplate = _.template(t)
{'fraction_non_alphanumeric': 0.11363636363636363, 'fraction_numerical': 0.0006887052341597796, 'mean_word_length': 2.263670411985019, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6681420', 'n_tokens_mistral': 1225, 'n_tokens_neox': 1183, 'n_words': 241}