forked from jm/rails_upgrade
-
Notifications
You must be signed in to change notification settings - Fork 60
/
new_configuration_generator.rb
57 lines (46 loc) · 1.42 KB
/
new_configuration_generator.rb
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
module Rails
module Upgrading
class NewConfigurationGenerator
def generate_new_configurations
if has_environment?
generate_new_application_rb
else
raise FileNotFoundError, "Can't find environment.rb [config/environment.rb]!"
end
end
def has_environment?
File.exists?("config/environment.rb")
end
def environment_code
File.open("config/environment.rb").read
end
def generate_new_application_rb
environment_file = environment_code
initializer_code = ""
if matches = environment_file.match(/Rails\:\:Initializer\.run do \|config\|\n(.*)\nend/m)
initializer_code = matches[1]
else
raise "There doesn't seem to be a real environment.rb in your app. Are you sure config/environment.rb has the right contents?"
end
frame = "# Put this in config/application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env) if defined?(Bundler)
module #{app_name.classify}
class Application < Rails::Application
config.autoload_paths += [config.root.join('lib')]
config.encoding = 'utf-8'
%s
end
end"
frame % [indent(initializer_code)]
end
def indent(text)
text.split("\n").map {|l| " #{l}"}.join("\n")
end
def app_name
File.basename(Dir.pwd)
end
end
end
end