summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Hellwig <a5308y@gmail.com>2015-09-26 17:55:57 +0200
committerAndreas Hellwig <a5308y@gmail.com>2015-09-30 08:44:51 +0200
commitb87adb03f455e2942bab171115462955be22f43b (patch)
tree4a7447b08922fcd3c889889e9421f3de80927734
parent84811218d9bdddec6b792da6b7cf4d44fa70d223 (diff)
downloadbundler-b87adb03f455e2942bab171115462955be22f43b.tar.gz
Extract class Standalone
I wanted to fix some minor Rubocop issues and got confused by '#{ruby_engine}/#{ruby_version}'. I managed to confuse two other people working on bundler as well, so I decided to refactor the part around it. I wanted to encapsulate the file creation, so it's (even) more obvious from the context that another ruby file is created. So having an uninterpolated string with interpolation syntax in it isn't so unusual. To be extra sure I added a comment here as Rubocop might lead others to the same spot. I found that Standalone works quite well as its own object. While extracting the class I shortened some expressions and extracted methods that made sense as a unit to me. I ended up with a generate method that only deals with file output. What I think might be controversial: * The tendency to use methods instead of variables for small things like version_dir and bundler_path: I think it's easier to understand because methdos are "read_only", while values of variables might change during code execution. * Array(spec.require_paths) instead of "next if spec.require_paths.nil? # builtin gems". We lose the comment here, but I thought Array was more concise and that the information about the type of gems isn't crucial here. * flat_map { map { ... } } instead of "collecting" in paths My perspective is that paths is the result of a transformation of @specs and that's best expressed by using map. It's also shorter.
-rw-r--r--lib/bundler/installer.rb41
-rw-r--r--lib/bundler/installer/standalone.rb48
2 files changed, 50 insertions, 39 deletions
diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb
index 923fbd1faf..1fd0b85dd9 100644
--- a/lib/bundler/installer.rb
+++ b/lib/bundler/installer.rb
@@ -2,6 +2,7 @@ require "erb"
require "rubygems/dependency_installer"
require "bundler/worker"
require "bundler/installer/parallel_installer"
+require "bundler/installer/standalone"
module Bundler
class Installer < Environment
@@ -68,7 +69,7 @@ module Bundler
install(options)
lock unless Bundler.settings[:frozen]
- generate_standalone(options[:standalone]) if options[:standalone]
+ Standalone.new(options[:standalone], @definition).generate if options[:standalone]
end
def install_gem_from_spec(spec, standalone = false, worker = 0, force = false)
@@ -209,44 +210,6 @@ module Bundler
end
end
- def generate_standalone(groups)
- standalone_path = Bundler.settings[:path]
- bundler_path = File.join(standalone_path, "bundler")
- SharedHelpers.filesystem_access(bundler_path) do |p|
- FileUtils.mkdir_p(p)
- end
-
- paths = []
-
- if groups.empty?
- specs = @definition.requested_specs
- else
- specs = @definition.specs_for groups.map(&:to_sym)
- end
-
- specs.each do |spec|
- next if spec.name == "bundler"
- next if spec.require_paths.nil? # builtin gems
-
- spec.require_paths.each do |path|
- full_path = Pathname.new(path).absolute? ? path : File.join(spec.full_gem_path, path)
- gem_path = Pathname.new(full_path).relative_path_from(Bundler.root.join(bundler_path))
- paths << gem_path.to_s.sub("#{Bundler.ruby_version.engine}/#{RbConfig::CONFIG["ruby_version"]}", '#{ruby_engine}/#{ruby_version}')
- end
- end
-
- File.open File.join(bundler_path, "setup.rb"), "w" do |file|
- file.puts "require 'rbconfig'"
- file.puts "# ruby 1.8.7 doesn't define RUBY_ENGINE"
- file.puts "ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'"
- file.puts "ruby_version = RbConfig::CONFIG[\"ruby_version\"]"
- file.puts "path = File.expand_path('..', __FILE__)"
- paths.each do |path|
- file.puts %{$:.unshift "\#{path}/#{path}"}
- end
- end
- end
-
def install_in_parallel(size, standalone, force = false)
ParallelInstaller.call(self, specs, size, standalone, force)
end
diff --git a/lib/bundler/installer/standalone.rb b/lib/bundler/installer/standalone.rb
new file mode 100644
index 0000000000..24a4183ea2
--- /dev/null
+++ b/lib/bundler/installer/standalone.rb
@@ -0,0 +1,48 @@
+module Bundler
+ class Standalone
+ def initialize(groups, definition)
+ @specs = groups.empty? ? definition.requested_specs : definition.specs_for(groups.map(&:to_sym))
+ end
+
+ def generate
+ SharedHelpers.filesystem_access(bundler_path) do |p|
+ FileUtils.mkdir_p(p)
+ end
+ File.open File.join(bundler_path, "setup.rb"), "w" do |file|
+ file.puts "require 'rbconfig'"
+ file.puts "# ruby 1.8.7 doesn't define RUBY_ENGINE"
+ file.puts "ruby_engine = defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby'"
+ file.puts "ruby_version = RbConfig::CONFIG[\"ruby_version\"]"
+ file.puts "path = File.expand_path('..', __FILE__)"
+ paths.each do |path|
+ file.puts %{$:.unshift "\#{path}/#{path}"}
+ end
+ end
+ end
+
+ private
+
+ def paths
+ @specs.flat_map do |spec|
+ next if spec.name == "bundler"
+ Array(spec.require_paths).map do |path|
+ gem_path(path, spec).sub(version_dir, '#{ruby_engine}/#{ruby_version}')
+ # This is a static string intentionally. It's interpolated at a later time.
+ end
+ end
+ end
+
+ def version_dir
+ "#{Bundler.ruby_version.engine}/#{RbConfig::CONFIG["ruby_version"]}"
+ end
+
+ def bundler_path
+ File.join(Bundler.settings[:path], "bundler")
+ end
+
+ def gem_path(path, spec)
+ full_path = Pathname.new(path).absolute? ? path : File.join(spec.full_gem_path, path)
+ Pathname.new(full_path).relative_path_from(Bundler.root.join(bundler_path)).to_s
+ end
+ end
+end