<commit_before>class Engineer
  module Generators
    class InstallGenerator < Rails::Generators::Base

      def self.source_root
        @source_root ||= File.expand_path('../templates', __FILE__)
      end

      def create_engine_files
        directory 'lib', 'lib'
      end

      def namespace_application_controller
        unnested_path = Rails.application.paths.app.controllers.to_a.detect do |p|
          File.exists? File.join(p, 'application_controller.rb')
        end

        return unless unnested_path

        nested_path = File.join(unnested_path, app_name)
        empty_directory nested_path

        create_file File.join(nested_path, 'application_controller.rb') do
          content = File.read File.join(unnested_path, 'application_controller.rb')
          content.gsub! "class ApplicationController", "class #{app_module}::ApplicationController"
          content
        end

        remove_file File.join(unnested_path, 'application_controller.rb')

        controller_files = Rails.application.paths.app.controllers.to_a.collect do |p|
          Dir[File.join(p, "**", "*_controller.rb")]
        end.flatten

        controller_files.reject { |f| f =~ /application_controller\.rb$/ }.each do |file|
          gsub_file file, "< ApplicationController", "< #{app_module}::ApplicationController"
        end
      end

      def append_gemspec_to_Rakefile
        in_root do
          unless IO.read('Rakefile') =~ /Engineer::Tasks.new/
            append_file 'Rakefile' do
<<-RAKE
<commit_msg>The root engine controller class extends ApplicationController if available.<commit_after>class Engineer
  module Generators
    class InstallGenerator < Rails::Generators::Base

      def self.source_root
        @source_root ||= File.expand_path('../templates', __FILE__)
      end

      def create_engine_files
        directory 'lib', 'lib'
      end

      def namespace_application_controller
        unnested_path = Rails.application.paths.app.controllers.to_a.detect do |p|
          File.exists? File.join(p, 'application_controller.rb')
        end

        return unless unnested_path

        nested_path = File.join(unnested_path, app_name)
        empty_directory nested_path

        create_file File.join(nested_path, 'application_controller.rb') do
          content = File.read File.join(unnested_path, 'application_controller.rb')
          content.gsub!("class ApplicationController < ActionController::Base",
            "class #{app_module}::ApplicationController < defined?(ApplicationController) ? ApplicationController : ActionController::Base")
          content
        end

        remove_file File.join(unnested_path, 'application_controller.rb')

        controller_files = Rails.application.paths.app.controllers.to_a.collect do |p|
          Dir[File.join(p, "**", "*_controller.rb")]
        end.flatten

        controller_files.reject { |f| f =~ /application_controller\.rb$/ }.each do |file|
          gsub_file file, "< ApplicationController", "< #{app_module}::ApplicationController"
        end
      end

      def append_gemspec_to_Rakefile
        in_root do
          unless IO.read('Rakefile') =~ /Engineer::Tasks.new/
            append_file 'Rakefile' do
<<-RAKE

{'fraction_non_alphanumeric': 0.10012360939431397, 'fraction_numerical': 0.0, 'mean_word_length': 2.895306859205776, 'pattern_counts': {'":': 0, '<': 15, '<?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': '3013633', 'n_tokens_mistral': 918, 'n_tokens_neox': 887, 'n_words': 183}