Skip to content

Commit 9ae81ed

Browse files
committed
Merge 68410854-embedded-version to master
[Completes #68410854]
2 parents 6ad1deb + a27a7d4 commit 9ae81ed

7 files changed

Lines changed: 212 additions & 95 deletions

File tree

lib/java_buildpack/buildpack.rb

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
require 'java_buildpack'
18+
require 'java_buildpack/buildpack_version'
1819
require 'java_buildpack/component/additional_libraries'
1920
require 'java_buildpack/component/application'
2021
require 'java_buildpack/component/droplet'
@@ -39,8 +40,6 @@ class Buildpack
3940
# this application. If no container can run the application, the array will be empty
4041
# (+[]+).
4142
def detect
42-
diagnose_git_info false
43-
4443
tags = tag_detection('container', @containers, true)
4544
tags.concat tag_detection('JRE', @jres, true) unless tags.empty?
4645
tags.concat tag_detection('framework', @frameworks, false) unless tags.empty?
@@ -54,7 +53,7 @@ def detect
5453
#
5554
# @return [Void]
5655
def compile
57-
diagnose_git_info true
56+
puts BUILDPACK_MESSAGE % @buildpack_version
5857

5958
container = component_detection(@containers).first
6059
fail 'No container can run this application' unless container
@@ -69,8 +68,6 @@ def compile
6968
#
7069
# @return [String] The payload required to run the application.
7170
def release
72-
diagnose_git_info false
73-
7471
container = component_detection(@containers).first
7572
fail 'No container can run this application' unless container
7673

@@ -93,14 +90,13 @@ def release
9390

9491
private
9592

96-
DEFAULT_BUILDPACK_MESSAGE = '-----> Java Buildpack source: unknown'.freeze
97-
98-
GIT_DIR = Pathname.new(__FILE__).dirname + '../../.git'
93+
BUILDPACK_MESSAGE = '-----> Java Buildpack Version: %s'.freeze
9994

10095
LOAD_ROOT = Pathname.new(__FILE__).dirname + '..'
10196

10297
def initialize(app_dir, application)
103-
@logger = Logging::LoggerFactory.instance.get_logger Buildpack
98+
@logger = Logging::LoggerFactory.instance.get_logger Buildpack
99+
@buildpack_version = BuildpackVersion.new
104100

105101
log_environment_variables
106102

@@ -123,31 +119,6 @@ def component_detection(components)
123119
components.select { |component| component.detect }
124120
end
125121

126-
def diagnose_git_info(print)
127-
if system("git --git-dir=#{GIT_DIR} status 2>/dev/null 1>/dev/null")
128-
remote_url = diagnose_remote
129-
head_commit_sha = diagnose_head_commit
130-
puts "-----> Java Buildpack source: #{remote_url}##{head_commit_sha}" if print
131-
else
132-
@logger.debug { DEFAULT_BUILDPACK_MESSAGE }
133-
puts DEFAULT_BUILDPACK_MESSAGE if print
134-
end
135-
end
136-
137-
def diagnose_head_commit
138-
git 'rev-parse --short HEAD', 'git HEAD commit: %s'
139-
end
140-
141-
def diagnose_remote
142-
git 'config --get remote.origin.url', 'git remote: %s'
143-
end
144-
145-
def git(command, message)
146-
result = `git --git-dir=#{GIT_DIR} #{command}`.chomp
147-
@logger.debug { message % result }
148-
result
149-
end
150-
151122
def instantiate(components, additional_libraries, application, java_home, java_opts, root)
152123
components.map do |component|
153124
@logger.debug { "Instantiating #{component}" }
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Encoding: utf-8
2+
# Cloud Foundry Java Buildpack
3+
# Copyright 2013 the original author or authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require 'java_buildpack'
18+
require 'java_buildpack/util/configuration_utils'
19+
20+
module JavaBuildpack
21+
22+
# A representation of the buildpack's version. The buildpack's version is determined using the following algorithm:
23+
#
24+
# 1. using the +config/version.yml+ file if it exists
25+
# 2. using +git+ to determine the remote and hash if the buildpack is in a git repository
26+
# 3. unknown
27+
class BuildpackVersion
28+
29+
# Creates a new instance
30+
def initialize
31+
configuration = JavaBuildpack::Util::ConfigurationUtils.load 'version'
32+
@hash = configuration['hash'] || hash
33+
@offline = configuration['offline']
34+
@remote = configuration['remote'] || remote
35+
@version = configuration['version']
36+
37+
logger = Logging::LoggerFactory.instance.get_logger BuildpackVersion
38+
logger.debug { to_s }
39+
end
40+
41+
# Creates a string representation of the version. The string representation looks like the following:
42+
# +[[<VERSION> [(offline)] | ] <REMOTE>#<HASH>] | [unknown]+. Some examples:
43+
#
44+
# +2.1.2 (offline) | https://github.com/cloudfoundry/java-buildpack.git#12345+ (custom version number, offline buildpack)
45+
# +abcde | https://github.com/cloudfoundry/java-buildpack.git#abcde+ (default version number, online buildpack)
46+
# +https://github.com/cloudfoundry/java-buildpack#12345+ (cloned buildpack)
47+
# +unknown+ (un-packaged, un-cloned)
48+
def to_s
49+
s = []
50+
s << @version if @version
51+
s << '(offline)' if @offline
52+
s << '|' if @version
53+
s << "#{@remote}##{@hash}" if @remote && @hash
54+
s << 'unknown' if s.empty?
55+
56+
s.join ' '
57+
end
58+
59+
private
60+
61+
GIT_DIR = (Pathname.new(__FILE__).dirname + '../../.git').freeze
62+
63+
def git(command)
64+
`git --git-dir=#{GIT_DIR} #{command}`.chomp if git? && git_dir?
65+
end
66+
67+
def git?
68+
system 'which git > /dev/null'
69+
end
70+
71+
def git_dir?
72+
GIT_DIR.exist?
73+
end
74+
75+
def hash
76+
git 'rev-parse --short HEAD'
77+
end
78+
79+
def remote
80+
git 'config --get remote.origin.url'
81+
end
82+
83+
end
84+
85+
end

rakelib/dependency_cache_task.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def initialize
3939
@cache = cache
4040

4141
configurations = component_ids.map { |component_id| configurations(configuration(component_id)) }.flatten
42-
uris(configurations).each { |uri| create_task(uri) }
42+
uris(configurations).each { |uri| task PACKAGE_NAME => [cache_task(uri)] }
4343
end
4444
end
4545

@@ -87,6 +87,15 @@ def cache
8787
JavaBuildpack::Util::Cache::DownloadCache.new(Pathname.new("#{STAGING_DIR}/resources/cache")).freeze
8888
end
8989

90+
def cache_task(uri)
91+
task uri do |t|
92+
rake_output_message "Caching #{t.name}"
93+
cache.get(t.name) {}
94+
end
95+
96+
uri
97+
end
98+
9099
def component_ids
91100
configuration('components').values.flatten.map { |component| component.split('::').last.snake_case }
92101
end
@@ -142,15 +151,6 @@ def version(configuration, index)
142151
JavaBuildpack::Repository::VersionResolver.resolve(JavaBuildpack::Util::TokenizedVersion.new(configuration['version']), index.keys)
143152
end
144153

145-
def create_task(uri)
146-
task uri do |t|
147-
rake_output_message "Caching #{t.name}"
148-
cache.get(t.name) {}
149-
end
150-
151-
task PACKAGE_NAME => [uri]
152-
end
153-
154154
end
155155

156156
end

rakelib/package.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ module Package
2222

2323
BUILD_DIR = 'build'.freeze
2424

25+
HASH = `git rev-parse --short HEAD`.chomp.freeze
26+
2527
OFFLINE = ENV['OFFLINE'].to_b.freeze
2628

2729
PLATFORMS = %w(centos6 lucid mountainlion precise).freeze
2830

31+
REMOTE = `git config --get remote.origin.url`.chomp.freeze
32+
2933
STAGING_DIR = "#{BUILD_DIR}/staging".freeze
3034

31-
VERSION = (ENV['VERSION'] || `git rev-parse --short HEAD`.chomp).freeze
35+
VERSION = (ENV['VERSION'] || HASH).freeze
3236

3337
PACKAGE_NAME = "#{BUILD_DIR}/java-buildpack#{'-offline' if OFFLINE}-#{VERSION}.tar.gz".freeze
3438

rakelib/stage_buildpack_task.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,55 @@
1616

1717
require 'rake/tasklib'
1818
require 'rakelib/package'
19+
require 'yaml'
1920

2021
module Package
2122

2223
class StageBuildpackTask < Rake::TaskLib
2324
include Package
2425

2526
def initialize(source_files)
26-
source_files.map { |source| create_task(source, target(source)) }
27+
source_files.map { |source| task PACKAGE_NAME => [copy_task(source, target(source))] }
28+
task PACKAGE_NAME => [version_task]
29+
disable_remote_downloads_task if OFFLINE
30+
end
2731

28-
if OFFLINE
29-
file "#{STAGING_DIR}/config/cache.yml" do |t|
30-
content = File.open(t.source, 'r') { |f| f.read.gsub(/enabled/, 'disabled') }
31-
File.open(t.name, 'w') { |f| f.write content }
32-
end
32+
private
33+
34+
def copy_task(source, target)
35+
parent = File.dirname target
36+
37+
directory parent
38+
file(target => [source, parent]) do |t|
39+
cp t.source, t.name
40+
end
41+
42+
target
43+
end
44+
45+
def disable_remote_downloads_task
46+
file "#{STAGING_DIR}/config/cache.yml" do |t|
47+
content = File.open(t.source, 'r') { |f| f.read.gsub(/enabled/, 'disabled') }
48+
File.open(t.name, 'w') { |f| f.write content }
3349
end
3450
end
3551

3652
def target(source)
3753
"#{STAGING_DIR}/#{source}"
3854
end
3955

40-
private
41-
42-
def create_task(source, target)
56+
def version_task
57+
target = target('config/version.yml')
4358
parent = File.dirname target
4459

4560
directory parent
46-
file(target => [source, parent]) do |t|
47-
cp t.source, t.name
61+
file target => [parent] do |t|
62+
File.open(t.name, 'w') do |f|
63+
f.write({ 'hash' => HASH, 'offline' => OFFLINE, 'remote' => REMOTE, 'version' => VERSION }.to_yaml)
64+
end
4865
end
4966

50-
task PACKAGE_NAME => [target]
67+
target
5168
end
5269

5370
end

spec/java_buildpack/buildpack_spec.rb

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -139,44 +139,6 @@
139139
buildpack.detect
140140
end
141141

142-
it 'logs information about the git repository of a buildpack',
143-
log_level: 'DEBUG' do
144-
145-
buildpack.detect
146-
147-
expect(stderr.string).to match(/git remote/)
148-
expect(stderr.string).to match(/git HEAD commit/)
149-
end
150-
151-
it 'prints output during compile showing the git repository of a buildpack' do
152-
expect { buildpack.compile }.to raise_error # ok since fixture has no application
153-
154-
expect(stdout.string).to match(/Java Buildpack source: .*#.*/)
155-
end
156-
157-
it 'realises when buildpack is not stored in a git repository',
158-
log_level: 'DEBUG' do
159-
160-
Dir.mktmpdir do |tmp_dir|
161-
stub_const(described_class.to_s + '::GIT_DIR', Pathname.new(tmp_dir))
162-
163-
with_buildpack { |buildpack| buildpack.detect }
164-
165-
expect(stderr.string).to match(/Java Buildpack source: unknown/)
166-
end
167-
end
168-
169-
it 'prints output during compile showing that buildpack is not stored in a git repository' do
170-
171-
Dir.mktmpdir do |tmp_dir|
172-
stub_const(described_class.to_s + '::GIT_DIR', Pathname.new(tmp_dir))
173-
174-
with_buildpack { |buildpack| expect { buildpack.compile }.to raise_error } # error ok since fixture has no application
175-
176-
expect(stdout.string).to match(/Java Buildpack source: unknown/)
177-
end
178-
end
179-
180142
it 'handles exceptions correctly' do
181143
expect { with_buildpack { |buildpack| fail 'an exception' } }.to raise_error SystemExit
182144
expect(stderr.string).to match(/an exception/)

0 commit comments

Comments
 (0)