Skip to content

Commit ab97e1a

Browse files
authored
Merge pull request #339 from DannyBen/update/libraries
Refactor libraries
2 parents 0012108 + 245ceb9 commit ab97e1a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+298
-319
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: 27 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -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}`\nRun 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

lib/bashly/commands/generate.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,19 @@ def generated_files
9292
end
9393

9494
def upgrade(existing_file, library_name, *args)
95-
if Library.exist? library_name
96-
upgrade! existing_file, library_name, *args
95+
source = Bashly::LibrarySource.new
96+
library = source.libraries[library_name.to_sym]
97+
98+
if library
99+
library.args = args
100+
upgrade! existing_file, library
97101
else
98102
quiet_say "r`warning` not upgrading c`#{existing_file}`, " \
99103
"unknown library '#{library_name}'"
100104
end
101105
end
102106

103-
def upgrade!(existing_file, library_name, *args)
104-
library = Bashly::Library.new library_name, *args
107+
def upgrade!(existing_file, library)
105108
file = library.find_file existing_file
106109

107110
if 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.yml

Lines changed: 0 additions & 60 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)