|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2018 Ville Koskela |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# For documentation please refer to: |
| 18 | +# https://github.com/KoskiLabs/jdk-wrapper/blob/master/README.md |
| 19 | + |
| 20 | +log_err() { |
| 21 | + l_prefix=$(date +'%H:%M:%S') |
| 22 | + printf "[%s] %s\n" "${l_prefix}" "$@" 1>&2; |
| 23 | +} |
| 24 | + |
| 25 | +log_out() { |
| 26 | + if [ -n "${JDKW_VERBOSE}" ]; then |
| 27 | + l_prefix=$(date +'%H:%M:%S') |
| 28 | + printf "[%s] %s\n" "${l_prefix}" "$@" |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +safe_command() { |
| 33 | + l_command=$1 |
| 34 | + log_out "${l_command}"; |
| 35 | + eval $1 |
| 36 | + l_result=$? |
| 37 | + if [ "${l_result}" -ne "0" ]; then |
| 38 | + log_err "ERROR: ${l_command} failed with ${l_result}" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | +} |
| 42 | + |
| 43 | +download() { |
| 44 | + file="$1" |
| 45 | + if [ ! -f "${JDKW_PATH}/${file}" ]; then |
| 46 | + jdkw_url="${JDKW_URI}/${file}" |
| 47 | + log_out "Downloading ${file} from ${jdkw_url}" |
| 48 | + safe_command "curl ${CURL_OPTIONS} -f -k -L -o \"${JDKW_PATH}/${file}\" \"${jdkw_url}\"" |
| 49 | + safe_command "chmod +x \"${JDKW_PATH}/${file}\"" |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +# Default curl options |
| 54 | +CURL_OPTIONS="" |
| 55 | + |
| 56 | +# Load properties file in home directory |
| 57 | +if [ -f "${HOME}/.jdkw" ]; then |
| 58 | + . "${HOME}/.jdkw" |
| 59 | +fi |
| 60 | + |
| 61 | +# Load properties file in working directory |
| 62 | +if [ -f ".jdkw" ]; then |
| 63 | + . "./.jdkw" |
| 64 | +fi |
| 65 | + |
| 66 | +# Process command line arguments |
| 67 | +for ARG in "$@"; do |
| 68 | + JDKW_ARG=$(echo "${ARG}" | grep 'JDKW_.*') |
| 69 | + if [ -n "${JDKW_ARG}" ]; then |
| 70 | + eval ${ARG} |
| 71 | + else |
| 72 | + break |
| 73 | + fi |
| 74 | +done |
| 75 | + |
| 76 | +# Globals |
| 77 | +JDKW_BASE_URI="https://github.com/KoskiLabs/jdk-wrapper" |
| 78 | +JDKW_IMPL="jdkw-impl.sh" |
| 79 | +JDKW_WRAPPER="jdk-wrapper.sh" |
| 80 | + |
| 81 | +# Process configuration |
| 82 | +if [ -z "${JDKW_RELEASE}" ]; then |
| 83 | + JDKW_RELEASE="latest" |
| 84 | + log_out "Defaulted to version ${JDKW_RELEASE}" |
| 85 | +fi |
| 86 | +if [ -z "${JDKW_TARGET}" ]; then |
| 87 | + JDKW_TARGET="${HOME}/.jdk" |
| 88 | + log_out "Defaulted to target ${JDKW_TARGET}" |
| 89 | +fi |
| 90 | +if [ -z "${JDKW_VERBOSE}" ]; then |
| 91 | + CURL_OPTIONS="${CURL_OPTIONS} --silent" |
| 92 | +fi |
| 93 | + |
| 94 | +# Resolve latest version |
| 95 | +if [ "${JDKW_RELEASE}" = "latest" ]; then |
| 96 | + JDKW_RELEASE=$(curl ${CURL_OPTIONS} -f -k -L -H 'Accept: application/json' "${JDKW_BASE_URI}/releases/latest" | sed -e 's/.*"tag_name":"\([^"]*\)".*/\1/') |
| 97 | + log_out "Resolved latest version to ${JDKW_RELEASE}" |
| 98 | +fi |
| 99 | + |
| 100 | +# Define source and target |
| 101 | +JDKW_URI="${JDKW_BASE_URI}/releases/download/${JDKW_RELEASE}" |
| 102 | +JDKW_PATH="${JDKW_TARGET}/jdkw/${JDKW_RELEASE}" |
| 103 | + |
| 104 | +# Ensure target directory exists |
| 105 | +if [ ! -d "${JDKW_PATH}" ]; then |
| 106 | + log_out "Creating target directory ${JDKW_PATH}" |
| 107 | + safe_command "mkdir -p \"${JDKW_PATH}\"" |
| 108 | +fi |
| 109 | + |
| 110 | +# Download the jdk wrapper version |
| 111 | +download "${JDKW_IMPL}" |
| 112 | +download "${JDKW_WRAPPER}" |
| 113 | + |
| 114 | +# Check whether this wrapper is the one specified for this version |
| 115 | +jdkw_download="${JDKW_PATH}/${JDKW_WRAPPER}" |
| 116 | +jdkw_current="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)/$(basename "$0")" |
| 117 | +if [ "$(cat "${jdkw_download}" | sha1sum )" != "$(cat "${jdkw_current}" | sha1sum)" ]; then |
| 118 | + printf "\e[0;31m[WARNING]\e[0m Your jdk-wrapper.sh file does not match the one in your JDKW_RELEASE.\n" |
| 119 | + printf "\e[0;32mUpdate your jdk-wrapper.sh to match by running:\e[0m\n" |
| 120 | + printf "cp \"%s\" \"%s\"\n" "${jdkw_download}" "${jdkw_current}" |
| 121 | +fi |
| 122 | + |
| 123 | +# Execute the provided command |
| 124 | +${JDKW_PATH}/${JDKW_IMPL} $@ |
| 125 | +exit $? |
0 commit comments