From 225c68ee29b29a7f706e3cfb36538a9f940cbe65 Mon Sep 17 00:00:00 2001 From: Samuel Giddins Date: Tue, 20 Mar 2018 21:18:08 -0700 Subject: [CLI::Gem] Store the VERSION in a separate file This way, the gemspec does not need to change the load path --- lib/bundler/cli/gem.rb | 3 ++- lib/bundler/templates/newgem/VERSION | 1 + lib/bundler/templates/newgem/lib/newgem/version.rb.tt | 2 +- lib/bundler/templates/newgem/newgem.gemspec.tt | 8 +------- spec/commands/newgem_spec.rb | 16 ++++++++++++++-- 5 files changed, 19 insertions(+), 11 deletions(-) create mode 100644 lib/bundler/templates/newgem/VERSION diff --git a/lib/bundler/cli/gem.rb b/lib/bundler/cli/gem.rb index 58e2f8a3fd..aad4341f5d 100644 --- a/lib/bundler/cli/gem.rb +++ b/lib/bundler/cli/gem.rb @@ -66,10 +66,11 @@ module Bundler "lib/newgem.rb.tt" => "lib/#{namespaced_path}.rb", "lib/newgem/version.rb.tt" => "lib/#{namespaced_path}/version.rb", "newgem.gemspec.tt" => "#{name}.gemspec", + "VERSION" => "VERSION", "Rakefile.tt" => "Rakefile", "README.md.tt" => "README.md", "bin/console.tt" => "bin/console", - "bin/setup.tt" => "bin/setup" + "bin/setup.tt" => "bin/setup", } executables = %w[ diff --git a/lib/bundler/templates/newgem/VERSION b/lib/bundler/templates/newgem/VERSION new file mode 100644 index 0000000000..6c6aa7cb09 --- /dev/null +++ b/lib/bundler/templates/newgem/VERSION @@ -0,0 +1 @@ +0.1.0 \ No newline at end of file diff --git a/lib/bundler/templates/newgem/lib/newgem/version.rb.tt b/lib/bundler/templates/newgem/lib/newgem/version.rb.tt index 389daf5048..518b4688a4 100644 --- a/lib/bundler/templates/newgem/lib/newgem/version.rb.tt +++ b/lib/bundler/templates/newgem/lib/newgem/version.rb.tt @@ -1,7 +1,7 @@ <%- config[:constant_array].each_with_index do |c, i| -%> <%= " " * i %>module <%= c %> <%- end -%> -<%= " " * config[:constant_array].size %>VERSION = "0.1.0" +<%= " " * config[:constant_array].size %>VERSION = File.read(File.expand_path("<%= "../" * (config[:constant_array].size + 2) + "VERSION" %>", __FILE__)).strip.freeze <%- (config[:constant_array].size-1).downto(0) do |i| -%> <%= " " * i %>end <%- end -%> diff --git a/lib/bundler/templates/newgem/newgem.gemspec.tt b/lib/bundler/templates/newgem/newgem.gemspec.tt index 116bb5fa62..d29fb6a020 100644 --- a/lib/bundler/templates/newgem/newgem.gemspec.tt +++ b/lib/bundler/templates/newgem/newgem.gemspec.tt @@ -1,16 +1,10 @@ <%- if RUBY_VERSION < "2.0.0" -%> # coding: utf-8 -lib = File.expand_path("../lib", __FILE__) -<%- else -%> -lib = File.expand_path("lib", __dir__) <%- end -%> -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require "<%= config[:namespaced_path] %>/version" - Gem::Specification.new do |spec| spec.name = <%= config[:name].inspect %> - spec.version = <%= config[:constant_name] %>::VERSION + spec.version = File.read(File.expand_path("../VERSION", __FILE__)).strip spec.authors = [<%= config[:author].inspect %>] spec.email = [<%= config[:email].inspect %>] diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index 9f45fb665b..d79162c566 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -278,7 +278,12 @@ RSpec.describe "bundle gem" do end it "starts with version 0.1.0" do - expect(bundled_app("test_gem/lib/test_gem/version.rb").read).to match(/VERSION = "0.1.0"/) + expect(bundled_app("test_gem/VERSION")).to read_as("0.1.0") + expect(bundled_app("test_gem/lib/test_gem/version.rb")).to read_as(strip_whitespace(<<-RUBY)) + module TestGem + VERSION = File.read(File.expand_path("../../../VERSION", __FILE__)).strip.freeze + end + RUBY end it "does not nest constants" do @@ -572,7 +577,14 @@ RSpec.describe "bundle gem" do end it "starts with version 0.1.0" do - expect(bundled_app("test-gem/lib/test/gem/version.rb").read).to match(/VERSION = "0.1.0"/) + expect(bundled_app("test-gem/VERSION")).to read_as("0.1.0") + expect(bundled_app("test-gem/lib/test/gem/version.rb")).to read_as(strip_whitespace(<<-RUBY)) + module Test + module Gem + VERSION = File.read(File.expand_path("../../../../VERSION", __FILE__)).strip.freeze + end + end + RUBY end it "nests constants so they work" do -- cgit v1.2.1