-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotfiles.elv
304 lines (261 loc) · 8.21 KB
/
dotfiles.elv
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
# Copyright (c) 2016, 2018-2022, Cody Opel <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# FIXME: maybe make this a generic git updater
# https://github.com/chlorm/kratos/blob/c82657c9565ce041ade093c473c3f6d0b25be0ad/modules/updater/main.bash
use github.com/chlorm/elvish-stl/env
use github.com/chlorm/elvish-stl/exec
use github.com/chlorm/elvish-stl/io
use github.com/chlorm/elvish-stl/map
use github.com/chlorm/elvish-stl/os
use github.com/chlorm/elvish-stl/path
use github.com/chlorm/elvish-stl/platform
use github.com/chlorm/elvish-stl/re
use github.com/chlorm/elvish-stl/str
use github.com/chlorm/elvish-xdg/xdg-dirs
var EXT-GENERATE = '.generate'
var EXT-INSTALL-PRE = '.install-pre'
var EXT-INSTALL = '.install'
var EXT-INSTALL-POST = '.install-post'
# TODO:
# - Should we use an ext for os-specific hooks or require implementing os logic as needed in hooks
# - allow many repos
# - clone to a single directory
# - link or install files from the lib dir
# - track all linked/installed files
# - add-repo, add-file, add-directory, update-file commands
# - Figure out windows support, possibly in a more minimal fashion.
# - Windows cannot use symlinks as they require admistrator privledges.
# - Need to rewrite current system to install files instead of symlinking
# Parse `@{VAR}@`
fn -environment-variable-parse-tmpl {|fileStr|
re:find '(@{.*?}@)' $fileStr
}
fn -environment-variable-parse-tmpl-inner {|varTmplStr|
re:find '@{(.*?)}@' $varTmplStr
}
# FIXME: add try/catch with useful error
fn -environment-variable-evaluate {|varNameStr|
env:get $varNameStr
}
fn -environment-variable-repls {|fileStr|
-environment-variable-parse-tmpl $fileStr | peach {|v|
put [
$v
(-environment-variable-parse-tmpl-inner $v)
]
}
}
# Parse `@(command --arg)@`
fn -command-substitution-parse-tmpl {|fileStr|
re:find '(@\(.*?\)@)' $fileStr
}
fn -command-substitution-parse-tmpl-inner {|csTmpl|
re:find '@\((.*?)\)@' $csTmpl
}
fn -command-substitution-evaluate {|cmd|
put (e:elvish -c 'print ('$cmd')')
}
fn -command-substitution-repls {|fileStr|
-command-substitution-parse-tmpl $fileStr | peach {|cs|
put [
$cs
(-command-substitution-parse-tmpl-inner $cs)
]
}
}
# Replace template sting with evaluated string.
fn -repl-tmpl {|replPair fileStr|
str:replace $replPair[0] $replPair[1] $fileStr
}
fn -evaluate-repls {|fileStr|
run-parallel {
-environment-variable-repls $fileStr | peach {|r|
put [
$r[0]
(-environment-variable-evaluate $r[1])
]
}
} {
-command-substitution-repls $fileStr | peach {|r|
put [
$r[0]
(-command-substitution-evaluate $r[1])
]
}
}
}
# TODO: maybe make repls an arg instead of calling here so that this only
# subs repls. Input is already sync so it would have no affect.
fn -sub-repls {|fileStr|
for i [ (-evaluate-repls $fileStr) ] {
set fileStr = (-repl-tmpl $i $fileStr)
}
put $fileStr
}
fn -install-path {|dotfile|
put (path:join (path:home) '.'$dotfile)
}
fn -hook-generate {|dotfilePath dotfilesDir dotfile|
var fileStr = (-sub-repls (io:open $dotfilePath))
# Override vars
set dotfile = (str:replace $EXT-GENERATE '' $dotfile)
set dotfilePath = (path:join $dotfilesDir $dotfile)
# FIXME: not sure we want to install this way, but works for now
echo $fileStr > $dotfilePath
}
fn -hook-run-script {|script|
try {
e:elvish $script
} catch error {
fail $error
}
}
fn -hook-install {|dotfilesDir dotfile|
var installPath = (-install-path $dotfile)
if (not (os:exists $installPath)) {
echo 'Installing: '$dotfile >&2
} else {
echo 'Updating: '$dotfile >&2
}
# FIXME: Test symlink target before removing
if (os:exists $installPath) {
os:remove $installPath
}
if (not (os:is-dir (path:dirname $installPath))) {
os:makedirs (path:dirname $installPath)
}
var source = (path:join $dotfilesDir $dotfile)
if $platform:is-windows {
os:link $source $installPath
} else {
os:symlink $source $installPath
}
}
fn install-singleton {|dotfilesDir dotfile|
var dotfilePath = (path:join $dotfilesDir $dotfile)
# Generate
if (str:has-suffix $dotfile $EXT-GENERATE) {
-hook-generate $dotfilePath $dotfilesDir $dotfile
set dotfile = (re:replace '.generate$' '' $dotfile)
}
if (os:exists $dotfilePath$EXT-GENERATE) {
return
}
# PRE-Install
var dotInstallPre = $dotfilePath$EXT-INSTALL-PRE
if (os:exists $dotInstallPre) {
-hook-run-script $dotInstallPre
}
# Install
var dotInstall = $dotfilePath$EXT-INSTALL
if (os:exists $dotInstall) {
-hook-run-script $dotInstall
} else {
-hook-install $dotfilesDir $dotfile
}
# POST-Install
var dotInstallPost = $dotfilePath$EXT-INSTALL-POST
if (os:exists $dotInstallPost) {
-hook-run-script $dotInstallPost
}
}
fn -path-rel-to-home {|path|
var home = (path:absolute (path:home))
set path = (path:relative-to $path $home)
set path = (re:replace '^\.' '' $path)
put $path
}
fn -path-rel-to-dotfilesdir {|dotfilesDir path|
set dotfilesDir = (path:absolute $dotfilesDir)
path:relative-to $path $dotfilesDir
}
fn -build-ignore-list {|dotfilesDir|
var ignoreList = [
(path:join (-path-rel-to-home (xdg-dirs:config-home)) 'systemd')
]
var dotIgnore = (path:join $dotfilesDir '.dotignore')
if (os:is-file $dotIgnore) {
set ignoreList = [
$@ignoreList
(-path-rel-to-dotfilesdir $dotfilesDir $dotIgnore)
(str:to-lines (io:open $dotIgnore))
]
}
put $ignoreList
}
fn -should-ignore {|ignoreList dotfilesDir dotfile|
# Exclude hooks
# FIXME: don't exclude .install, allow install without an associated dotfile
var dotfileHooks = [
$EXT-INSTALL-PRE
$EXT-INSTALL
$EXT-INSTALL-POST
]
for hook $dotfileHooks {
if (str:has-suffix $dotfile $hook) {
put $true
return
}
}
set dotfile = (-path-rel-to-dotfilesdir $dotfilesDir $dotfile)
# Ignore hidden files
if (path:is-hidden $dotfile) {
put $true
return
}
for ignore $ignoreList {
if (==s '' $ignore) {
continue
}
# Respect ignoring files within ignored directories
if (re:match '.*'$ignore'.*' $dotfile) {
put $true
return
}
}
put $false
}
fn -find-files {|dotfilesDir|
if (not (os:is-dir $dotfilesDir)) {
fail 'Dotfile directory does not exist: '$dotfilesDir
}
var ignoreList = (-build-ignore-list $dotfilesDir)
var dotfiles = [ ]
# These loops should remain synchronous to avoid hitting open file limits.
# The issue is actually install-singleton/-hook-generate accessing to
# many files. Making this sync is slow enough to avoid hitting the limit
# and still faster than running install-singleton in sync.
for path [ (path:walk $dotfilesDir) ] {
var dir = $path['root']
for file $path['files'] {
var dotfile = (path:join $dir $file)
if (-should-ignore $ignoreList $dotfilesDir $dotfile) {
continue
}
put (-path-rel-to-dotfilesdir $dotfilesDir $dotfile)
}
}
}
# FIXME: support multiple dirs (repos)
fn install {|dotfilesDir|
xdg-dirs:populate-env
-find-files $dotfilesDir | peach {|dotfile|
try {
install-singleton $dotfilesDir $dotfile
} catch _ { }
}
}
fn update {
# TODO
}