-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.fish
executable file
·177 lines (148 loc) · 5.04 KB
/
bootstrap.fish
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
#!/usr/bin/env fish
#
# bootstrap installs things.
set -gx DOTFILES_ROOT (pwd -P)
set -gx DOTFILES (pwd -P)
. ./fish/functions/_logging_functions.fish
function on_exit -p %self
if not contains $argv[3] 0
echo [(set_color --bold red) FAIL (set_color normal)] "Couldn't setup dotfiles"
end
end
function link_winhome -d "links the windows home directory back to wsl"
set -l USERPROFILE_DIRTY /mnt/(cmd.exe /c "echo %USERPROFILE%" 2>/dev/null | tr -d '\r')
set -l USERPROFILE_DIRTY2 (string replace -a "\\" "/" $USERPROFILE_DIRTY)
set -l USERPROFILE_DIRTY3 (string replace ":" "" $USERPROFILE_DIRTY2)
set -Ux WINHOME (string lower $USERPROFILE_DIRTY3)
ln -sf $WINHOME "$HOME/winhome"
end
function setup_gitconfig
set managed (git config --global --get dotfiles.managed)
# if there is no user.email, we'll assume it's a new machine/setup and ask it
if test -z (git config --global --get user.email)
user 'What is your github author name?'
read user_name
user 'What is your github author email?'
read user_email
test -n $user_name
or echo "please inform the git author name"
test -n $user_email
or abort "please inform the git author email"
git config --global user.name $user_name
and git config --global user.email $user_email
or abort 'failed to setup git user name and email'
else if test '$managed' = "true"
# if user.email exists, let's check for dotfiles.managed config. If it is
# not true, we'll backup the gitconfig file and set previous user.email and
# user.name in the new one
set user_name (git config --global --get user.name)
and set user_email (git config --global --get user.email)
and mv ~/.gitconfig ~/.gitconfig.backup
and git config --global user.name $user_name
and git config --global user.email $user_email
and success "moved ~/.gitconfig to ~/.gitconfig.backup"
or abort 'failed to setup git user name and email'
else
# otherwise this gitconfig was already made by the dotfiles
info "already managed by dotfiles"
end
# include the gitconfig.local file
# finally make git knows this is a managed config already, preventing later
# overrides by this script
git config --global include.path ~/.gitconfig.local
and git config --global core.hooksPath $DOTFILES/git/hooks
and git config --global alias.fixup "!git log -n 50 --pretty=format:'%h %s' --no-merges | fzf | cut -c -7 | xargs -o git commit --fixup"
and git config --global dotfiles.managed true
and git config --global rebase.autosquash true
or abort 'failed to setup git'
end
function link_file -d "links a file keeping a backup"
echo $argv | read -l old new backup
if test -e $new
set newf (readlink $new)
if test "$newf" = "$old"
success "skipped $old"
return
else
mv $new $new.$backup
and success moved $new to $new.$backup
or abort "failed to backup $new to $new.$backup"
end
end
mkdir -p (dirname $new)
and ln -sf $old $new
and success "linked $old to $new"
or abort "could not link $old to $new"
end
function set_universal_vars
set -Ux EDITOR vim
set -Ux VISUAL $EDITOR
set -Ux WEDITOR code
set -Ux DOTFILES ~/.dotfiles.fish
set -Ua fish_user_paths $DOTFILES/bin $HOME/.bin
end
function install_dotfiles
# config files typically sit in .config/<>
for src in $DOTFILES_ROOT/*/*.symlink
set dir (string split -- / $src)[-2]
mkdir -p $dir
link_file $src $HOME/.config/$dir/(basename $src .symlink) backup
or abort 'failed to link config file'
end
for f in $DOTFILES/*/functions
set -Up fish_function_path $f
end
for f in $DOTFILES/*/conf.d/*.fish
ln -sf $f ~/.config/fish/conf.d/(basename $f)
end
# link up config files
link_file $DOTFILES_ROOT/fisher/plugins $__fish_config_dir/fish_plugins backup
or abort plugins
end
function is_wsl
switch (uname -a)
case '*WSL*'
return 0
case '*Microsoft*'
return 0
case '*'
return 1
end
end
is_wsl
and link_winhome
and set -Ux IS_WSL 0
set_universal_vars
curl -sL git.io/fisher | source && fisher install jorgebucaran/fisher
and success 'fisher'
or abort 'fisher'
setup_gitconfig
and success 'gitconfig'
or abort 'gitconfig'
install_dotfiles
and success 'dotfiles'
or abort 'dotfiles'
fisher update
and success 'plugins'
or abort 'plugins'
mkdir -p ~/.config/fish/completions/
and success 'completions'
or abort 'completions'
for installer in */install.fish
$installer
and success $installer
or abort $installer
end
if ! grep (command -v fish) /etc/shells
command -v fish | sudo tee -a /etc/shells
and success 'added fish to /etc/shells'
or abort 'setup /etc/shells'
end
test (which fish) = $SHELL
and success 'dotfiles installed/updated!'
and exit 0
# the exit 0 is required for this to run in github actions - since the macos
# runner is passwordless - the assumption is that this should pass locally
chsh -s (which fish)
and success set (fish --version) as the default shell.
and exit 0