Skip to content

Commit 616b762

Browse files
learned to install builder
1 parent d87c539 commit 616b762

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The `:latest` version annotation is added implicitly if omitted. To pin to a spe
4040
| Feature | Description
4141
| --- | ---
4242
| [`bosh-cli`](./src/bosh-cli) | Installs [BOSH CLI](https://bosh.io/docs/cli-v2/), an open source tool for release engineering, deployment, lifecycle management, and monitoring of distributed systems.
43+
| [`builder`](./src/builder) | Installs [Builder](https://github.com/jtarchie/builder): Static Site Generation Tool.
4344
| [`cf-cli`](./src/cf-cli) | Installs [Cloud Foundry CLI](https://bosh.io/docs/cli-v2/), the official command line client for Cloud Foundry.
4445
| [`credhub-cli`](./src/credhub-cli) | Installs [CredHub CLI](https://github.com/cloudfoundry/credhub-cli), a credential manager for managing passwords, certificates, certificate authorities, ssh keys, rsa keys and arbitrary values (strings and JSON blobs).
4546
| [`fly-cli`](./src/fly-cli) | Installs the [Concourse Fly CLI](https://concourse-ci.org/fly.html).

Diff for: src/builder/devcontainer-feature.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "Builder: Static Site Generation Tool",
3+
"id": "builder",
4+
"version": "1.0.0",
5+
"description": "Builder is a streamlined static site generation tool designed with a focus on convention over configuration. https://github.com/jtarchie/builder",
6+
"options": {
7+
"version": {
8+
"type": "string",
9+
"proposals": [
10+
"latest",
11+
"none"
12+
],
13+
"default": "latest",
14+
"description": "Select or enter a Builder version."
15+
}
16+
},
17+
"installsAfter": [
18+
"ghcr.io/devcontainers/features/common-utils"
19+
]
20+
}

Diff for: src/builder/install.sh

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env bash
2+
3+
: ${VERSION:="latest"}
4+
: ${INSTALL_DEPENDENCIES:=true}
5+
6+
USERNAME=${USERNAME:-${_REMOTE_USER:-"automatic"}}
7+
8+
set -e
9+
10+
export TMPDIR=$(mktemp -d /tmp/feature.XXXXXX)
11+
trap "rm -rf $TMPDIR" EXIT
12+
13+
# Clean up
14+
rm -rf /var/lib/apt/lists/*
15+
16+
if [ "$(id -u)" -ne 0 ]; then
17+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
18+
exit 1
19+
fi
20+
21+
architecture="$(dpkg --print-architecture)"
22+
if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "arm64" ]; then
23+
echo "(!) Architecture $architecture unsupported"
24+
exit 1
25+
fi
26+
27+
# Determine the appropriate non-root user
28+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
29+
USERNAME=""
30+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
31+
for CURRENT_USER in "${POSSIBLE_USERS[@]}"; do
32+
if id -u "${CURRENT_USER}" >/dev/null 2>&1; then
33+
USERNAME=${CURRENT_USER}
34+
break
35+
fi
36+
done
37+
if [ "${USERNAME}" = "" ]; then
38+
USERNAME=root
39+
fi
40+
elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} >/dev/null 2>&1; then
41+
USERNAME=root
42+
fi
43+
44+
apt_get_update() {
45+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
46+
echo "Running apt-get update..."
47+
apt-get update -y
48+
fi
49+
}
50+
51+
# Checks if packages are installed and installs them if not
52+
check_packages() {
53+
if ! dpkg -s "$@" >/dev/null 2>&1; then
54+
apt_get_update
55+
apt-get -y install --no-install-recommends "$@"
56+
fi
57+
}
58+
59+
check_git() {
60+
if [ ! -x "$(command -v git)" ]; then
61+
check_packages git
62+
fi
63+
}
64+
65+
find_version_from_git_tags() {
66+
local variable_name=$1
67+
local requested_version=${!variable_name}
68+
if [ "${requested_version}" = "none" ]; then return; fi
69+
local repository=$2
70+
local prefix=${3:-"tags/v"}
71+
local separator=${4:-"."}
72+
local last_part_optional=${5:-"false"}
73+
if [ "$(echo "${requested_version}" | grep -o "." | wc -l)" != "2" ]; then
74+
local escaped_separator=${separator//./\\.}
75+
local last_part
76+
if [ "${last_part_optional}" = "true" ]; then
77+
last_part="(${escaped_separator}[0-9]+)*?"
78+
else
79+
last_part="${escaped_separator}[0-9]+"
80+
fi
81+
local regex="${prefix}\\K[0-9]+${escaped_separator}[0-9]+${last_part}$"
82+
local version_list
83+
check_git
84+
check_packages ca-certificates
85+
version_list="$(git ls-remote --tags "${repository}" | grep -oP "${regex}" | tr -d ' ' | tr "${separator}" "." | sort -rV)"
86+
if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ] || [ "${requested_version}" = "lts" ]; then
87+
declare -g "${variable_name}"="$(echo "${version_list}" | head -n 1)"
88+
else
89+
set +e
90+
declare -g "${variable_name}"="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
91+
set -e
92+
fi
93+
fi
94+
if [ -z "${!variable_name}" ] || ! echo "${version_list}" | grep "^${!variable_name//./\\.}$" >/dev/null 2>&1; then
95+
echo -e "Invalid ${variable_name} value: ${requested_version}\nValid values:\n${version_list}" >&2
96+
exit 1
97+
fi
98+
echo "${variable_name}=${!variable_name}"
99+
}
100+
101+
export DEBIAN_FRONTEND=noninteractive
102+
103+
### BEGIN install
104+
105+
# Soft version matching
106+
find_version_from_git_tags VERSION "https://github.com/jtarchie/builder"
107+
108+
check_packages curl ca-certificates
109+
echo "Downloading builder..."
110+
curl -sL "https://github.com/jtarchie/builder/releases/download/v${VERSION}/builder_linux_${architecture}.tar.gz" -o "${TMPDIR}/builder.tar.gz"
111+
112+
# Uncompress the tar.gz file
113+
tar -xzf "${TMPDIR}/builder.tar.gz" -C "${TMPDIR}"
114+
115+
# Make the builder executable and move it to /usr/local/bin
116+
install "${TMPDIR}/builder" /usr/local/bin/builder
117+
118+
builder --help
119+
echo "Builder version ${VERSION} for architecture ${architecture} installed successfully."
120+
121+
### END install
122+
123+
# Clean up
124+
rm -rf /var/lib/apt/lists/*
125+
126+
echo "Done!"

Diff for: test/builder/test.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# This test can be run with the following command (from the root of this repo)
4+
# devcontainer features test --features builder --base-image mcr.microsoft.com/devcontainers/base:ubuntu .
5+
6+
set -e
7+
8+
# Import 'check' command
9+
source dev-container-features-test-lib
10+
11+
check "help" builder --help
12+
13+
reportResults

0 commit comments

Comments
 (0)