@@ -3,116 +3,56 @@ module Commands
33 class Add < Base
44 help 'Add extra features and customization to your script'
55
6- usage 'bashly add colors [--force]'
7- usage 'bashly add comp FORMAT [OUTPUT --force]'
8- usage 'bashly add config [--force]'
9- usage 'bashly add help [--force]'
10- usage 'bashly add lib [--force]'
11- usage 'bashly add settings [--force]'
12- usage 'bashly add strings [--force]'
13- usage 'bashly add test [--force]'
14- usage 'bashly add validations [--force]'
15- usage 'bashly add yaml [--force]'
6+ usage 'bashly add LIBRARY [ARGS...] [--force]'
7+ usage 'bashly add --list'
168 usage 'bashly add (-h|--help)'
179
1810 option '-f --force' , 'Overwrite existing files'
19-
20- param 'FORMAT' , <<~USAGE
21- Output format, can be one of:
22- function : generate a function file to be included in your script.
23- script : generate a standalone bash completions script.
24- yaml : generate a yaml compatible with completely.
25- USAGE
26-
27- param 'OUTPUT' , <<~USAGE
28- For the 'comp function' command: Name of the generated function.
29- For the 'comp script' or 'comp yaml' commands: path to output file.
30- In all cases, this is optional and will have sensible defaults.
31- USAGE
32-
33- command 'colors' , 'Add standard functions for printing colorful and formatted text to the lib directory.'
34- command 'comp' , 'Generate a bash completions script or function.'
35- command 'config' , 'Add standard functions for handling INI files to the lib directory.'
36- command 'help' , 'Add a help command, in addition to the standard --help flag.'
37- command 'lib' , <<~USAGE
38- Create the lib directory for any additional user scripts.
39- All *.sh scripts in this directory will be included in the final bash script.
40- Note that if you configured a different partials_extension, then the extensions of the files in this directory need to match.
41- USAGE
42-
43- command 'settings' , 'Copy a sample settings.yml file to your project, allowing you to customize some ' \
44- 'bashly options.'
45-
46- command 'strings' , 'Copy an additional configuration file to your project, allowing you to customize all the ' \
47- 'tips and error strings.'
48-
49- command 'test' , 'Add approval testing.'
50- command 'validations' , 'Add argument validation functions to the lib directory.'
51- command 'yaml' , 'Add standard functions for reading YAML files to the lib directory.'
52- example 'bashly add strings --force'
53- example 'bashly add comp function'
54- example 'bashly add comp script completions.bash'
11+ option '-l --list' , 'Show available libraries'
5512
5613 attr_reader :skip_src_check
5714
58- def colors_command
59- add_lib 'colors'
60- end
61-
62- def comp_command
63- format = args [ 'FORMAT' ]
64- output = args [ 'OUTPUT' ]
65-
66- case format
67- when 'script' then add_lib 'completions_script' , output
68- when 'function' then add_lib 'completions' , output
69- when 'yaml' then add_lib 'completions_yaml' , output
70- else raise Error , "Unrecognized format: #{ format } "
15+ def run
16+ if args [ '--list' ]
17+ show_list
18+ else
19+ add_lib args [ 'LIBRARY' ]
7120 end
7221 end
7322
74- def config_command
75- add_lib 'config'
76- end
77-
78- def lib_command
79- add_lib 'lib'
80- end
81-
82- def settings_command
83- @skip_src_check = true
84- add_lib 'settings'
85- end
23+ private
8624
87- def strings_command
88- add_lib 'strings'
25+ def lib_source
26+ @lib_source ||= Bashly :: LibrarySource . new
8927 end
9028
91- def test_command
92- add_lib 'test'
29+ def show_list
30+ lib_source . config . each do |key , config |
31+ usage = key
32+ usage += " #{ config [ 'usage' ] } " if config [ 'usage' ]
33+ say "g`bashly add #{ usage } `"
34+ say word_wrap ( " #{ config [ 'help' ] } " )
35+ say ''
36+ end
9337 end
9438
95- def help_command
96- add_lib 'help'
97- end
39+ def add_lib ( name )
40+ library = lib_source . libraries [ name . to_sym ]
41+ raise "Unknown library: g` #{ name } ` \n Run m`bashly add --list` to see available libraries" unless library
9842
99- def validations_command
100- add_lib 'validations'
101- end
43+ library . args = args [ 'ARGS' ]
44+ @skip_src_check = lib_source . config . dig name , 'skip_src_check'
10245
103- def yaml_command
104- add_lib 'yaml'
46+ add_library_files library
10547 end
10648
107- private
108-
109- def add_lib ( name , *args )
110- library = Bashly ::Library . new name , *args
49+ def add_library_files ( library )
11150 files_created = 0
11251 library . files . each do |file |
11352 created = safe_write file [ :path ] , file [ :content ]
11453 files_created += 1 if created
11554 end
55+
11656 message = library . post_install_message
11757 say "\n #{ message } " if message && files_created . positive?
11858 end
0 commit comments