@@ -3,12 +3,13 @@ module Commands
33 class Generate < Base
44 help "Generate the bash script and required files"
55
6- usage "bashly generate [--force --quiet --wrap FUNCTION]"
6+ usage "bashly generate [--force --quiet --upgrade -- wrap FUNCTION]"
77 usage "bashly generate (-h|--help)"
88
99 option "-f --force" , "Overwrite existing files"
10- option "-w --wrap FUNCTION" , "Wrap the entire script in a function so it can also be sourced"
1110 option "-q --quiet" , "Disable on-screen progress report"
11+ 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"
1213
1314 environment "BASHLY_SOURCE_DIR" , "The path containing the bashly configuration and source files [default: src]"
1415 environment "BASHLY_TARGET_DIR" , "The path to use for creating the bash script [default: .]"
@@ -18,6 +19,7 @@ class Generate < Base
1819
1920 def run
2021 create_user_files
22+ upgrade_libs if args [ '--upgrade' ]
2123 create_master_script
2224 quiet_say "run !txtpur!#{ master_script_path } --help!txtrst! to test your bash script"
2325 end
@@ -28,6 +30,44 @@ def quiet_say(message)
2830 say message unless args [ '--quiet' ]
2931 end
3032
33+ def upgrade_libs
34+ generated_files . each do |file |
35+ content = File . read file
36+
37+ if content =~ /\[ @bashly-upgrade (.+)\] /
38+ lib = $1
39+
40+ case lib
41+ when "colors"
42+ upgrade file , Library ::Colors . new
43+ when "config"
44+ upgrade file , Library ::Config . new
45+ when "yaml"
46+ upgrade file , Library ::YAML . new
47+ when "validations"
48+ upgrade file , Library ::Validations . new
49+ when /completions (.+)/
50+ upgrade file , Library ::CompletionsFunction . new ( file , function : $1)
51+ end
52+ end
53+ end
54+ end
55+
56+ def generated_files
57+ Dir [ "#{ Settings . source_dir } /**/*.*" ] . sort
58+ end
59+
60+ def upgrade ( existing_file , handler )
61+ file = handler . files . select { |f | f [ :path ] == existing_file } . first
62+
63+ if file
64+ File . deep_write file [ :path ] , file [ :content ]
65+ quiet_say "!txtcyn!updated!txtrst! #{ file [ :path ] } "
66+ else
67+ quiet_say "!txtred!warning!txtrst! not upgrading !txtcyn!#{ existing_file } !txtrst!, path mismatch"
68+ end
69+ end
70+
3171 def create_user_files
3272 quiet_say "creating user files in !txtgrn!#{ Settings . source_dir } "
3373
@@ -55,17 +95,17 @@ def create_all_command_files
5595
5696 def create_file ( file , content )
5797 if File . exist? file and !args [ '--force' ]
58- quiet_say "skipped !txtgrn! #{ file } !txtrst! (exists)"
98+ quiet_say "!txtblu! skipped!txtrst! #{ file } (exists)"
5999 else
60100 File . write file , content
61- quiet_say "created !txtgrn!#{ file } "
101+ quiet_say "!txtgrn!created!txtrst! #{ file } "
62102 end
63103 end
64104
65105 def create_master_script
66106 File . write master_script_path , script . code
67107 FileUtils . chmod "+x" , master_script_path
68- quiet_say "created !txtgrn!#{ master_script_path } "
108+ quiet_say "!txtgrn!created!txtrst! #{ master_script_path } "
69109 end
70110
71111 def script
0 commit comments