summaryrefslogtreecommitdiff
path: root/lib/bundler/installer
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 /lib/bundler/installer
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.
Diffstat (limited to 'lib/bundler/installer')
-rw-r--r--lib/bundler/installer/standalone.rb48
1 files changed, 48 insertions, 0 deletions
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