-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall
More file actions
executable file
·219 lines (179 loc) · 5.14 KB
/
Copy pathinstall
File metadata and controls
executable file
·219 lines (179 loc) · 5.14 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
#!/usr/bin/env bash
#
# Install dotfiles.
set -e
BASEDIR=$(cd "$(dirname "$0")"; pwd)
##
# Functions
##
# Function to echo a message for a phase
phase() {
printf "\\n$(tput bold)$(tput sgr 0 1)%s$(tput sgr0)\\n" "$@"
}
# Function to run a command respecting the DRY_RUN environment variable
run_command() {
local command="$1"
if [[ -z "$DRY_RUN" ]]; then
bash -c "$command"
else
echo " dry-run: not actually running \`$command\`"
fi
}
# Function to create symbolic link and backups the target if something already exists there
link() {
local source="$1"
local target="$2"
echo "[Link] $source → $target"
if [[ -L "$target" ]]; then
# Already a symbolic link: skip if it already points to the expected source
local current_target
current_target="$(readlink "$target")"
if [[ "$current_target" == "$source" ]]; then
echo " already linked to the expected source, skipping"
return
fi
echo " overriding existing symbolic link"
run_command "rm '$target'"
elif [[ -e "$target" ]]; then
# An actual file/directory exists (not a symlink), so it still needs to be
# replaced with the link
local backup_name
backup_name="$target.$(date +%Y%m%d_%H%M%S).bak"
echo " creating backup: $backup_name"
run_command "mv -f '$target' '$backup_name'"
fi
local target_dir
target_dir="$(dirname "$target")"
if [[ ! -d "$target_dir" ]]; then
echo " target directory does not exist, creating"
run_command "mkdir -p '$target_dir'"
fi
run_command "ln -sf '$source' '$target'"
}
##
# Main Phases
##
echo "[Dotfiles Installation]"
# Switch to the dotfiles directory
cd "$BASEDIR"
# Check if git exists
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "Error: can't find git."
exit -1
fi
phase "Preparing Git Submodules"
git submodule init
git submodule update
echo "submodules ready"
phase "Link General Files"
for i in _*; do
# Skip _config (handled separately)
if [[ "$i" == "_config" ]]; then
continue
fi
source="$BASEDIR/$i"
target="$HOME/${i/#_/.}"
link "$source" "$target"
done
phase "Link .config/ Files"
for i in _config/*; do
source="$BASEDIR/$i"
target="$HOME/${i/#_config/.config}"
link "$source" "$target"
done
if [[ $(uname) == "Darwin" ]]; then
phase "Build and link User Scripts for macOS"
mac_jxa_dir="$BASEDIR/mac/JXA"
cd "$mac_jxa_dir"
for i in *.js; do
name=${i/.js/}
osacompile -l JavaScript -o "$name.scptd" "$i"
osacompile -l JavaScript -o "$name.app" "$i"
done
cd "$BASEDIR"
link "$mac_jxa_dir" "$HOME/Library/JXA"
fi
phase "Link User Packages for Sublime Text 3"
if [[ -d "$BASEDIR/subl3/Packages" ]]; then
subl_source_packages_dir="$BASEDIR/subl3/Packages"
if [[ $(uname) == "Darwin" ]]; then
subl_target_packages_dir="$HOME/Library/Application Support/Sublime Text 3/Packages"
else
subl_target_packages_dir="$HOME/.config/sublime-text-3/Packages"
fi
cd "$subl_source_packages_dir"
for i in *; do
link "$subl_source_packages_dir/$i" "$subl_target_packages_dir/$i"
done
cd "$BASEDIR"
fi
phase "Link User Directory for VSCode"
if [[ -d "$BASEDIR/vscode/User" ]]; then
source="$BASEDIR/vscode/User"
if [[ $(uname) == "Darwin" ]]; then
target="$HOME/Library/Application Support/Code/User"
else
# TODO: Support other OS?
target="/tmp/null"
fi
link "$source" "$target"
fi
if [[ -d "$BASEDIR/vscode-oss/User" ]]; then
source="$BASEDIR/vscode-oss/User"
if [[ $(uname) == "Darwin" ]]; then
target_1="$HOME/Library/Application Support/Code - OSS/User"
target_2="$HOME/Library/Application Support/code-oss/User"
target_3="$HOME/Library/Application Support/VSCodium/User"
else
# TODO: Support other OS?
target_1="/tmp/null"
target_2="/tmp/null"
target_3="/tmp/null"
fi
link "$source" "$target_1"
link "$source" "$target_2"
link "$source" "$target_3"
fi
phase "Link User Directory for Cursor"
if [[ -d "$BASEDIR/cursor/User" ]]; then
source="$BASEDIR/cursor/User"
if [[ $(uname) == "Darwin" ]]; then
target="$HOME/Library/Application Support/Cursor/User"
else
# TODO: Support other OS?
target="/tmp/null"
fi
link "$source" "$target"
fi
phase "Process Secret Dotfiles"
if [[ -d "$BASEDIR/secret" ]]; then
echo "removing all permissions for group/world on secret dotfiles"
chmod -R o-r "$BASEDIR/secret"
chmod -R o-w "$BASEDIR/secret"
chmod -R o-x "$BASEDIR/secret"
chmod -R g-r "$BASEDIR/secret"
chmod -R g-w "$BASEDIR/secret"
chmod -R g-x "$BASEDIR/secret"
phase "Link Files From Secret Dotfiles"
for i in secret/_*
do
source="$BASEDIR/$i"
target="$HOME/${i/secret\/_/.}"
link "$source" "$target"
done
phase "Run Installation Script for Secret Dotfiles"
if [[ -x "$BASEDIR/secret/install" ]]; then
echo "running $BASEDIR/secret/install..."
export -f phase
export -f run_command
export -f link
"$BASEDIR/secret/install"
else
echo "$BASEDIR/secret/install is not an executable, skipping"
fi
else
echo "$BASEDIR/secret does not exist, skipping"
echo "place your secret dotfiles in $BASEDIR/secret before running install if you want them to got taken care of"
fi
echo ""
echo "[Done]"