-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRakefile
188 lines (156 loc) · 4.83 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
require 'erb'
desc 'Install dotcss'
task :install => 'install:all'
DAEMON_INSTALL_DIR = '/usr/local/bin'
is_linux = true if RUBY_PLATFORM.downcase =~ /linux/
is_sudo = true if ENV['SUDO_USER']
namespace :install do
task :all => [:prompt, :daemon, :create_dir, :autostart, :chrome, :done]
task :prompt do
puts ''
puts ' dotcss'
puts ' ------'
puts " With your permission, I'll install:"
puts " 1. The dcssd daemon in #{DAEMON_INSTALL_DIR}"
if is_linux
puts " 2. dotcss.desktop in ~/.config/autostart"
else
puts " 2. ca.stwrt.dotcss.plist in ~/Library/LaunchAgents"
end
puts ''
puts ' Ok? (y|n)'
begin
until %w( k ok y yes n no ).include?(answer = $stdin.gets.chomp.downcase)
puts ' Please type y/yes or n/no.'
puts ' Install dotcss? (y|n)'
end
rescue Interrupt
exit 1
end
exit 1 if answer =~ /n/
end
task :done do
puts ''
if system 'curl https://localhost:1243 &> /dev/null'
puts ' dotcss installation worked!'
puts ' Drop files like github.com.css in ~/.css and have fun tweaking the web!'
else
puts ' dotcss installation failed!'
puts ' Check Console.app or open an issue on GitHub'
end
end
desc 'Install launch agent'
task :autostart do
if is_linux
autorun_script = 'dotcss.desktop'
install_dir = File.expand_path('~/.config/autostart')
launcher = File.join(install_dir, autorun_script)
else
autorun_script = 'ca.stwrt.dotcss.plist'
install_dir = File.expand_path('~/Library/LaunchAgents')
end
launcher = File.join(install_dir, autorun_script)
File.open(launcher, 'w') do |file|
file.puts ERB.new(IO.read(autorun_script)).result(binding)
end
if is_linux && is_sudo
system "chown #{ENV['SUDO_USER']}:#{ENV['SUDO_USER']} #{launcher}"
end
puts ' starting dcssd...'
if is_linux
command = "exo-open #{launcher} > /dev/null 2> /dev/null &"
else
command = "launchctl load -w #{launcher}"
end
system command
# wait for server to get started
sleep 5
end
desc 'Install dotcss daemon'
task :daemon => :install_dir_writeable do
cp 'bin/dcssd', DAEMON_INSTALL_DIR, :verbose => true, :preserve => true
end
desc 'Create ~/.css'
task :create_dir do
css_dir = File.join(ENV['HOME'], '.css')
if !File.directory?(css_dir)
mkdir css_dir
if is_sudo
system "chown -R #{ENV["SUDO_USER"]} #{css_dir}"
end
chmod 0755, css_dir
end
end
desc 'Install Chrome/Chromium extension'
task :chrome do
puts " Now you need to install the dotcss extension for Chrome."
puts " You can get it here: http://stwrt.ca/dotcss"
end
end
desc 'Uninstall dcssd daemon'
task :uninstall => 'uninstall:all'
namespace :uninstall do
task :all => [:prompt, :daemon, :autostart, :chrome, :done]
task :prompt do
puts ''
puts ' I will remove:'
puts " 1. dcssd(1) from #{DAEMON_INSTALL_DIR}"
if is_linux
puts " 2. dotcss.desktop from ~/.config/autostart"
else
puts " 2. ca.stwrt.dotcss.plist from ~/Library/LaunchAgents"
end
puts ''
puts ' I will not remove:'
puts ' 1. ~/.css'
puts ' 2. The dotcss Chrome extension'
puts ''
print ' Ok? (y|n)'
begin
until %w( k ok y yes n no ).include?(answer = $stdin.gets.chomp.downcase)
puts ' Please type y/yes or n/no.'
puts ' Install dotcss? (y|n)'
end
rescue Interrupt
exit 1
end
exit 1 if answer =~ /n/
end
task :done do
puts ''
if system 'curl https://localhost:1243 2> /dev/null > /dev/null'
puts ' dotcss uninstall failed!'
puts ' dcssd is still running.'
else
puts ' dotcss uninstall worked!'
puts ' Your ~/.css folder was not touched.'
end
end
desc "Uninstall launch agent or Linux launcher"
task :autostart do
if is_linux
launcher = File.expand_path('~/.config/autostart/dotcss.desktop')
system "kill $(ps aux | grep '^#{ENV['SUDO_USER']}' | grep '.*dcssd' | awk '{print $2}') 2> /dev/null; true"
else
launcher = File.expand_path('~/Library/LaunchAgents/ca.stwrt.dotcss.plist')
system "launchctl unload #{launcher}"
end
rm launcher, :verbose => true
end
desc 'Uninstall dotcss daemon'
task :daemon => :install_dir_writeable do
rm File.join(DAEMON_INSTALL_DIR, 'dcssd'), :verbose => true
end
desc 'Uninstall Chrome extension'
task :chrome do
puts ''
puts " Please uninstall the chrome extension manually:"
puts ' Chrome > Window > Extensions > dotcss > Uninstall.'
end
end
# Check write permissions on DAEMON_INSTALL_DIR
task :install_dir_writeable do
if not File.writable?(DAEMON_INSTALL_DIR)
abort " Error: Can't write to #{DAEMON_INSTALL_DIR}. Try again using `sudo`."
end
end