|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ------------------------------------------------------------ |
| 4 | +# Copyright The KCL Authors |
| 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 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# Reference: https://github.com/dapr/cli/tree/master/install |
| 16 | +# ------------------------------------------------------------ |
| 17 | + |
| 18 | +# sudo is required to copy binary to KCL_INSTALL_DIR for linux |
| 19 | +: ${USE_SUDO:="false"} |
| 20 | + |
| 21 | +# Http request CLI |
| 22 | +KCL_HTTP_REQUEST_CLI=curl |
| 23 | + |
| 24 | +# GitHub Organization and repo name to download release |
| 25 | +GITHUB_ORG=kcl-lang |
| 26 | +GITHUB_REPO=kcl |
| 27 | + |
| 28 | +# KCL languge server filename |
| 29 | +CLI_FILENAME=kcl-language-server |
| 30 | + |
| 31 | +# --- helper functions for logs --- |
| 32 | +info() { |
| 33 | + local action="$1" |
| 34 | + local details="$2" |
| 35 | + command printf '\033[1;32m%12s\033[0m %s\n' "$action" "$details" 1>&2 |
| 36 | +} |
| 37 | + |
| 38 | +warn() { |
| 39 | + command printf '\033[1;33mWarn\033[0m: %s\n' "$1" 1>&2 |
| 40 | +} |
| 41 | + |
| 42 | +error() { |
| 43 | + command printf '\033[1;31mError\033[0m: %s\n' "$1" 1>&2 |
| 44 | +} |
| 45 | + |
| 46 | +request() { |
| 47 | + command printf '\033[1m%s\033[0m\n' "$1" 1>&2 |
| 48 | +} |
| 49 | + |
| 50 | +eprintf() { |
| 51 | + command printf '%s\n' "$1" 1>&2 |
| 52 | +} |
| 53 | + |
| 54 | +bold() { |
| 55 | + command printf '\033[1m%s\033[0m' "$1" |
| 56 | +} |
| 57 | + |
| 58 | +# If file exists, echo it |
| 59 | +echo_fexists() { |
| 60 | + [ -f "$1" ] && echo "$1" |
| 61 | +} |
| 62 | + |
| 63 | +runAsRoot() { |
| 64 | + local CMD="$*" |
| 65 | + |
| 66 | + if [ $EUID -ne 0 -a $USE_SUDO = "true" ]; then |
| 67 | + CMD="sudo $CMD" |
| 68 | + fi |
| 69 | + |
| 70 | + $CMD |
| 71 | +} |
| 72 | + |
| 73 | +checkHttpRequestCLI() { |
| 74 | + if type "curl" > /dev/null; then |
| 75 | + KCL_HTTP_REQUEST_CLI=curl |
| 76 | + elif type "wget" > /dev/null; then |
| 77 | + KCL_HTTP_REQUEST_CLI=wget |
| 78 | + else |
| 79 | + error "Either curl or wget is required" |
| 80 | + exit 1 |
| 81 | + fi |
| 82 | +} |
| 83 | + |
| 84 | +getLatestRelease() { |
| 85 | + local KCLReleaseUrl="https://api.github.com/repos/${GITHUB_ORG}/${GITHUB_REPO}/releases" |
| 86 | + local latest_release="" |
| 87 | + |
| 88 | + if [ "$KCL_HTTP_REQUEST_CLI" == "curl" ]; then |
| 89 | + latest_release=$(curl -s $KCLReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') |
| 90 | + else |
| 91 | + latest_release=$(wget -q --header="Accept: application/json" -O - $KCLReleaseUrl | grep \"tag_name\" | grep -v rc | awk 'NR==1{print $2}' | sed -n 's/\"\(.*\)\",/\1/p') |
| 92 | + fi |
| 93 | + |
| 94 | + ret_val=$latest_release |
| 95 | +} |
| 96 | + |
| 97 | +downloadFile() { |
| 98 | + LATEST_RELEASE_TAG=$1 |
| 99 | + OS=$2 |
| 100 | + ARCH=$3 |
| 101 | + KCL_CLI_UNIX_ARTIFACT="kclvm-${LATEST_RELEASE_TAG}-${OS}-${ARCH}.tar.gz" |
| 102 | + KCL_CLI_WINDOWS_ARTIFACT="kclvm-${LATEST_RELEASE_TAG}-${OS}.zip" |
| 103 | + # Unix tar.gz artifact |
| 104 | + KCL_CLI_ARTIFACT=$KCL_CLI_UNIX_ARTIFACT |
| 105 | + # Windows zip artifact |
| 106 | + if [ "$OS" == "windows" ]; then |
| 107 | + KCL_CLI_ARTIFACT=$KCL_CLI_WINDOWS_ARTIFACT |
| 108 | + fi |
| 109 | + DOWNLOAD_BASE="https://github.com/${GITHUB_ORG}/${GITHUB_REPO}/releases/download" |
| 110 | + DOWNLOAD_URL="${DOWNLOAD_BASE}/${LATEST_RELEASE_TAG}/${KCL_CLI_ARTIFACT}" |
| 111 | + |
| 112 | + # Create the temp directory |
| 113 | + KCL_TMP_ROOT=$(mktemp -dt kcl-lsp-install-XXXXXX) |
| 114 | + ARTIFACT_TMP_FILE="$KCL_TMP_ROOT/$KCL_CLI_ARTIFACT" |
| 115 | + |
| 116 | + info "Downloading $DOWNLOAD_URL ..." |
| 117 | + if [ "$KCL_HTTP_REQUEST_CLI" == "curl" ]; then |
| 118 | + curl -SsL "$DOWNLOAD_URL" -o "$ARTIFACT_TMP_FILE" |
| 119 | + else |
| 120 | + wget -q -O "$ARTIFACT_TMP_FILE" "$DOWNLOAD_URL" |
| 121 | + fi |
| 122 | + |
| 123 | + if [ ! -f "$ARTIFACT_TMP_FILE" ]; then |
| 124 | + error "Failed to download $DOWNLOAD_URL ..." |
| 125 | + exit 1 |
| 126 | + else |
| 127 | + info "Scucessful to download $DOWNLOAD_URL" |
| 128 | + fi |
| 129 | + |
| 130 | + info "Build kcl language server artifact..." |
| 131 | + |
| 132 | + INSTALL_FOLDER="./kcl-lsp-${OS}-${ARCH}" |
| 133 | + mkdir -p "./bin/$INSTALL_FOLDER" |
| 134 | + |
| 135 | + tar xf $ARTIFACT_TMP_FILE -C $KCL_TMP_ROOT |
| 136 | + local tmp_kclvm_folder=$KCL_TMP_ROOT/kclvm |
| 137 | + |
| 138 | + if [ ! -f "$tmp_kclvm_folder/bin/kcl-language-server" ]; then |
| 139 | + error "Failed to unpack KCL language server executable." |
| 140 | + exit 1 |
| 141 | + fi |
| 142 | + |
| 143 | + # Copy kcl-languge-server in the temp folder into the target installation directory. |
| 144 | + info "Copy the kcl language server binary $tmp_kclvm_folder/bin/kcl-language-server into the target installation directory ./bin" |
| 145 | + cp -f $tmp_kclvm_folder/bin/kcl-language-server "./bin/$INSTALL_FOLDER" |
| 146 | + cd ./bin |
| 147 | + if [ "$OS" == "windows" ]; then |
| 148 | + TARBALL="./kcl-lsp-${LATEST_RELEASE_TAG}-${OS}-${ARCH}.zip" |
| 149 | + info "Zip $TARBALL..." |
| 150 | + zip -r $TARBALL $INSTALL_FOLDER |
| 151 | + else |
| 152 | + TARBALL="./kcl-lsp-${LATEST_RELEASE_TAG}-${OS}-${ARCH}.tar.gz" |
| 153 | + info "Tar $TARBALL..." |
| 154 | + tar -zcf $TARBALL $INSTALL_FOLDER |
| 155 | + fi |
| 156 | + cd .. |
| 157 | + info "Build kcl language server artifact successful!" |
| 158 | +} |
| 159 | + |
| 160 | +fail_trap() { |
| 161 | + result=$? |
| 162 | + if [ "$result" != "0" ]; then |
| 163 | + error "Failed to install KCL language server" |
| 164 | + info "For support, go to https://kcl-lang.io" |
| 165 | + fi |
| 166 | + cleanup |
| 167 | + exit $result |
| 168 | +} |
| 169 | + |
| 170 | +cleanup() { |
| 171 | + if [[ -d "${KCL_TMP_ROOT:-}" ]]; then |
| 172 | + rm -rf "$KCL_TMP_ROOT" |
| 173 | + fi |
| 174 | +} |
| 175 | + |
| 176 | +# ----------------------------------------------------------------------------- |
| 177 | +# main |
| 178 | +# ----------------------------------------------------------------------------- |
| 179 | +trap "fail_trap" EXIT |
| 180 | + |
| 181 | +checkHttpRequestCLI |
| 182 | + |
| 183 | +if [ -z "$1" ]; then |
| 184 | + echo "Getting the latest KCL language server ..." |
| 185 | + getLatestRelease |
| 186 | +else |
| 187 | + ret_val=v$1 |
| 188 | +fi |
| 189 | + |
| 190 | +info "Find the latest KCL language server version $ret_val" |
| 191 | + |
| 192 | +downloadFile $ret_val "darwin" "amd64" |
| 193 | +downloadFile $ret_val "darwin" "arm64" |
| 194 | +downloadFile $ret_val "linux" "amd64" |
| 195 | +downloadFile $ret_val "windows" "amd64" |
| 196 | +cleanup |
0 commit comments