aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/tasks/framework.rake
blob: a3109a92430a4a6098ae0e049ea4d3dbe26ef50f (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
namespace :rails do
  namespace :freeze do
    desc "The rails:freeze:gems is deprecated, please use bundle install instead"
    task :gems do
      abort "The rails:freeze:gems is deprecated, please use bundle install instead"
    end

    desc 'The freeze:edge command has been deprecated, specify the path setting in your app Gemfile instead and bundle install'
    task :edge do
      abort 'The freeze:edge command has been deprecated, specify the path setting in your app Gemfile instead and bundle install'
    end
  end

  desc 'The unfreeze command has been deprecated, please use bundler commands instead'
  task :unfreeze do
    abort 'The unfreeze command has been deprecated, please use bundler commands instead'
  end

  desc "Update both configs, scripts and public/javascripts from Rails"
  task :update => [ "update:configs", "update:javascripts", "update:scripts", "update:application_controller", "update:test_directory" ]

  desc "Applies the template supplied by LOCATION=/path/to/template"
  task :template do
    template = ENV["LOCATION"]
    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 :update do
    def invoke_from_app_generator(method)
      app_generator.invoke(method)
    end

    def app_generator
      @app_generator ||= begin
        require 'rails/generators'
        require 'rails/generators/rails/app/app_generator'
        gen = Rails::Generators::AppGenerator.new ["rails"], { :with_dispatchers => true },
                                                             :destination_root => Rails.root
        File.exists?(Rails.root.join("config", "application.rb")) ?
          gen.send(:app_const) : gen.send(:valid_app_const?)
        gen
      end
    end

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

    desc "Update Prototype javascripts from your current rails install"
    task :javascripts do
      invoke_from_app_generator :create_prototype_files
    end

    desc "Adds new scripts to the application script/ directory"
    task :scripts do
      invoke_from_app_generator :create_script_files
    end

    desc "Rename application.rb to application_controller.rb"
    task :application_controller do
      old_style = Rails.root + '/app/controllers/application.rb'
      new_style = Rails.root + '/app/controllers/application_controller.rb'
      if File.exists?(old_style) && !File.exists?(new_style)
        FileUtils.mv(old_style, new_style)
        puts "#{old_style} has been renamed to #{new_style}, update your SCM as necessary"
      end
    end

    desc "Move test directories to new locations"
    task :test_directory do
      if File.exists?(Rails.root.join('test'))
        FileUtils.mkdir(Rails.root.join('test/controllers')) unless File.exists?(Rails.root.join('test/controllers'))
        [Rails.root.join('test/functional'), Rails.root.join('test/integration')].each do |controller_test_dir|
          if File.exists?(controller_test_dir)
            puts "#{controller_test_dir} exists"
            FileUtils.mv(Dir["#{controller_test_dir}/**/*"], Rails.root.join('test/controllers'), :force => true)
            FileUtils.rm_rf(controller_test_dir)
          end
        end

        if File.exists?(Rails.root.join('test/unit/helpers'))
          FileUtils.mkdir(Rails.root.join('test/helpers')) unless File.exists?(Rails.root.join('test/helpers'))
          FileUtils.mv(Dir[Rails.root.join('test/unit/helpers/**/*')], Rails.root.join('test/helpers'), :force => true)
        else
          unless File.exists?(Rails.root.join('test/helpers'))
            FileUtils.mkdir(Rails.root.join('test/helpers'))
          end
        end
        
        if File.exists?(Rails.root.join('test/unit'))
          observer_tests = "#{Rails.root}/test/unit/**/*_observer_test.rb"
          unless observer_tests.empty?
            FileUtils.mkdir(Rails.root.join('test/observers')) unless File.exists?(Rails.root.join('test/observers'))
            FileUtils.mv(observer_tests, Rails.root.join('test/observers'), :force => true)
          end
          FileUtils.mkdir(Rails.root.join('test/models')) unless File.exists?(Rails.root.join('test/models'))
          FileUtils.mv(Dir[Rails.root.join('test/unit/*')], Rails.root.join('test/models'), :force => true)
          FileUtils.rm_rf(Rails.root.join('test/unit'))
        end
      end
      
      puts <<-TEST
      
      All test directories have been updated:
      
      test/functional                   ->      test/controllers
      test/functional                   ->      test/controllers
      test/unit/helpers                 ->      test/helpers
      test/unit/**/*_observer_test.rb   ->      test/observers
      test/unit                         ->      test/models
      TEST
    end
  end
end