-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvulcan.sh
More file actions
executable file
·691 lines (586 loc) · 15.3 KB
/
vulcan.sh
File metadata and controls
executable file
·691 lines (586 loc) · 15.3 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env bash
#
# Run this script to install all dependencies and configurations. If you wish to
# perform only specific task or tasks pass them as arguments, space-separated.
#
# ./vulcan.sh [command] [theme] ...
#
# For example,
#
# ./vulcan.sh linking repositories packages
#
# or ./vulcan.sh upgrade -SSH -Linking -Repositories -Emacs -git
#
# Fast failure
#
set -e
#
# Get the OS info
#
# OS detection
KERNEL_NAME=$(uname -s | tr '[:upper:]' '[:lower:]')
[[ "$OSTYPE" =~ ^darwin ]] 2>/dev/null && KERNEL_RELEASE="darwin"
[[ "$(cat /etc/issue 2>/dev/null)" =~ Ubuntu ]] && KERNEL_RELEASE="ubuntu"
[[ "$(cat /etc/redhat-release 2>/dev/null)" =~ "Red Hat" ]] && KERNEL_RELEASE="redhat"
OS_NAME="unknown"
OS_VERSION="unknown"
case $KERNEL_NAME in
darwin)
OS_NAME=macos
OS_VERSION=$(sw_vers -productVersion)
;;
linux)
case $(echo $KERNEL_RELEASE) in
*arch* | *coreos*)
OS_NAME="arch"
;;
ubuntu)
OS_NAME="ubuntu"
OS_VERSION="$(sed 's/Ubuntu \([0-9]\+\.[0-9]\+\.[0-9]\+\) .*/\1/' /etc/issue)"
alias gstat=stat
;;
redhat | ol)
OS_NAME="redhat"
;;
android)
OS_NAME="android"
OS_VERSION=$TERMUX_VERSION
;;
esac
;;
*) ;;
esac
#
# Setup USER
#
if [ -z "$USER" ]; then
USER=$(whoami)
fi
#
# Setup PATH
#
export PATH=$HOME/.local/bin:$PATH
target=$HOME/.config
if [[ -d "$XDG_CONFIG_HOME" ]]; then
target="$XDG_CONFIG_HOME"
fi
if [[ -d "$GITHUB_WORKSPACE" ]]; then
target="$GITHUB_WORKSPACE"
fi
export XDG_CONFIG_HOME=$target
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"
export DEVELOPER=$HOME/dev/frap
#
# Logging
#
# These colours are meant to be used with `echo -e`
black="\033[0;30m"
red="\033[0;31m"
green="\033[0;32m"
yellow="\033[0;33m"
blue="\033[0;34m"
purple="\033[0;35m"
cyan="\033[0;36m"
white="\033[0;37;1m"
orange="\033[0;91m"
bblack="\033[30;1m"
bred="\033[31;1m"
bgreen="\033[32;1m"
byellow="\033[33;1m"
bblue="\033[34;1m"
bpurple="\033[35;1m"
bcyan="\033[36;1m"
bwhite="\033[37;1m"
borange="\033[91;1m"
normal="\033[0m"
reset="\033[39m"
function error() {
echo -e "${bred}🦤 $*${normal}"
}
function info() {
echo -e "${bcyan} $*${normal}"
}
function owl() {
echo -e "${bgreen}🦉 $*${reset}"
}
function log() {
echo -e "${bgreen}✔ $*${reset}"
}
function install() {
echo -e "${cyan}🚀 $*${reset}"
}
function section() {
echo -e "${blue}🦍 $*${normal}"
}
function a_theme() {
local text=">>> $2 :: ${*:3}"
local length="${#text}"
echo
echo '┌────────────────────────────────────────────────────────────────────────────┐'
echo -ne "│ \033[$1m$text${normal}"
printf "%$((75 - length))s│\n"
echo '└────────────────────────────────────────────────────────────────────────────┘'
}
function theme() {
echo -e "\033[1;36m ✅ $1 Theme :: ${*:2}${reset}"
}
function optional_theme() {
a_theme "1;32" "$1" "${*:2}"
}
function inactive_theme() {
a_theme "1;37" "$1" "${*:2}"
}
#
# Greetings
#
intro "La programmation n'est PAS une question de saisie, il s'agit de réfléchir."
echo ""
info "Kernel name: $KERNEL_NAME"
info "Kernel release: $KERNEL_RELEASE"
info "Operating system: $OS_NAME"
info "OS version: $OS_VERSION"
info "User: $USER"
info "XDG_CONFIG_HOME: $XDG_CONFIG_HOME"
info
#
# Helpers
#
section "Defining helpers"
# ${@:2} is all parameters starting at $2
# ${!parameter} returns what is in parameter
# theme guard was called theme_guard $REPO rep whee $REPO=true whether you wan to run the repo code
function theme_guard() {
key=$(echo "$1" | tr '[:upper:]' '[:lower:]')
local guard_ref="guard_$key"
local ignore_guard_ref="guard_ignore_$key"
# guard is VARIABLE denoting whether theme is true or false
guard="${!guard_ref}"
ignore_guard="${!ignore_guard_ref}"
if [[ ("$ALL" = "true" || "$guard" = "true") && "$ignore_guard" = "" ]]; then
optional_theme "$1" "${@:2}" # logging
return 0
else
inactive_theme "$1" "${@:2}" # logging
return 1
fi
}
function install_guard() {
[[ "$ACTION" == "install" ]]
return
}
function upgrade_guard() {
[[ "$ACTION" == "upgrade" ]]
return
}
function test_guard() {
[[ "$ACTION" == "test" ]]
return
}
function ubuntu_guard() {
[[ "$OS_NAME" == "ubuntu" ]]
return
}
function macos_guard() {
[[ "$OS_NAME" == "macos" ]]
return
}
function file_exists() {
local file=$1
[[ -f $file ]]
}
function qualify_repo_url() {
if [[ "$1" = "https://"* || "$1" = "git@"* ]]; then
echo "$1"
elif [[ "$2" = "github" ]]; then
if [[ "$USE_HTTPS" = "true" ]]; then
echo "https://github.com/$1.git"
else
echo "git@github.com:$1.git"
fi
elif [[ "$2" = "gitlab" ]]; then
if [[ "$USE_HTTPS" = "true" ]]; then
echo "https://gitlab.com/$1.git"
else
echo "git@gitlab.com:$1.git"
fi
fi
}
function git_lg() {
git --no-pager \
log \
--graph \
--pretty=format:'%Cred%h%Creset %C(bold blue)<%an> -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' \
"$*"
}
function sync_repo() {
section "sync_repo $*"
# working directory usually .config
wd=$(eval echo "$1")
remote="$2"
url=$(qualify_repo_url "$3" "$remote")
branch="$4"
if [[ $branch = "" ]]; then
branch="main"
fi
if [[ -d "$wd/.git" ]]; then
info "$wd existe déjà"
else
git clone "$url" "$wd" -b "$branch"
fi
cd "$wd" && {
git diff-index --quiet HEAD -- || {
error "Votre répertoire de travail n'est pas propre."
error "Veuillez valider ou cacher tous les changements avant de continuer."
return 1
}
current_branch=$(git symbolic-ref --short HEAD)
if [[ $branch != "$current_branch" ]]; then
log "Passer de $current_branch à $branch"
git checkout "$branch"
fi
if [[ -d .git/refs/remotes/$remote ]]; then
current_url=$(git remote get-url $remote)
if [[ $current_url != "$url" ]]; then
log "Distante '$remote' a une mauvaise URL, donc la mettre à jour"
log " $current_url -> $url"
git remote set-url $remote "$url"
fi
else
log "Impossible de trouver distante '$remote', donc en ajoutant"
git remote add $remote "$url"
fi
log "fetch $remote"
git fetch $remote
if [[ $(git rev-parse HEAD) == $(git rev-parse $remote/$branch) ]]; then
log "Tout est à jour"
return 0
fi
if [ "$(git rev-list HEAD..$remote/$branch --count)" != 0 ]; then
log "Modifications récupérées:"
git_lg HEAD..$remote/$branch
log
fi
log "rebaser sur $remote/$branch"
git rebase $remote/$branch
if [[ "$url" = *"$fellow"* ]]; then
if [ "$(git rev-list $remote/$branch..HEAD --count)" != 0 ]; then
log "Changements à pousser:"
git_lg $remote/$branch..HEAD
log
fi
log "pousser les changements"
git push $remote $branch
fi
}
}
function ensure_dir() {
if [[ ! -d "$1" ]]; then
log "créer le $1"
mkdir -p "$1"
fi
}
function check() {
command -v "$1" >/dev/null 2>&1
}
function linkfile() {
local file="$1"
if [ -f "$file" ]; then
(
cd "$(dirname "$file")"
map_lines safe_link "$file"
)
fi
}
function safe_link() {
local f
local s
local t
local d
local owner
# shellcheck disable=SC2086
f=$(eval echo $1)
s="$(pwd)/$f"
t=$(eval echo "$2")
d=$(dirname "$t")
if [[ -d "$d" ]]; then
#owner=$(stat -c '%U' "$d") # linux stat
owner=$(stat -f "%Su" "$d")
if [[ "$owner" != "root" && "$owner" != "$USER" ]]; then
error "ne peut pas lier '$s' à '$t'"
error "propriétaire de '$d' est $owner"
error "propriétaires autorisés: root o $USER"
exit 1
fi
fi
if [[ ! -f "$s" && ! -d "$s" ]]; then
error "ne peut pas lier '$s' comme il n'existe pas"
exit 1
fi
if [[ ! -d $d ]]; then
log "créer $d"
mkdir -p "$d"
fi
if [[ -L "$t" ]]; then
log "relier $s -> $t"
if [[ "$owner" = "root" ]]; then
sudo rm "$t"
else
rm "$t"
fi
else
log "lier $s -> $t"
fi
if [[ "$owner" = "root" ]]; then
sudo ln -s "$s" "$t"
else
ln -s "$s" "$t"
fi
}
function map_lines() {
if [[ -f "$2" ]]; then
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ "$line" != "#"* ]]; then
# shellcheck disable=SC2086
$1 $line
fi
done <"$2"
fi
}
function download_bin() {
fp="$HOME/.local/bin/$1"
curl --silent -o "$fp" "$2"
chmod a+x "$HOME/.local/bin/$1"
hash -r
}
#
# Setup variables
#
theme "Setup VULCAN" "Defining variables"
ALL="true"
ACTION=
case $1 in
install | upgrade | test)
ACTION="${1}"
# Shift the first argument off, as we are using that.
# http://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html
shift
;;
*)
echo
if [ -z "$1" ]; then
ACTION=upgrade
else
error "l'action '$1' n'est pas prise en charge"
log "les actions soutenues sont: install, upgrade, test"
exit 1
fi
;;
esac
# $# is the number of arguments
# $* is the postitonal parameters, starting from 1. If not in " each parameter is a separate word
# Syntax Effective Result
# $* $1 $2 $3 … ${N}
# $@ $1 $2 $3 … ${N}
# "$*" "$1c$2c$3c…c${N}"
# "$@" "$1" "$2" "$3" … "${N}"
# ${parameter#word} If the pattern matches the beginning of the expanded value of parameter, then
# the result of the expansion is the expanded value of parameter with the shortest matching pattern
# set -- the positional parameters are set to the arguments, even if some of them begin with a ‘-’.
POSITIONAL=()
while [[ $# -gt 0 ]]; do
if [[ "$1" != "" ]]; then
if [[ "$1" = -* ]]; then # add a - to disable a theme
key=$(echo "${1#-}" | tr '[:upper:]' '[:lower:]')
declare -r "guard_ignore_$key=true"
else
key=$(echo "$1" | tr '[:upper:]' '[:lower:]')
declare -r "guard_$key=true"
ALL="false"
fi
fi
shift
done
set -- "${POSITIONAL[@]}" # restore positional parameters
if [[ "$INTERACTIVE" = "" ]]; then
INTERACTIVE=true
fi
#
# Lock
#
LOCK_FILE=$XDG_CACHE_HOME/vulcan/vulcan.lock
if [ -f "$LOCK_FILE" ]; then
error "
Yet another world is being shaped by Hephaestus
One must either wait patiently or embrace the horrors of the unknown and
manually delete the $LOCK_FILE"
exit 1
fi
mkdir -p "$(dirname "$LOCK_FILE")"
touch "$LOCK_FILE"
function unlock() {
rm -rf "$LOCK_FILE"
}
trap unlock INT TERM EXIT
#
# Actual bootstrap
#
theme "Guardian" "Assurez-vous que tous les répertoires existent"
ensure_dir "$HOME/.local/bin"
ensure_dir "$XDG_CONFIG_HOME/git"
ensure_dir "$XDG_CACHE_HOME/eldev"
ensure_dir "$XDG_CACHE_HOME/zsh"
ensure_dir "$XDG_DATA_HOME/cargo"
ensure_dir "$XDG_STATE_HOME"
ensure_dir "$DEVELOPER"
theme_guard "system" "make Vulcan more approachable" && {
"$XDG_CONFIG_HOME/bin/safe_link" "$XDG_CONFIG_HOME/vulcan.sh" "$HOME/.local/bin/vulcan"
}
# TODO: make it work on Linux from command line
install_guard && theme_guard "SSH" "Vérification des clés SSH" && {
if [[ "$INTERACTIVE" = "true" ]]; then
ssh_key_add_url="https://github.com/settings/ssh/new"
ssh_key_path="$HOME/.ssh/id_rsa"
ssh_key_pub_path="${ssh_key_path}.pub"
ssh_config_path="$HOME/.ssh/config"
if [[ -f "$ssh_key_path" ]]; then
log "clé SSH trouvée sur $ssh_key_path."
else
log "Aucune clé SSH trouvée."
mkdir -p "$(dirname "$ssh_key_path")"
ssh-keygen -t rsa -b 4096 -C "$USER" -f "$ssh_key_path"
log "SSH key was generated."
macos_guard && {
log "Charger automatiquement la clé SSH et utiliser le trousseau"
echo "Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile $ssh_key_path" >"$ssh_config_path"
}
log "Ajouter une clé SSH à ssh-agent"
ssh-add -K ~/.ssh/id_rsa
log "Assurez-vous d'ajouter la clé SSH à GitHub"
pbcopy <"$ssh_key_pub_path"
open "$ssh_key_add_url"
read -rp "Appuyez sur Entrée pour continuer"
fi
log "Démarrage de ssh-agent"
eval "$(ssh-agent -s)"
fi
}
theme_guard "Linking" "Lier tous les fichiers comme défini dans Linkfile" && {
linkfile "$target/Linkfile"
linkfile "$XDG_CACHE_HOME/vulcan/Linkfile"
linkfile "$XDG_CACHE_HOME/vulcan/Linkfile_${KERNEL_NAME}"
for f in "$target"/**/Linkfile; do
linkfile "$f"
done
for f in "$target"/**/Linkfile_"${KERNEL_NAME}"; do
linkfile "$f"
done
}
theme_guard "Repositories" "Synchroniser les référentiels à partir de Repofiles" && {
map_lines sync_repo "$target/Repofile" || true
map_lines sync_repo "$XDG_CACHE_HOME/vulcan/Repofile" || true
}
ubuntu_guard && {
theme_guard "packages" "Amorcer Ubuntu Linux" && {
section "Installer des dépendances cruciales"
sudo add-apt-repository ppa:kelleyk/emacs -y
sudo apt update
sudo apt install emacs29-nox python3-pygments
sudo snap install starship
}
upgrade_guard && {
theme_guard "packages" "Mettre à niveau Ubuntu" && {
sudo apt update
sudo apt upgrade -y
}
}
}
macos_guard && {
theme_guard "packages" "Ensure brew exists" && {
check brew || {
log "Installing brew"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
eval "$(/opt/homebrew/bin/brew shellenv)"
brew update
}
}
test_guard && {
theme_guard "babashka" "Install all deps"
brew install borkdude/brew/babashka
log "Installed Babashka"
}
install_guard && {
theme_guard "packages" "Install all dependencies" && {
cd "$target/macos" && brew bundle
}
theme_guard "gnupg" "Fix permissions" && {
# make sure that I am the owner
chown -R "$(whoami)" ~$XDG_CONFIG_HOME/gnupg/
# correct permissions
find $XDG_CONFIG_HOME/gnupg -type f -exec chmod 600 {} \;
find $XDG_CONFIG_HOME/gnupg -type d -exec chmod 700 {} \;
}
}
upgrade_guard && {
theme_guard "packages" "Upgrade packages" && {
brew update
brew upgrade
}
}
}
install_guard && {
theme "Git" "Create a local git config file"
touch "$target/git/local.config"
}
nontest_guard && macos_guard && {
theme_guard "OS" "Write all defaults" && {
cd "$target/macos" && sudo ./defaults.sh
}
theme_guard "skhd" "Patch skhd application PATH" && {
check skhd && {
"$target/bin/patch_skhd_path"
}
}
theme_guard "yabai" "Ensure scripting addition is installed" && {
# reinstall the scripting addition
sudo yabai --uninstall-sa
sudo yabai --install-sa
# load the scripting addition
killall Dock || true
sudo yabai --load-sa
}
}
theme_guard "Emacs" "Setup Eldev" && {
eldev_bin=$HOME/.local/bin/eldev
curl -fsSL https://raw.github.com/doublep/eldev/master/bin/eldev >"$eldev_bin"
chmod a+x "$eldev_bin"
}
install_guard && {
theme_guard "Emacs" "Setup Emacs configurations" && {
cd "$XDG_CONFIG_HOME/emacs" && {
make bootstrap compile roam
}
}
}
doom_guard && upgrade_guard && {
theme_guard "Emacs" "Upgrade Emacs packages" && {
cd "$XDG_CONFIG_HOME/emacs" && {
bin/doom sync -u
}
}
}
test_guard && {
theme_guard "Emacs" "Test Emacs configurations" && {
cd "$XDG_CONFIG_HOME/emacs" && {
make test
}
}
}
theme_guard "Guardian" "Check that Emacs runs as expected" && {
emacs --batch -l "$target/emacs/test.el"
}
true