-
Notifications
You must be signed in to change notification settings - Fork 8
/
link-dotfiles.rb
executable file
·74 lines (57 loc) · 1.33 KB
/
link-dotfiles.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
#!/usr/bin/env ruby
class String
def red
color(31)
end
def green
color(32)
end
private
def color(code)
"\e[#{code}m#{self}\e[0m"
end
end
class Dotfile
attr_reader :filename
IGNORED_DOTFILES = %w[. .. .git .github .circleci .gitignore].freeze
NESTED_LINKS = %w[.config].freeze
def initialize(filename)
@filename = filename
end
def self.link_path!(path)
Dir.glob(File.join(path, '.*'))
.map { |file| File.basename(file) }
.map do |file|
if NESTED_LINKS.include?(file)
Dir.glob("#{path}/#{file}/*")
.map { |f| f.match(%r{#{file}/\S }).to_s }
else
file
end
end
.flatten
.reject { |f| IGNORED_DOTFILES.include?(f) }
.map { |f| new(f) }
.each(&:link!)
end
def ignored?
IGNORED_DOTFILES.include?(filename)
end
def link!
return false if ignored?
print "#{full_path} -> "
if File.exist? link_path
puts File.symlink?(link_path) ? 'Already linked'.green : 'File Exists (Delete and re-run)'.red
return false
end
File.symlink(full_path, link_path)
puts 'Linked!'.green
end
def full_path
"#{ENV['HOME']}/.dotfiles/#{filename}"
end
def link_path
"#{ENV['HOME']}/#{filename}"
end
end
Dotfile.link_path!("#{ENV['HOME']}/.dotfiles/")