oop_rails_server 0.0.18 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 29b80ec1bb5be7a985162051b138b86dcf1ba2df
4
- data.tar.gz: 5250a68f7067cb3bd77c210673ba05b8623ceb14
3
+ metadata.gz: f07bd13a4e1384b5c4aaead8af1b980892fa6de8
4
+ data.tar.gz: 2d8a3a3a3af8fd99a6247bafcf9d4af731a1c583
5
5
  SHA512:
6
- metadata.gz: 90699dbfd9174985ad3d9caf94362624d7c32ae2d7246d0488a64675a15a8bc3b13a6930b38d5f8bcc23706235720bb1ee990eaa3632f4e49c6f67b0237aa42b
7
- data.tar.gz: 34f90b90785bd67871c7dced38dd782128255db383f39c592fffdfd1642c5a705f1fa4ed4cb69224be8630678b7fc4faca605cbb71e561bd7d410705edbc06ea
6
+ metadata.gz: 96d680a1199a5aa1eb124e1ea2bbc2efbec0c7ccd85a0e76f6e10d79ec4c1fb2a3f32f0e4a769eb96274700fe4a8040a0052c48d01b364bdb716e27737f0eb61
7
+ data.tar.gz: f9302281fcb1216edaf939e2dab8c6341f468f0272130ab1c9817ec5c5e8c2ad7e32055a5fccb3083be0a92fe9e6c956f2e1ba273d1522c2ef79b5072a995a0d
data/CHANGES.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # `oop_rails_server` Releases
2
2
 
3
+ ## 0.0.19, 20 September 2016
4
+
5
+ * Significant internal refactor to how Gemfiles get modified that's much more reliable; changes
6
+ `:additional_gemfile_lines` into `:gemfile_modifier`, and from an array of `String`s to a `Proc`.
7
+
3
8
  ## 0.0.18, 20 September 2016
4
9
 
5
10
  * Replicate the workaround for `mime-types` into not just the Rails bootstrap Gemfile, but the Rails gemfile, too.
@@ -0,0 +1,89 @@
1
+ module OopRailsServer
2
+ # This is not intended to be anything even close to a real Gemfile parser -- it is just enough to create/parse
3
+ # the simple Gemfiles we create or that are generated by 'rails new'.
4
+ class Gemfile
5
+ def initialize(file_path)
6
+ @file_path = File.expand_path(file_path)
7
+ end
8
+
9
+ def add_version_constraints!(gem_name, *version_specs)
10
+ additional_version_constraints = version_specs.map { |vs| ", '#{vs}'" }.join
11
+
12
+ update_gemfile_line!(gem_name) do |gemdecl, specs|
13
+ "#{gemdecl}#{additional_version_constraints}#{specs}"
14
+ end
15
+ end
16
+
17
+ def set_specs!(gem_name, new_specs)
18
+ new_specs_line = new_specs.inspect
19
+ if new_specs_line =~ /^\s*\{\s*(.*?)\s*\}\s*$/
20
+ new_specs_line = $1
21
+ else
22
+ raise "Object doesn't seem to #inspect into a Hash string: #{new_specs.inspect}"
23
+ end
24
+
25
+ update_gemfile_line!(gem_name) do |gemdecl, specs|
26
+ "#{gemdecl}, #{new_specs_line}"
27
+ end
28
+ end
29
+
30
+ def ensure_gem!(gem_name)
31
+ update_gemfile_line!(gem_name) do |gemdecl, specs|
32
+ "#{gemdecl}#{specs}"
33
+ end
34
+ end
35
+
36
+ def write!
37
+ File.open(file_path, "w") do |f|
38
+ contents.each { |l| f.puts l }
39
+ end
40
+ end
41
+
42
+ private
43
+ attr_reader :file_path
44
+
45
+ GEM_LINE_REGEXP = /^(\s*gem\s+['"])([^"']+)(['"])(.*)$/
46
+
47
+ def update_gemfile_line!(gem_name)
48
+ found = nil
49
+
50
+ contents.each_with_index do |line, index|
51
+ if line =~ GEM_LINE_REGEXP && $2.strip.downcase == gem_name.strip.downcase
52
+ if found
53
+ raise "Multiple lines in '#{file_path}' seem to specify gem '#{gem_name}':\n #{found[0]}: #{found[1]}\nand #{index}: #{line}"
54
+ end
55
+
56
+ found = [ index, line ]
57
+ end
58
+ end
59
+
60
+ unless found
61
+ new_line = "gem '#{gem_name}'"
62
+ contents << new_line
63
+ found = [ (contents.length - 1), new_line ]
64
+ end
65
+
66
+ matchdata = GEM_LINE_REGEXP.match(found[1])
67
+ gemdecl = [ matchdata[1], matchdata[2], matchdata[3] ].join
68
+
69
+ modified_line = yield gemdecl, matchdata[4]
70
+ contents[found[0]] = modified_line
71
+ end
72
+
73
+ def contents
74
+ @contents ||= begin
75
+ c = if File.exist?(file_path)
76
+ File.read(file_path)
77
+ else
78
+ empty_gemfile_contents
79
+ end
80
+
81
+ c.split(/\n/)
82
+ end
83
+ end
84
+
85
+ def empty_gemfile_contents
86
+ "source 'https://rubygems.org'\n\n"
87
+ end
88
+ end
89
+ end
@@ -77,8 +77,8 @@ it should return the fully-qualified path to the root of your project (gem, appl
77
77
  @rails_server_runtime_base_directory ||= File.join(rails_server_project_root, "tmp/spec/rails")
78
78
  end
79
79
 
80
- def rails_server_additional_gemfile_lines
81
- [ ]
80
+ def rails_server_gemfile_modifier
81
+ Proc.new { |gemfile| }
82
82
  end
83
83
 
84
84
  def rails_server_default_version
@@ -184,14 +184,19 @@ it should return the fully-qualified path to the root of your project (gem, appl
184
184
 
185
185
  template_paths = rails_server_template_paths(templates)
186
186
 
187
- additional_gemfile_lines = Array(rails_server_additional_gemfile_lines || [ ])
188
- additional_gemfile_lines += Array(options[:additional_gemfile_lines] || [ ])
187
+ rsgm = rails_server_gemfile_modifier
188
+ agm = options[:additional_gemfile_modifier] || (Proc.new { |gemfile| })
189
+
190
+ gemfile_modifier = Proc.new do |gemfile|
191
+ rsgm.call(gemfile)
192
+ agm.call(gemfile)
193
+ end
189
194
 
190
195
  server = ::OopRailsServer::RailsServer.new(
191
196
  :name => name, :template_paths => template_paths,
192
197
  :runtime_base_directory => rails_server_runtime_base_directory,
193
198
  :rails_version => (options[:rails_version] || rails_server_default_version),
194
- :rails_env => options[:rails_env], :additional_gemfile_lines => additional_gemfile_lines)
199
+ :rails_env => options[:rails_env], :gemfile_modifier => gemfile_modifier)
195
200
 
196
201
  rails_servers[name] = server
197
202
 
@@ -3,6 +3,7 @@ require 'find'
3
3
  require 'net/http'
4
4
  require 'uri'
5
5
  require 'file/tail'
6
+ require 'oop_rails_server/gemfile'
6
7
 
7
8
  module OopRailsServer
8
9
  class RailsServer
@@ -11,7 +12,7 @@ module OopRailsServer
11
12
  def initialize(options)
12
13
  options.assert_valid_keys(
13
14
  :name, :template_paths, :runtime_base_directory,
14
- :rails_version, :rails_env, :additional_gemfile_lines,
15
+ :rails_version, :rails_env, :gemfile_modifier,
15
16
  :log, :verbose
16
17
  )
17
18
 
@@ -30,7 +31,7 @@ module OopRailsServer
30
31
  @log = options[:log] || $stderr
31
32
  @verbose = options.fetch(:verbose, true)
32
33
 
33
- @additional_gemfile_lines = Array(options[:additional_gemfile_lines] || [ ])
34
+ @gemfile_modifier = options[:gemfile_modifier] || (Proc.new { |gemfile| })
34
35
 
35
36
 
36
37
  @rails_root = File.expand_path(File.join(@runtime_base_directory, rails_version.to_s, name.to_s))
@@ -122,7 +123,7 @@ module OopRailsServer
122
123
  end
123
124
 
124
125
  private
125
- attr_reader :template_paths, :runtime_base_directory, :rails_env, :additional_gemfile_lines, :port, :server_pid
126
+ attr_reader :template_paths, :runtime_base_directory, :rails_env, :gemfile_modifier, :port, :server_pid
126
127
 
127
128
  def base_template_directories
128
129
  [
@@ -192,22 +193,14 @@ module OopRailsServer
192
193
  end
193
194
 
194
195
  def splat_bootstrap_gemfile!
195
- File.open("Gemfile", "w") do |f|
196
- rails_version_spec = if rails_version == :default then "" else ", \"= #{rails_version}\"" end
197
- f << <<-EOS
198
- source 'https://rubygems.org'
196
+ rails_version_specs = if rails_version == :default then [ ] else [ "= #{rails_version}" ] end
199
197
 
200
- gem 'rails'#{rails_version_spec}
201
- EOS
198
+ gemfile = ::OopRailsServer::Gemfile.new("Gemfile")
199
+ gemfile.add_version_constraints!("rails", *rails_version_specs)
202
200
 
203
- f.puts "gem 'i18n', '< 0.7.0'" if RUBY_VERSION =~ /^1\.8\./
204
- f.puts "gem 'rack-cache', '< 1.3.0'" if RUBY_VERSION =~ /^1\./
205
- f.puts "gem 'rake', '< 11.0.0'" if RUBY_VERSION =~ /^1\.8\./
206
-
207
- # mime-types 3.x depends on mime-types-data, which is incompatible with Ruby < 1.x
208
- f.puts "gem 'mime-types', '< 3.0.0'" if RUBY_VERSION =~ /^1\./
209
- end
201
+ backcompat_bootstrap_gems!(gemfile)
210
202
 
203
+ gemfile.write!
211
204
  run_bundle_install!(:bootstrap)
212
205
  end
213
206
 
@@ -220,48 +213,16 @@ EOS
220
213
  end
221
214
 
222
215
  def update_gemfile!
223
- gemfile = File.join(rails_root, 'Gemfile')
224
- gemfile_contents = File.read(gemfile)
225
-
226
- # Since Rails 3.0.20 was released, a new version of the I18n gem, 0.5.2, was released that moves a constant
227
- # into a different namespace. (See https://github.com/mislav/will_paginate/issues/347 for more details.)
228
- # So, if we're running Rails 3.0.x, we lock the 'i18n' gem to an earlier version.
229
- if rails_version && rails_version =~ /^3\.0\./
230
- gemfile_contents << "\ngem 'i18n', '= 0.5.0'\n"
231
- elsif RUBY_VERSION =~ /^1\.8\./
232
- # Since Rails 3.x was released, a new version of the I18n gem, 0.7.0, was released that is incompatible
233
- # with Ruby 1.8.7. So, if we're running with Ruby 1.8.7, we lock the 'i18n' gem to an earlier version.
234
- gemfile_contents << "\ngem 'i18n', '< 0.7.0'\n"
235
- end
216
+ gemfile = ::OopRailsServer::Gemfile.new(File.join(rails_root, 'Gemfile'))
236
217
 
237
- # Since Rails 3.1.12 was released, a new version of the rack-cache gem, 1.3.0, was released that requires
238
- # Ruby 2.0 or above. So, if we're running Rails 3.1.x or 3.2.x on Ruby 1.8.x, we lock the 'rack-cache' gem
239
- # to an earlier version.
240
- if rails_version && rails_version =~ /^3\.[12]\./ && RUBY_VERSION =~ /^1\./
241
- gemfile_contents << "\ngem 'rack-cache', '< 1.3.0'\n"
242
- end
218
+ backcompat_bootstrap_gems!(gemfile)
243
219
 
244
- if RUBY_VERSION =~ /^1\.8\./
245
- # Apparently execjs released a version 2.2.0 that will happily install on Ruby 1.8.7, but which contains some
246
- # new-style hash syntax. As a result, we pin the version backwards in this one specific case.
247
- gemfile_contents << "\ngem 'execjs', '~> 2.0.0'\n"
248
-
249
- # Rake 11 is incompatible with Ruby 1.8
250
- gemfile_contents << "\ngem 'rake', '< 11.0.0'\n"
220
+ backcompat_execjs!(gemfile)
221
+ backcompat_uglifier!(gemfile)
251
222
 
252
- # Uglifier 3 is incompatible with Ruby 1.8
253
- gemfile_contents.gsub!(/^(.*['"]uglifier['"].*)$/, "\\1, '< 3.0.0'")
254
- end
255
-
256
- if RUBY_VERSION =~ /^1\./
257
- # mime-types 3.x depends on mime-types-data, which is incompatible with Ruby < 1.x
258
- gemfile_contents << "\ngem 'mime-types', '< 3.0.0'\n"
259
- end
223
+ gemfile_modifier.call(gemfile)
260
224
 
261
- gemfile_contents << additional_gemfile_lines.join("\n")
262
- gemfile_contents << "\n"
263
-
264
- File.open(gemfile, 'w') { |f| f << gemfile_contents }
225
+ gemfile.write!
265
226
  end
266
227
 
267
228
  def with_env(new_env)
@@ -562,5 +523,82 @@ and output:
562
523
 
563
524
  output
564
525
  end
526
+
527
+
528
+ def is_ruby_18
529
+ !! (RUBY_VERSION =~ /^1\.8\./)
530
+ end
531
+
532
+ def is_ruby_1
533
+ !! (RUBY_VERSION =~ /^1\./)
534
+ end
535
+
536
+ def is_rails_30
537
+ rails_version && rails_version =~ /^3\.0\./
538
+ end
539
+
540
+ def is_rails_31
541
+ rails_version && rails_version =~ /^3\.1\./
542
+ end
543
+
544
+ def is_rails_32
545
+ rails_version && rails_version =~ /^3\.2\./
546
+ end
547
+
548
+ def backcompat_i18n!(gemfile)
549
+ if is_rails_30
550
+ # Since Rails 3.0.20 was released, a new version of the I18n gem, 0.5.2, was released that moves a constant
551
+ # into a different namespace. (See https://github.com/mislav/will_paginate/issues/347 for more details.)
552
+ # So, if we're running Rails 3.0.x, we lock the 'i18n' gem to an earlier version.
553
+ gemfile.add_version_constraints!('i18n', '= 0.5.0')
554
+ elsif is_ruby_18
555
+ # Since Rails 3.x was released, a new version of the I18n gem, 0.7.0, was released that is incompatible
556
+ # with Ruby 1.8.7. So, if we're running with Ruby 1.8.7, we lock the 'i18n' gem to an earlier version.
557
+ gemfile.add_version_constraints!('i18n', '< 0.7.0')
558
+ end
559
+ end
560
+
561
+ def backcompat_rake!(gemfile)
562
+ if is_ruby_18
563
+ # Rake 11 is incompatible with Ruby 1.8
564
+ gemfile.add_version_constraints!('rake', '< 11.0.0')
565
+ end
566
+ end
567
+
568
+ def backcompat_rack_cache!(gemfile)
569
+ if is_ruby_1 && (is_rails_31 || is_rails_32)
570
+ # Since Rails 3.1.12 was released, a new version of the rack-cache gem, 1.3.0, was released that requires
571
+ # Ruby 2.0 or above. So, if we're running Rails 3.1.x or 3.2.x on Ruby 1.x, we lock the 'rack-cache' gem
572
+ # to an earlier version.
573
+ gemfile.add_version_constraints!('rack-cache', '< 1.3.0')
574
+ end
575
+ end
576
+
577
+ def backcompat_mime_types!(gemfile)
578
+ if is_ruby_1
579
+ # mime-types 3.x depends on mime-types-data, which is not compatible with ruby < 2.x
580
+ gemfile.add_version_constraints!('mime-types', '< 3.0.0')
581
+ end
582
+ end
583
+
584
+ def backcompat_execjs!(gemfile)
585
+ if is_ruby_18
586
+ # Apparently execjs released a version 2.2.0 that will happily install on Ruby 1.8.7, but which contains some
587
+ # new-style hash syntax. As a result, we pin the version backwards in this one specific case.
588
+ gemfile.add_version_constraints!('execjs', '~> 2.0.0')
589
+ end
590
+ end
591
+
592
+ def backcompat_uglifier!(gemfile)
593
+ # Uglifier 3 is incompatible with Ruby 1.8
594
+ gemfile.add_version_constraints!('uglifier', '< 3.0.0')
595
+ end
596
+
597
+ def backcompat_bootstrap_gems!(gemfile)
598
+ backcompat_i18n!(gemfile)
599
+ backcompat_rake!(gemfile)
600
+ backcompat_rack_cache!(gemfile)
601
+ backcompat_mime_types!(gemfile)
602
+ end
565
603
  end
566
604
  end
@@ -1,3 +1,3 @@
1
1
  module OopRailsServer
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.19"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oop_rails_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Geweke
@@ -80,6 +80,7 @@ files:
80
80
  - README.md
81
81
  - Rakefile
82
82
  - lib/oop_rails_server.rb
83
+ - lib/oop_rails_server/gemfile.rb
83
84
  - lib/oop_rails_server/helpers.rb
84
85
  - lib/oop_rails_server/rails_server.rb
85
86
  - lib/oop_rails_server/version.rb