forked from ryanb/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile
executable file
·137 lines (123 loc) · 4.63 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'rake'
require 'erb'
require 'fileutils'
desc "install the dot files into user's home directory"
task :install do
replace_all = ENV['REPLACE_ALL'] == 'true' ? true : false
preload_private_environment
Dir['*'].each do |file|
next if %w[Rakefile README.rdoc LICENSE symlinks Gemfile Gemfile.lock].include? file
puts file
if File.exist?(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"))
if File.identical? file, File.join(ENV['HOME'], ".#{file.sub('.erb', '')}")
puts "identical ~/.#{file.sub('.erb', '')}"
elsif replace_all
replace_file(file)
else
print "overwrite ~/.#{file.sub('.erb', '')}? [ynaq] "
case $stdin.gets.chomp
when 'a'
replace_all = true
replace_file(file)
when 'y'
replace_file(file)
when 'q'
exit
else
puts "skipping ~/.#{file.sub('.erb', '')}"
end
end
else
link_file(file)
end
end
setup_symlinks
end
def replace_file(file)
system %Q{rm -rf "$HOME/.#{file.sub('.erb', '')}"}
link_file(file)
end
def link_file(file)
if file =~ /.erb$/
puts "generating ~/.#{file.sub('.erb', '')}"
File.open(File.join(ENV['HOME'], ".#{file.sub('.erb', '')}"), 'w') do |new_file|
new_file.write ERB.new(File.read(file)).result(binding)
end
else
puts "linking ~/.#{file}"
system %Q{ln -fvs "$PWD/#{file}" "$HOME/.#{file}"}
end
end
def setup_symlinks
puts "Setting up symlinks..."
#Sublime text 3
#Package Control.sublime-settings + Preferences.sublime-settings
symlinks_source_directory = "#{ENV['HOME']}/Library/Application Support/Sublime Text/Packages/User/"
symlinks_dotfiles_directory = File.join('symlinks', 'Sublime Text User Settings')
symlink_collection(
symlinks_source_directory,
symlinks_dotfiles_directory,
'Sublime Text',
[
'Package Control.cache',
'Package Control.last-run',
'Package Control.merged-ca-bundle',
'Package Control.user-ca-bundle',
'oscrypto-ca-bundle.crt'
]
)
end
#Symlink each directory/file in source directory to a target directory
# Caveat - In root directory, it must be *either* a folder or file wanting to be symlinked
def symlink_collection(source_directory, dotfiles_directory, running_process_to_kill = nil, file_exclusions = [])
puts " -- Generating symlinks for '#{File.basename(dotfiles_directory)}' -> '#{File.basename(source_directory)}'"
FileUtils.mkdir_p(dotfiles_directory) unless File.exist?(dotfiles_directory)
Dir.glob(File.join(source_directory, '*')).each do |file|
if file_exclusions.include?(File.basename(file))
puts "Skipping - #{file}"
next
else
puts "Processing - #{file}"
end
is_directory = File.directory?(file)
source_path = File.expand_path(file)
dotfiles_path = File.join(File.expand_path(dotfiles_directory), File.basename(file))
if File.symlink?(source_path) && File.readlink(source_path) == dotfiles_path && File.exist?(dotfiles_path)
#Symlink already exists and content exists in dotfiles
puts " --- Symlink exists already '#{dotfiles_path}' --> '#{source_path}'"
else
#Generate symlink
kill_running_process(running_process_to_kill) if running_process_to_kill
if File.exist?(source_path) && !File.symlink?(source_path)
# First run
puts " - Removing existing source for '#{File.basename(file)}' and moving to dotfiles location."
if File.exist?(dotfiles_path)
is_directory ? FileUtils.rm_rf(dotfiles_path) : FileUtils.rm(dotfiles_path)
end
FileUtils.mv(source_path, dotfiles_path)
elsif File.exist?(source_path)
puts " - Removing existing target for '#{File.basename(file)}'."
is_directory ? FileUtils.rm_rf(source_path) : FileUtils.rm(source_path)
end
puts " --+ Linking '#{dotfiles_path}' --> '#{source_path}'"
system %Q{ln -fvs "#{dotfiles_path}" "#{source_path}"}
end
end
puts " -- Done."
end
def kill_running_process(process_name)
pid = `pgrep '#{process_name}'`
unless pid.to_s == ''
puts " ** Killing running process : '#{process_name}' - #{pid}"
Process.kill "USR2", pid.to_i
end
end
#Babushka loads in one ruby process, this will load any env file previously generated (like in private-dot-files)
# Search and match on any variable assignment and ensure current ruby process has it loaded
def preload_private_environment(private_file_location = File.join(ENV['HOME'], '.localrc'))
if File.exist?(private_file_location)
File.open(private_file_location).read.scan(/^([\w_]+)\=["']?([^"']+)["']?$/) do |key, value|
ENV[key] = value
end
end
end