-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
109 lines (87 loc) · 2.29 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
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
require 'pathname'
require 'tempfile'
require 'English'
#------------------------------------------------------------------------------
task default: %w(update)
task setup: %w(dotfiles)
verbose(false)
#------------------------------------------------------------------------------
ROOT = File.expand_path(File.dirname(__FILE__))
HOME = File.expand_path('~')
#------------------------------------------------------------------------------
DOTFILES = [
'config'
].freeze
LOCAL_FILES = [
'config/fish/local.fish',
'config/git/local.gitconfig',
].freeze
#------------------------------------------------------------------------------
task :dotfiles do
DOTFILES.map { |f|
symlink_path(File.join(ROOT, f), File.join(HOME, ".#{File.basename(f)}"))
}
LOCAL_FILES.map { |f| File.join(ROOT, f) }.select { |f| !File.exist?(f) }.each do |f|
FileUtils.touch(f)
info("created empty local file '#{f}'")
end
end
task :update do
sh 'git pull'
sh 'git submodule update --init --recursive'
end
#------------------------------------------------------------------------------
# Extend String with coloring functions
class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
def green
colorize(32)
end
def yellow
colorize(33)
end
def blue
colorize(34)
end
end
def realpath(path)
Pathname.new(path).realpath
end
def readlink(path)
`readlink "#{path}"`.strip
end
def symlink?(path)
readlink(path) != ''
end
def delete_broken_symlink(dest)
info("deleting broken symlink '#{dest}' to '#{readlink(dest)}'")
File.delete(dest)
end
def backup_existing_symlink(dest)
backup = "#{dest}.#{Time.now.strftime('%Y%m%d%H%M%S')}"
warning("target '#{dest}' already exists, backing up to '#{backup}'")
File.rename(dest, backup)
end
def symlink_path(source, dest)
if File.exist?(dest)
return if realpath(source) == realpath(dest)
backup_existing_symlink(dest)
end
delete_broken_symlink(dest) if !File.exist?(dest) && symlink?(dest)
sh "ln -s '#{source}' '#{dest}'"
info("symlinked '#{source}' to '#{dest}'")
end
def info(msg, *args)
puts "#{'info'.green}: #{msg % args}"
end
def warning(msg, *args)
puts "#{'warning'.yellow}: #{msg % args}"
end
def error(msg, *args)
puts "#{'error'.red}: #{msg % args}"
end