Add new generator for generating an application.rb from your existing environment.rb code
This commit is contained in:
parent
328e4de9fd
commit
8fb1e9f4e8
|
@ -0,0 +1,51 @@
|
||||||
|
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__)
|
||||||
|
|
||||||
|
module #{app_name.classify}
|
||||||
|
class Application < Rails::Application
|
||||||
|
%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
|
|
@ -2,6 +2,7 @@ $:.unshift(File.dirname(__FILE__) + "/../lib")
|
||||||
require 'routes_upgrader'
|
require 'routes_upgrader'
|
||||||
require 'gemfile_generator'
|
require 'gemfile_generator'
|
||||||
require 'application_checker'
|
require 'application_checker'
|
||||||
|
require 'new_configuration_generator'
|
||||||
|
|
||||||
namespace :rails do
|
namespace :rails do
|
||||||
namespace :upgrade do
|
namespace :upgrade do
|
||||||
|
@ -26,5 +27,13 @@ namespace :rails do
|
||||||
|
|
||||||
puts new_routes
|
puts new_routes
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc "Extracts your configuration code so you can create a new config/application.rb"
|
||||||
|
task :configuration do
|
||||||
|
upgrader = Rails::Upgrading::NewConfigurationGenerator.new
|
||||||
|
new_config = upgrader.generate_new_application_rb
|
||||||
|
|
||||||
|
puts new_config
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -0,0 +1,63 @@
|
||||||
|
require 'test_helper'
|
||||||
|
require 'new_configuration_generator'
|
||||||
|
|
||||||
|
# Stub out methods on upgrader class
|
||||||
|
module Rails
|
||||||
|
module Upgrading
|
||||||
|
class NewConfigurationGenerator
|
||||||
|
attr_writer :environment_code
|
||||||
|
|
||||||
|
def has_environment?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def environment_code
|
||||||
|
@environment_code
|
||||||
|
end
|
||||||
|
|
||||||
|
def app_name
|
||||||
|
"my_application"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class NewConfigurationGeneratorTest < ActiveSupport::TestCase
|
||||||
|
FRAME = "# Put this in config/application.rb
|
||||||
|
require File.expand_path('../boot', __FILE__)
|
||||||
|
|
||||||
|
module MyApplication
|
||||||
|
class Application < Rails::Application
|
||||||
|
%s
|
||||||
|
end
|
||||||
|
end"
|
||||||
|
|
||||||
|
CONFIG = " config.what_have_you = 'thing'
|
||||||
|
config.action_controller = 'what'"
|
||||||
|
|
||||||
|
CODE = "require 'w/e'
|
||||||
|
|
||||||
|
this_happens_before_the(code)
|
||||||
|
more_before_the_code!
|
||||||
|
|
||||||
|
Rails::Initializer.run do |config|
|
||||||
|
%s
|
||||||
|
end
|
||||||
|
|
||||||
|
this_is_after_the_code
|
||||||
|
"
|
||||||
|
|
||||||
|
def test_raises_error_with_no_code
|
||||||
|
generator = Rails::Upgrading::NewConfigurationGenerator.new
|
||||||
|
generator.environment_code = ""
|
||||||
|
|
||||||
|
assert_raises(RuntimeError) { generator.generate_new_application_rb }
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_generates_with_code
|
||||||
|
generator = Rails::Upgrading::NewConfigurationGenerator.new
|
||||||
|
generator.environment_code = CODE % [CONFIG]
|
||||||
|
|
||||||
|
assert_equal FRAME % [generator.indent(CONFIG)], generator.generate_new_application_rb
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue