<commit_before>require 'may/templator'
require 'may/path_resolver'
require 'may/xcodeproj'

module May
  class Command
    class Generate
      def self.run(context, *args)
        new(context).run_with_root_dir(*args)
      end

      def initialize(context)
        @context = context
      end

      def run_with_root_dir(*args)
        Dir.chdir(@context.root_dir) do
          run(*args)
        end
      end

      def run(*args)
        parse_args(args)

        xcodeproj = May::Xcodeproj.new(@context.xcodeproj_path)
        resolver.each(@path, @template_name) do |template_path, destination|
          puts "use emplate: #{template_path}"
          puts "write: #{destination}"
          write_generate_file(template_path, destination)
          xcodeproj.add_file(destination)
          puts ''
        end

        xcodeproj.save
      end

      def parse_args(args)
        h = args.shift
        raise "Can't parse options" unless h.kind_of?(Hash)
        @path = h[:path]
        @class_name = File.basename(h[:path])
        @template_name = h[:super_class_name] || 'NSObject'
      end

      def resolver
        @resolver ||= May::PathResolver.new(@context)
      end

      def write_generate_file(template_path, destination)
        binding = May::RenderBinding.new(class_name: @class_name)
        May::Templator.new(template_path, destination, binding).write
      end
    end
  end
end<commit_msg>extract a method<commit_after>require 'may/templator'
require 'may/path_resolver'
require 'may/xcodeproj'

module May
  class Command
    class Generate
      def self.run(context, *args)
        new(context).run_with_root_dir(*args)
      end

      def initialize(context)
        @context = context
      end

      def run_with_root_dir(*args)
        Dir.chdir(@context.root_dir) do
          run(*args)
        end
      end

      def run(*args)
        parse_args(args)

        resolver.each(@path, @template_name) do |template_path, destination|
          puts "use emplate: #{template_path}"
          puts "write: #{destination}"

          write_generate_file(template_path, destination)
          xcodeproj.add_file(destination)

          puts ''
        end

        xcodeproj.save
      end

      def parse_args(args)
        h = args.shift
        raise "Can't parse options" unless h.kind_of?(Hash)
        @path = h[:path]
        @class_name = File.basename(h[:path])
        @template_name = h[:super_class_name] || 'NSObject'
      end

      def resolver
        @resolver ||= May::PathResolver.new(@context)
      end

      def xcodeproj
        @xcodeproj ||= May::Xcodeproj.new(@context.xcodeproj_path)
      end

      def write_generate_file(template_path, destination)
        binding = May::RenderBinding.new(class_name: @class_name)
        May::Templator.new(template_path, destination, binding).write
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10003461405330565, 'fraction_numerical': 0.0, 'mean_word_length': 2.372228704784131, 'pattern_counts': {'":': 0, '<': 3, '<?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': '330687', 'n_tokens_mistral': 906, 'n_tokens_neox': 903, 'n_words': 182}