aboutsummaryrefslogtreecommitdiff
path: root/src/java
diff options
context:
space:
mode:
authorJosh Spicer <josh@joshspicer.com>2022-05-12 23:10:51 +0300
committerGitHub <noreply@github.com>2022-05-12 23:10:51 +0300
commit3a277ad757dbc51a43a5dd452d030c67a49d49f1 (patch)
tree7d4f27493f946d8792b2ff5b6383258cb9f03ce0 /src/java
parent2707a37ca0d4a45c7487f856bf95f8c5e10bce94 (diff)
restructure and update cli (#11)
* restructure and update cli * typo
Diffstat (limited to 'src/java')
-rw-r--r--src/java/feature.json26
-rw-r--r--src/java/install.sh145
-rw-r--r--src/java/wrapper.sh52
3 files changed, 223 insertions, 0 deletions
diff --git a/src/java/feature.json b/src/java/feature.json
new file mode 100644
index 0000000..263c2c8
--- /dev/null
+++ b/src/java/feature.json
@@ -0,0 +1,26 @@
+{
+ "id": "java",
+ "name": "Java (via SDKMAN!)",
+ "options": {
+ "version": {
+ "type": "string",
+ "proposals": ["lts", "latest", "17", "11", "8"],
+ "default": "lts",
+ "description": "Select or enter a Java version to install"
+ }
+ },
+ "buildArg": "_VSC_INSTALL_JAVA",
+ "extensions": ["vscjava.vscode-java-pack"],
+ "containerEnv": {
+ "SDKMAN_DIR": "/usr/local/sdkman",
+ "PATH": "${SDKMAN_DIR}/bin:${SDKMAN_DIR}/candidates/java/current/bin:${PATH}"
+ },
+ "settings": {
+ "java.home": "/extension-java-home",
+ "java.import.gradle.java.home": "/usr/local/sdkman/candidates/java/current"
+ },
+ "install": {
+ "app": "",
+ "file": "install.sh"
+ }
+}
diff --git a/src/java/install.sh b/src/java/install.sh
new file mode 100644
index 0000000..f5880cd
--- /dev/null
+++ b/src/java/install.sh
@@ -0,0 +1,145 @@
+#!/usr/bin/env bash
+#-------------------------------------------------------------------------------------------------------------
+# Copyright (c) Microsoft Corporation. All rights reserved.
+# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
+#-------------------------------------------------------------------------------------------------------------
+#
+# Docs: https://github.com/microsoft/vscode-dev-containers/blob/main/script-library/docs/java.md
+# Maintainer: The VS Code and Codespaces Teams
+#
+# Syntax: ./java-debian.sh [JDK version] [SDKMAN_DIR] [non-root user] [Add to rc files flag]
+
+JAVA_VERSION=${1:-"lts"}
+export SDKMAN_DIR=${2:-"/usr/local/sdkman"}
+USERNAME=${3:-"automatic"}
+UPDATE_RC=${4:-"true"}
+
+set -e
+
+if [ "$(id -u)" -ne 0 ]; then
+ echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
+ exit 1
+fi
+
+# Ensure that login shells get the correct path if the user updated the PATH using ENV.
+rm -f /etc/profile.d/00-restore-env.sh
+echo "export PATH=${PATH//$(sh -lc 'echo $PATH')/\$PATH}" > /etc/profile.d/00-restore-env.sh
+chmod +x /etc/profile.d/00-restore-env.sh
+
+# Determine the appropriate non-root user
+if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
+ USERNAME=""
+ POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
+ for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
+ if id -u ${CURRENT_USER} > /dev/null 2>&1; then
+ USERNAME=${CURRENT_USER}
+ break
+ fi
+ done
+ if [ "${USERNAME}" = "" ]; then
+ USERNAME=root
+ fi
+elif [ "${USERNAME}" = "none" ] || ! id -u ${USERNAME} > /dev/null 2>&1; then
+ USERNAME=root
+fi
+
+updaterc() {
+ if [ "${UPDATE_RC}" = "true" ]; then
+ echo "Updating /etc/bash.bashrc and /etc/zsh/zshrc..."
+ if [[ "$(cat /etc/bash.bashrc)" != *"$1"* ]]; then
+ echo -e "$1" >> /etc/bash.bashrc
+ fi
+ if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"$1"* ]]; then
+ echo -e "$1" >> /etc/zsh/zshrc
+ fi
+ fi
+}
+
+# Function to run apt-get if needed
+apt_get_update_if_needed()
+{
+ if [ ! -d "/var/lib/apt/lists" ] || [ "$(ls /var/lib/apt/lists/ | wc -l)" = "0" ]; then
+ echo "Running apt-get update..."
+ apt-get update
+ else
+ echo "Skipping apt-get update."
+ fi
+}
+
+# Checks if packages are installed and installs them if not
+check_packages() {
+ if ! dpkg -s "$@" > /dev/null 2>&1; then
+ apt_get_update_if_needed
+ apt-get -y install --no-install-recommends "$@"
+ fi
+}
+
+# Use SDKMAN to install something using a partial version match
+sdk_install() {
+ local install_type=$1
+ local requested_version=$2
+ local prefix=$3
+ local suffix="${4:-"\\s*"}"
+ local full_version_check=${5:-".*-[a-z]+"}
+ if [ "${requested_version}" = "none" ]; then return; fi
+ # Blank will install latest stable version SDKMAN has
+ if [ "${requested_version}" = "lts" ] || [ "${requested_version}" = "default" ]; then
+ requested_version=""
+ elif echo "${requested_version}" | grep -oE "${full_version_check}" > /dev/null 2>&1; then
+ echo "${requested_version}"
+ else
+ local regex="${prefix}\\K[0-9]+\\.[0-9]+\\.[0-9]+${suffix}"
+ local version_list="$(. ${SDKMAN_DIR}/bin/sdkman-init.sh && sdk list ${install_type} 2>&1 | grep -oP "${regex}" | tr -d ' ' | sort -rV)"
+ if [ "${requested_version}" = "latest" ] || [ "${requested_version}" = "current" ]; then
+ requested_version="$(echo "${version_list}" | head -n 1)"
+ else
+ set +e
+ requested_version="$(echo "${version_list}" | grep -E -m 1 "^${requested_version//./\\.}([\\.\\s]|$)")"
+ set -e
+ fi
+ if [ -z "${requested_version}" ] || ! echo "${version_list}" | grep "^${requested_version//./\\.}$" > /dev/null 2>&1; then
+ echo -e "Version $2 not found. Available versions:\n${version_list}" >&2
+ exit 1
+ fi
+ fi
+ su ${USERNAME} -c "umask 0002 && . ${SDKMAN_DIR}/bin/sdkman-init.sh && sdk install ${install_type} ${requested_version} && sdk flush archives && sdk flush temp"
+}
+
+export DEBIAN_FRONTEND=noninteractive
+
+architecture="$(uname -m)"
+if [ "${architecture}" != "amd64" ] && [ "${architecture}" != "x86_64" ] && [ "${architecture}" != "arm64" ] && [ "${architecture}" != "aarch64" ]; then
+ echo "(!) Architecture $architecture unsupported"
+ exit 1
+fi
+
+# Install dependencies
+check_packages curl ca-certificates zip unzip sed
+
+# Install sdkman if not installed
+if [ ! -d "${SDKMAN_DIR}" ]; then
+ # Create sdkman group, dir, and set sticky bit
+ if ! cat /etc/group | grep -e "^sdkman:" > /dev/null 2>&1; then
+ groupadd -r sdkman
+ fi
+ usermod -a -G sdkman ${USERNAME}
+ umask 0002
+ # Install SDKMAN
+ curl -sSL "https://get.sdkman.io?rcupdate=false" | bash
+ chown -R :sdkman ${SDKMAN_DIR}
+ find ${SDKMAN_DIR} -type d | xargs -d '\n' chmod g+s
+ # Add sourcing of sdkman into bashrc/zshrc files (unless disabled)
+ updaterc "export SDKMAN_DIR=${SDKMAN_DIR}\n. \${SDKMAN_DIR}/bin/sdkman-init.sh"
+fi
+
+# Use Microsoft JDK for everything but JDK 8
+jdk_distro="ms"
+if echo "${JAVA_VERSION}" | grep -E '^8([\s\.]|$)' > /dev/null 2>&1; then
+ jdk_distro="tem"
+fi
+if [ "${JAVA_VERSION}" = "lts" ]; then
+ JAVA_VERSION="17"
+fi
+sdk_install java ${JAVA_VERSION} "\\s*" "(\\.[a-z0-9]+)*-${jdk_distro}\\s*" ".*-[a-z]+$"
+
+echo "Done!" \ No newline at end of file
diff --git a/src/java/wrapper.sh b/src/java/wrapper.sh
new file mode 100644
index 0000000..4d2aef3
--- /dev/null
+++ b/src/java/wrapper.sh
@@ -0,0 +1,52 @@
+#!/usr/bin/env bash
+# Wrapper function that also installs JDK 11 if JDK 8 is selected since this is required for the Java extension
+
+set -e
+
+JAVA_VERSION=${1:-"default"}
+SDKMAN_DIR=${2:-"/usr/local/sdkman"}
+USERNAME=${3:-"automatic"}
+UPDATE_RC=${4:-"true"}
+ADDITIONAL_JAVA_VERSION=11
+
+cd "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+chmod +x install.sh
+
+is_jdk_8="false"
+if echo "${JAVA_VERSION}" | grep -E '^8([\s\.]|$)' > /dev/null 2>&1; then
+ is_jdk_8="true"
+fi
+
+# If the user selected JDK 8, install the JDK 11 as well since this is needed by the Java extension
+if [ "${is_jdk_8}" = "true" ]; then
+ echo "(*) Installing JDK ${ADDITIONAL_JAVA_VERSION} as Java VS Code extension requires a recent JDK..."
+ ./install.sh "${ADDITIONAL_JAVA_VERSION}" "${SDKMAN_DIR}" "${USERNAME}" "${UPDATE_RC}"
+ jdk_11_folder="$(ls --format=single-column ${SDKMAN_DIR}/candidates/java | grep -oE -m 1 '11\..+')"
+ ln -s "${SDKMAN_DIR}/candidates/java/${jdk_11_folder}" /extension-java-home
+
+ # Determine the appropriate non-root user
+ username=""
+ possible_users=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
+ for current_user in ${possible_users[@]}; do
+ if id -u ${current_user} > /dev/null 2>&1; then
+ username=${current_user}
+ break
+ fi
+ done
+ if [ "${username}" = "" ]; then
+ username=root
+ fi
+else
+ ln -s ${SDKMAN_DIR}/candidates/java/current /extension-java-home
+fi
+
+echo "(*) Installing JDK ${JAVA_VERSION}..."
+./install.sh "${JAVA_VERSION}" "${SDKMAN_DIR}" "${USERNAME}" "${UPDATE_RC}"
+if [ "${is_jdk_8}" = "true" ]; then
+ # Set current and default version to last SDK installed
+ jdk_full_version="$(ls --format=single-column "${SDKMAN_DIR}/candidates/java" | sort -rV | grep -oE -m 1 "${JAVA_VERSION}\\..+" )"
+ echo "(*) Setting default JDK to ${jdk_full_version}..."
+ . ${SDKMAN_DIR}/bin/sdkman-init.sh
+ sdk use java "${jdk_full_version}"
+ sdk default java "${jdk_full_version}"
+fi \ No newline at end of file