-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathct.rb
executable file
·217 lines (183 loc) · 4.41 KB
/
ct.rb
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env ruby
# The MIT License
#
# Copyright (c) 2012 Michael V. O'Brien.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'rubygems'
require 'ostruct'
require 'optparse'
require 'tempfile'
VERSION = "0.1.0"
BOLD = `tput bold`
RED = `tput setaf 1`
GREEN = `tput setaf 2`
YELLOW = `tput setaf 3`
BLUE = `tput setaf 4`
RESET = `tput sgr0`
@_settings = OpenStruct.new
@_settings.noop = false
@_settings.interactive = false
def _diff(first, second)
left, right = Tempfile.new('ct'), Tempfile.new('ct')
begin
left.write(first)
left.flush
right.write(second)
right.flush
diff_str = `git diff --color #{left.path} #{right.path} | sed 1,4d`
ensure
left.close
left.unlink
right.close
right.unlink
end
diff_str
end
def _write_file(path, content)
return nil if noop?
File.open(path, 'w') do |f|
f.write(content)
end
end
def _print_script(script)
puts "#{YELLOW}{"
lines = script.split("\n")
indentation = (lines.first.slice(/^ +/) || "").length
lines.each do |line|
puts line.gsub(/^.{#{indentation}}/, ' ')
end
puts "}#{RESET}"
end
def noop?
@_settings.noop
end
def info(text)
puts "#{YELLOW}==>#{RESET} #{BOLD}#{text}#{RESET}"
end
def error(text)
puts "#{RED}==>#{RESET} #{BOLD}#{text}#{RESET}"
end
def sh_out(commands)
_print_script commands
Kernel.system(commands) unless noop?
end
def sh_exitstatus(commands)
`#{commands}`
$?.exitstatus == 0
end
def file(path, content)
original = ""
if File.exists?(path)
original = File.read(path)
end
diff_str = _diff(original, content)
unless diff_str.empty?
info "[DO] Write #{path}"
puts diff_str
_write_file(path, content)
else
info "[SKIP] Write #{path}"
end
end
def script(description, should_run, commands)
if should_run
info "[DO] #{description.capitalize}"
sh_out commands
else
info "[SKIP] #{description.capitalize}"
end
end
def _clear_brew_list_cache
@_brew_list = nil
end
def _clear_brew_outdated_cache
@_brew_outdated = nil
end
def _brew_list
@_brew_list ||= `brew list`
end
def _brew_outdated
@_brew_outdated ||= `brew outdated`
end
def _installed?(name)
_brew_list.match(/^#{name}$/)
end
def _install(name)
_clear_brew_list_cache
sh_out "brew install #{name}"
end
def _outdated?(name)
_brew_outdated.match(/^#{name}$/)
end
def _upgrade(name)
_clear_brew_outdated_cache
sh_out "brew upgrade #{name}"
end
def package(name)
if _installed? name
if _outdated? name
info "[DO] Upgrade #{name}"
_upgrade name
else
info "[SKIP] Install #{name}"
end
else
info "[DO] Install #{name}"
_install name
end
end
OptionParser.new do |opts|
opts.banner = "Usage: ct [options] [file]"
opts.on("-n", "--noop", "Run in no-operation mode (dry-run)") do |n|
@_settings.noop = n
end
opts.on("-i", "--interactive", "Run in interactive mode") do |i|
@_settings.interactive = i
end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("--version", "Show version") do
puts VERSION
exit
end
end.parse!
if @_settings.interactive
require 'irb'
require 'irb/completion'
ARGV.clear
IRB.setup(nil)
IRB.conf[:PROMPT_MODE] = :SIMPLE
irb = IRB::Irb.new
IRB.conf[:MAIN_CONTEXT] = irb.context
trap("SIGINT") do
irb.signal_handle
end
begin
catch(:IRB_EXIT) do
irb.eval_input
end
ensure
IRB.irb_at_exit
end
else
eval ARGF.read
end