-
Notifications
You must be signed in to change notification settings - Fork 56
/
Rakefile
67 lines (55 loc) · 2 KB
/
Rakefile
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
require 'pathname'
# methods
def env_or_default(env: nil, default: nil)
ENV.fetch(env, default)
end
def ensure_pathname(maybe_pathname)
maybe_pathname === Pathname ? maybe_pathname : Pathname.new(maybe_pathname)
end
class Recipe
include FileUtils::Verbose
attr_reader :name, :source, :destination
def initialize(name: nil, source: nil, destination: nil)
@name = name
@source = ensure_pathname(source)
@destination = ensure_pathname(destination)
end
def install
if self.destination.exist?
puts "Skip #{self.name}"
return
end
ln_s self.source.to_s, self.destination.to_s
end
def clean
unless self.destination.exist? && self.destination.symlink?
puts "Skip #{self.destination}; the destination seems not to be installed with the Rake task"
return
end
rm self.destination.to_s
end
end
# constants
INSTALL_DIRECTORY = ensure_pathname(env_or_default(env: 'INSTALL_DIRECTORY', default: '~')).expand_path
SOURCE_DIRECTORY = ensure_pathname(env_or_default(env: 'SOURCE_DIRECTORY', default: Dir.pwd)).expand_path
DOTFILES_IGNORE_FILE = ensure_pathname(env_or_default(env: 'DOTFILES_IGNORE_FILE', default: SOURCE_DIRECTORY + '.dotfiles.ignore')).expand_path
IGNORED_DOTFILES = DOTFILES_IGNORE_FILE.each_line.map {|f| SOURCE_DIRECTORY.join(f.strip) }
DOTFILES = SOURCE_DIRECTORY.each_child.select {|e| e.basename.to_s.start_with?(?.) }
INSTALLABLE_DOTFILES = DOTFILES - IGNORED_DOTFILES
INSTALL_NAMES = INSTALLABLE_DOTFILES.map {|f| f.relative_path_from(SOURCE_DIRECTORY) }
INSTALL_RECIPES = INSTALL_NAMES.map {|name|
Recipe.new(name: name, source: SOURCE_DIRECTORY.join(name), destination: INSTALL_DIRECTORY.join(name))
}
task :default => :install
desc "Install dotfiles into #{INSTALL_DIRECTORY}"
task :install do
INSTALL_RECIPES.each do |recipe|
recipe.install
end
end
desc "Cleanup dotfiles that installed into #{INSTALL_DIRECTORY}"
task :clean do
INSTALL_RECIPES.each do |recipe|
recipe.clean
end
end