<commit_before># encoding: utf-8
# frozen_string_literal: true

require 'pastel'
require 'pathname'
require 'ostruct'

require_relative '../cmd'
require_relative '../gemspec'
require_relative '../licenses'
require_relative '../plugins'
require_relative '../templater'
require_relative '../version'


module TTY
  module Commands
    # The `new` command
    #
    # @api private
    class New < TTY::Cmd
      include TTY::Licenses

      GEMSPEC_PATH = Pathname(__dir__).join('../../../tty.gemspec').realpath.to_s

      # @api private
      attr_reader :app_name

      attr_reader :app_path

      # @api private
      attr_reader :options

      # @api private
      attr_reader :target_path

      attr_reader :templates

      def initialize(app_path, options)
        @app_path = relative_path_from(root_path, app_path)
        @app_name = name_from_path(app_path)
        @options  = options
        @pastel   = Pastel.new

        @target_path = root_path.join(@app_path)
        @templater = Templater.new('new', @app_path)
        @runner = command(printer: :null)
      end

      def git_exist?
        exec_exist?('git')
      end

      def git_author
        git_exist? ? `git config user.name`.chomp : ''
      end

      def author
        if !Array(options['author']).empty?
          options['author'].join(', ')
        elsif !git_author.empty?
          git_author
        else
          'TODO: Write your name'
        end
      end

      def namespaced_path
        app_name.tr('-', '/')
      end

      def underscored_name
        app_name.tr('-', '_')
      end

      def constantinized_name
        constantinize(app_name)
      end

      def template_options
        opts = OpenStruct.new
        opts[:app_name] = app_name
        opts[:author]   = author
        opts[:namespaced_path]  = namespaced_path
        opts[:underscored_name] = underscored_name
        opts[:constantinized_name] = constantinized_name
        opts[:constantinized_parts] = constantinized_name.split('::')
        opts[:indent] = '  ' * constantinized_name.split('::').size
        opts
      end

      def color_option
        options['no-color'] ? { color: false } : {}
      end

      def gemspec_name
        "#{app_name}.gemspec"
      end

      def gemspec_path
        target_path.join("#{app_name}.gemspec").to_s
      end

      # Execute the command
      #
      # @api public
      def execute
        puts "OPTS: #{options}" if options['debug']

        coc_opt  = options['coc'] ? '--coc' : '--no-coc'
        ext_opt  = options['ext'] ? '--ext' : '--no-ext'
        test_opt = options['test']
        command = [
          "bundle gem #{target_path}",
          '--no-mit',
          '--no-exe',
          coc_opt,
          ext_opt,
          "-t #{test_opt}"
        ].join(' ')

        git_out = ''

        @runner.run(command) do |out, err|
          next unless out
          if out =~ /^Initializing git/
            git_out = out.dup
            next
          end

          if !options['no-color']
            puts color_actions(out)
          else
            puts out
          end
        end

        add_app_templates
        add_empty_directories
        add_required_libs_to_gemspec
        @templater.generate(template_options, color_option)
        make_executable
        puts git_out unless git_out.empty?
      end

      private

      def color_actions(out)
        out.gsub(/^(\s+)(create)/, '\1' + @pastel.green('\2'))
           .gsub(/^(\s+)(identical)/, '\1' + @pastel.blue('\2'))
           .gsub(/^(\s+)(conflict)/, '\1' + @pastel.red('\2'))
           .gsub(/^(\s+)(forced|skipped)/, '\1' + @pastel.yellow('\2'))
      end

      def make_executable
        exec_path = target_path.join("exe/#{app_name}")
        exec_path.chmod(exec_path.stat.mode | 0o111)
      end<commit_msg>Add helpful messaging after cli creation<commit_after># encoding: utf-8
# frozen_string_literal: true

require 'pastel'
require 'pathname'
require 'ostruct'

require_relative '../cmd'
require_relative '../gemspec'
require_relative '../licenses'
require_relative '../plugins'
require_relative '../templater'
require_relative '../version'


module TTY
  module Commands
    # The `new` command
    #
    # @api private
    class New < TTY::Cmd
      include TTY::Licenses

      GEMSPEC_PATH = Pathname(__dir__).join('../../../tty.gemspec').realpath.to_s

      # @api private
      attr_reader :app_name

      attr_reader :app_path

      # @api private
      attr_reader :options

      # @api private
      attr_reader :target_path

      attr_reader :templates

      def initialize(app_path, options)
        @app_path = relative_path_from(root_path, app_path)
        @app_name = name_from_path(app_path)
        @options  = options
        @pastel   = Pastel.new

        @target_path = root_path.join(@app_path)
        @templater = Templater.new('new', @app_path)
        @runner = command(printer: :null)
      end

      def git_exist?
        exec_exist?('git')
      end

      def git_author
        git_exist? ? `git config user.name`.chomp : ''
      end

      def author
        if !Array(options['author']).empty?
          options['author'].join(', ')
        elsif !git_author.empty?
          git_author
        else
          'TODO: Write your name'
        end
      end

      def namespaced_path
        app_name.tr('-', '/')
      end

      def underscored_name
        app_name.tr('-', '_')
      end

      def constantinized_name
        constantinize(app_name)
      end

      def template_options
        opts = OpenStruct.new
        opts[:app_name] = app_name
        opts[:author]   = author
        opts[:namespaced_path]  = namespaced_path
        opts[:underscored_name] = underscored_name
        opts[:constantinized_name] = constantinized_name
        opts[:constantinized_parts] = constantinized_name.split('::')
        opts[:indent] = '  ' * constantinized_name.split('::').size
        opts
      end

      def color_option
        options['no-color'] ? { color: false } : {}
      end

      def gemspec_name
        "#{app_name}.gemspec"
      end

      def gemspec_path
        target_path.join("#{app_name}.gemspec").to_s
      end

      # Execute the command
      #
      # @api public
      def execute
        puts "OPTS: #{options}" if options['debug']

        coc_opt  = options['coc'] ? '--coc' : '--no-coc'
        ext_opt  = options['ext'] ? '--ext' : '--no-ext'
        test_opt = options['test']
        command = [
          "bundle gem #{target_path}",
          '--no-mit',
          '--no-exe',
          coc_opt,
          ext_opt,
          "-t #{test_opt}"
        ].join(' ')

        git_out = ''

        @runner.run(command) do |out, err|
          next unless out
          if out =~ /^Initializing git/
            git_out = out.dup
            next
          end

          if !options['no-color']
            puts color_actions(out)
          else
            puts out
          end
        end

        add_app_templates
        add_empty_directories
        add_required_libs_to_gemspec
        @templater.generate(template_options, color_option)
        make_executable
        puts git_out unless git_out.empty?

        puts "\n" + @pastel.green("Your teletype project has been created successfully.")
        puts "\n" + @pastel.green("Run \"teletype help\" for more commands.\n")
      end

      private

      def color_actions(out)
        out.gsub(/^(\s+)(create)/, '\1' + @pastel.green('\2'))
           .gsub(/^(\s+)(identical)/, '\1' + @pastel.blue('\2'))
           .gsub(/^(\s+)(conflict)/, '\1' + @pastel.red('\2'))
           .gsub(/^(\s+)(forced|skipped)/, '\1' + @pastel.yellow('\2'))
      end

      def make_executable
        exec_path = target_path.join("exe/#{app_name}")
        exec_path.chmod(exec_path.stat.mode | 0o111)
      end
{'fraction_non_alphanumeric': 0.1285206800304491, 'fraction_numerical': 0.0032986551636640447, 'mean_word_length': 1.9272187151875233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '202584', 'n_tokens_mistral': 2554, 'n_tokens_neox': 2467, 'n_words': 501}