Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/completions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This example was generated with:
```bash
$ bashly init
# ... now edit src/bashly.yml to match the example ...
$ bashly add comp function
$ bashly add completions
$ bashly generate
# ... now edit src/completions_command.sh ...
$ bashly generate
Expand Down Expand Up @@ -112,7 +112,7 @@ commands:
```bash
# Call the `send_completions` function which was added by running:
#
# $ bashly add comp function
# $ bashly add completions
#
# Users can now enable bash completion for this script by running:
#
Expand Down
2 changes: 1 addition & 1 deletion examples/completions/src/completions_command.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Call the `send_completions` function which was added by running:
#
# $ bashly add comp function
# $ bashly add completions
#
# Users can now enable bash completion for this script by running:
#
Expand Down
2 changes: 1 addition & 1 deletion examples/completions/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -x

bashly add comp function --force
bashly add completions --force
bashly generate

### Try Me ###
Expand Down
114 changes: 27 additions & 87 deletions lib/bashly/commands/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,116 +3,56 @@ module Commands
class Add < Base
help 'Add extra features and customization to your script'

usage 'bashly add colors [--force]'
usage 'bashly add comp FORMAT [OUTPUT --force]'
usage 'bashly add config [--force]'
usage 'bashly add help [--force]'
usage 'bashly add lib [--force]'
usage 'bashly add settings [--force]'
usage 'bashly add strings [--force]'
usage 'bashly add test [--force]'
usage 'bashly add validations [--force]'
usage 'bashly add yaml [--force]'
usage 'bashly add LIBRARY [ARGS...] [--force]'
usage 'bashly add --list'
usage 'bashly add (-h|--help)'

option '-f --force', 'Overwrite existing files'

param 'FORMAT', <<~USAGE
Output format, can be one of:
function : generate a function file to be included in your script.
script : generate a standalone bash completions script.
yaml : generate a yaml compatible with completely.
USAGE

param 'OUTPUT', <<~USAGE
For the 'comp function' command: Name of the generated function.
For the 'comp script' or 'comp yaml' commands: path to output file.
In all cases, this is optional and will have sensible defaults.
USAGE

command 'colors', 'Add standard functions for printing colorful and formatted text to the lib directory.'
command 'comp', 'Generate a bash completions script or function.'
command 'config', 'Add standard functions for handling INI files to the lib directory.'
command 'help', 'Add a help command, in addition to the standard --help flag.'
command 'lib', <<~USAGE
Create the lib directory for any additional user scripts.
All *.sh scripts in this directory will be included in the final bash script.
Note that if you configured a different partials_extension, then the extensions of the files in this directory need to match.
USAGE

command 'settings', 'Copy a sample settings.yml file to your project, allowing you to customize some ' \
'bashly options.'

command 'strings', 'Copy an additional configuration file to your project, allowing you to customize all the ' \
'tips and error strings.'

command 'test', 'Add approval testing.'
command 'validations', 'Add argument validation functions to the lib directory.'
command 'yaml', 'Add standard functions for reading YAML files to the lib directory.'
example 'bashly add strings --force'
example 'bashly add comp function'
example 'bashly add comp script completions.bash'
option '-l --list', 'Show available libraries'

attr_reader :skip_src_check

def colors_command
add_lib 'colors'
end

def comp_command
format = args['FORMAT']
output = args['OUTPUT']

case format
when 'script' then add_lib 'completions_script', output
when 'function' then add_lib 'completions', output
when 'yaml' then add_lib 'completions_yaml', output
else raise Error, "Unrecognized format: #{format}"
def run
if args['--list']
show_list
else
add_lib args['LIBRARY']
end
end

def config_command
add_lib 'config'
end

def lib_command
add_lib 'lib'
end

def settings_command
@skip_src_check = true
add_lib 'settings'
end
private

def strings_command
add_lib 'strings'
def lib_source
@lib_source ||= Bashly::LibrarySource.new
end

def test_command
add_lib 'test'
def show_list
lib_source.config.each do |key, config|
usage = key
usage += " #{config['usage']}" if config['usage']
say "g`bashly add #{usage}`"
say word_wrap(" #{config['help']}")
say ''
end
end

def help_command
add_lib 'help'
end
def add_lib(name)
library = lib_source.libraries[name.to_sym]
raise "Unknown library: g`#{name}`\nRun m`bashly add --list` to see available libraries" unless library

def validations_command
add_lib 'validations'
end
library.args = args['ARGS']
@skip_src_check = lib_source.config.dig name, 'skip_src_check'

def yaml_command
add_lib 'yaml'
add_library_files library
end

private

def add_lib(name, *args)
library = Bashly::Library.new name, *args
def add_library_files(library)
files_created = 0
library.files.each do |file|
created = safe_write file[:path], file[:content]
files_created += 1 if created
end

message = library.post_install_message
say "\n#{message}" if message && files_created.positive?
end
Expand Down
11 changes: 7 additions & 4 deletions lib/bashly/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,19 @@ def generated_files
end

def upgrade(existing_file, library_name, *args)
if Library.exist? library_name
upgrade! existing_file, library_name, *args
source = Bashly::LibrarySource.new
library = source.libraries[library_name.to_sym]

if library
library.args = args
upgrade! existing_file, library
else
quiet_say "r`warning` not upgrading c`#{existing_file}`, " \
"unknown library '#{library_name}'"
end
end

def upgrade!(existing_file, library_name, *args)
library = Bashly::Library.new library_name, *args
def upgrade!(existing_file, library)
file = library.find_file existing_file

if file
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/docs/command.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ command.commands:
help: Register a local repository

command.completions:
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add comp`.
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add completions`.
url: https://bashly.dannyb.co/configuration/command/#completions
example: |-
commands:
Expand Down
2 changes: 1 addition & 1 deletion lib/bashly/docs/flag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ flag.arg:
help: Clone using SSH

flag.completions:
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add comp`. Must be accompanied by `arg`.
help: Specify a list of additional completion suggestions when used in conjunction with `bashly add completions`. Must be accompanied by `arg`.
url: https://bashly.dannyb.co/configuration/flag/#completions
example: |-
flags:
Expand Down
60 changes: 0 additions & 60 deletions lib/bashly/libraries.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def post_install_message
private

def help_command
asset_content('templates/lib/help_command.sh') % { name: command.name }
asset_content('libraries/help/help_command.sh') % { name: command.name }
end
end
end
Expand Down
85 changes: 85 additions & 0 deletions lib/bashly/libraries/libraries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
colors:
help: Add standard functions for printing colorful and formatted text to the lib directory.
files:
- source: "colors/colors.sh"
target: "%{user_lib_dir}/colors.%{user_ext}"

completions:
help: Generate a bash completions function.
usage: '[PATH]'
handler: Bashly::Libraries::CompletionsFunction

completions_script:
help: Generate a standalone bash completions script.
usage: '[PATH]'
handler: Bashly::Libraries::CompletionsScript

completions_yaml:
help: Generate a completions YAML configuration for Completely.
usage: '[PATH]'
handler: Bashly::Libraries::CompletionsYAML

config:
help: Add standard functions for handling INI files to the lib directory.
files:
- source: "config/config.sh"
target: "%{user_lib_dir}/config.%{user_ext}"

help:
help: Add a help command, in addition to the standard --help flag.
handler: Bashly::Libraries::Help

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

settings:
help: Copy a sample settings.yml file to your project, allowing you to customize some bashly options.
skip_src_check: true
files:
- source: "settings/settings.yml"
target: "settings.yml"

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

test:
help: Add approval testing.
files:
- source: "test/approvals.bash"
target: "%{user_target_dir}/test/approvals.bash"
- source: "test/approve"
target: "%{user_target_dir}/test/approve"

post_install_message: |
Edit your tests in g`test/approve` and then run:

m`$ test/approve`

Docs: bu`https://github.com/DannyBen/approvals.bash`

validations:
help: Add argument validation functions to the lib directory.
files:
- source: "validations/validate_dir_exists.sh"
target: "%{user_lib_dir}/validations/validate_dir_exists.%{user_ext}"
- source: "validations/validate_file_exists.sh"
target: "%{user_lib_dir}/validations/validate_file_exists.%{user_ext}"
- source: "validations/validate_integer.sh"
target: "%{user_lib_dir}/validations/validate_integer.%{user_ext}"
- source: "validations/validate_not_empty.sh"
target: "%{user_lib_dir}/validations/validate_not_empty.%{user_ext}"

yaml:
help: Add standard functions for reading YAML files.
files:
- source: "yaml/yaml.sh"
target: "%{user_lib_dir}/yaml.%{user_ext}"
File renamed without changes.
File renamed without changes.
Loading