aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/tasks/framework.rake
blob: 255312493f685735e40b922bbe4bc18e483ad845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require 'active_support/deprecation'

namespace :app do
  desc "Update configs and some other initially generated files (or use just update:configs or update:bin)"
  task update: [ "update:configs", "update:bin", "update:upgrade_guide_info" ]

  desc "Applies the template supplied by LOCATION=(/path/to/template) or URL"
  task template: :environment do
    template = ENV["LOCATION"]
    raise "No LOCATION value given. Please set LOCATION either as path to a file or a URL" if template.blank?
    template = File.expand_path(template) if template !~ %r{\A[A-Za-z][A-Za-z0-9+\-\.]*://}
    require 'rails/generators'
    require 'rails/generators/rails/app/app_generator'
    generator = Rails::Generators::AppGenerator.new [Rails.root], {}, destination_root: Rails.root
    generator.apply template, verbose: false
  end

  namespace :templates do
    # desc "Copy all the templates from rails to the application directory for customization. Already existing local copies will be overwritten"
    task :copy do
      generators_lib = File.expand_path("../../generators", __FILE__)
      project_templates = "#{Rails.root}/lib/templates"

      default_templates = { "erb"   => %w{controller mailer scaffold},
                            "rails" => %w{controller helper scaffold_controller assets} }

      default_templates.each do |type, names|
        local_template_type_dir = File.join(project_templates, type)
        mkdir_p local_template_type_dir, verbose: false

        names.each do |name|
          dst_name = File.join(local_template_type_dir, name)
          src_name = File.join(generators_lib, type, name, "templates")
          cp_r src_name, dst_name, verbose: false
        end
      end
    end
  end

  namespace :update do
    class RailsUpdate
      def self.invoke_from_app_generator(method)
        app_generator.send(method)
      end

      def self.app_generator
        @app_generator ||= begin
          require 'rails/generators'
          require 'rails/generators/rails/app/app_generator'
          gen = Rails::Generators::AppGenerator.new ["rails"],
                                                    { api: !!Rails.application.config.api_only, update: true },
                                                    destination_root: Rails.root
          File.exist?(Rails.root.join("config", "application.rb")) ?
            gen.send(:app_const) : gen.send(:valid_const?)
          gen
        end
      end
    end

    # desc "Update config/boot.rb from your current rails install"
    task :configs do
      RailsUpdate.invoke_from_app_generator :create_boot_file
      RailsUpdate.invoke_from_app_generator :update_config_files
    end

    # desc "Adds new executables to the application bin/ directory"
    task :bin do
      RailsUpdate.invoke_from_app_generator :create_bin_files
    end

    task :upgrade_guide_info do
      RailsUpdate.invoke_from_app_generator :display_upgrade_guide_info
    end
  end
end

namespace :rails do
  %i(update template templates:copy update:configs update:bin).each do |task_name|
    task "#{task_name}" do
      ActiveSupport::Deprecation.warn(<<-MSG.squish)
        Running #{task_name} with the rails: namespace is deprecated in favor of app: namespace.
        Run bin/rails app:#{task_name} instead.
      MSG
      Rake.application.invoke_task("app:#{task_name}")
    end
  end
end