-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
113 lines (99 loc) · 3.38 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
110
111
112
113
# encoding: utf-8
#
# Gregory Igelmund
#
require 'pathname'
HOSTNAME = `hostname -s`.chomp
HOME_PATH = Pathname.new(ENV.fetch('HOME'))
BASE_PATH = HOME_PATH + ".dotfiles"
DOTFILES_BASE_PATH = BASE_PATH + "dotfiles"
DOTFILES_MACHINE_PATH = BASE_PATH + "dotfiles/machines/#{HOSTNAME}"
DOTDIRS_PATH = BASE_PATH + "dotdirs"
VIM_PATH = HOME_PATH + ".vim"
BACKUPS_PATH = DOTFILES_BASE_PATH + "backups"
BAK_TIME_ID = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
module Dotfiles
module Utils
module_function
def safe_symlink(source, target)
fail "Cannot symlink #{source} since it does not exist" if !File.exist? source
if File.symlink? target
puts "removing old symlink: #{target}"
FileUtils.rm target
elsif File.exist? target
puts "backing up existing file/dir #{target}"
bak_path = BACKUPS_PATH + "#{File.basename(target)}-#{BAK_TIME_ID}"
bak_path.mkpath
puts " \tmoving #{target} -> #{bak_path}"
FileUtils.mv target, bak_path
end
FileUtils.ln_s source, target
puts "~> creating symlink: #{target} -> #{source}"
end
end
end
namespace :setup do
namespace :symlink do
task dotfiles: %w[dotfiles_base dotfiles_for_machine]
task :dotfiles_base do
puts "Symlinking base dotfiles for all machines: #{DOTFILES_BASE_PATH}"
DOTFILES_BASE_PATH.children.select { |p| p.file? }.each do |original_file|
symlink = HOME_PATH + original_file.basename
Dotfiles::Utils.safe_symlink original_file, symlink
end
end
task :dotfiles_for_machine do
next unless DOTFILES_MACHINE_PATH.exist?
puts "Symlinking machine specific dotfiles: #{DOTFILES_MACHINE_PATH}"
DOTFILES_MACHINE_PATH.children.select { |p| p.file? }.each do |original_file|
symlink = HOME_PATH + original_file.basename
Dotfiles::Utils.safe_symlink original_file, symlink
end
end
task :dotdirs do
puts "Symlinking dotdirs: #{DOTDIRS_PATH}"
DOTDIRS_PATH.children.select { |p| p.directory? }.each do |original_dir|
symlink = HOME_PATH + original_dir.basename
Dotfiles::Utils.safe_symlink original_dir, symlink
end
end
end
namespace :bash do
task :install do
bashrc = HOME_PATH + ".bashrc"
bashrc_local = HOME_PATH + ".bashrc_local"
puts "------------"
if File.exist?(bashrc) || File.symlink?(bashrc)
puts "The system already provides a ~/.bashrc file. You need to manually include ~/.bashrc_local"
else
puts "Symlinking ~/.bashrc -> ~/.bashrc_local"
FileUtils.ln_s bashrc_local, bashrc
end
puts "------------"
end
end
namespace :vim do
task :install do
FileUtils.cd "#{VIM_PATH}/bundle" do |_|
sh "git submodule init"
sh "git submodule update"
end
end
end
desc "Installs dotfiles"
task install: %w[symlink:dotfiles symlink:dotdirs vim:install bash:install]
desc "remove all backed up files"
task :cleanup do
FileUtils.remove_entry_secure BACKUPS_PATH, :force => true
FileUtils.mkdir BACKUPS_PATH
FileUtils.touch "#{BACKUPS_PATH}/.gitkeep"
end
desc "show help"
task :help do
puts "Use `rake install` to install dotfiles"
end
end
task :default => 'setup:help'
task :install => 'setup:install'
task :cleanup => 'setup:cleanup'