Skip to content

Commit

Permalink
Helper behaviors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nebhale committed Dec 2, 2013
1 parent 0492b8d commit d1aaaf6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/java_buildpack/application/java_home.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ def as_env_var
"JAVA_HOME=#{self}"
end

# Execute a block with the +JAVA_HOME+ environment variable set
#
# @yield yields to block with the +JAVA_HOME+ environment variable set
def do_with
previous_value = ENV['JAVA_HOME']
begin
ENV['JAVA_HOME'] = to_s
yield
ensure
ENV['JAVA_HOME'] = previous_value
end
end

end

end
11 changes: 7 additions & 4 deletions lib/java_buildpack/base_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ def release

protected

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

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

# The home directory for this component
Expand Down
33 changes: 33 additions & 0 deletions spec/java_buildpack/application/java_home_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Encoding: utf-8
# Cloud Foundry Java Buildpack
# Copyright 2013 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'spec_helper'
require 'application_helper'

describe JavaBuildpack::Application::JavaHome do
include_context 'application_helper'

let(:java_home) { application.component_directory('test-java-home') }

before do
application.java_home.set java_home
end

it 'should set JAVA_HOME environment variable' do
application.java_home.do_with { expect(ENV['JAVA_HOME']).to eq("$PWD/#{java_home.relative_path_from app_dir}") }
end

end

0 comments on commit d1aaaf6

Please sign in to comment.