-
Notifications
You must be signed in to change notification settings - Fork 27
/
sentry.cr
279 lines (233 loc) · 7.68 KB
/
sentry.cr
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
require "yaml"
require "colorize"
module Sentry
FILE_TIMESTAMPS = {} of String => String # {file => timestamp}
class Config
include YAML::Serializable
# `shard_name` is set as a class property so that it can be inferred from
# the `shard.yml` in the project directory.
class_property shard_name : String?
@[YAML::Field(ignore: true)]
property? sets_display_name : Bool = false
@[YAML::Field(ignore: true)]
property? sets_build_command : Bool = false
@[YAML::Field(ignore: true)]
property? sets_run_command : Bool = false
property info : Bool = false
property? colorize : Bool = true
property src_path : String = "./src/#{Sentry::Config.shard_name}.cr"
property? install_shards : Bool = false
setter build_args : String = ""
setter run_args : String = ""
property watch : Array(String) = ["./src/**/*.cr", "./src/**/*.ecr"]
property install_shards : Bool = false
# Initializing an empty configuration provides no default values.
def initialize
@display_name = nil
@sets_display_name = false
@info = false
@src_path = "./src/#{Sentry::Config.shard_name}.cr"
@build = nil
@build_args = ""
@run = nil
@run_args = ""
@watch = [] of String
@install_shards = false
@colorize = true
end
@display_name : String?
def display_name
@display_name ||= self.class.shard_name
end
def display_name=(new_display_name : String)
@sets_display_name = true
@display_name = new_display_name
end
def display_name!
display_name.not_nil!
end
@build : String?
def build
@build ||= "crystal build #{self.src_path}"
end
def build=(new_command : String)
@sets_build_command = true
@build = new_command
end
def build_args
@build_args.strip.split(" ").reject(&.empty?)
end
@run : String?
def run
@run ||= "./#{self.class.shard_name}"
end
def run=(new_command : String)
@sets_run_command = true
@run = new_command
end
def run_args
@run_args.strip.split(" ").reject(&.empty?)
end
@[YAML::Field(ignore: true)]
setter should_build : Bool = true
def should_build?
@should_build ||= begin
if build_command = @build
build_command.empty?
else
false
end
end
end
def merge!(other : self)
self.display_name = other.display_name! if other.sets_display_name?
self.info = other.info if other.info
self.build = other.build if other.sets_build_command?
self.build_args = other.build_args.join(" ") unless other.build_args.empty?
self.run = other.run if other.sets_run_command?
self.run_args = other.run_args.join(" ") unless other.run_args.empty?
self.watch = other.watch unless other.watch.empty?
self.install_shards = other.install_shards?
self.colorize = other.colorize?
self.src_path = other.src_path
end
def to_s(io : IO)
io << <<-CONFIG
🤖 Sentry configuration:
display name: #{display_name}
shard name: #{self.class.shard_name}
install shards: #{install_shards?}
info: #{info}
build: #{build}
build_args: #{build_args}
src_path: #{src_path}
run: #{run}
run_args: #{run_args}
watch: #{watch}
colorize: #{colorize?}
CONFIG
end
end
class ProcessRunner
getter app_process : (Nil | Process) = nil
property display_name : String
property should_build = true
property files = [] of String
def initialize(
@display_name : String,
@build_command : String,
@run_command : String,
@build_args : Array(String) = [] of String,
@run_args : Array(String) = [] of String,
files = [] of String,
should_build = true,
install_shards = false,
colorize = true
)
@files = files
@should_build = should_build
@should_kill = false
@app_built = false
@should_install_shards = install_shards
@colorize = colorize
end
private def stdout(str : String)
if @colorize
puts str.colorize.fore(:yellow)
else
puts str
end
end
private def build_app_process
stdout "🤖 compiling #{display_name}..."
build_args = @build_args
if build_args.size > 0
Process.run(@build_command, build_args, shell: true, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
else
Process.run(@build_command, shell: true, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
end
end
private def create_app_process
app_process = @app_process
if app_process.is_a? Process
unless app_process.terminated?
stdout "🤖 killing #{display_name}..."
app_process.signal(:kill)
app_process.wait
end
end
stdout "🤖 starting #{display_name}..."
run_args = @run_args
if run_args.size > 0
@app_process = Process.new(@run_command, run_args, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
else
@app_process = Process.new(@run_command, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
end
end
private def get_timestamp(file : String)
File.info(file).modification_time.to_unix.to_s
end
# Compiles and starts the application
#
def start_app
return create_app_process unless @should_build
build_result = build_app_process()
if build_result && build_result.success?
@app_built = true
create_app_process()
elsif !@app_built # if build fails on first time compiling, then exit
stdout "🤖 Compile time errors detected. SentryBot shutting down..."
exit 1
end
end
# Scans all of the `@files`
#
def scan_files
file_changed = false
app_process = @app_process
files = @files
begin
Dir.glob(files) do |file|
timestamp = get_timestamp(file)
if FILE_TIMESTAMPS[file]? && FILE_TIMESTAMPS[file] != timestamp
FILE_TIMESTAMPS[file] = timestamp
file_changed = true
stdout "🤖 #{file}"
elsif FILE_TIMESTAMPS[file]?.nil?
stdout "🤖 watching file: #{file}"
FILE_TIMESTAMPS[file] = timestamp
file_changed = true if (app_process && !app_process.terminated?)
end
end
rescue ex : File::Error
# The underlining lib for reading directories will fail very rarely, crashing Sentry
# This catches that error and allows Sentry to carry on normally
# https://github.com/crystal-lang/crystal/blob/677422167cbcce0aeea49531896dbdcadd2762db/src/crystal/system/unix/dir.cr#L19
end
start_app() if (file_changed || app_process.nil?)
end
def run_install_shards
stdout "🤖 Installing shards..."
install_result = Process.run("shards", ["install"], shell: true, output: Process::Redirect::Inherit, error: Process::Redirect::Inherit)
if !install_result || !install_result.success?
stdout "🤖 Error installing shards. SentryBot shutting down..."
exit 1
end
end
def run
stdout "🤖 Your SentryBot is vigilant. beep-boop..."
run_install_shards if @should_install_shards
loop do
if @should_kill
stdout "🤖 Powering down your SentryBot..."
break
end
scan_files
sleep 1
end
end
def kill
@should_kill = true
end
end
end