Skip to content

Commit ee2cf92

Browse files
authored
- Add --watch to the generate command
1 parent 535d039 commit ee2cf92

File tree

6 files changed

+97
-6
lines changed

6 files changed

+97
-6
lines changed

bashly.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_runtime_dependency 'completely', '~> 0.4.2'
2222
s.add_runtime_dependency 'mister_bin', '~> 0.7'
2323
s.add_runtime_dependency 'requires', '~> 0.2'
24+
s.add_runtime_dependency 'filewatcher', '~> 1.1'
2425

2526
s.metadata = {
2627
"bug_tracker_uri" => "https://github.com/DannyBen/bashly/issues",

lib/bashly/commands/generate.rb

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'filewatcher'
2+
13
module Bashly
24
module Commands
35
class Generate < Base
@@ -9,7 +11,8 @@ class Generate < Base
911
option "-f --force", "Overwrite existing files"
1012
option "-q --quiet", "Disable on-screen progress report"
1113
option "-u --upgrade", "Upgrade all added library functions"
12-
option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
14+
option "-w --watch", "Watch the source directory for changes and regenerate on change"
15+
option "-r --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
1316
option "-e --env ENV", "Force the generation environment (see BASHLY_ENV)"
1417

1518
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
@@ -27,17 +30,50 @@ class Generate < Base
2730

2831
example "bashly generate --force"
2932
example "bashly generate --wrap my_function"
33+
example "bashly g -uw"
34+
35+
attr_reader :watching
3036

3137
def run
38+
Settings.env = args['--env'] if args['--env']
39+
@watching = args['--watch']
40+
41+
generate
42+
watch if watching
43+
end
44+
45+
private
46+
47+
def watch
48+
quiet_say "!txtgrn!watching!txtrst! #{Settings.source_dir}\n"
49+
50+
Filewatcher.new([Settings.source_dir]).watch do
51+
reset
52+
generate
53+
54+
rescue Bashly::ConfigurationError => e
55+
say! "!undred!#{e.class}!txtrst!\n#{e.message}"
56+
57+
ensure
58+
quiet_say "!txtgrn!waiting\n"
59+
60+
end
61+
end
62+
63+
def generate
3264
with_valid_config do
33-
Settings.env = args['--env'] if args['--env']
3465
quiet_say "creating !txtgrn!production!txtrst! version" if Settings.production?
3566
generate_all_files
36-
quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script"
67+
quiet_say "run !txtpur!#{master_script_path} --help!txtrst! to test your bash script" unless watching
3768
end
3869
end
3970

40-
private
71+
def reset
72+
@config = nil
73+
@config_validator = nil
74+
@command = nil
75+
@script = nil
76+
end
4177

4278
def quiet_say(message)
4379
say message unless args['--quiet']

spec/approvals/cli/generate/help

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ Options:
1414
-u --upgrade
1515
Upgrade all added library functions
1616

17-
-w --wrap FUNCTION
17+
-w --watch
18+
Watch the source directory for changes and regenerate on change
19+
20+
-r --wrap FUNCTION
1821
Wrap the entire script in a function so it can also be sourced
1922

2023
-e --env ENV
@@ -51,3 +54,4 @@ Environment Variables:
5154
Examples:
5255
bashly generate --force
5356
bashly generate --wrap my_function
57+
bashly g -uw

spec/approvals/cli/generate/watch

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
creating user files in spec/tmp/src
2+
created spec/tmp/src/initialize.sh
3+
created spec/tmp/src/download_command.sh
4+
created spec/tmp/src/upload_command.sh
5+
created spec/tmp/cli
6+
watching spec/tmp/src
7+
8+
creating user files in spec/tmp/src
9+
skipped spec/tmp/src/initialize.sh (exists)
10+
skipped spec/tmp/src/download_command.sh (exists)
11+
skipped spec/tmp/src/upload_command.sh (exists)
12+
created spec/tmp/cli
13+
waiting
14+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bashly::ConfigurationError
2+
root contains invalid options: invalid_option

spec/bashly/commands/generate_spec.rb

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
end
9999

100100
it "generates the cli script wrapped in a function without bash3 bouncer" do
101-
expect { subject.run %w[generate -w function] }.to output_approval('cli/generate/wrap-function')
101+
expect { subject.run %w[generate --wrap function] }.to output_approval('cli/generate/wrap-function')
102102
expect(File).to exist(cli_script)
103103
lines = File.readlines cli_script
104104
expect(lines[0..10].join).to match_approval('cli/generate/wrap-script').except(/\d+\.\d+\.\d+/)
@@ -201,6 +201,40 @@
201201
end
202202
end
203203

204+
context "with --watch" do
205+
before do
206+
reset_tmp_dir create_src: true
207+
cp "lib/bashly/templates/bashly.yml", bashly_config_path
208+
end
209+
210+
let(:bashly_config_path) { "#{source_dir}/bashly.yml" }
211+
let(:bashly_config) { YAML.load_file bashly_config_path }
212+
213+
it "generates immediately and on change" do
214+
expect do
215+
expect_any_instance_of(Filewatcher).to receive(:watch) do |watcher, &block|
216+
block.call
217+
end
218+
219+
subject.run %W[generate --watch]
220+
end.to output_approval('cli/generate/watch')
221+
end
222+
223+
context "when ConfigurationError is raised during watch" do
224+
it "shows the error gracefully and continues to watch" do
225+
expect do
226+
expect_any_instance_of(Filewatcher).to receive(:watch) do |watcher, &block|
227+
bashly_config['invalid_option'] = 'error this'
228+
File.write bashly_config_path, bashly_config.to_yaml
229+
block.call
230+
end
231+
232+
subject.run %W[generate --watch]
233+
end.to output_approval('cli/generate/watch-stderr').to_stderr
234+
end
235+
end
236+
end
237+
204238
# DEPRECATION 0.8.0
205239
context "with deprecated command.short option" do
206240
before do

0 commit comments

Comments
 (0)