summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThe Bundler Bot <bot@bundler.io>2017-06-16 01:11:10 +0000
committerThe Bundler Bot <bot@bundler.io>2017-06-16 01:11:10 +0000
commit0c651a97a8edff5be1a4a00e0c1f240dcf000137 (patch)
tree9e7dfd47c004d54a5673fd9b404b7f02eae80703
parent72e20ac28ce6faed24c053fffdbcf459b444335c (diff)
parent3c552274695aae4439ce0a6d83dd36c8179089cf (diff)
downloadbundler-0c651a97a8edff5be1a4a00e0c1f240dcf000137.tar.gz
Auto merge of #5716 - bundler:seg-version-build-metadata, r=indirect
Print build metadata when running `bundle version` Closes #5049. Will get all the build metadata into `bundle env` once https://github.com/bundler/bundler/pull/5703 lands, since I don't want conflicts and want to use that code for generating the "tables"
-rw-r--r--.gitignore3
-rw-r--r--Rakefile6
-rw-r--r--lib/bundler.rb1
-rw-r--r--lib/bundler/build_metadata.rb36
-rw-r--r--lib/bundler/cli.rb5
-rw-r--r--lib/bundler/env.rb1
-rw-r--r--spec/commands/version_spec.rb26
-rw-r--r--spec/install/gemfile/git_spec.rb4
-rw-r--r--task/build_metadata.rake33
-rw-r--r--task/git_hooks.rake35
-rw-r--r--task/release.rake7
11 files changed, 150 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index 88730f82cd..6230ec1c99 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,3 +16,6 @@ man/*
# rspec failure tracking
.rspec_status
+
+# files generated during packaging
+/lib/bundler/generated/
diff --git a/Rakefile b/Rakefile
index 00b0649f7d..fcbf7b5866 100644
--- a/Rakefile
+++ b/Rakefile
@@ -366,10 +366,8 @@ task :update_certs => "spec:rubygems:clone_rubygems_master" do
Bundler::SSLCerts::CertificateManager.update_from!(RUBYGEMS_REPO)
end
-require "bundler/gem_tasks"
-task :build => ["man:build"]
-task :release => ["man:require", "man:build"]
-
task :default => :spec
Dir["task/*.{rb,rake}"].each(&method(:load))
+
+task :generate_files => Rake::Task.tasks.select {|t| t.name.start_with?("lib/bundler/generated") }
diff --git a/lib/bundler.rb b/lib/bundler.rb
index 1228785734..9bcadc381f 100644
--- a/lib/bundler.rb
+++ b/lib/bundler.rb
@@ -13,6 +13,7 @@ require "bundler/rubygems_integration"
require "bundler/version"
require "bundler/constants"
require "bundler/current_ruby"
+require "bundler/build_metadata"
module Bundler
environment_preserver = EnvironmentPreserver.new(ENV, EnvironmentPreserver::BUNDLER_KEYS)
diff --git a/lib/bundler/build_metadata.rb b/lib/bundler/build_metadata.rb
new file mode 100644
index 0000000000..54436f982d
--- /dev/null
+++ b/lib/bundler/build_metadata.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Bundler
+ # Represents metadata from when the Bundler gem was built.
+ module BuildMetadata
+ # begin ivars
+ @release = false
+ # end ivars
+
+ # A hash representation of the build metadata.
+ def self.to_h
+ {
+ "Built At" => built_at,
+ "Git SHA" => git_commit_sha,
+ "Released Version" => release?,
+ }
+ end
+
+ # A string representing the date the bundler gem was built.
+ def self.built_at
+ @built_at ||= Time.now.utc.strftime("%Y-%m-%d").freeze
+ end
+
+ # The SHA for the git commit the bundler gem was built from.
+ def self.git_commit_sha
+ @git_commit_sha ||= Dir.chdir(File.expand_path("..", __FILE__)) do
+ `git rev-parse --short HEAD`.strip.freeze
+ end
+ end
+
+ # Whether this is an official release build of Bundler.
+ def self.release?
+ @release
+ end
+ end
+end
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb
index e93fc9529d..0c89e41656 100644
--- a/lib/bundler/cli.rb
+++ b/lib/bundler/cli.rb
@@ -395,7 +395,10 @@ module Bundler
desc "version", "Prints the bundler's version information"
def version
- Bundler.ui.info "Bundler version #{Bundler::VERSION}"
+ if ARGV.include?("version")
+ build_info = " (#{BuildMetadata.built_at} commit #{BuildMetadata.git_commit_sha})"
+ end
+ Bundler.ui.info "Bundler version #{Bundler::VERSION}#{build_info}"
end
map %w[-v --version] => :version
diff --git a/lib/bundler/env.rb b/lib/bundler/env.rb
index 325b96fbfa..333173ac27 100644
--- a/lib/bundler/env.rb
+++ b/lib/bundler/env.rb
@@ -14,6 +14,7 @@ module Bundler
out = String.new
append_formatted_table("Environment", environment, out)
+ append_formatted_table("Bundler Build Metadata", BuildMetadata.to_h, out)
unless Bundler.settings.all.empty?
out << "\n## Bundler settings\n\n```\n"
diff --git a/spec/commands/version_spec.rb b/spec/commands/version_spec.rb
new file mode 100644
index 0000000000..478edb9e67
--- /dev/null
+++ b/spec/commands/version_spec.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+RSpec.describe "bundle version" do
+ context "with -v" do
+ it "outputs the version" do
+ bundle! "-v"
+ expect(out).to eq("Bundler version #{Bundler::VERSION}")
+ end
+ end
+
+ context "with --version" do
+ it "outputs the version" do
+ bundle! "--version"
+ expect(out).to eq("Bundler version #{Bundler::VERSION}")
+ end
+ end
+
+ context "with version" do
+ it "outputs the version with build metadata" do
+ date = Bundler::BuildMetadata.built_at
+ git_commit_sha = Bundler::BuildMetadata.git_commit_sha
+ bundle! "version"
+ expect(out).to eq("Bundler version #{Bundler::VERSION} (#{date} commit #{git_commit_sha})")
+ end
+ end
+end
diff --git a/spec/install/gemfile/git_spec.rb b/spec/install/gemfile/git_spec.rb
index 31abb4de72..59bbc9c016 100644
--- a/spec/install/gemfile/git_spec.rb
+++ b/spec/install/gemfile/git_spec.rb
@@ -1144,11 +1144,11 @@ RSpec.describe "bundle install with git sources" do
end
`git commit -m 'commit for iteration #{i}' ext/foo.c`
end
- git_sha = git_reader.ref_for("HEAD")
+ git_commit_sha = git_reader.ref_for("HEAD")
install_gemfile <<-G
source "file://#{gem_repo1}"
- gem "foo", :git => "#{lib_path("foo-1.0")}", :ref => "#{git_sha}"
+ gem "foo", :git => "#{lib_path("foo-1.0")}", :ref => "#{git_commit_sha}"
G
run <<-R
diff --git a/task/build_metadata.rake b/task/build_metadata.rake
new file mode 100644
index 0000000000..e06a795259
--- /dev/null
+++ b/task/build_metadata.rake
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+def write_build_metadata(build_metadata)
+ build_metadata_file = "lib/bundler/build_metadata.rb"
+
+ ivars = build_metadata.sort.map do |k, v|
+ " @#{k} = #{BUNDLER_SPEC.send(:ruby_code, v)}"
+ end.join("\n")
+
+ contents = File.read(build_metadata_file)
+ contents.sub!(/^(\s+# begin ivars).+(^\s+# end ivars)/m, "\\1\n#{ivars}\n\\2")
+ File.open(build_metadata_file, "w") {|f| f << contents }
+end
+
+task :build_metadata do
+ build_metadata = {
+ :built_at => BUNDLER_SPEC.date.utc.strftime("%Y-%m-%d"),
+ :git_commit_sha => `git rev-parse --short HEAD`.strip,
+ :release => Rake::Task["release"].instance_variable_get(:@already_invoked),
+ }
+
+ write_build_metadata(build_metadata)
+end
+
+namespace :build_metadata do
+ task :clean do
+ build_metadata = {
+ :release => false,
+ }
+
+ write_build_metadata(build_metadata)
+ end
+end
diff --git a/task/git_hooks.rake b/task/git_hooks.rake
new file mode 100644
index 0000000000..e70d66afaa
--- /dev/null
+++ b/task/git_hooks.rake
@@ -0,0 +1,35 @@
+# frozen_string_literal: true
+directory ".git/hooks"
+
+file ".git/hooks/pre-commit" => [__FILE__] do |t|
+ File.open(t.name, "w") {|f| f << <<-SH }
+#!/bin/sh
+
+set -e
+
+.git/hooks/run-ruby bin/rubocop
+.git/hooks/run-ruby bin/rspec spec/quality_spec.rb
+ SH
+
+ chmod 0o755, t.name, :verbose => false
+end
+
+file ".git/hooks/pre-push" => [__FILE__] do |_t|
+ Dir.chdir(".git/hooks") do
+ safe_ln "pre-commit", "pre-push", :verbose => false
+ end
+end
+
+file ".git/hooks/run-ruby" => [__FILE__] do |t|
+ File.open(t.name, "w") {|f| f << <<-SH }
+#!/bin/bash
+
+ruby="ruby"
+command -v chruby-exec >/dev/null 2>&1 && [[ -f ~/.ruby-version ]] && ruby="chruby-exec $(cat ~/.ruby-version) --"
+ruby $@
+ SH
+
+ chmod 0o755, t.name, :verbose => false
+end
+
+task :git_hooks => Rake::Task.tasks.select {|t| t.name.start_with?(".git/hooks") }
diff --git a/task/release.rake b/task/release.rake
index 104fe1f857..8feaec3ae4 100644
--- a/task/release.rake
+++ b/task/release.rake
@@ -1,4 +1,11 @@
# frozen_string_literal: true
+
+require "bundler/gem_tasks"
+task :build => ["build_metadata", "man:build", "generate_files"] do
+ Rake::Task["build_metadata:clean"].tap(&:reenable).real_invoke
+end
+task :release => ["man:require", "man:build", "build_metadata"]
+
namespace :release do
def confirm(prompt = "")
loop do