<commit_before>module Railsthemes
  class ThemeInstaller
    include Railsthemes::Logging

    def install_from_archive filepath
      Railsthemes::Utils.with_tempdir do |tempdir|
        Utils.unarchive filepath, tempdir
        install_from_file_system tempdir
      end
    end

    def install_from_file_system original_source_filepath
      source_filepath = original_source_filepath.gsub(/\\/, '/')
      logger.warn 'Installing main theme...'
      logger.info "Source filepath: #{source_filepath}"

      theme_name = nil
      if File.directory?(source_filepath)
        theme_name = install_from_directory source_filepath
      elsif Utils.archive?(source_filepath + '.tar.gz')
        install_from_archive(source_filepath + '.tar.gz')
      else
        Safe.log_and_abort 'Expected either a directory or archive.'
      end

      post_copying_changes(theme_name)
    end

    def copy_theme_portions source_filepath, file_mappings
      file_mappings.each do |src_dir, dest_prefix|
        Dir["#{source_filepath}/#{src_dir}/**/*"].each do |src|
          dest = src.sub("#{source_filepath}", dest_prefix).sub(/^\//, '')
          if File.file?(src) && !override?(dest) && !system_file?(src)
            Utils.copy_ensuring_directory_exists(src, dest)
          end
        end
      end
    end

    def install_from_directory source_filepath
      # this file causes issues when HAML is also present, and we overwrite
      # it in the ERB case, so safe to delete before copying files
      logger.debug 'Removing file app/views/layouts/application.html.erb'
      Utils.remove_file('app/views/layouts/application.html.erb')

      copy_theme_portions source_filepath, [
        ['controllers', 'app'],
        ['helpers', 'app'],
        ['layouts', 'app/views'],
        ['stylesheets', 'app/assets'],
        ['javascripts', 'app/assets'],
        ['doc', ''],
        ['images', 'app/assets'],
        ['mailers', 'app'],
      ]

      logger.warn 'Done installing.'
      return Utils.read_file(File.join(source_filepath, 'theme_name'))
    end

    def post_copying_changes theme_name
      remove_unwanted_public_files
      create_railsthemes_demo_routes
      add_needed_gems
      set_layout_in_application_controller theme_name
      add_to_asset_precompilation_list theme_name
    end

    def remove_unwanted_public_files
      ['index', '404', '422', '500'].each do |filename|
        Utils.remove_file "public/#{filename}.html"
      end
    end

    def create_railsthemes_demo_routes
      lines = Utils.lines('config/routes.rb')<commit_msg>Don't think we need this thing any more.<commit_after>module Railsthemes
  class ThemeInstaller
    include Railsthemes::Logging

    def install_from_archive filepath
      Railsthemes::Utils.with_tempdir do |tempdir|
        Utils.unarchive filepath, tempdir
        install_from_file_system tempdir
      end
    end

    def install_from_file_system original_source_filepath
      source_filepath = original_source_filepath.gsub(/\\/, '/')
      logger.warn 'Installing main theme...'
      logger.info "Source filepath: #{source_filepath}"

      theme_name = nil
      if File.directory?(source_filepath)
        theme_name = install_from_directory source_filepath
      elsif Utils.archive?(source_filepath + '.tar.gz')
        install_from_archive(source_filepath + '.tar.gz')
      else
        Safe.log_and_abort 'Expected either a directory or archive.'
      end

      post_copying_changes(theme_name)
    end

    def copy_theme_portions source_filepath, file_mappings
      file_mappings.each do |src_dir, dest_prefix|
        Dir["#{source_filepath}/#{src_dir}/**/*"].each do |src|
          dest = src.sub("#{source_filepath}", dest_prefix).sub(/^\//, '')
          if File.file?(src) && !override?(dest) && !system_file?(src)
            Utils.copy_ensuring_directory_exists(src, dest)
          end
        end
      end
    end

    def install_from_directory source_filepath
      copy_theme_portions source_filepath, [
        ['controllers', 'app'],
        ['helpers', 'app'],
        ['layouts', 'app/views'],
        ['stylesheets', 'app/assets'],
        ['javascripts', 'app/assets'],
        ['doc', ''],
        ['images', 'app/assets'],
        ['mailers', 'app'],
      ]

      logger.warn 'Done installing.'
      return Utils.read_file(File.join(source_filepath, 'theme_name'))
    end

    def post_copying_changes theme_name
      remove_unwanted_public_files
      create_railsthemes_demo_routes
      add_needed_gems
      set_layout_in_application_controller theme_name
      add_to_asset_precompilation_list theme_name
    end

    def remove_unwanted_public_files
      ['index', '404', '422', '500'].each do |filename|
        Utils.remove_file "public/#{filename}.html"
      end
    end

    def create_railsthemes_demo_routes
      lines = Utils.lines('config/routes.rb')
{'fraction_non_alphanumeric': 0.10549989777141688, 'fraction_numerical': 0.0036802289920261706, 'mean_word_length': 2.9356395816572807, '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': '801035', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1487, 'n_words': 301}