Skip to content

Commit b1e58f8

Browse files
Merge pull request #1000 from cloudfoundry/byo-ruby
Bring your own Ruby (whoops)
2 parents c453103 + 215c6c7 commit b1e58f8

File tree

8 files changed

+183
-121
lines changed

8 files changed

+183
-121
lines changed

bin/compile

Lines changed: 0 additions & 27 deletions
This file was deleted.

bin/compile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./run

bin/detect

Lines changed: 0 additions & 35 deletions
This file was deleted.

bin/detect

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./run

bin/finalize

Lines changed: 0 additions & 29 deletions
This file was deleted.

bin/finalize

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./run

bin/release

Lines changed: 0 additions & 29 deletions
This file was deleted.

bin/release

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./run

bin/ruby-run

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Cloud Foundry Java Buildpack
5+
# Copyright 2013-2020 the original author or authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
$stdout.sync = true
20+
$stderr.sync = true
21+
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
22+
require 'java_buildpack/buildpack'
23+
24+
case ARGV[0]
25+
when "compile"
26+
app_dir = ARGV[1]
27+
JavaBuildpack::Buildpack.with_buildpack(app_dir, nil, nil, 'Compile failed with exception %s', &:compile)
28+
29+
when "detect"
30+
app_dir = ARGV[1]
31+
components = JavaBuildpack::Buildpack.with_buildpack(app_dir, nil, nil, 'Detect failed with exception %s',
32+
&:detect).compact
33+
if components.empty?
34+
abort
35+
else
36+
str = components.join(' ')
37+
puts str.length > 255 ? str.slice(0..251) + '...' : str
38+
end
39+
40+
when "finalize"
41+
app_dir = ARGV[1]
42+
deps_dir = ARGV[3]
43+
index = ARGV[4]
44+
JavaBuildpack::Buildpack.with_buildpack(app_dir, deps_dir, index, 'Finalize failed with exception %s', &:compile)
45+
46+
when "release"
47+
app_dir = ARGV[1]
48+
output = JavaBuildpack::Buildpack.with_buildpack(app_dir, nil, nil, 'Release failed with exception %s', &:release)
49+
puts output
50+
end

bin/run

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
BUILDPACK_DIR="$(cd "$(dirname "${0}")/.." && pwd)"
8+
readonly BUILDPACK_DIR
9+
10+
RUBY_DIR="/tmp/ruby"
11+
readonly RUBY_DIR
12+
13+
function util::config::lookup() {
14+
sed '/^#/d' < "${BUILDPACK_DIR}/config/ruby.yml"
15+
}
16+
17+
function util::cache::present() {
18+
if [[ -e "${BUILDPACK_DIR}/resources/cache" ]]; then
19+
return 0
20+
else
21+
return 1
22+
fi
23+
}
24+
25+
function util::index::lookup() {
26+
local repository_root
27+
repository_root="$(grep "repository_root" <<< "$(util::config::lookup)" | cut -d' ' -f2)"
28+
29+
local uri
30+
uri="${repository_root}/index.yml"
31+
32+
if util::cache::present; then
33+
local sha
34+
sha="$(printf "%s" "${uri}" | shasum -a 256 | cut -d' ' -f1)"
35+
cat "${BUILDPACK_DIR}/resources/cache/${sha}.cached"
36+
else
37+
curl -ssL "${uri}"
38+
fi
39+
}
40+
41+
function util::semver::parse() {
42+
local version major minor patch
43+
version="$(grep "version" <<< "$(util::config::lookup)" | cut -d' ' -f2)"
44+
major="$(cut -d'.' -f1 <<< "${version}")"
45+
minor="$(cut -d'.' -f2 <<< "${version}")"
46+
patch="$(cut -d'.' -f3 <<< "${version}")"
47+
48+
printf "%s" "${major/+/*}\\.${minor/+/*}\\.${patch/+/*}"
49+
}
50+
51+
function util::ruby::stream() {
52+
local uri
53+
uri="${1}"
54+
55+
if util::cache::present; then
56+
local sha
57+
sha="$(printf "%s" "${uri}" | shasum -a 256 | cut -d' ' -f1)"
58+
cat "${BUILDPACK_DIR}/resources/cache/${sha}.cached"
59+
else
60+
curl -ssL "${uri}"
61+
fi
62+
}
63+
64+
function util::install() {
65+
local index semver
66+
index="$(util::index::lookup)"
67+
semver="$(util::semver::parse)"
68+
69+
local uri
70+
uri="$(grep "${semver}" <<< "${index}" | head -n 1 | awk '{print $2}')"
71+
72+
util::ruby::stream "${uri}" | tar xz -C "${RUBY_DIR}"
73+
}
74+
75+
function util::print::error() {
76+
local message red reset
77+
message="${1}"
78+
red="\033[0;31m"
79+
reset="\033[0;39m"
80+
81+
echo -e "${red}${message}${reset}" >&2
82+
exit 1
83+
}
84+
85+
function util::environment::setup() {
86+
export PATH="${RUBY_DIR}/bin:${PATH:-}"
87+
export LIBRARY_PATH="${RUBY_DIR}/lib:${LIBRARY_PATH:-}"
88+
export LD_LIBRARY_PATH="${RUBY_DIR}/lib:${LIBRARY_PATH:-}"
89+
export CPATH="${RUBY_DIR}/include:${CPATH:-}"
90+
}
91+
92+
function main() {
93+
local phase
94+
phase="$(basename "${0}")"
95+
96+
if [[ "${CF_STACK}" == "cflinuxfs4" ]]; then
97+
if [[ ! -e "${RUBY_DIR}" ]]; then
98+
mkdir -p "${RUBY_DIR}"
99+
100+
util::install
101+
fi
102+
103+
util::environment::setup
104+
fi
105+
106+
exec "${BUILDPACK_DIR}/bin/ruby-run" "${phase}" "${@-}"
107+
}
108+
109+
main "${@:-}"

0 commit comments

Comments
 (0)