Skip to content

Commit a6b5d76

Browse files
committed
Unify version detection
A previous attempt at making the package build work without being in a git repository ended up duplicating quite a lot of code. This change refactors that original effort so that it re-uses the BuildpackVersion logic that already existed. [#69076566]
1 parent e525d38 commit a6b5d76

6 files changed

Lines changed: 105 additions & 23 deletions

File tree

lib/java_buildpack/buildpack_version.rb

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
require 'java_buildpack'
1818
require 'java_buildpack/util/configuration_utils'
19+
require 'java_buildpack/util/to_b'
1920

2021
module JavaBuildpack
2122

@@ -26,16 +27,48 @@ module JavaBuildpack
2627
# 3. unknown
2728
class BuildpackVersion
2829

30+
# @!attribute [r] hash
31+
# @return [String, nil] the Git hash of this version, or +nil+ if it cannot be determined
32+
attr_reader :hash
33+
34+
# @!attribute [r] offline
35+
# @return [Boolean] +true+ if the buildpack is offline, +false+ otherwise
36+
attr_reader :offline
37+
38+
# @!attribute [r] remote
39+
# @return [String, nil] the Git remote of this version, or +nil+ if it cannot be determined
40+
attr_reader :remote
41+
42+
# @!attribute [r] version
43+
# @return [String, nil] the version name of this version, or +nil+ if it cannot be determined
44+
attr_reader :version
45+
2946
# Creates a new instance
30-
def initialize
31-
configuration = JavaBuildpack::Util::ConfigurationUtils.load 'version'
47+
def initialize(should_log = true)
48+
configuration = JavaBuildpack::Util::ConfigurationUtils.load 'version', should_log
3249
@hash = configuration['hash'] || hash
33-
@offline = configuration['offline']
50+
@offline = configuration['offline'] || ENV['OFFLINE'].to_b
3451
@remote = configuration['remote'] || remote
35-
@version = configuration['version']
52+
@version = configuration['version'] || ENV['VERSION'] || @hash
53+
54+
if should_log
55+
logger = Logging::LoggerFactory.instance.get_logger BuildpackVersion
56+
logger.debug { to_s }
57+
end
58+
end
59+
60+
# Returns a +Hash+ representation of the buildpack version.
61+
#
62+
# @return [Hash] a representation of the buildpack version
63+
def to_hash
64+
h = {}
65+
66+
h['hash'] = @hash if @hash
67+
h['offline'] = @offline if @offline
68+
h['remote'] = @remote if @remote
69+
h['version'] = @version if @version
3670

37-
logger = Logging::LoggerFactory.instance.get_logger BuildpackVersion
38-
logger.debug { to_s }
71+
h
3972
end
4073

4174
# Creates a string representation of the version. The string representation looks like the following:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,26 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
# A mixin that adds the ability to turn a +String+ into a boolean
1718
class String
19+
20+
# Converts a +String+ to a boolean
21+
#
22+
# @return [Boolean] +true+ if +<STRING>.downcase == 'true'+. +false+ otherwise
1823
def to_b
1924
downcase == 'true'
2025
end
26+
2127
end
2228

29+
# A mixin that adds the ability to turn a +nil+ into a boolean
2330
class NilClass
31+
32+
# Converts a +nil+ to a boolean
33+
#
34+
# @return [Boolean] +false+ always
2435
def to_b
2536
false
2637
end
38+
2739
end

rakelib/dependency_cache_task.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DependencyCacheTask < Rake::TaskLib
3232
include Package
3333

3434
def initialize
35-
if OFFLINE
35+
if BUILDPACK_VERSION.offline
3636
JavaBuildpack::Logging::LoggerFactory.instance.setup "#{BUILD_DIR}/"
3737

3838
@default_repository_root = default_repository_root

rakelib/package.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,28 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
require 'rakelib/to_b'
17+
require 'java_buildpack/buildpack_version'
1818

1919
module Package
2020

21-
def self.git(command)
22-
`git #{command}`.chomp
23-
rescue
24-
nil
21+
def self.offline
22+
'-offline' if BUILDPACK_VERSION.offline
23+
end
24+
25+
def self.version
26+
BUILDPACK_VERSION.version || 'unknown'
2527
end
2628

2729
ARCHITECTURES = %w(x86_64).freeze
2830

2931
BUILD_DIR = 'build'.freeze
3032

31-
HASH = git 'rev-parse --short HEAD'.freeze
32-
33-
OFFLINE = ENV['OFFLINE'].to_b.freeze
33+
BUILDPACK_VERSION = JavaBuildpack::BuildpackVersion.new(false).freeze
3434

3535
PLATFORMS = %w(centos6 lucid mountainlion precise).freeze
3636

37-
REMOTE = git 'config --get remote.origin.url'.freeze
38-
3937
STAGING_DIR = "#{BUILD_DIR}/staging".freeze
4038

41-
VERSION = (ENV['VERSION'] || HASH).freeze
42-
43-
PACKAGE_NAME = "#{BUILD_DIR}/java-buildpack#{'-offline' if OFFLINE}-#{VERSION}.zip".freeze
39+
PACKAGE_NAME = "#{BUILD_DIR}/java-buildpack#{offline}-#{version}.zip".freeze
4440

4541
end

rakelib/stage_buildpack_task.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class StageBuildpackTask < Rake::TaskLib
2626
def initialize(source_files)
2727
source_files.map { |source| multitask PACKAGE_NAME => [copy_task(source, target(source))] }
2828
multitask PACKAGE_NAME => [version_task]
29-
disable_remote_downloads_task if OFFLINE
29+
disable_remote_downloads_task if BUILDPACK_VERSION.offline
3030
end
3131

3232
private
@@ -60,7 +60,7 @@ def version_task
6060
directory parent
6161
file target => [parent] do |t|
6262
File.open(t.name, 'w') do |f|
63-
f.write({ 'hash' => HASH, 'offline' => OFFLINE, 'remote' => REMOTE, 'version' => VERSION }.to_yaml)
63+
f.write(BUILDPACK_VERSION.to_hash.to_yaml)
6464
end
6565
end
6666

spec/java_buildpack/buildpack_version_spec.rb

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18+
require 'application_helper'
1819
require 'logging_helper'
1920
require 'java_buildpack/buildpack_version'
2021
require 'pathname'
2122

2223
describe JavaBuildpack::BuildpackVersion do
24+
include_context 'application_helper'
2325
include_context 'logging_helper'
2426

2527
let(:buildpack_version) { described_class.new }
2628

2729
before do |example|
2830
configuration = example.metadata[:configuration] || {}
29-
allow(JavaBuildpack::Util::ConfigurationUtils).to receive(:load).with('version').and_return(configuration)
31+
allow(JavaBuildpack::Util::ConfigurationUtils).to receive(:load).with('version', true).and_return(configuration)
3032
end
3133

3234
it 'should create offline version string from config/version.yml',
@@ -79,4 +81,43 @@
7981
expect(stderr.string).to match(/unknown/)
8082
end
8183

84+
it 'should create a has from the values',
85+
configuration: { 'hash' => 'test-hash', 'offline' => true,
86+
'remote' => 'test-remote', 'version' => 'test-version' } do |example|
87+
88+
allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
89+
90+
expect(buildpack_version.to_hash).to eq(example.metadata[:configuration])
91+
end
92+
93+
it 'should exclude non-populated values from the hash' do
94+
allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
95+
96+
expect(buildpack_version.to_hash).to eq({})
97+
end
98+
99+
context do
100+
101+
let(:environment) { { 'OFFLINE' => 'true' } }
102+
103+
it 'should pick up offline from the environment' do
104+
allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
105+
106+
expect(buildpack_version.offline).to be
107+
end
108+
109+
end
110+
111+
context do
112+
113+
let(:environment) { { 'VERSION' => 'test-version' } }
114+
115+
it 'should pick up version from the environment' do
116+
allow_any_instance_of(described_class).to receive(:system).with('which git > /dev/null').and_return(false)
117+
118+
expect(buildpack_version.version).to match(/test-version/)
119+
end
120+
121+
end
122+
82123
end

0 commit comments

Comments
 (0)