Skip to content

Commit d1aaaf6

Browse files
committed
Helper behaviors
This change adds some helper behaviors that can be used by downstream components. Specifically, this adds the ability to execute a block with JAVA_HOME set, and makes the copy_resources and download_jar methods a bit more robust.
1 parent 0492b8d commit d1aaaf6

3 files changed

Lines changed: 53 additions & 4 deletions

File tree

lib/java_buildpack/application/java_home.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ def as_env_var
4343
"JAVA_HOME=#{self}"
4444
end
4545

46+
# Execute a block with the +JAVA_HOME+ environment variable set
47+
#
48+
# @yield yields to block with the +JAVA_HOME+ environment variable set
49+
def do_with
50+
previous_value = ENV['JAVA_HOME']
51+
begin
52+
ENV['JAVA_HOME'] = to_s
53+
yield
54+
ensure
55+
ENV['JAVA_HOME'] = previous_value
56+
end
57+
end
58+
4659
end
4760

4861
end

lib/java_buildpack/base_component.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,12 @@ def release
7979

8080
protected
8181

82-
# Copy resources from a components resources directory to its +home+ directory
83-
def copy_resources
82+
# Copy resources from a components resources directory to a directory
83+
#
84+
# @param [Pathname] target_directory the directory to copy to. Default to a component's +home+
85+
def copy_resources(target_directory = home)
8486
resources = Pathname.new(File.expand_path('../../../resources', __FILE__)) + @parsable_component_name
85-
FileUtils.cp_r((resources.to_s + '/.'), home) if resources.exist?
87+
FileUtils.cp_r((resources.to_s + '/.'), target_directory) if resources.exist?
8688
end
8789

8890
# Downloads an item with the given name and version from the given URI, then yields the resultant file to the given
@@ -111,7 +113,8 @@ def download(version, uri, description = @component_name, &block)
111113
# +@lib_directory+
112114
# @param [String] description an optional description for the download. Defaults to +@component_name+.
113115
def download_jar(version, uri, jar_name, target_directory = @lib_directory, description = @component_name)
114-
download(version, uri, description) { |file| FileUtils.cp file.path, File.join(target_directory, jar_name) }
116+
FileUtils.mkdir_p target_directory
117+
download(version, uri, description) { |file| FileUtils.cp file.path, (target_directory + jar_name) }
115118
end
116119

117120
# The home directory for this component
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 'spec_helper'
18+
require 'application_helper'
19+
20+
describe JavaBuildpack::Application::JavaHome do
21+
include_context 'application_helper'
22+
23+
let(:java_home) { application.component_directory('test-java-home') }
24+
25+
before do
26+
application.java_home.set java_home
27+
end
28+
29+
it 'should set JAVA_HOME environment variable' do
30+
application.java_home.do_with { expect(ENV['JAVA_HOME']).to eq("$PWD/#{java_home.relative_path_from app_dir}") }
31+
end
32+
33+
end

0 commit comments

Comments
 (0)