-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall
executable file
·184 lines (149 loc) · 4.2 KB
/
install
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
#!/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 [[ -e "$target" ]] && [[ ! -L "$target" ]]; then
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'"
elif [[ -L "$target" ]]; then
echo " overriding existing symbolic link"
run_command "rm '$target'"
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 "Copy Existing .config Files"
if [[ -d "$HOME/.config" ]] && [[ ! -L "$HOME/.config" ]]; then
echo "coping files from $HOME/.config..."
for i in "$HOME/.config"/*; do
from="$i"
to="$BASEDIR/_config/$(basename "$from")"
if [[ ! -e "$to" ]]; then
echo " copying $from to $to"
run_command "cp -rn $from $to"
else
echo " $to exists, skipping"
fi
done
else
echo "$HOME/.config is not a normal directory, skipping"
fi
phase "Link General Files"
for i in _*; do
source="$BASEDIR/$i"
target="$HOME/${i/_/.}"
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
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]"