lib.sh 3.4 KB
Newer Older
A
Anmol Sethi 已提交
1
#!/usr/bin/env bash
2
set -euo pipefail
A
Anmol Sethi 已提交
3

4
pushd() {
5
  builtin pushd "$@" >/dev/null
6 7 8
}

popd() {
9
  builtin popd >/dev/null
10 11 12 13 14 15
}

pkg_json_version() {
  jq -r .version package.json
}

A
Anmol Sethi 已提交
16 17 18 19
vscode_version() {
  jq -r .version lib/vscode/package.json
}

20 21 22 23 24 25 26 27 28 29 30
os() {
  local os
  os=$(uname | tr '[:upper:]' '[:lower:]')
  if [[ $os == "linux" ]]; then
    # Alpine's ldd doesn't have a version flag but if you use an invalid flag
    # (like --version) it outputs the version to stderr and exits with 1.
    local ldd_output
    ldd_output=$(ldd --version 2>&1 || true)
    if echo "$ldd_output" | grep -iq musl; then
      os="alpine"
    fi
A
Anmol Sethi 已提交
31 32
  elif [[ $os == "darwin" ]]; then
    os="macos"
A
Anmol Sethi 已提交
33
  fi
34 35 36 37 38 39 40 41
  echo "$os"
}

arch() {
  case "$(uname -m)" in
  aarch64)
    echo arm64
    ;;
A
Anmol Sethi 已提交
42
  x86_64 | amd64)
43 44 45 46 47 48 49
    echo amd64
    ;;
  *)
    echo "unknown architecture $(uname -a)"
    exit 1
    ;;
  esac
A
Anmol Sethi 已提交
50
}
A
Anmol Sethi 已提交
51 52 53 54 55

# Grabs the most recent ci.yaml github workflow run that was successful and triggered from the same commit being pushd.
# This will contain the artifacts we want.
# https://developer.github.com/v3/actions/workflow-runs/#list-workflow-runs
get_artifacts_url() {
56
  local artifacts_url
57
  local workflow_runs_url="repos/:owner/:repo/actions/workflows/ci.yaml/runs?status=success&event=pull_request"
58
  # For releases, we look for run based on the branch name v$code_server_version
59
  # example: v3.9.3
60
  local version_branch="v$VERSION"
61
  artifacts_url=$(gh api "$workflow_runs_url" | jq -r ".workflow_runs[] | select(.head_branch == \"$version_branch\") | .artifacts_url" | head -n 1)
62 63
  if [[ -z "$artifacts_url" ]]; then
    echo >&2 "ERROR: artifacts_url came back empty"
64
    echo >&2 "We looked for a successful run triggered by a pull_request with for code-server version: $code_server_version and a branch named $version_branch"
65
    echo >&2 "URL used for gh API call: $workflow_runs_url"
66 67 68 69
    exit 1
  fi

  echo "$artifacts_url"
A
Anmol Sethi 已提交
70 71 72 73 74 75
}

# Grabs the artifact's download url.
# https://developer.github.com/v3/actions/artifacts/#list-workflow-run-artifacts
get_artifact_url() {
  local artifact_name="$1"
76
  gh api "$(get_artifacts_url)" | jq -r ".artifacts[] | select(.name == \"$artifact_name\") | .archive_download_url" | head -n 1
A
Anmol Sethi 已提交
77 78 79 80 81 82 83 84 85 86
}

# Uses the above two functions to download a artifact into a directory.
download_artifact() {
  local artifact_name="$1"
  local dst="$2"

  local tmp_file
  tmp_file="$(mktemp)"

87
  gh api "$(get_artifact_url "$artifact_name")" >"$tmp_file"
88
  unzip -q -o "$tmp_file" -d "$dst"
A
Anmol Sethi 已提交
89 90
  rm "$tmp_file"
}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

rsync() {
  command rsync -a --del "$@"
}

VERSION="$(pkg_json_version)"
export VERSION
ARCH="$(arch)"
export ARCH
OS=$(os)
export OS

# RELEASE_PATH is the destination directory for the release from the root.
# Defaults to release
RELEASE_PATH="${RELEASE_PATH-release}"
106

A
Asher 已提交
107 108 109 110 111 112 113
# VS Code bundles some modules into an asar which is an archive format that
# works like tar. It then seems to get unpacked into node_modules.asar.
#
# I don't know why they do this but all the dependencies they bundle already
# exist in node_modules so just symlink it. We have to do this since not only VS
# Code itself but also extensions will look specifically in this directory for
# files (like the ripgrep binary or the oniguruma wasm).
114
symlink_asar() {
115
  if [ ! -L node_modules.asar ]; then
116 117 118 119 120 121 122 123 124
    if [ "${WINDIR-}" ]; then
      # mklink takes the link name first.
      mklink /J node_modules.asar node_modules
    else
      # ln takes the link name second.
      ln -s node_modules node_modules.asar
    fi
  fi
}