Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
[CLI::Gem] Store the VERSION in a separate file
Browse files Browse the repository at this point in the history
This way, the gemspec does not need to change the load path
  • Loading branch information
segiddins committed May 1, 2018
1 parent 0c5d3b8 commit 225c68e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/bundler/cli/gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ def run
"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[
Expand Down
1 change: 1 addition & 0 deletions lib/bundler/templates/newgem/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
2 changes: 1 addition & 1 deletion lib/bundler/templates/newgem/lib/newgem/version.rb.tt
Original file line number Diff line number Diff line change
@@ -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 -%>
8 changes: 1 addition & 7 deletions lib/bundler/templates/newgem/newgem.gemspec.tt
Original file line number Diff line number Diff line change
@@ -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 %>]

Expand Down
16 changes: 14 additions & 2 deletions spec/commands/newgem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,12 @@ def create_temporary_dir(dir)
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
Expand Down Expand Up @@ -572,7 +577,14 @@ def create_temporary_dir(dir)
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
Expand Down

0 comments on commit 225c68e

Please sign in to comment.