1+ require 'filewatcher'
2+
13module 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' ]
0 commit comments