-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdotfiles.rb
More file actions
160 lines (128 loc) · 3.45 KB
/
dotfiles.rb
File metadata and controls
160 lines (128 loc) · 3.45 KB
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
# TODO(timuruski)
# - Warn if file exists (and not symlink)
# - Install individual file
# - Import a file into repo and replace with symlink
# - Cleanup dead links
require "fileutils"
require "optparse"
require "pathname"
module Dotfiles
def self.install(src: ".", **opts_override, &block)
opts = Options.new(ARGV, opts_override)
installer = Installer.new(src, opts.dest, &block)
if opts.dry_run?
installer.dry_run
else
installer.install(force: opts.force?)
end
end
class Installer
def initialize(src_dir, dest_dir, &block)
@src_dir = Pathname.new(src_dir)
@dest_dir = Pathname.new(dest_dir)
@links = []
instance_eval(&block)
end
def dry_run
@links.each do |link|
# link.debug
puts link.inspect
end
end
def install(force: false)
@links.each do |link|
link.install
puts link.inspect
end
end
def dotfile(pattern, to: nil)
symlink(pattern, dot_prefix: true, to: to)
end
def symlink_each(pattern, dot_prefix: false)
Dir.glob(pattern).each do |filename|
symlink(filename, dot_prefix: dot_prefix)
end
end
def symlink(filename, dot_prefix: false, to: nil)
dest = to || filename
dest = "." + dest if dot_prefix
src_path = File.expand_path(filename, @src_dir)
dest_path = File.expand_path(dest, @dest_dir)
@links << Symlink.new(src_path, dest_path)
end
end
class Symlink
attr_reader :src_path, :dest_path
def initialize(src_path, dest_path)
@src_path = src_path
@dest_path = dest_path
end
def install
FileUtils.mkdir_p(File.dirname(@dest_path))
FileUtils.symlink(@src_path, @dest_path) unless installed? || conflict?
end
def debug
puts @src_path
puts @dest_path
if File.symlink?(@dest_path)
puts File.readlink(@dest_path)
puts File.readlink(@dest_path) == @src_path
end
puts "---"
end
def inspect
src_path = Pathname.new(@src_path).relative_path_from(__dir__)
"[#{status || " "}] #{src_path} -> #{@dest_path}"
end
def status
if installed?
"x"
elsif conflict?
"?"
end
end
def installed?
File.symlink?(@dest_path) && File.readlink(@dest_path) == @src_path
end
def conflict?
File.file?(@dest_path) || File.symlink?(@dest_path) && File.readlink(@dest_path) != @src_path
end
end
class Options
DEFAULT_DEST = "~"
def initialize(args, defaults = {})
@dry_run = defaults.fetch(:dry_run, false)
@force = defaults.fetch(:force, false)
parser = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options] [target_dir]"
opts.separator ""
opts.separator "Default target: #{ENV["HOME"]}"
# opts.separator ""
opts.on "-d", "--debug", "Show debug info" do |value|
@debug = value
end
opts.on "-l", "--list", "Only list files installed" do |value|
@dry_run = value
end
opts.on "-f", "--force", "Overwrite existing symlinks" do |value|
@force = value
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end
parser.parse!(args)
@dest = ARGV.first || DEFAULT_DEST
end
def dest
@dest
end
def force?
@force
end
def dry_run?
@dry_run
end
end
end