<commit_before>require 'erb'

module Codegens
  module Command
    class Generate
      CURRENT_DIRECTORY = ""

      attr_accessor :template_args, :template_name, :template_path, :template_answers

      def initialize(name, args, answers)
        self.template_name = name
        self.template_path = Codegens.generator_path(File.join(name, 'template'))
        self.template_args = args
        self.template_answers = answers
      end

      def create_content(relative_template_path, relative_content_path, content)
        if File.directory?(File.join(template_path, relative_template_path, content))
          relative_content_path = create_directory(relative_content_path, content)
          render_directory(File.join(relative_template_path, content), relative_content_path)
        else
          create_file(relative_template_path, relative_content_path, content)
        end
      end

      def create_directory(relative_content_path, path)
        while /arg(\d+)/.match(path)
          path = path.gsub("arg#{$1}", template_args[$1.to_i])
        end
        path = File.join(relative_content_path, path)
        Dir.mkdir(Codegens.generation_path(path))

        return path
      end

      def create_file(relative_template_path, relative_content_path, file_name)
        source_path = File.join(template_path, relative_template_path, file_name)
        destination_path = Codegens.generation_path(File.join(relative_content_path, file_name))
        
        args = self.template_args
        answers = self.template_answers
        File.open(destination_path, 'w') do |f|
          f.puts(ERB.new(File.read(source_path)).result(binding))
        end
      end

      def execute
        render_directory(CURRENT_DIRECTORY, CURRENT_DIRECTORY)
      end

      def render_directory(relative_template_path, relative_content_path)
        Codegens.directory_contents(File.join(template_path, relative_template_path)).each do |content|
          create_content(relative_template_path, relative_content_path, content)
        end
      end<commit_msg>Added argument substitution into file names.<commit_after>require 'erb'

module Codegens
  module Command
    class Generate
      CURRENT_DIRECTORY = ""

      attr_accessor :template_args, :template_name, :template_path, :template_answers

      def initialize(name, args, answers)
        self.template_name = name
        self.template_path = Codegens.generator_path(File.join(name, 'template'))
        self.template_args = args
        self.template_answers = answers
      end

      def create_content(relative_template_path, relative_content_path, content)
        if File.directory?(File.join(template_path, relative_template_path, content))
          relative_content_path = create_directory(relative_content_path, content)
          render_directory(File.join(relative_template_path, content), relative_content_path)
        else
          create_file(relative_template_path, relative_content_path, content)
        end
      end

      def create_directory(relative_content_path, path)
        path = File.join(relative_content_path, interpret(path))
        Dir.mkdir(Codegens.generation_path(path))

        return path
      end

      def create_file(relative_template_path, relative_content_path, file_name)
        source_path = File.join(template_path, relative_template_path, file_name)
        destination_path = Codegens.generation_path(File.join(relative_content_path, interpret(file_name)))
        
        File.open(destination_path, 'w') do |f|
          f.puts(ERB.new(File.read(source_path)).result(binding))
        end
      end

      def execute
        render_directory(CURRENT_DIRECTORY, CURRENT_DIRECTORY)
      end

      def interpret(name)
        while /arg(\d+)/.match(name)
          name = name.gsub("arg#{$1}", self.template_args[$1.to_i])
        end
        name
      end

      def render_directory(relative_template_path, relative_content_path)
        Codegens.directory_contents(File.join(template_path, relative_template_path)).each do |content|
          create_content(relative_template_path, relative_content_path, content)
        end
      end
{'fraction_non_alphanumeric': 0.07965451055662189, 'fraction_numerical': 0.0009596928982725527, 'mean_word_length': 3.2846865364850975, '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': '5607122', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1158, 'n_words': 210}