|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +repo_url="${RPACK_REPO_URL:-https://github.com/ppotepa/rpack.git}" |
| 5 | +branch="${RPACK_BRANCH:-main}" |
| 6 | +source_dir="${RPACK_SOURCE_DIR:-$HOME/.local/share/rpack/source}" |
| 7 | +publish_dir="${RPACK_PUBLISH_DIR:-$HOME/.local/share/rpack/publish}" |
| 8 | +bin_dir="${RPACK_BIN_DIR:-$HOME/.local/bin}" |
| 9 | +dotnet_dir="${RPACK_DOTNET_DIR:-$HOME/.dotnet}" |
| 10 | +dotnet_channel="${RPACK_DOTNET_CHANNEL:-10.0}" |
| 11 | + |
| 12 | +log() { |
| 13 | + printf 'rpack installer: %s\n' "$*" |
| 14 | +} |
| 15 | + |
| 16 | +fail() { |
| 17 | + printf 'rpack installer: %s\n' "$*" >&2 |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +has_command() { |
| 22 | + command -v "$1" >/dev/null 2>&1 |
| 23 | +} |
| 24 | + |
| 25 | +download() { |
| 26 | + local url="$1" |
| 27 | + local output="$2" |
| 28 | + |
| 29 | + if has_command curl; then |
| 30 | + curl -fsSL "$url" -o "$output" |
| 31 | + return |
| 32 | + fi |
| 33 | + |
| 34 | + if has_command wget; then |
| 35 | + wget -qO "$output" "$url" |
| 36 | + return |
| 37 | + fi |
| 38 | + |
| 39 | + fail "curl or wget is required." |
| 40 | +} |
| 41 | + |
| 42 | +ensure_git() { |
| 43 | + if ! has_command git; then |
| 44 | + fail "git is required. Install git with your distribution package manager and rerun this script." |
| 45 | + fi |
| 46 | +} |
| 47 | + |
| 48 | +dotnet_has_required_sdk() { |
| 49 | + has_command dotnet && dotnet --list-sdks 2>/dev/null | grep -Eq "^${dotnet_channel}[.]" |
| 50 | +} |
| 51 | + |
| 52 | +ensure_dotnet() { |
| 53 | + if [ -x "$dotnet_dir/dotnet" ]; then |
| 54 | + export DOTNET_ROOT="$dotnet_dir" |
| 55 | + export PATH="$dotnet_dir:$PATH" |
| 56 | + fi |
| 57 | + |
| 58 | + if dotnet_has_required_sdk; then |
| 59 | + return |
| 60 | + fi |
| 61 | + |
| 62 | + log ".NET SDK ${dotnet_channel} not found; installing user-local SDK to ${dotnet_dir}" |
| 63 | + mkdir -p "$dotnet_dir" |
| 64 | + |
| 65 | + local installer |
| 66 | + installer="$(mktemp)" |
| 67 | + download "https://dot.net/v1/dotnet-install.sh" "$installer" |
| 68 | + bash "$installer" --channel "$dotnet_channel" --install-dir "$dotnet_dir" |
| 69 | + rm -f "$installer" |
| 70 | + |
| 71 | + export DOTNET_ROOT="$dotnet_dir" |
| 72 | + export PATH="$dotnet_dir:$PATH" |
| 73 | + |
| 74 | + dotnet_has_required_sdk || fail "failed to install .NET SDK ${dotnet_channel}." |
| 75 | +} |
| 76 | + |
| 77 | +sync_source() { |
| 78 | + mkdir -p "$(dirname "$source_dir")" |
| 79 | + |
| 80 | + if [ -d "$source_dir/.git" ]; then |
| 81 | + log "updating ${source_dir}" |
| 82 | + git -C "$source_dir" fetch origin "$branch" |
| 83 | + git -C "$source_dir" checkout "$branch" |
| 84 | + git -C "$source_dir" pull --ff-only origin "$branch" |
| 85 | + return |
| 86 | + fi |
| 87 | + |
| 88 | + if [ -e "$source_dir" ]; then |
| 89 | + fail "${source_dir} exists but is not a git repository. Set RPACK_SOURCE_DIR or remove that directory." |
| 90 | + fi |
| 91 | + |
| 92 | + log "cloning ${repo_url} into ${source_dir}" |
| 93 | + git clone --branch "$branch" "$repo_url" "$source_dir" |
| 94 | +} |
| 95 | + |
| 96 | +publish_rpack() { |
| 97 | + log "publishing rpack CLI" |
| 98 | + rm -rf "$publish_dir" |
| 99 | + mkdir -p "$publish_dir" |
| 100 | + |
| 101 | + dotnet publish "$source_dir/src/Rpack.Cli/Rpack.Cli.csproj" \ |
| 102 | + --configuration Release \ |
| 103 | + --self-contained false \ |
| 104 | + --output "$publish_dir" |
| 105 | +} |
| 106 | + |
| 107 | +install_launcher() { |
| 108 | + mkdir -p "$bin_dir" |
| 109 | + |
| 110 | + cat > "$bin_dir/rpack" <<EOF |
| 111 | +#!/usr/bin/env bash |
| 112 | +if [ -x "${dotnet_dir}/dotnet" ]; then |
| 113 | + export DOTNET_ROOT="${dotnet_dir}" |
| 114 | + export PATH="${dotnet_dir}:\$PATH" |
| 115 | +fi |
| 116 | +exec dotnet "${publish_dir}/rpack.dll" "\$@" |
| 117 | +EOF |
| 118 | + |
| 119 | + chmod +x "$bin_dir/rpack" |
| 120 | +} |
| 121 | + |
| 122 | +main() { |
| 123 | + ensure_git |
| 124 | + ensure_dotnet |
| 125 | + sync_source |
| 126 | + publish_rpack |
| 127 | + install_launcher |
| 128 | + |
| 129 | + log "installed $("$bin_dir/rpack" version 2>/dev/null || printf 'rpack')" |
| 130 | + log "binary: ${bin_dir}/rpack" |
| 131 | + |
| 132 | + case ":$PATH:" in |
| 133 | + *":$bin_dir:"*) ;; |
| 134 | + *) log "add ${bin_dir} to PATH if 'rpack' is not found in a new shell." ;; |
| 135 | + esac |
| 136 | +} |
| 137 | + |
| 138 | +main "$@" |
0 commit comments