summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThe Bundler Bot <bot@bundler.io>2017-06-13 03:02:50 +0000
committerThe Bundler Bot <bot@bundler.io>2017-06-13 03:02:50 +0000
commite8fd5795778379bd821e34173b678f76f240fa97 (patch)
tree98dad102bf9492b7252f7bb15cb9f0fd8eb7daa4
parent85cd5d8e6e85e852c7b57d2a8c77f79b0f9e55ee (diff)
parent34c9537501aed2355748ee9bfaf4aefe366e266d (diff)
downloadbundler-e8fd5795778379bd821e34173b678f76f240fa97.tar.gz
Auto merge of #5704 - bundler:colby/init-new-gemfile-format, r=segiddins
generate gems.rb in bundle init with feature flag This PR adds a feature flag to generate a new `gems.rb` with `bundle init`.
-rw-r--r--lib/bundler/cli/init.rb18
-rw-r--r--lib/bundler/feature_flag.rb1
-rw-r--r--lib/bundler/settings.rb3
-rw-r--r--lib/bundler/templates/gems.rb5
-rw-r--r--man/bundle-config.ronn2
-rw-r--r--spec/commands/init_spec.rb63
-rw-r--r--spec/quality_spec.rb1
7 files changed, 86 insertions, 7 deletions
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb
index 8ffd1db41a..16330f6c6d 100644
--- a/lib/bundler/cli/init.rb
+++ b/lib/bundler/cli/init.rb
@@ -7,8 +7,8 @@ module Bundler
end
def run
- if File.exist?("Gemfile")
- Bundler.ui.error "Gemfile already exists at #{SharedHelpers.pwd}/Gemfile"
+ if File.exist?(gemfile)
+ Bundler.ui.error "#{gemfile} already exists at #{SharedHelpers.pwd}/#{gemfile}"
exit 1
end
@@ -21,15 +21,21 @@ module Bundler
spec = Bundler.load_gemspec_uncached(gemspec)
- puts "Writing new Gemfile to #{SharedHelpers.pwd}/Gemfile"
- File.open("Gemfile", "wb") do |file|
+ File.open(gemfile, "wb") do |file|
file << "# Generated from #{gemspec}\n"
file << spec.to_gemfile
end
else
- puts "Writing new Gemfile to #{SharedHelpers.pwd}/Gemfile"
- FileUtils.cp(File.expand_path("../../templates/Gemfile", __FILE__), "Gemfile")
+ FileUtils.cp(File.expand_path("../../templates/#{gemfile}", __FILE__), gemfile)
end
+
+ puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}"
+ end
+
+ private
+
+ def gemfile
+ @gemfile ||= Bundler.feature_flag.init_gems_rb? ? "gems.rb" : "Gemfile"
end
end
end
diff --git a/lib/bundler/feature_flag.rb b/lib/bundler/feature_flag.rb
index 5ee69f538c..3a4353f2af 100644
--- a/lib/bundler/feature_flag.rb
+++ b/lib/bundler/feature_flag.rb
@@ -18,6 +18,7 @@ module Bundler
settings_flag(:only_update_to_newer_versions) { bundler_2_mode? }
settings_flag(:plugins) { @bundler_version >= Gem::Version.new("1.14") }
settings_flag(:error_on_stderr) { bundler_2_mode? }
+ settings_flag(:init_gems_rb) { bundler_2_mode? }
def initialize(bundler_version)
@bundler_version = Gem::Version.create(bundler_version)
diff --git a/lib/bundler/settings.rb b/lib/bundler/settings.rb
index 68a7add942..7857e00ea0 100644
--- a/lib/bundler/settings.rb
+++ b/lib/bundler/settings.rb
@@ -16,18 +16,19 @@ module Bundler
disable_local_branch_check
disable_shared_gems
disable_version_check
+ error_on_stderr
force_ruby_platform
frozen
gem.coc
gem.mit
ignore_messages
+ init_gems_rb
major_deprecations
no_install
no_prune
only_update_to_newer_versions
plugins
silence_root_warning
- error_on_stderr
].freeze
NUMBER_KEYS = %w[
diff --git a/lib/bundler/templates/gems.rb b/lib/bundler/templates/gems.rb
new file mode 100644
index 0000000000..a6ef23ba3e
--- /dev/null
+++ b/lib/bundler/templates/gems.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+# A sample gems.rb
+source "https://rubygems.org"
+
+# gems "rails"
diff --git a/man/bundle-config.ronn b/man/bundle-config.ronn
index b7ae07698c..18773017f8 100644
--- a/man/bundle-config.ronn
+++ b/man/bundle-config.ronn
@@ -224,6 +224,8 @@ learn more about their operation in [bundle install(1)][bundle-install].
command with a private gemstash server.
* `error_on_stderr` (`BUNDLE_ERROR_ON_STDERR`)
Print Bundler errors to stderr
+* `init_gems_rb` (`BUNDLE_NEW_GEMFILE_NAME`)
+ generate a new gems.rb on `bundle init` instead of the `Gemfile`
In general, you should set these settings per-application by using the applicable
flag to the [bundle install(1)][bundle-install] or [bundle package(1)][bundle-package] command.
diff --git a/spec/commands/init_spec.rb b/spec/commands/init_spec.rb
index 6bbbfa8c96..7be379b510 100644
--- a/spec/commands/init_spec.rb
+++ b/spec/commands/init_spec.rb
@@ -6,6 +6,11 @@ RSpec.describe "bundle init" do
expect(bundled_app("Gemfile")).to exist
end
+ it "prints a message to the user" do
+ bundle :init
+ expect(out).to include("Writing new Gemfile")
+ end
+
context "when a Gemfile already exists" do
before do
gemfile <<-G
@@ -62,4 +67,62 @@ RSpec.describe "bundle init" do
end
end
end
+
+ context "when init_gems_rb setting is enabled" do
+ before { bundle "config init_gems_rb true" }
+
+ it "generates a gems.rb file" do
+ bundle :init
+ expect(bundled_app("gems.rb")).to exist
+ end
+
+ context "when gems.rb already exists" do
+ before do
+ create_file("gems.rb", <<-G)
+ gem "rails"
+ G
+ end
+
+ it "does not change existing Gemfiles" do
+ expect { bundle :init }.not_to change { File.read(bundled_app("gems.rb")) }
+ end
+
+ it "notifies the user that an existing gems.rb already exists" do
+ bundle :init
+ expect(out).to include("gems.rb already exists")
+ end
+ end
+
+ context "given --gemspec option" do
+ let(:spec_file) { tmp.join("test.gemspec") }
+
+ before do
+ File.open(spec_file, "w") do |file|
+ file << <<-S
+ Gem::Specification.new do |s|
+ s.name = 'test'
+ s.add_dependency 'rack', '= 1.0.1'
+ s.add_development_dependency 'rspec', '1.2'
+ end
+ S
+ end
+ end
+
+ it "should generate from an existing gemspec" do
+ bundle :init, :gemspec => spec_file
+
+ gemfile = bundled_app("gems.rb").read
+ expect(gemfile).to match(%r{source 'https://rubygems.org'})
+ expect(gemfile.scan(/gem "rack", "= 1.0.1"/).size).to eq(1)
+ expect(gemfile.scan(/gem "rspec", "= 1.2"/).size).to eq(1)
+ expect(gemfile.scan(/group :development/).size).to eq(1)
+ end
+
+ it "prints message to user" do
+ bundle :init, :gemspec => spec_file
+
+ expect(out).to include("Writing new gems.rb")
+ end
+ end
+ end
end
diff --git a/spec/quality_spec.rb b/spec/quality_spec.rb
index 5f55f123e2..db019e03de 100644
--- a/spec/quality_spec.rb
+++ b/spec/quality_spec.rb
@@ -244,6 +244,7 @@ RSpec.describe "The library itself" do
bundler/capistrano.rb
bundler/gem_tasks.rb
bundler/vlad.rb
+ bundler/templates/gems.rb
]
lib_files = `git ls-files -z`.split("\x0").grep(/\.rb$/) - exclusions
lib_files.reject! {|f| f.start_with?("bundler/vendor") }