summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwycats <wycats@gmail.com>2010-07-19 12:07:50 -0700
committerwycats <wycats@gmail.com>2010-07-19 12:07:50 -0700
commitacf0d935cfb1144799d094374e637b2475e53aec (patch)
treec855fd99af0a83d6d72495a738b66ff3b4fed1df
parent6b15a06577d5bce78ea717d56c6609265e122c3f (diff)
downloadbundler-acf0d935cfb1144799d094374e637b2475e53aec.tar.gz
Add bundle config for global config settings
-rw-r--r--lib/bundler/cli.rb44
-rw-r--r--lib/bundler/settings.rb54
-rw-r--r--spec/install/gems/simple_case_spec.rb59
3 files changed, 115 insertions, 42 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index 3e4274fdd5..3a26a3be9d 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -15,7 +15,7 @@ module Bundler
Gem::DefaultUserInteraction.ui = UI::RGProxy.new(Bundler.ui)
end
- check_unknown_options! unless ARGV.include?("exec")
+ check_unknown_options! unless ARGV.include?("exec") || ARGV.include?("config")
default_task :install
class_option "no-color", :type => :boolean, :banner => "Disable colorization in output"
@@ -234,6 +234,48 @@ module Bundler
end
end
+ desc "config NAME [VALUE]", "retrieve or set a configuration value"
+ long_desc <<-D
+ Retrieves or sets a configuration value. If only parameter is provided, retrieve the value. If two parameters are provided, replace the
+ existing value with the newly provided one.
+
+ By default, setting a configuration value sets it for all projects
+ on the machine. If you want to set the configuration for a specific
+ project, use the --local flag.
+
+ If a global setting is superceded by local configuration, this command
+ will show the current value, as well as any superceded values and
+ where they were specified.
+ D
+ def config(name, *values)
+ locations = Bundler.settings.locations(name)
+
+ if values.empty?
+ # TODO: Say something more useful here
+ locations.each do |location, value|
+ if value
+ Bundler.ui.info "#{location}: #{value}"
+ end
+ end
+ else
+ if local = locations[:local]
+ Bundler.ui.info "Your application has set #{name} to #{local.inspect}. This will override the " \
+ "system value you are currently setting"
+ end
+
+ if global = locations[:global]
+ Bundler.ui.info "You are replacing the current system value of #{name}, which is currently #{global}"
+ end
+
+ if env = locations[:env]
+ Bundler.ui.info "You have set a bundler environment variable for #{env}. This will take precedence " \
+ "over the system value you are setting"
+ end
+
+ Bundler.settings.set_global(name, values.join(" "))
+ end
+ end
+
desc "open GEM", "Opens the source directory of the given bundled gem"
def open(name)
editor = [ENV['BUNDLER_EDITOR'], ENV['VISUAL'], ENV['EDITOR']].find{|e| !e.nil? && !e.empty? }
diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb
index 08cf46d1c2..16f7f0264f 100644
--- a/lib/bundler/settings.rb
+++ b/lib/bundler/settings.rb
@@ -2,24 +2,31 @@ module Bundler
class Settings
def initialize(root)
@root = root
- @config = File.exist?(config_file) ? YAML.load_file(config_file) : {}
+ @local_config = File.exist?(local_config_file) ? YAML.load_file(local_config_file) : {}
+ @global_config = File.exist?(global_config_file) ? YAML.load_file(global_config_file) : {}
end
def [](key)
key = key_for(key)
- @config[key] || ENV[key]
+ @local_config[key] || ENV[key] || @global_config[key]
end
def []=(key, value)
+ set_key(key, value, @local_config, local_config_file)
+ end
+
+ def set_global(key, value)
+ set_key(key, value, @global_config, global_config_file)
+ end
+
+ def locations(key)
key = key_for(key)
- unless @config[key] == value
- @config[key] = value
- FileUtils.mkdir_p(config_file.dirname)
- File.open(config_file, 'w') do |f|
- f.puts @config.to_yaml
- end
- end
- value
+
+ locations = {}
+ locations[:local] = @local_config[key]
+ locations[:env] = ENV[key]
+ locations[:global] = @global_config[key]
+ locations
end
def without=(array)
@@ -32,11 +39,10 @@ module Bundler
self[:without] ? self[:without].split(":").map { |w| w.to_sym } : []
end
- # @config["BUNDLE_PATH"] should be prioritized over ENV["BUNDLE_PATH"]
+ # @local_config["BUNDLE_PATH"] should be prioritized over ENV["BUNDLE_PATH"]
def path
- path = ENV[key_for(:path)]
-
- return path if path && !@config.key?(key_for(:path))
+ path = ENV[key_for(:path)] || @global_config[key_for(:path)]
+ return path if path && !@local_config.key?(key_for(:path))
if path = self[:path]
"#{path}/#{Bundler.ruby_scope}"
@@ -47,12 +53,28 @@ module Bundler
private
+ def set_key(key, value, hash, file)
+ key = key_for(key)
+
+ unless hash[key] == value
+ hash[key] = value
+ FileUtils.mkdir_p(file.dirname)
+ File.open(file, "w") { |f| f.puts hash.to_yaml }
+ end
+ value
+ end
+
def key_for(key)
"BUNDLE_#{key.to_s.upcase}"
end
- def config_file
+ def global_config_file
+ file = ENV["BUNDLE_CONFIG"] || File.join(Gem.user_home, ".bundle/config")
+ Pathname.new(file)
+ end
+
+ def local_config_file
Pathname.new("#{@root}/.bundle/config")
end
end
-end \ No newline at end of file
+end
diff --git a/spec/install/gems/simple_case_spec.rb b/spec/install/gems/simple_case_spec.rb
index 455adbe1b0..213f377e63 100644
--- a/spec/install/gems/simple_case_spec.rb
+++ b/spec/install/gems/simple_case_spec.rb
@@ -256,7 +256,7 @@ describe "bundle install with gem sources" do
end
end
- describe "when BUNDLE_PATH is set" do
+ describe "when BUNDLE_PATH or the global path config is set" do
before :each do
build_lib "rack", "1.0.0", :to_system => true do |s|
s.write "lib/rack.rb", "raise 'FAIL'"
@@ -268,23 +268,44 @@ describe "bundle install with gem sources" do
G
end
- it "installs gems to a path if one is specified" do
- ENV["BUNDLE_PATH"] = bundled_app("vendor2").to_s
+ def set_bundle_path(type, location)
+ if type == :env
+ ENV["BUNDLE_PATH"] = location
+ elsif type == :global
+ bundle "config path #{location}"
+ end
+ end
- bundle "install vendor"
+ [:env, :global].each do |type|
+ it "installs gems to a path if one is specified" do
+ set_bundle_path(type, bundled_app("vendor2").to_s)
+ bundle "install vendor"
- vendored_gems("gems/rack-1.0.0").should be_directory
- bundled_app("vendor2").should_not be_directory
- should_be_installed "rack 1.0.0"
- end
+ vendored_gems("gems/rack-1.0.0").should be_directory
+ bundled_app("vendor2").should_not be_directory
+ should_be_installed "rack 1.0.0"
+ end
- it "installs gems to BUNDLE_PATH" do
- ENV['BUNDLE_PATH'] = bundled_app('vendor').to_s
+ it "installs gems to BUNDLE_PATH" do
+ set_bundle_path(type, bundled_app("vendor").to_s)
- bundle :install
+ bundle :install
- bundled_app('vendor/gems/rack-1.0.0').should be_directory
- should_be_installed "rack 1.0.0"
+ bundled_app('vendor/gems/rack-1.0.0').should be_directory
+ should_be_installed "rack 1.0.0"
+ end
+
+ it "installs gems to BUNDLE_PATH relative to root when relative" do
+ set_bundle_path(type, "vendor")
+
+ FileUtils.mkdir_p bundled_app('lol')
+ Dir.chdir(bundled_app('lol')) do
+ bundle :install
+ end
+
+ bundled_app('vendor/gems/rack-1.0.0').should be_directory
+ should_be_installed "rack 1.0.0"
+ end
end
it "installs gems to BUNDLE_PATH from .bundle/config" do
@@ -296,18 +317,6 @@ describe "bundle install with gem sources" do
should_be_installed "rack 1.0.0"
end
- it "installs gems to BUNDLE_PATH relative to root when relative" do
- ENV['BUNDLE_PATH'] = 'vendor'
-
- FileUtils.mkdir_p bundled_app('lol')
- Dir.chdir(bundled_app('lol')) do
- bundle :install
- end
-
- bundled_app('vendor/gems/rack-1.0.0').should be_directory
- should_be_installed "rack 1.0.0"
- end
-
it "sets BUNDLE_PATH as the first argument to bundle install" do
bundle "install ./vendor"