Repository navigation
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·116 lines (104 loc) · 4.19 KB
/
Copy pathbootstrap
File metadata and controls
executable file
·116 lines (104 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
set -euo pipefail
# ===========================
# Bootstrap the Python script
# ===========================
readonly GH_USER="${GITHUB_USER:-diegoferigo}"
readonly GH_REPO="${GITHUB_REPO:-dotfiles}"
readonly GH_BRANCH="${GITHUB_BRANCH:-main}"
readonly GH_REPO_RAW="https://raw.githubusercontent.com/${GH_USER}/${GH_REPO}/${GH_BRANCH}"
# Determine the directory of this script. When piped from stdin (curl | bash)
# BASH_SOURCE[0] is unset, so guard it under 'set -u' and fall back to /dev/fd
# (which fails the local-checkout test below and triggers the download path).
source="${BASH_SOURCE[0]:-}"
if [[ -n "${source}" ]]; then
script_dir="$(cd "$(dirname "${source}")" && pwd)"
else
script_dir="/dev/fd"
fi
readonly script_dir
# Create a temporary directory for downloaded files, and ensure it's cleaned up on exit.
tmpdir=""
function cleanup() {
[[ -n "${tmpdir}" && -d "${tmpdir}" ]] && rm -rf "${tmpdir}"
}
trap cleanup EXIT
# Use a local dotfiles script if available (running from a cloned repo),
# otherwise download it from GitHub.
readonly local_bootstrap="${script_dir}/.local/bin/dotfiles"
if [[ -f "${local_bootstrap}" && -d "${script_dir}/.git" && "${script_dir}" != "/dev/fd" ]]; then
readonly bootstrap="${local_bootstrap}"
readonly dotfiles_repo_uri="file://${script_dir}"
echo "==> Using local .local/bin/dotfiles"
# Codespaces clones the selected dotfiles repo and runs this script from it,
# so the checkout's own origin is the repo to follow, not the local path.
if [[ "${CODESPACES:-}" == "true" ]]; then
origin_uri="$(git -C "${script_dir}" config --get remote.origin.url || true)"
if [[ -n "${origin_uri}" ]]; then
set -- --origin-uri "${origin_uri}" "$@"
fi
fi
else
echo "==> Downloading dotfiles script from GitHub..."
tmpdir="$(mktemp -d)"
curl -fsSL "${GH_REPO_RAW}/.local/bin/dotfiles" >"${tmpdir}/dotfiles"
chmod +x "${tmpdir}/dotfiles"
readonly bootstrap="${tmpdir}/dotfiles"
readonly dotfiles_repo_uri="https://github.com/${GH_USER}/${GH_REPO}.git"
# An explicit GITHUB_BRANCH also selects the branch to clone.
if [[ -n "${GITHUB_BRANCH:-}" ]]; then
set -- --branch "${GITHUB_BRANCH}" "$@"
fi
fi
# ========================
# Bootstrap pixi if needed
# ========================
# If the PATH is not already set to include pixi, add it temporarily.
if ! command -v pixi &>/dev/null; then
if [[ -x $HOME/.pixi/bin/pixi ]]; then
export PATH="$HOME/.pixi/bin:$PATH"
fi
fi
# Update pixi if explicitly requested (PIXI_UPDATE=1).
if [[ "${PIXI_UPDATE:-0}" == "1" ]] && command -v pixi &>/dev/null; then
pixi self-update
fi
# With --shell nothing may be written outside the cache directory, so pixi is
# installed there and the installer must not edit any shell profile.
pixi_home="$HOME/.pixi"
for arg in "$@"; do
if [[ "${arg}" == "--shell" ]]; then
shell_base="${XDG_CACHE_HOME:-$HOME/.cache}/diegoferigo-dotfiles"
pixi_home="${shell_base}/pixi-home"
export PIXI_HOME="${pixi_home}" PIXI_NO_PATH_UPDATE=1
export RATTLER_CACHE_DIR="${shell_base}/cache/rattler" PIXI_CACHE_DIR="${shell_base}/cache/pixi"
if [[ -x "${pixi_home}/bin/pixi" ]]; then
export PATH="${pixi_home}/bin:$PATH"
fi
fi
done
# Install pixi if not already installed.
if ! command -v pixi &>/dev/null; then
echo "Pixi not found. Installing pixi..."
if command -v curl &>/dev/null; then
curl -fsSL https://pixi.sh/install.sh | bash
elif command -v wget &>/dev/null; then
wget -qO- https://pixi.sh/install.sh | bash
else
echo "ERROR: Neither curl nor wget found. Cannot install pixi."
exit 1
fi
export PATH="${pixi_home}/bin:$PATH"
if ! command -v pixi &>/dev/null; then
echo "ERROR: Could not find pixi after installation, something failed."
exit 1
fi
echo "... done!"
fi
# ==========================
# Run bootstrap.py with pixi
# ==========================
# Run the dotfiles script.
# This file has a shebang that points to pixi.
# It creates a temporary pixi environment with the required dependencies to run the script.
exec "$bootstrap" --repo-uri "$dotfiles_repo_uri" "$@"