-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.rb
executable file
·75 lines (62 loc) · 1.57 KB
/
install.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
#!/usr/bin/env ruby
# Install all files to ~ by symlinking them,
# this way, updating them is as simple as git pull
#
# Author: Sterling Hamilton <[email protected]>
# Date: 2024.06.24
# License: MIT
#
# Kudos to @bahamas10 for the obvious pilfering/inspiration.
# We could just use `File` instead of `FileUtils` but
# we want an easy way to truck over existing files.
require 'fileutils'
links = [
{
source: "gitconfig",
target: "#{ENV['HOME']}/.gitconfig"
},
{
source: "gitignore",
target: "#{ENV['HOME']}/.gitignore"
},
{
source: "nvim",
target: "#{ENV['HOME']}/.config/nvim"
},
{
source: "zshrc",
target: "#{ENV['HOME']}/.zshrc"
}
]
changes = []
# This is purely for display purposes.
def collect_changes(source, target, changes)
regex = /^#{Regexp.escape(ENV['HOME'])}/
source_path = source.sub(regex, '~')
target_path = target.sub(regex, '~')
changes << "%30s -> %s" % [source_path, target_path]
end
def symlink(source, target, changes)
collect_changes(source, target, changes)
FileUtils.ln_s(source, target, force: true)
end
def prompt_to_remove(target)
puts "Target #{target} exists. Do you want to remove it? [y/N]"
response = $stdin.gets.chomp.downcase
response == 'y'
end
links.each do |file|
source = File.join(Dir.pwd, file[:source])
target = file[:target]
if File.exist?(target)
if prompt_to_remove(target)
FileUtils.rm_r(target)
else
puts "Skipping #{target}"
next
end
end
symlink(source, target, changes)
end
puts changes
puts "No changes made." if changes.empty?