<commit_before>require 'fileutils'
require 'json'

namespace :exrails do
  desc 'Generates component files for models in the angular app'
  task :ng => :environment do |t, args|
    models = []
    ng_task_found = false
    ARGV.each do |arg|
      ng_task_found = (arg == 'exrails:ng') unless ng_task_found
      next if !ng_task_found || (arg == 'exrails:ng')

      model_match = /^[a-zA-Z][a-zA-Z,_]+$/.match(arg)
      if model_match
        models.push(model_match[0])
      end
    end
    
    # Create fake tasks
    models.each { |model| task model.to_sym do ; end }

    # Generate components for models
    models.each do |model|
      begin
        klass = model.classify.constantize
        puts "Generating files for #{klass.name} ..."
        ng_generate_component_files(klass)
        insert_component_scripts(klass)
        generate_controller_file(klass)
        add_resources_route(klass)
      rescue
      end
    end
  end

  namespace :ng do
    desc 'Generates component files for all resourcified models in the angular app'
    task :all => :environment do |t, args|
      # Generate components for models
      resourcified_models.each do |model|
        begin
          klass = model.classify.constantize
          puts "Generating files for #{klass.name} ..."
          ng_generate_component_files(klass)
          insert_component_scripts(klass)
          generate_controller_file(klass)
          add_resources_route(klass)
        rescue
        end
      end
    end
  end

  def ng_generate_component_files(klass)
    components_dir = Rails.root.join('angular', 'src', 'app', 'components').to_s

    dir = File.join(components_dir, klass.name.pluralize.underscore.dasherize)
    Dir.mkdir(dir) unless File.directory?(dir)

    # Generate views: index.html, new.html, show.html, edit.html
    ['index', 'new', 'show', 'edit', 'uploads', 'bulk'].each do |action|
      file_path = File.join(dir, "#{action}.html")
      File.open(file_path, 'w') do |file|
        file_content = ng_component_files_tpl[action.to_sym]
        file_content.gsub! '/* MODELNAME */', klass.name
        file.write(file_content)
      end unless File.exists?(file_path)
    end

    # Generate config.js
    file_path = File.join(dir, "config.js")
    File.open(file_path, 'w') do |file|
      config_tpl = ng_component_files_tpl[:config]
      
      @model_class = klass
      config_tpl.gsub! '/* GRIDCONFIG */', ng_grid.to_s
      config_tpl.gsub! '/* FIELDCONFIG */', ng_fields.to_s

      config_tpl.gsub! '/* MODELNAME */', klass.name
      config_tpl.gsub! '/* MODELKEY */', klass.name.underscore
      file.write(config_tpl)

    end unless File.exists?(file_path)
  end

  def insert_component_scripts(klass)
    index_file = Rails.root.join('angular', 'src', 'index.html')
    pattern = '<script src="app/components/roles/config.js"></script>'
    url = klass.name.underscore.dasherize.pluralize
    replace = "#{' ' * 8}<script src=\"app/components/#{url}/config.js\"></script>\n"
    insert_into_file(index_file, pattern, replace)
  end

  def generate_controller_file(klass)
    controllers_dir = Rails.root.join('app', 'controllers').to_s

    controller_tpl = "class #{klass.name.classify.pluralize}Controller < ApplicationController\n"
    controller_tpl += "  resourcify\n"
    controller_tpl += "end\n"
    file_path = File.join(controllers_dir, "#{klass.name.pluralize.underscore.dasherize}_controller.rb")
    File.open(file_path, 'w') do |file|
      file.write(controller_tpl)
    end unless File.exists?(file_path)
  end

  def add_resources_route(klass)
    routes_file = Rails.root.join('config', 'routes.rb')
    pattern = 'resources :roles'
    replace = "#{' ' * 4}resources :#{klass.name.underscore.pluralize}\n"
    insert_into_file(routes_file, pattern, replace)
  end

  def insert_into_file(file, pattern, replace, after_pattern = true)
    lines = File.readlines(file)
    lines.each_with_index do |line, index|
      if line.include? pattern<commit_msg>Fixed controller file name bug<commit_after>require 'fileutils'
require 'json'

namespace :exrails do
  desc 'Generates component files for models in the angular app'
  task :ng => :environment do |t, args|
    models = []
    ng_task_found = false
    ARGV.each do |arg|
      ng_task_found = (arg == 'exrails:ng') unless ng_task_found
      next if !ng_task_found || (arg == 'exrails:ng')

      model_match = /^[a-zA-Z][a-zA-Z,_]+$/.match(arg)
      if model_match
        models.push(model_match[0])
      end
    end
    
    # Create fake tasks
    models.each { |model| task model.to_sym do ; end }

    # Generate components for models
    models.each do |model|
      begin
        klass = model.classify.constantize
        puts "Generating files for #{klass.name} ..."
        ng_generate_component_files(klass)
        insert_component_scripts(klass)
        generate_controller_file(klass)
        add_resources_route(klass)
      rescue
      end
    end
  end

  namespace :ng do
    desc 'Generates component files for all resourcified models in the angular app'
    task :all => :environment do |t, args|
      # Generate components for models
      resourcified_models.each do |model|
        begin
          klass = model.classify.constantize
          puts "Generating files for #{klass.name} ..."
          ng_generate_component_files(klass)
          insert_component_scripts(klass)
          generate_controller_file(klass)
          add_resources_route(klass)
        rescue
        end
      end
    end
  end

  def ng_generate_component_files(klass)
    components_dir = Rails.root.join('angular', 'src', 'app', 'components').to_s

    dir = File.join(components_dir, klass.name.pluralize.underscore.dasherize)
    Dir.mkdir(dir) unless File.directory?(dir)

    # Generate views: index.html, new.html, show.html, edit.html
    ['index', 'new', 'show', 'edit', 'uploads', 'bulk'].each do |action|
      file_path = File.join(dir, "#{action}.html")
      File.open(file_path, 'w') do |file|
        file_content = ng_component_files_tpl[action.to_sym]
        file_content.gsub! '/* MODELNAME */', klass.name
        file.write(file_content)
      end unless File.exists?(file_path)
    end

    # Generate config.js
    file_path = File.join(dir, "config.js")
    File.open(file_path, 'w') do |file|
      config_tpl = ng_component_files_tpl[:config]
      
      @model_class = klass
      config_tpl.gsub! '/* GRIDCONFIG */', ng_grid.to_s
      config_tpl.gsub! '/* FIELDCONFIG */', ng_fields.to_s

      config_tpl.gsub! '/* MODELNAME */', klass.name
      config_tpl.gsub! '/* MODELKEY */', klass.name.underscore
      file.write(config_tpl)

    end unless File.exists?(file_path)
  end

  def insert_component_scripts(klass)
    index_file = Rails.root.join('angular', 'src', 'index.html')
    pattern = '<script src="app/components/roles/config.js"></script>'
    url = klass.name.underscore.dasherize.pluralize
    replace = "#{' ' * 8}<script src=\"app/components/#{url}/config.js\"></script>\n"
    insert_into_file(index_file, pattern, replace)
  end

  def generate_controller_file(klass)
    controllers_dir = Rails.root.join('app', 'controllers').to_s

    controller_tpl = "class #{klass.name.classify.pluralize}Controller < ApplicationController\n"
    controller_tpl += "  resourcify\n"
    controller_tpl += "end\n"
    file_path = File.join(controllers_dir, "#{klass.name.pluralize.underscore}_controller.rb")
    File.open(file_path, 'w') do |file|
      file.write(controller_tpl)
    end unless File.exists?(file_path)
  end

  def add_resources_route(klass)
    routes_file = Rails.root.join('config', 'routes.rb')
    pattern = 'resources :roles'
    replace = "#{' ' * 4}resources :#{klass.name.underscore.pluralize}\n"
    insert_into_file(routes_file, pattern, replace)
  end

  def insert_into_file(file, pattern, replace, after_pattern = true)
    lines = File.readlines(file)
    lines.each_with_index do |line, index|
      if line.include? pattern
{'fraction_non_alphanumeric': 0.12329793878825734, 'fraction_numerical': 0.0007495315427857589, 'mean_word_length': 3.122554067971164, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '5965916', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2649, 'n_words': 543}