Skip to content

Commit 5cf63e3

Browse files
committed
- Refactor 'bashly add libraries' command
1 parent 01e3ea1 commit 5cf63e3

File tree

20 files changed

+147
-211
lines changed

20 files changed

+147
-211
lines changed

examples/completions/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This example was generated with:
77
```bash
88
$ bashly init
99
# ... now edit src/bashly.yml to match the example ...
10-
$ bashly add comp function
10+
$ bashly add completions
1111
$ bashly generate
1212
# ... now edit src/completions_command.sh ...
1313
$ bashly generate
@@ -112,7 +112,7 @@ commands:
112112
```bash
113113
# Call the `send_completions` function which was added by running:
114114
#
115-
# $ bashly add comp function
115+
# $ bashly add completions
116116
#
117117
# Users can now enable bash completion for this script by running:
118118
#

examples/completions/src/completions_command.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Call the `send_completions` function which was added by running:
22
#
3-
# $ bashly add comp function
3+
# $ bashly add completions
44
#
55
# Users can now enable bash completion for this script by running:
66
#

examples/completions/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -x
44

5-
bashly add comp function --force
5+
bashly add completions --force
66
bashly generate
77

88
### Try Me ###

lib/bashly/commands/add.rb

Lines changed: 25 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -3,113 +3,45 @@ 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
22+
23+
private
7324

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
86-
87-
def strings_command
88-
add_lib 'strings'
89-
end
90-
91-
def test_command
92-
add_lib 'test'
93-
end
94-
95-
def help_command
96-
add_lib 'help'
97-
end
98-
99-
def validations_command
100-
add_lib 'validations'
25+
def lib_source
26+
@lib_source ||= Bashly::LibrarySource.new
10127
end
10228

103-
def yaml_command
104-
add_lib 'yaml'
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
10537
end
10638

107-
private
39+
def add_lib(name)
40+
library = lib_source.libraries[name.to_sym]
41+
raise "Unknown library: g`#{name}`\nRun m`bashly add --list` to see available libraries" unless library
10842

109-
def add_lib(name, *args)
110-
source = Bashly::LibrarySource.new
111-
library = source.libraries[name.to_sym]
112-
library.args = args
43+
library.args = args['ARGS']
44+
@skip_src_check = lib_source.config.dig name, 'skip_src_check'
11345

11446
files_created = 0
11547
library.files.each do |file|

lib/bashly/docs/command.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ command.commands:
7575
help: Register a local repository
7676
7777
command.completions:
78-
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add comp`.
78+
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add completions`.
7979
url: https://bashly.dannyb.co/configuration/command/#completions
8080
example: |-
8181
commands:

lib/bashly/docs/flag.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ flag.arg:
4646
help: Clone using SSH
4747
4848
flag.completions:
49-
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add comp`. Must be accompanied by `arg`.
49+
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add completions`. Must be accompanied by `arg`.
5050
url: https://bashly.dannyb.co/configuration/flag/#completions
5151
example: |-
5252
flags:

lib/bashly/libraries/libraries.yml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,58 @@
11
colors:
2+
help: Add standard functions for printing colorful and formatted text to the lib directory.
23
files:
34
- source: "colors/colors.sh"
45
target: "%{user_lib_dir}/colors.%{user_ext}"
56

6-
completions: Bashly::Libraries::CompletionsFunction
7-
completions_script: Bashly::Libraries::CompletionsScript
8-
completions_yaml: Bashly::Libraries::CompletionsYAML
7+
completions:
8+
help: Generate a bash completions function.
9+
usage: '[PATH]'
10+
handler: Bashly::Libraries::CompletionsFunction
11+
12+
completions_script:
13+
help: Generate a standalone bash completions script.
14+
usage: '[PATH]'
15+
handler: Bashly::Libraries::CompletionsScript
16+
17+
completions_yaml:
18+
help: Generate a completions YAML configuration for Completely.
19+
usage: '[PATH]'
20+
handler: Bashly::Libraries::CompletionsYAML
921

1022
config:
23+
help: Add standard functions for handling INI files to the lib directory.
1124
files:
1225
- source: "config/config.sh"
1326
target: "%{user_lib_dir}/config.%{user_ext}"
1427

15-
help: Bashly::Libraries::Help
28+
help:
29+
help: Add a help command, in addition to the standard --help flag.
30+
handler: Bashly::Libraries::Help
1631

1732
lib:
33+
help: |-
34+
Create the lib directory for any additional user scripts.
35+
All *.sh scripts in this directory will be included in the final bash script.
36+
Note that if you configured a different partials_extension, then the extensions of the files in this directory need to match.
1837
files:
1938
- source: "lib/sample_function.sh"
2039
target: "%{user_lib_dir}/sample_function.%{user_ext}"
2140

2241
settings:
42+
help: Copy a sample settings.yml file to your project, allowing you to customize some bashly options.
43+
skip_src_check: true
2344
files:
2445
- source: "settings/settings.yml"
2546
target: "settings.yml"
2647

2748
strings:
49+
help: Copy an additional configuration file to your project, allowing you to customize all the tips and error strings.
2850
files:
2951
- source: "strings/strings.yml"
3052
target: "%{user_source_dir}/bashly-strings.yml"
3153

3254
test:
55+
help: Add approval testing.
3356
files:
3457
- source: "test/approvals.bash"
3558
target: "%{user_target_dir}/test/approvals.bash"
@@ -44,6 +67,7 @@ test:
4467
Docs: bu`https://github.com/DannyBen/approvals.bash`
4568
4669
validations:
70+
help: Add argument validation functions to the lib directory.
4771
files:
4872
- source: "validations/validate_dir_exists.sh"
4973
target: "%{user_lib_dir}/validations/validate_dir_exists.%{user_ext}"
@@ -55,6 +79,7 @@ validations:
5579
target: "%{user_lib_dir}/validations/validate_not_empty.%{user_ext}"
5680

5781
yaml:
82+
help: Add standard functions for reading YAML files.
5883
files:
5984
- source: "yaml/yaml.sh"
6085
target: "%{user_lib_dir}/yaml.%{user_ext}"

lib/bashly/library.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def find_file(path)
3737
private
3838

3939
def custom_handler
40-
return nil unless config.is_a? String
40+
return nil unless config['handler']
4141

42-
@custom_handler ||= Module.const_get(config).new(*args)
42+
@custom_handler ||= Module.const_get(config['handler']).new(*args)
4343
end
4444

4545
def target_file_args

spec/approvals/cli/add/comp-error

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/approvals/cli/add/help

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,16 @@
11
Add extra features and customization to your script
22

33
Usage:
4-
bashly add colors [--force]
5-
bashly add comp FORMAT [OUTPUT --force]
6-
bashly add config [--force]
7-
bashly add help [--force]
8-
bashly add lib [--force]
9-
bashly add settings [--force]
10-
bashly add strings [--force]
11-
bashly add test [--force]
12-
bashly add validations [--force]
13-
bashly add yaml [--force]
4+
bashly add LIBRARY [ARGS...] [--force]
5+
bashly add --list
146
bashly add (-h|--help)
157

16-
Commands:
17-
colors
18-
Add standard functions for printing colorful and formatted text to the lib
19-
directory.
20-
21-
comp
22-
Generate a bash completions script or function.
23-
24-
config
25-
Add standard functions for handling INI files to the lib directory.
26-
27-
help
28-
Add a help command, in addition to the standard --help flag.
29-
30-
lib
31-
Create the lib directory for any additional user scripts.
32-
All *.sh scripts in this directory will be included in the final bash
33-
script.
34-
Note that if you configured a different partials_extension, then the
35-
extensions of the files in this directory need to match.
36-
37-
settings
38-
Copy a sample settings.yml file to your project, allowing you to customize
39-
some bashly options.
40-
41-
strings
42-
Copy an additional configuration file to your project, allowing you to
43-
customize all the tips and error strings.
44-
45-
test
46-
Add approval testing.
47-
48-
validations
49-
Add argument validation functions to the lib directory.
50-
51-
yaml
52-
Add standard functions for reading YAML files to the lib directory.
53-
548
Options:
559
-f --force
5610
Overwrite existing files
5711

12+
-l --list
13+
Show available libraries
14+
5815
-h --help
5916
Show this help
60-
61-
Parameters:
62-
FORMAT
63-
Output format, can be one of:
64-
function : generate a function file to be included in your script.
65-
script : generate a standalone bash completions script.
66-
yaml : generate a yaml compatible with completely.
67-
68-
OUTPUT
69-
For the 'comp function' command: Name of the generated function.
70-
For the 'comp script' or 'comp yaml' commands: path to output file.
71-
In all cases, this is optional and will have sensible defaults.
72-
73-
Examples:
74-
bashly add strings --force
75-
bashly add comp function
76-
bashly add comp script completions.bash

0 commit comments

Comments
 (0)