-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfect.rb
executable file
·209 lines (165 loc) · 4.22 KB
/
infect.rb
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
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
class Pathname
def *(subs)
subs.map { |sub| self + sub }
end
end
def dir(base, *subs)
dir = Pathname.new(base).join(*subs).expand_path
FileUtils.mkdir_p(dir) unless dir.exist?
dir.realpath
end
# TODO:
# - relax context as current cwd only
# - switch back from some branch to master / main when branch: nil
# - save updates into stash
def github(repo, branch: nil)
local = Pathname.new(repo.split('/').last)
unless (local / '.git').exist?
git_clone = "git clone https://github.com/#{repo}"
git_clone << " --branch #{branch}" unless branch.nil?
return system git_clone
end
git = -> (sub, *mods) {
command = "git -C #{local.realpath} #{sub}"
if mods.any? :stdout
stdout = `#{command}`
stdout if $?.success? or throw :fail, command
else
system command
end
}
git.call('fetch --all')
current = git.call('branch --show-current', :stdout).strip
# TODO: check refs, local branch must track remote one
if branch.nil? || current == branch
git.call('pull')
else
warn "current branch is #{current}, switching to #{branch}"
git.call("checkout #{branch}")
git.call("pull")
end
end
def ln(path, to:)
path = Pathname.new(path) unless path.is_a?(Pathname)
return unless path.exist?
prefix = path.realpath.dirname.relative_path_from(to)
FileUtils.ln_s(prefix / path.basename, to, force: true, verbose: true)
path
end
def lnx(of, to:)
of = of.children if of.is_a?(Pathname) && of.directory?
of.each.map { |piece| ln(piece, to: to) }
end
# TODO: cd pwd func wrapper
# TODO: sshh module @home, shell, github().pull/clone
This = Pathname.new(__FILE__).realpath.dirname
Home = Pathname.new('~').expand_path
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Config = dir(Home / '.config')
DataDir = dir(Home / '.local' / 'var')
Executables = dir(Home / '.local' / 'bin')
module Env
Homies = This * %w[
.gitconfig
.gitignore
.tmux.conf
.ghci
.irbrc
]
def self.call
lnx Homies, to: Home
end
def self.configs
ln This / '.ripgreprc', to: Config
end
def self.extras
ln This / '.psqlrc', to: Home
ln This / '.tcshrc', to: Home
end
def self.hushlogin
hushlogin = Home / '.hushlogin'
system "touch #{hushlogin}" unless hushlogin.exist?
end
end
module Zsh
def self.call
ln This / '.zshrc', to: Home
lnx This / 'zsh', to: dir(Home / '.zsh')
end
end
module Emacs
Els = This / 'emacs'
Init = Els / 'init.el'
Themes = Els.glob('*-theme.el')
Scripts = Els.glob('*.el') - ([Init] + Themes)
Target = dir(Home / '.emacs.d')
def self.init
ln Init, to: Target
end
def self.themes
lnx Themes, to: Target
end
def self.scripts
lnx Scripts, to: dir(Target / 'elisp')
end
def self.patches
FileUtils.cd(dir(Target / 'mods'), verbose: true)
# https://github.com/flycheck/flycheck/pull/1948
github "hrls/flycheck", branch: "fix/find-cargo-subcommand"
end
end
module Vim
Plugins = %w[
hrls/bullfinch
vim-airline/vim-airline
wincent/ferret
dyng/ctrlsf.vim
w0rp/ale
junegunn/fzf.vim
scrooloose/nerdtree
scrooloose/nerdcommenter
majutsushi/tagbar
vim-scripts/restore_view.vim
rust-lang/rust.vim
keith/swift.vim
idris-hackers/idris-vim
tpope/vim-markdown
chr4/nginx.vim
]
def self.rc
ln This / '.vimrc', to: Home
end
def self.plugins
# install https://github.com/tpope/vim-pathogen
autoload_dir = dir(Home / '.vim' / 'autoload')
pathogen = autoload_dir / 'pathogen.vim'
system "curl -LSso #{pathogen} https://tpo.pe/pathogen.vim"
bundle = dir(Home / '.vim' / 'bundle')
FileUtils.cd(bundle, verbose: true)
Plugins.each { |plugin| github plugin }
end
def self.helptags
system "vim -es +Helptags +exit"
end
end
module Nvim
def self.call
ln This / 'nvim', to: Config
end
end
if __FILE__ == $PROGRAM_NAME
Zsh.()
Env.()
Env.configs
Env.extras
Env.hushlogin
Nvim.()
lnx This / 'scripts', to: Executables
else
# Modules.*.for_each(Module.*)
# TODO: prompt lists available modules and their commands
# wildcards for interactive launch with 'irb -I . -r infect' => 'Env.*'
end