diff options
author | Colby Swandale <colby@taplaboratories.com> | 2017-06-04 20:36:49 +1000 |
---|---|---|
committer | Colby Swandale <colby@taplaboratories.com> | 2017-06-04 20:41:57 +1000 |
commit | 581f0cde1a60e1a6c300ae4028acf476723ca2ce (patch) | |
tree | fa9366dd84503027236ae302da9f29af062c11c0 | |
parent | 2e46e6705770e5a4685b1f3d3fbbe60f957599ae (diff) | |
download | bundler-581f0cde1a60e1a6c300ae4028acf476723ca2ce.tar.gz |
generate gems.rb in bundle init with feature flag
-rw-r--r-- | lib/bundler/cli/init.rb | 18 | ||||
-rw-r--r-- | lib/bundler/feature_flag.rb | 1 | ||||
-rw-r--r-- | lib/bundler/settings.rb | 1 | ||||
-rw-r--r-- | lib/bundler/templates/gems.rb | 5 | ||||
-rw-r--r-- | man/bundle-config.ronn | 2 | ||||
-rw-r--r-- | spec/commands/init_spec.rb | 50 |
6 files changed, 71 insertions, 6 deletions
diff --git a/lib/bundler/cli/init.rb b/lib/bundler/cli/init.rb index 8ffd1db41a..ae5d68853e 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| + puts "Writing new Gemfile to #{SharedHelpers.pwd}/#{gemfile}" + 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") + puts "Writing new #{gemfile} to #{SharedHelpers.pwd}/#{gemfile}" + FileUtils.cp(File.expand_path("../../templates/#{gemfile}", __FILE__), gemfile) end end + + private + + def gemfile + @gemfile ||= Bundler.feature_flag.new_gemfile_name? ? "gems.rb" : "Gemfile" + end end end diff --git a/lib/bundler/feature_flag.rb b/lib/bundler/feature_flag.rb index 5ee69f538c..a75bcfb5f7 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(:new_gemfile_name) { 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..d5927afd87 100644 --- a/lib/bundler/settings.rb +++ b/lib/bundler/settings.rb @@ -28,6 +28,7 @@ module Bundler plugins silence_root_warning error_on_stderr + new_gemfile_name ].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..6747c2521b 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 +* `new_gemfile_name` (`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..7edcabd85e 100644 --- a/spec/commands/init_spec.rb +++ b/spec/commands/init_spec.rb @@ -62,4 +62,54 @@ RSpec.describe "bundle init" do end end end + + context "when new_gemfile_name setting is enabled" do + before { bundle "config new_gemfile_name 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") } + + it "should generate from an existing gemspec" 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 + + 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 + end + end end |