Skip to content

Commit 2948dee

Browse files
committed
ruby-package
1 parent 97a4851 commit 2948dee

File tree

6 files changed

+248
-72
lines changed

6 files changed

+248
-72
lines changed

bin/ruby-package

Lines changed: 16 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
package_platform() {
5-
echo "$(uname -m)-$(uname -s)" | tr '[A-Z] ' '[a-z]_'
6-
}
7-
84
resolve_link() {
95
$(type -p greadlink readlink | head -1) $1
106
}
@@ -24,80 +20,28 @@ abs_dirname() {
2420
}
2521

2622
usage() {
27-
echo "usage: ruby-package [-v | --verbose] [--resume] DEFINITION"
23+
echo "usage: ruby-package <command> [<args>]" >&2
2824
exit 1
2925
}
3026

31-
bin_root="$(abs_dirname "$0")"
27+
bin_dir="$(abs_dirname "$0")"
28+
export PATH="${bin_dir}/../libexec:${bin_dir}:$PATH"
3229

33-
verbose=""
34-
resume=""
30+
command="$1"
31+
command_path="$(command -v ruby-package-"$1" || true)"
3532

36-
while :; do
37-
case "$1" in
38-
"-v" | "--verbose" )
39-
verbose="-v"
40-
shift
41-
;;
42-
"--resume" )
43-
resume=1
44-
shift
45-
;;
46-
"-"* )
47-
usage
48-
;;
49-
* )
50-
break
51-
;;
52-
esac
53-
done
54-
55-
definition="$1"
56-
if [ -z "$definition" ]; then
57-
usage
33+
if [ -z "$TMPDIR" ]; then
34+
export TMPDIR="/tmp"
35+
else
36+
export TMPDIR="${TMPDIR%/}"
5837
fi
5938

60-
package="${definition##*/}"
61-
prefix="/tmp/ruby-build/-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/$package"
62-
63-
package_dir="${package}.$(package_platform)"
64-
65-
if [ -z "$resume" ]; then
66-
"${bin_root}/ruby-build" $verbose "$definition" "$prefix"
39+
if [ -z "$command" ]; then
40+
usage
41+
elif [ -z "$command_path" ]; then
42+
echo "ruby-package: ${command}: command not found" >&2
43+
usage
6744
fi
6845

69-
cwd="$(pwd)"
70-
cd "${prefix}/.."
71-
72-
mkdir -p "${package_dir}/bin"
73-
cp "${bin_root}/../share/ruby-package/ruby-package-install" "${package_dir}/bin"
74-
cp "${bin_root}/../share/ruby-package/ruby-package-rewrite-text" "${package_dir}/bin"
75-
cc -Wall "${bin_root}/../share/ruby-package/ruby-package-rewrite-binary.c" -o "${package_dir}/bin/ruby-package-rewrite-binary"
76-
77-
mkdir -p "${package_dir}/metadata"
78-
echo -n "$prefix" > "${package_dir}/metadata/prefix"
79-
echo -n "$package" > "${package_dir}/metadata/package"
80-
81-
cd "$package"
82-
83-
while read line; do
84-
binary="${line#Binary file }"
85-
if [ "$line" = "$binary" ]; then
86-
# plain text match
87-
text="${line%%:*}"
88-
echo "$text" >> "../${package_dir}/metadata/text-files"
89-
else
90-
# binary match
91-
binary="${binary% matches}"
92-
echo "$binary" >> "../${package_dir}/metadata/binary-files"
93-
fi
94-
done < <( grep -m 1 -R "$prefix" * )
95-
96-
tar cf "../${package_dir}/package.tar" *
97-
98-
cd ..
99-
100-
tar czf "${cwd}/${package_dir}.rubypackage" "$package_dir"
101-
rm -fr "$package_dir"
102-
103-
echo "${package_dir}.rubypackage"
46+
shift
47+
exec "$command_path" "$@"

libexec/ruby-package-build

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
resolve_link() {
5+
$(type -p greadlink readlink | head -1) $1
6+
}
7+
8+
abs_dirname() {
9+
local cwd="$(pwd)"
10+
local path="$1"
11+
12+
while [ -n "$path" ]; do
13+
cd "${path%/*}"
14+
local name="${path##*/}"
15+
path="$(resolve_link "$name" || true)"
16+
done
17+
18+
pwd
19+
cd "$cwd"
20+
}
21+
22+
usage() {
23+
echo "usage: ruby-package build [-v | --verbose] [--resume] DEFINITION"
24+
exit 1
25+
}
26+
27+
bin_root="$(abs_dirname "$0")"
28+
29+
verbose=""
30+
resume=""
31+
32+
while :; do
33+
case "$1" in
34+
"-v" | "--verbose" )
35+
verbose="-v"
36+
shift
37+
;;
38+
"--resume" )
39+
resume=1
40+
shift
41+
;;
42+
"-"* )
43+
usage
44+
;;
45+
* )
46+
break
47+
;;
48+
esac
49+
done
50+
51+
definition="$1"
52+
if [ -z "$definition" ]; then
53+
usage
54+
fi
55+
56+
package="${definition##*/}"
57+
prefix="/tmp/ruby-build/-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------/$package"
58+
59+
package_dir="${package}.$(ruby-package platform)"
60+
61+
if [ -z "$resume" ]; then
62+
"${bin_root}/ruby-build" $verbose "$definition" "$prefix"
63+
fi
64+
65+
cwd="$(pwd)"
66+
cd "${prefix}/.."
67+
68+
mkdir -p "${package_dir}/bin"
69+
cp "${bin_root}/../share/ruby-package/ruby-package-install" "${package_dir}/bin"
70+
cp "${bin_root}/../share/ruby-package/ruby-package-rewrite-text" "${package_dir}/bin"
71+
cc -Wall "${bin_root}/../share/ruby-package/ruby-package-rewrite-binary.c" -o "${package_dir}/bin/ruby-package-rewrite-binary"
72+
73+
mkdir -p "${package_dir}/metadata"
74+
echo -n "$prefix" > "${package_dir}/metadata/prefix"
75+
echo -n "$package" > "${package_dir}/metadata/package"
76+
77+
cd "$package"
78+
79+
while read line; do
80+
binary="${line#Binary file }"
81+
if [ "$line" = "$binary" ]; then
82+
# plain text match
83+
text="${line%%:*}"
84+
echo "$text" >> "../${package_dir}/metadata/text-files"
85+
else
86+
# binary match
87+
binary="${binary% matches}"
88+
echo "$binary" >> "../${package_dir}/metadata/binary-files"
89+
fi
90+
done < <( grep -m 1 -R "$prefix" * )
91+
92+
tar cf "../${package_dir}/package.tar" *
93+
94+
cd ..
95+
96+
tar czf "${cwd}/${package_dir}.rubypackage" "$package_dir"
97+
rm -fr "$package_dir"
98+
99+
echo "${package_dir}.rubypackage"

libexec/ruby-package-fetch

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
usage() {
5+
echo "usage: ruby-package fetch PACKAGE" >&2
6+
exit 1
7+
}
8+
9+
if [ -z "$1" ]; then
10+
usage
11+
fi
12+
package_name="${1}.$(ruby-package platform).rubypackage"
13+
14+
package_repo="${RUBY_PACKAGE_REPO%/}"
15+
if [ -z "$ruby_package_repo" ]; then
16+
package_repo="https://github.com/downloads/sstephenson/ruby-packages"
17+
fi
18+
19+
url="${package_repo}/${package_name}"
20+
filename="${TMPDIR}/${package_name}.$$"
21+
22+
set +e
23+
curl -Lfs "$url" > "$filename"
24+
result="$?"
25+
set -e
26+
27+
if [ "$result" -eq 22 ]; then
28+
exit 3
29+
elif [ ! -f "$filename" ]; then
30+
exit 2
31+
else
32+
echo "$filename"
33+
fi

libexec/ruby-package-install

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
usage() {
5+
echo "usage: ruby-package install PACKAGE DESTINATION" >&2
6+
exit 1
7+
}
8+
9+
cleanup_packages() {
10+
if [ -n "$package_root" ]; then
11+
rm -rf "${package_root}/.."
12+
fi
13+
14+
if [ -n "$package_file" ]; then
15+
if [ "$package" != "$package_file" ]; then
16+
rm -rf "$package_file"
17+
fi
18+
fi
19+
}
20+
21+
package="$1"
22+
if [ -z "$package" ]; then
23+
usage
24+
fi
25+
26+
destination="$2"
27+
if [ -z "$destination" ]; then
28+
usage
29+
fi
30+
31+
if [ -f "$package" ]; then
32+
package_file="$package"
33+
else
34+
package_file="$(ruby-package fetch "$package")"
35+
fi
36+
37+
trap cleanup_packages SIGINT SIGTERM EXIT
38+
39+
package_root="$(ruby-package unpack "$package_file")"
40+
41+
"${package_root}/bin/ruby-package-install" "$destination"

libexec/ruby-package-platform

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
echo "$(uname -m)-$(uname -s)" | tr '[A-Z] ' '[a-z]_'

libexec/ruby-package-unpack

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
resolve_link() {
5+
$(type -p greadlink readlink | head -1) $1
6+
}
7+
8+
abs_dirname() {
9+
local cwd="$(pwd)"
10+
local path="$1"
11+
12+
while [ -n "$path" ]; do
13+
cd "${path%/*}" 2>/dev/null
14+
local name="${path##*/}"
15+
path="$(resolve_link "$name" || true)"
16+
done
17+
18+
pwd
19+
cd "$cwd"
20+
}
21+
22+
usage() {
23+
echo "usage: ruby-package unpack PACKAGE_FILE" >&2
24+
exit 1
25+
}
26+
27+
package_file="$1"
28+
if [ -z "$package_file" ]; then
29+
usage
30+
fi
31+
32+
package_path="$(abs_dirname "$package_file")/${package_file##*/}"
33+
if [ ! -f "$package_path" ]; then
34+
echo "error: file not found: $1" >&2
35+
exit 1
36+
fi
37+
38+
root="${TMPDIR}/ruby-package.$$"
39+
rm -fr "$root"
40+
mkdir -p "$root"
41+
42+
cd "$root"
43+
tar xzf "$package_path"
44+
45+
cd *
46+
package_root="$(pwd)"
47+
package_name="$(cat "$package_root"/metadata/package)"
48+
package_name_with_platform="${package_root##*/}"
49+
50+
if [ "${package_name}.$(ruby-package platform)" != "$package_name_with_platform" ]; then
51+
echo "error: invalid package format" >&2
52+
exit 2
53+
fi
54+
55+
echo "$package_root"

0 commit comments

Comments
 (0)