Skip to content

Commit 7665904

Browse files
committed
- Add before/after hooks
1 parent 3b27d8b commit 7665904

File tree

5 files changed

+60
-13
lines changed

5 files changed

+60
-13
lines changed

lib/bashly/concerns/renderable.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def strings
1010
@strings ||= MessageStrings.new
1111
end
1212

13+
# Outputs a comment that describes the view unless in production mode
1314
def view_marker(id = nil)
1415
id ||= ":#{caller_locations(1..1).first.path}"
1516
"# #{id}" unless Settings.production?
@@ -31,6 +32,9 @@ def load_user_file(file, placeholder: true)
3132
Settings.production? ? content : "#{view_marker path}\n#{content}"
3233
end
3334

35+
# Returns a path to a file in the user's source_dir. The file argument
36+
# should either be without exteneion, or with the user's configured
37+
# partials_extension.
3438
def user_file_path(file)
3539
path = "#{Settings.source_dir}/#{file}"
3640
ext = ".#{Settings.partials_extension}"
@@ -39,6 +43,11 @@ def user_file_path(file)
3943
"#{path}#{ext}"
4044
end
4145

46+
# Returns true if the user's source file exists
47+
def user_file_exist?(file)
48+
File.exist? user_file_path(file)
49+
end
50+
4251
private
4352

4453
def view_path(view)

lib/bashly/views/command/master_script.gtx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
= render :user_lib if user_lib.any?
99
= render :command_functions
1010
= render :parse_requirements
11+
= render :user_hooks
1112
= render :initialize
1213
= render :run
1314

lib/bashly/views/command/run.gtx

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
> declare -a input=()
88
> normalize_input "$@"
99
> parse_requirements "${input[@]}"
10+
if user_file_exist?('before')
11+
> before_hook
12+
end
1013
>
1114
> case "$action" in
1215

1316
deep_commands.each do |command|
14-
> "{{ command.action_name }}")
15-
> if [[ ${args['--help']:-} ]]; then
16-
> long_usage=yes
17-
> {{ command.function_name }}_usage
18-
> else
19-
> {{ command.function_name }}_command
20-
> fi
21-
> ;;
22-
>
17+
> "{{ command.action_name }}") {{ command.function_name }}_command ;;
2318
end
2419

2520
if commands.empty?
26-
> "root")
27-
> root_command
28-
> ;;
21+
> "root") root_command ;;
2922
end
30-
>
3123
> esac
24+
25+
if user_file_exist?('after')
26+
>
27+
> after_hook
28+
end
29+
3230
> }
31+
32+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
if user_file_exist?('before') || user_file_exist?('after')
2+
= view_marker
3+
end
4+
5+
if user_file_exist?('before')
6+
> before_hook() {
7+
= load_user_file("before").indent 2
8+
> }
9+
>
10+
end
11+
12+
if user_file_exist?('after')
13+
> after_hook() {
14+
= load_user_file("after").indent 2
15+
> }
16+
>
17+
end

spec/bashly/script/command_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,26 @@
353353
end
354354
end
355355

356+
describe '#user_file_exist?' do
357+
before { FileUtils.mkdir_p 'spec/tmp/src' }
358+
359+
context 'when the file exists in the user source path' do
360+
before { FileUtils.touch 'spec/tmp/src/test.sh' }
361+
362+
it 'returns true' do
363+
expect(subject.user_file_exist?('test')).to be true
364+
end
365+
end
366+
367+
context 'when the file does not in the user source path' do
368+
before { FileUtils.rm_f 'spec/tmp/src/test.sh' }
369+
370+
it 'returns false' do
371+
expect(subject.user_file_exist?('test')).to be false
372+
end
373+
end
374+
end
375+
356376
describe '#required_args' do
357377
it 'returns an array of only the required Argument objects' do
358378
expect(subject.required_args.size).to eq 1

0 commit comments

Comments
 (0)