forked from ecl1pse/spree_export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rails_engine_plan.rb
206 lines (167 loc) · 5.47 KB
/
rails_engine_plan.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
PROJECT_PATH = File.expand_path(Dir.pwd)
ROOT_PATH = File.expand_path('spec/dummy', Dir.pwd)
ENV_PATH = File.expand_path('config/environment', ROOT_PATH)
BOOT_PATH = File.expand_path('config/boot', ROOT_PATH)
APP_PATH = File.expand_path('config/application', ROOT_PATH)
require 'zeus'
def gem_is_bundled?(gem)
gemfile_lock_contents = File.read(PROJECT_PATH + "/Gemfile.lock")
gemfile_lock_contents.scan(/\b#{gem} \(([^=~><]+?)\)/).flatten.first
end
if version = gem_is_bundled?('method_source')
gem 'method_source', version
end
require 'zeus/m'
module Zeus
class Rails < Plan
def deprecated
puts "Zeus 0.11.0 changed zeus.json. You'll have to rm zeus.json && zeus init."
end
alias_method :spec_helper, :deprecated
alias_method :testrb, :deprecated
alias_method :rspec, :deprecated
def after_fork
reconnect_activerecord
restart_girl_friday
reconnect_redis
end
def boot
require BOOT_PATH
# config/application.rb normally requires 'rails/all'.
# Some 'alternative' ORMs such as Mongoid give instructions to switch this require
# out for a list of railties, not including ActiveRecord.
# We grep config/application.rb for all requires of rails/all or railties, and require them.
rails_components = File.read(APP_PATH + ".rb").
scan(/^\s*require\s*['"](.*railtie.*|rails\/all)['"]/).flatten
rails_components = ["rails/all"] if rails_components == []
rails_components.each do |component|
require component
end
end
def default_bundle
Bundler.require(:default)
Zeus::LoadTracking.add_feature('./Gemfile.lock')
end
def development_environment
Bundler.require(:development)
::Rails.env = ENV['RAILS_ENV'] = "development"
require APP_PATH
::Rails.application.require_environment!
end
def generate
load_rails_generators
require 'rails/commands/generate'
end
def destroy
load_rails_generators
require 'rails/commands/destroy'
end
def runner
require 'rails/commands/runner'
end
def console
require 'rails/commands/console'
if defined?(Pry) && IRB == Pry
require "pry"
Pry.start
else
::Rails::Console.start(::Rails.application)
end
end
def dbconsole
require 'rails/commands/dbconsole'
meth = ::Rails::DBConsole.method(:start)
# ails::DBConsole.starthas been changed to load faster in
# https://github.com/rails/rails/commit/346bb018499cde6699fcce6c68dd7e9be45c75e1
#
# This will work with both versions.
if meth.arity.zero?
::Rails::DBConsole.start
else
::Rails::DBConsole.start(::Rails.application)
end
end
def server
require 'rails/commands/server'
server = ::Rails::Server.new
Dir.chdir(::Rails.application.root)
server.start
end
def test_environment
Bundler.require(:test)
::Rails.env = ENV['RAILS_ENV'] = 'test'
require APP_PATH
$rails_rake_task = 'yup' # lie to skip eager loading
::Rails.application.require_environment!
$rails_rake_task = nil
$LOAD_PATH.unshift ".", "./lib", "./test", "./spec"
end
def test_helper
if File.exists?(PROJECT_PATH + "/spec/spec_helper.rb")
require 'spec_helper'
elsif File.exist?(PROJECT_PATH + "/test/minitest_helper.rb")
require 'minitest_helper'
else
require 'test_helper'
end
end
def test
if spec_file?(ARGV) && defined?(RSpec)
# disable autorun in case the user left it in spec_helper.rb
RSpec::Core::Runner.disable_autorun!
exit RSpec::Core::Runner.run(ARGV)
else
Zeus::M.run(ARGV)
end
end
def cucumber_environment
require 'cucumber/rspec/disable_option_parser'
require 'cucumber/cli/main'
@cucumber_runtime = Cucumber::Runtime.new
end
def cucumber
cucumber_main = Cucumber::Cli::Main.new(ARGV.dup)
had_failures = cucumber_main.execute!(@cucumber_runtime)
exit_code = had_failures ? 1 : 0
exit exit_code
end
private
SPEC_DIR_REGEXP = %r"(^|/)spec"
SPEC_FILE_REGEXP = /.+_spec\.rb$/
def spec_file? argv
last_arg = argv[-1]
last_arg.match (Regexp.union(SPEC_DIR_REGEXP, SPEC_FILE_REGEXP))
end
def restart_girl_friday
return unless defined?(GirlFriday::WorkQueue)
# The Actor is run in a thread, and threads don't persist post-fork.
# We just need to restart each one in the newly-forked process.
ObjectSpace.each_object(GirlFriday::WorkQueue) do |obj|
obj.send(:start)
end
end
def reconnect_activerecord
return unless defined?(ActiveRecord::Base)
begin
ActiveRecord::Base.clear_all_connections!
ActiveRecord::Base.establish_connection
if ActiveRecord::Base.respond_to?(:shared_connection)
ActiveRecord::Base.shared_connection = ActiveRecord::Base.retrieve_connection
end
rescue ActiveRecord::AdapterNotSpecified
end
end
def reconnect_redis
return unless defined?(Redis::Client)
ObjectSpace.each_object(Redis::Client) do |client|
client.connect rescue nil
end
end
def load_rails_generators
require 'rails/generators'
::Rails.application.load_generators
rescue LoadError # Rails 3.0 doesn't require this block to be run, but 3.2+ does
end
end
end
Zeus.plan ||= Zeus::Rails.new