Skip to content

Commit b681a79

Browse files
committed
main files
0 parents  commit b681a79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+15008
-0
lines changed

.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[*.cr]
2+
charset = utf-8
3+
end_of_line = lf
4+
insert_final_newline = true
5+
indent_style = space
6+
indent_size = 2
7+
trim_trailing_whitespace = true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/doc/
2+
/lib/
3+
/bin/
4+
/.shards/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
language: crystal

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Rubi Jihantoro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# jihantoroblog
2+
3+
TODO: Write a description here
4+
5+
## Installation
6+
7+
TODO: Write installation instructions here
8+
9+
## Usage
10+
11+
TODO: Write usage instructions here
12+
13+
## Development
14+
15+
TODO: Write development instructions here
16+
17+
## Contributing
18+
19+
1. Fork it ( https://github.com/[your-github-name]/jihantoroblog/fork )
20+
2. Create your feature branch (git checkout -b my-new-feature)
21+
3. Commit your changes (git commit -am 'Add some feature')
22+
4. Push to the branch (git push origin my-new-feature)
23+
5. Create a new Pull Request
24+
25+
## Contributors
26+
27+
- [[your-github-name]](https://github.com/[your-github-name]) Rubi Jihantoro - creator, maintainer

dev/sentry.cr

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
module Sentry
2+
FILE_TIMESTAMPS = {} of String => String # {file => timestamp}
3+
4+
class ProcessRunner
5+
getter app_process : (Nil | Process) = nil
6+
property process_name : String
7+
property should_build = true
8+
property files = [] of String
9+
10+
def initialize(
11+
@process_name : String,
12+
@build_command : String,
13+
@run_command : String,
14+
@build_args : Array(String) = [] of String,
15+
@run_args : Array(String) = [] of String,
16+
files = [] of String,
17+
should_build = true)
18+
@files = files
19+
@should_build = should_build
20+
@should_kill = false
21+
@app_built = false
22+
end
23+
24+
private def build_app_process
25+
puts "🤖 compiling #{process_name}..."
26+
build_args = @build_args
27+
if build_args.size > 0
28+
Process.run(@build_command, build_args, shell: true, output: true, error: true)
29+
else
30+
Process.run(@build_command, shell: true, output: true, error: true)
31+
end
32+
end
33+
34+
private def create_app_process
35+
app_process = @app_process
36+
if app_process.is_a? Process
37+
unless app_process.terminated?
38+
puts "🤖 killing #{process_name}..."
39+
app_process.kill
40+
end
41+
end
42+
43+
puts "🤖 starting #{process_name}..."
44+
run_args = @run_args
45+
if run_args.size > 0
46+
@app_process = Process.new(@run_command, run_args, output: true, error: true)
47+
else
48+
@app_process = Process.new(@run_command, output: true, error: true)
49+
end
50+
end
51+
52+
private def get_timestamp(file : String)
53+
File.stat(file).mtime.to_s("%Y%m%d%H%M%S")
54+
end
55+
56+
# Compiles and starts the application
57+
#
58+
def start_app
59+
return create_app_process unless @should_build
60+
build_result = build_app_process()
61+
if build_result && build_result.success?
62+
@app_built = true
63+
create_app_process()
64+
elsif !@app_built # if build fails on first time compiling, then exit
65+
puts "🤖 Compile time errors detected. SentryBot shutting down..."
66+
exit 1
67+
end
68+
end
69+
70+
# Scans all of the `@files`
71+
#
72+
def scan_files
73+
file_changed = false
74+
app_process = @app_process
75+
files = @files
76+
Dir.glob(files) do |file|
77+
timestamp = get_timestamp(file)
78+
if FILE_TIMESTAMPS[file]? && FILE_TIMESTAMPS[file] != timestamp
79+
FILE_TIMESTAMPS[file] = timestamp
80+
file_changed = true
81+
puts "🤖 #{file}"
82+
elsif FILE_TIMESTAMPS[file]?.nil?
83+
puts "🤖 watching file: #{file}"
84+
FILE_TIMESTAMPS[file] = timestamp
85+
file_changed = true if (app_process && !app_process.terminated?)
86+
end
87+
end
88+
89+
start_app() if (file_changed || app_process.nil?)
90+
end
91+
92+
def run
93+
puts "🤖 Your SentryBot is vigilant. beep-boop..."
94+
95+
loop do
96+
if @should_kill
97+
puts "🤖 Powering down your SentryBot..."
98+
break
99+
end
100+
scan_files
101+
sleep 1
102+
end
103+
end
104+
105+
def kill
106+
@should_kill = true
107+
end
108+
end
109+
end

dev/sentry_cli.cr

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require "option_parser"
2+
require "yaml"
3+
require "./sentry"
4+
5+
process_name = nil
6+
7+
begin
8+
shard_yml = YAML.parse File.read("shard.yml")
9+
name = shard_yml["name"]?
10+
process_name = name.as_s if name
11+
rescue e
12+
end
13+
14+
build_args = [] of String
15+
build_command = "crystal build ./src/#{process_name}.cr"
16+
run_args = [] of String
17+
run_command = "./#{process_name}"
18+
files = ["./src/**/*.cr", "./src/**/*.ecr"]
19+
files_cleared = false
20+
show_help = false
21+
should_build = true
22+
23+
OptionParser.parse! do |parser|
24+
parser.banner = "Usage: ./sentry [options]"
25+
parser.on(
26+
"-n NAME",
27+
"--name=NAME",
28+
"Sets the name of the app process (current name: #{process_name})") { |name| process_name = name }
29+
parser.on(
30+
"-b COMMAND",
31+
"--build=COMMAND",
32+
"Overrides the default build command") { |command| build_command = command }
33+
parser.on(
34+
"--build-args=ARGS",
35+
"Specifies arguments for the build command") do |args|
36+
args_arr = args.strip.split(" ")
37+
build_args = args_arr if args_arr.size > 0
38+
end
39+
parser.on(
40+
"--no-build",
41+
"Skips the build step") { should_build = false }
42+
parser.on(
43+
"-r COMMAND",
44+
"--run=COMMAND",
45+
"Overrides the default run command") { |command| run_command = command }
46+
parser.on(
47+
"--run-args=ARGS",
48+
"Specifies arguments for the run command") do |args|
49+
args_arr = args.strip.split(" ")
50+
run_args = args_arr if args_arr.size > 0
51+
end
52+
parser.on(
53+
"-w FILE",
54+
"--watch=FILE",
55+
"Overrides default files and appends to list of watched files") do |file|
56+
unless files_cleared
57+
files.clear
58+
files_cleared = true
59+
end
60+
files << file
61+
end
62+
parser.on(
63+
"-i",
64+
"--info",
65+
"Shows the values for build/run commands, build/run args, and watched files") do
66+
puts "
67+
name: #{process_name}
68+
build: #{build_command}
69+
build args: #{build_args}
70+
run: #{run_command}
71+
run args: #{run_args}
72+
files: #{files}
73+
"
74+
end
75+
parser.on(
76+
"-h",
77+
"--help",
78+
"Show this help") do
79+
puts parser
80+
exit 0
81+
end
82+
end
83+
84+
if process_name
85+
process_runner = Sentry::ProcessRunner.new(
86+
process_name: process_name.as(String),
87+
build_command: build_command,
88+
run_command: run_command,
89+
build_args: build_args,
90+
run_args: run_args,
91+
should_build: should_build,
92+
files: files
93+
)
94+
95+
process_runner.run
96+
else
97+
puts "🤖 Sentry error: 'name' not given and not found in shard.yml"
98+
exit 1
99+
end

jihantoroblog

4.31 MB
Binary file not shown.

public/blogcover.jpg

27.8 KB
Loading

0 commit comments

Comments
 (0)