-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbb.edn
113 lines (96 loc) · 4.47 KB
/
bb.edn
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
{:tasks
{:requires ([babashka.fs :as fs])
:init (do
(def files
[["vim"]
["vimrc"]
["bash_aliases"]
["gitaliases"]
["githelpers"]
["gitignore_global"]
["tmux.conf"]
["spacehammer"]
["wezterm.lua"]
["ghostty/" ".config/ghostty/"]
["karabiner" ".config/karabiner/"]
["starship.toml" ".config/starship.toml"]
["nvim/" ".config/nvim/"]
["vscodesettings.json",
"Library/Application Support/Code/User/settings.json"]])
(def base-path (System/getProperty "user.dir"))
;; Utility functions
(defn log [level & msg]
(let [prefix (case level
:info "[*]"
:warn "[!]"
:error "[✗]"
:success "[✓]")]
(apply println prefix msg)))
(defn file-contains? [filename re]
(try
(let [contents (slurp filename)
contains (re-find (re-pattern re) contents)]
contains)
(catch java.io.FileNotFoundException e false)))
(defn append-to-file [filename s]
(try (fs/create-file filename)
(catch java.nio.file.FileAlreadyExistsException e))
(spit filename (str s "\n") :append true))
(defn append-if-missing [filename s compare-string]
(if-not (file-contains? filename compare-string)
(append-to-file filename s)
(log :warn (str compare-string " already in " filename))))
(defn backup-file! [path]
(when (fs/exists? path {:nofollow-links true})
(let [backup (str path ".bak")]
(log :info "Backing up" path "to" backup)
(fs/create-dirs (fs/parent backup))
(fs/move path backup {:nofollow-links true
:replace-existing true}))))
(defn create-symlink! [src dst]
(log :info "Symlinking" src "→" dst)
(fs/create-dirs (fs/parent dst))
(try
(fs/delete-if-exists dst)
(fs/create-sym-link dst src)
(catch Exception e
(log :error "Failed to create symlink:" (.getMessage e))
(throw e))))
(defn home-dir [path]
(-> (System/getenv "HOME") (fs/path path) str))
(defn install-vim-plugins! []
; Plugins may be vim or neovim specific, so we
; have to install using both
(shell "vim +'PlugInstall --sync' +qa")
(shell "nvim +qa"))
(defn install-bash-aliases! []
(let [filename (home-dir ".bashrc")]
(append-if-missing filename
"\n[ -f $HOME/.bash_aliases ] && source $HOME/.bash_aliases"
".bash_aliases")
(append-if-missing filename
"\n[ -f $HOME/.bash_aliases_local ] && source $HOME/.bash_aliases_local"
".bash_aliases_local"))))
symlink (doseq [[src dst] files
:let [dstname (or dst (str "." src))
dst (home-dir dstname)
absrc (str (fs/absolutize src))]]
(backup-file! dst)
(create-symlink! absrc dst))
set-path {:task (let [filename (home-dir ".bashrc")]
(append-if-missing filename
(str "\n# Add dotfiles bin to PATH\n" "export PATH=$PATH:" base-path "/bin")
(str (fs/path base-path "bin"))))
:depends [symlink]}
bash-aliases {:task (install-bash-aliases!)
:depends [symlink]}
git-aliases {:task (let [filename (home-dir ".gitconfig")]
(append-if-missing filename "[include]\n\tpath = ~/.gitaliases" ".gitaliases"))
:depends [symlink]}
git-ignore {:task (let [filename (home-dir ".gitconfig")]
(append-if-missing filename "[core]\n\texcludesfile = ~/.gitignore_global" ".gitignore_global"))
:depends [symlink]}
vim-plugins {:task install-vim-plugins!
:depends [symlink]}
setup {:depends [symlink set-path bash-aliases git-aliases git-ignore vim-plugins]
:task (log :success "Dotfiles installation complete!")}}}