trusty-layouts-extension 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,344 +0,0 @@
1
- # TODO: Fix formatting on member/collection methods
2
-
3
- module Rails
4
- module Upgrading
5
- module FakeRouter
6
- module ActionController
7
- module Routing
8
- class Routes
9
- def self.setup
10
- @redrawer = Rails::Upgrading::RouteRedrawer.new
11
- end
12
-
13
- def self.redrawer
14
- @redrawer
15
- end
16
-
17
- def self.draw
18
- yield @redrawer
19
- end
20
- end
21
- end
22
- end
23
- end
24
-
25
- class RoutesUpgrader
26
- def generate_new_routes
27
- if has_routes_file?
28
- upgrade_routes
29
- else
30
- raise FileNotFoundError, "Can't find your routes file [config/routes.rb]!"
31
- end
32
- end
33
-
34
- def has_routes_file?
35
- File.exists?("config/routes.rb")
36
- end
37
-
38
- def routes_code
39
- File.read("config/routes.rb")
40
- end
41
-
42
- def upgrade_routes
43
- FakeRouter::ActionController::Routing::Routes.setup
44
-
45
- # Read and eval the file; our fake route mapper will capture
46
- # the calls to draw routes and generate new route code
47
- FakeRouter.module_eval(routes_code)
48
-
49
- # Give the route set to the code generator and get its output
50
- generator = RouteGenerator.new(FakeRouter::ActionController::Routing::Routes.redrawer.routes)
51
- generator.generate
52
- end
53
- end
54
-
55
- class RouteRedrawer
56
- attr_accessor :routes
57
-
58
- def self.stack
59
- @stack
60
- end
61
-
62
- def self.stack=(val)
63
- @stack = val
64
- end
65
-
66
- def initialize
67
- @routes = []
68
-
69
- # The old default route was actually two routes; we generate the new style
70
- # one only if we haven't generated it for the first old default route.
71
- @default_route_generated = false
72
-
73
- # Setup the stack for parents; used use proper indentation
74
- self.class.stack = [@routes]
75
- end
76
-
77
- def root(options)
78
- debug "mapping root"
79
- @routes << FakeRoute.new("/", options)
80
- end
81
-
82
- def connect(path, options={})
83
- debug "connecting #{path}"
84
-
85
- if (path == ":controller/:action/:id.:format" || path == ":controller/:action/:id")
86
- if !@default_route_generated
87
- current_parent << FakeRoute.new("/:controller(/:action(/:id))", {:default_route => true})
88
-
89
- @default_route_generated = true
90
- end
91
- else
92
- current_parent << FakeRoute.new(path, options)
93
- end
94
- end
95
-
96
- def resources(*args, &block)
97
- _res(FakeResourceRoute, args, &block)
98
- end
99
-
100
- def resource(*args, &block)
101
- _res(FakeSingletonResourceRoute, args, &block)
102
- end
103
-
104
- def _res(klass, args)
105
- if args.last.is_a?(Hash)
106
- options = args.pop
107
- debug "options #{options.inspect}"
108
- end
109
-
110
- args.each do |a|
111
- current_parent << klass.new(a, options || {})
112
- debug "mapping resources #{current_parent.last.name}"
113
-
114
- if block_given?
115
- parent = current_parent.last
116
-
117
- parent = stack(parent) do
118
- yield(self)
119
- end
120
- end
121
- end
122
- end
123
-
124
- def namespace(name, options = {})
125
- debug "mapping namespace #{name}"
126
- namespace = FakeNamespace.new(name, options)
127
-
128
- namespace = stack(namespace) do
129
- yield(self)
130
- end
131
-
132
- current_parent << namespace
133
- end
134
-
135
- def method_missing(m, *args)
136
- debug "named route: #{m}"
137
- current_parent << FakeRoute.new(args.shift, args.pop, m.to_s)
138
- end
139
-
140
- def self.indent
141
- ' ' * ((stack.length) * 2)
142
- end
143
-
144
- private
145
- def debug(txt)
146
- puts txt if ENV['DEBUG']
147
- end
148
-
149
- def stack(obj)
150
- self.class.stack << obj
151
- yield
152
- self.class.stack.pop
153
- end
154
-
155
- def current_parent
156
- self.class.stack.last
157
- end
158
- end
159
-
160
- class RouteObject
161
- def indent_lines(code_lines)
162
- if code_lines.length > 1
163
- code_lines.flatten.map {|l| "#{@indent}#{l.chomp}"}.join("\n") + "\n"
164
- else
165
- "#{@indent}#{code_lines.shift}"
166
- end
167
- end
168
-
169
- def opts_to_string(opts)
170
- opts.is_a?(Hash) ? opts.map {|k, v|
171
- ":#{k} => " + (v.is_a?(Hash) ? ('{ ' + opts_to_string(v) + ' }') : "#{value_to_string(v)}")
172
- }.join(", ") : opts.to_s
173
- end
174
-
175
- def value_to_string(value)
176
- case value
177
- when Regexp, Symbol, Array then value.inspect
178
- when String then "'" + value.to_s + "'"
179
- else value.to_s
180
- end
181
- end
182
- end
183
-
184
- class FakeNamespace < RouteObject
185
- attr_accessor :routes, :name, :options
186
-
187
- def initialize(name, options = {})
188
- @routes = []
189
- @name, @options = name, options
190
- @indent = RouteRedrawer.indent
191
- end
192
-
193
- def to_route_code
194
- if !@options.empty?
195
- options = ', ' + opts_to_string(@options)
196
- else
197
- options = ''
198
- end
199
-
200
- lines = ["namespace :#{@name}#{options} do", @routes.map {|r| r.to_route_code}, "end"]
201
-
202
- indent_lines(lines)
203
- end
204
-
205
- def <<(val)
206
- @routes << val
207
- end
208
-
209
- def last
210
- @routes.last
211
- end
212
- end
213
-
214
- class FakeRoute < RouteObject
215
- attr_accessor :name, :path, :options
216
-
217
- def initialize(path, options, name = "")
218
- @path = path
219
- @options = options || {}
220
- @name = name
221
- @indent = RouteRedrawer.indent
222
- end
223
-
224
- def to_route_code
225
- if @options[:default_route]
226
- indent_lines ["match '#{@path}'"]
227
- else
228
- base = "match '%s' => '%s#%s'"
229
- extra_options = []
230
-
231
- if not name.empty?
232
- extra_options << ":as => :#{name}"
233
- end
234
-
235
- if @options[:requirements]
236
- @options[:constraints] = @options.delete(:requirements)
237
- end
238
-
239
- if @options[:conditions]
240
- @options[:via] = @options.delete(:conditions).delete(:method)
241
- end
242
-
243
- @options ||= {}
244
- base = (base % [@path, @options.delete(:controller), (@options.delete(:action) || "index")])
245
- opts = opts_to_string(@options)
246
-
247
- route_pieces = ([base] + extra_options + [opts])
248
- route_pieces.delete("")
249
-
250
- indent_lines [route_pieces.join(", ")]
251
- end
252
- end
253
- end
254
-
255
- class FakeResourceRoute < RouteObject
256
- attr_accessor :name, :children
257
-
258
- def initialize(name, options = {})
259
- @name = name
260
- @children = []
261
- @options = options
262
- @indent = RouteRedrawer.indent
263
- end
264
-
265
- def to_route_code
266
- # preserve :only & :except options
267
- copied_options = @options.reject { |k,v| ![:only, :except].member?(k) }
268
- unless copied_options.empty?
269
- copied_options_str = ", " + copied_options.map { |k, v| "#{k.inspect} => #{v.inspect}" }.join(",")
270
- end
271
-
272
- if !@children.empty? || @options.has_key?(:collection) || @options.has_key?(:member)
273
- prefix = ["#{route_method} :#{@name}#{copied_options_str} do"]
274
- lines = prefix + custom_methods + [@children.map {|r| r.to_route_code}.join("\n"), "end"]
275
-
276
- indent_lines(lines)
277
- else
278
- base = "#{route_method} :%s#{copied_options_str}"
279
- indent_lines [base % [@name]]
280
- end
281
- end
282
-
283
- def custom_methods
284
- collection_code = generate_custom_methods_for(:collection)
285
- member_code = generate_custom_methods_for(:member)
286
- [collection_code, member_code]
287
- end
288
-
289
- def generate_custom_methods_for(group)
290
- return "" unless @options[group]
291
-
292
- method_code = []
293
-
294
- RouteRedrawer.stack << self
295
- @options[group].each do |name, methods|
296
- [*methods].each do |method|
297
- method_code << "#{method} :#{name}"
298
- end
299
- end
300
- RouteRedrawer.stack.pop
301
-
302
- indent_lines ["#{group} do", method_code, "end"].flatten
303
- end
304
-
305
- def route_method
306
- "resources"
307
- end
308
-
309
- def <<(val)
310
- @children << val
311
- end
312
-
313
- def last
314
- @children.last
315
- end
316
- end
317
-
318
- class FakeSingletonResourceRoute < FakeResourceRoute
319
- def route_method
320
- "resource"
321
- end
322
- end
323
-
324
- class RouteGenerator
325
- def initialize(routes)
326
- @routes = routes
327
- @new_code = ""
328
- end
329
-
330
- def generate
331
- @new_code = @routes.map do |r|
332
- r.to_route_code
333
- end.join("\n")
334
-
335
- "#{app_name.underscore.classify}::Application.routes.draw do\n#{@new_code}\nend\n"
336
- end
337
-
338
- private
339
- def app_name
340
- File.basename(Dir.pwd)
341
- end
342
- end
343
- end
344
- end
@@ -1,79 +0,0 @@
1
- $:.unshift(File.dirname(__FILE__) + "/../../lib")
2
- require 'routes_upgrader'
3
- require 'gemfile_generator'
4
- require 'application_checker'
5
- require 'new_configuration_generator'
6
- require "active_support"
7
-
8
- require 'fileutils'
9
-
10
- namespace :rails do
11
- namespace :upgrade do
12
- desc "Runs a battery of checks on your Rails 2.x app and generates a report on required upgrades for Rails 3"
13
- task :check do
14
- checker = Rails::Upgrading::ApplicationChecker.new
15
- checker.run
16
- end
17
-
18
- desc "Generates a Gemfile for your Rails 3 app out of your config.gem directives"
19
- task :gems do
20
- generator = Rails::Upgrading::GemfileGenerator.new
21
- new_gemfile = generator.generate_new_gemfile
22
-
23
- puts new_gemfile
24
- end
25
-
26
- desc "Create a new, upgraded route file from your current routes.rb"
27
- task :routes do
28
- upgrader = Rails::Upgrading::RoutesUpgrader.new
29
- new_routes = upgrader.generate_new_routes
30
-
31
- puts new_routes
32
- end
33
-
34
- desc "Extracts your configuration code so you can create a new config/application.rb"
35
- task :configuration do
36
- upgrader = Rails::Upgrading::NewConfigurationGenerator.new
37
- new_config = upgrader.generate_new_application_rb
38
-
39
- puts new_config
40
- end
41
-
42
- CLEAR = "\e[0m" unless defined? CLEAR
43
- CYAN = "\e[36m" unless defined? CYAN
44
- WHITE = "\e[37m" unless defined? WHITE
45
-
46
- desc "Backs up your likely modified files so you can run the Rails 3 generator on your app with little risk"
47
- task :backup do
48
- files = [".gitignore",
49
- "app/controllers/application_controller.rb",
50
- "app/helpers/application_helper.rb",
51
- "config/routes.rb",
52
- "config/environment.rb",
53
- "config/environments/development.rb",
54
- "config/environments/production.rb",
55
- "config/environments/staging.rb",
56
- "config/database.yml",
57
- "config.ru",
58
- "doc/README_FOR_APP",
59
- "test/test_helper.rb"]
60
-
61
- puts
62
- files.each do |f|
63
- if File.exist?(f)
64
- puts "#{CYAN}* #{CLEAR}backing up #{WHITE}#{f}#{CLEAR} to #{WHITE}#{f}.rails2#{CLEAR}"
65
- FileUtils.cp(f, "#{f}.rails2")
66
- end
67
- end
68
-
69
- puts
70
- puts "This is a list of the files analyzed and backed up (if they existed);\nyou will probably not want the generator to replace them since\nyou probably modified them (but now they're safe if you accidentally do!)."
71
- puts
72
-
73
- files.each do |f|
74
- puts "#{CYAN}- #{CLEAR}#{f}"
75
- end
76
- puts
77
- end
78
- end
79
- end