-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathThorfile
More file actions
48 lines (39 loc) · 1.14 KB
/
Thorfile
File metadata and controls
48 lines (39 loc) · 1.14 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
class Dotfiles < Thor
include Thor::Actions
def self.source_root
File.dirname(__FILE__)
end
desc "install name", "Installs the dotfiles of the given type into your home directory."
def install(name)
copy = []
Dir["**/*"].each do |f|
next if %W[Thorfile README.md].include?(f)
next if File.directory?(f)
next if name && !(f =~ /^#{name}/)
file = home_file(f)
# Deal with the file existing. We ignore directories and files which
# are identical in content.
next if File.exist?(file) && file_equal?(file, f)
# Add the file to the proper copy. We don't do anything right away
# so that its possibly to cancel at any point.
copy << f
end
# Finally copy all the files over
copy.each do |file|
copy_file(file, home_file(file))
end
end
protected
def home_file(f)
File.join(ENV["HOME"], ".#{f}")
end
def file_equal?(a, b)
# Compares the contents of each file in memory, since the dotfiles are all
# pretty small.
File.open(a, "r") do |fa|
File.open(b, "r") do |fb|
return fb.read == fa.read
end
end
end
end