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
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Each of these examples demonstrates one aspect or feature of bashly.
- [command-paths](command-paths#readme) - configuring nested paths for your source scripts
- [command-function](command-function#readme) - configuring custom internal function names
- [split-config](split-config#readme) - splitting your `bashly.yml` into several smaller files
- [multiple-lib-dirs](multiple-lib-dirs#readme) - loading libraries from multiple locations

## Bashly library features

Expand Down
2 changes: 1 addition & 1 deletion examples/command-paths/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ commands:
# The path to use for command files, relative to source_dir
# When set to nil (~), command files will be placed directly under source_dir
# When set to any other string, command files will be placed under this
# directory, and each command will get its own subdirectory
# directory, and each command will get its own sub-directory

# commands_dir: ~
commands_dir: commands
Expand Down
2 changes: 1 addition & 1 deletion examples/command-paths/settings.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# The path to use for command files, relative to source_dir
# When set to nil (~), command files will be placed directly under source_dir
# When set to any other string, command files will be placed under this
# directory, and each command will get its own subdirectory
# directory, and each command will get its own sub-directory

# commands_dir: ~
commands_dir: commands
2 changes: 1 addition & 1 deletion examples/custom-includes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ echo "After custom code"
# Note that code here should be wrapped inside bash functions, and it is
# recommended to have a separate file for each function.
#
# Subdirectories will also be scanned for *.sh, so you have no reason not
# Sub-directories will also be scanned for *.sh, so you have no reason not
# to organize your code neatly.
#
sample_function() {
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-includes/src/lib/sample_function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Note that code here should be wrapped inside bash functions, and it is
# recommended to have a separate file for each function.
#
# Subdirectories will also be scanned for *.sh, so you have no reason not
# Sub-directories will also be scanned for *.sh, so you have no reason not
# to organize your code neatly.
#
sample_function() {
Expand Down
1 change: 1 addition & 0 deletions examples/multiple-lib-dirs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
download
77 changes: 77 additions & 0 deletions examples/multiple-lib-dirs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Multiple Lib Dirs

Demonstrates how to include more than one lib directories in the generated
script.

This example was generated with:

```bash
$ bashly init --minimal
$ bashly add settings
# ... now edit settings.yml to match the example ...
$ bashly generate
# ... now edit src/root_command.sh to match the example ...
$ bashly generate
```

<!-- include: settings.yml src/root_command.sh -->

-----

## `bashly.yml`

````yaml
name: download
help: Sample minimal application without commands
version: 0.1.0

args:
- name: source
required: true
help: URL to download from
- name: target
help: "Target filename (default: same as source)"

flags:
- long: --force
short: -f
help: Overwrite existing files

examples:
- download example.com
- download example.com ./output -f
````

## `settings.yml`

````yaml
# An array or comma delimited string of additional directories to search for
# bash functions. Any bash script found in any of these directories
# (or sub-directories) will be merged into the final script.
# Note that this is relative to the working directory.
extra_lib_dirs: [common_lib, cloud_lib]

````

## `src/root_command.sh`

````bash
# Calling library functions
common_function
cloud_function
````


## Output

### `$ ./download some_source`

````shell
common_function called
cloud_function called


````



3 changes: 3 additions & 0 deletions examples/multiple-lib-dirs/cloud_lib/cloud_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cloud_function() {
echo "cloud_function called"
}
3 changes: 3 additions & 0 deletions examples/multiple-lib-dirs/common_lib/common_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
common_function() {
echo "common_function called"
}
5 changes: 5 additions & 0 deletions examples/multiple-lib-dirs/settings.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# An array or comma delimited string of additional directories to search for
# bash functions. Any bash script found in any of these directories
# (or sub-directories) will be merged into the final script.
# Note that this is relative to the working directory.
extra_lib_dirs: [common_lib, cloud_lib]
19 changes: 19 additions & 0 deletions examples/multiple-lib-dirs/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: download
help: Sample minimal application without commands
version: 0.1.0

args:
- name: source
required: true
help: URL to download from
- name: target
help: "Target filename (default: same as source)"

flags:
- long: --force
short: -f
help: Overwrite existing files

examples:
- download example.com
- download example.com ./output -f
3 changes: 3 additions & 0 deletions examples/multiple-lib-dirs/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Calling library functions
common_function
cloud_function
9 changes: 9 additions & 0 deletions examples/multiple-lib-dirs/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -x

bashly generate

### Try Me ###

./download some_source
2 changes: 1 addition & 1 deletion lib/bashly/libraries/lib/sample_function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Note that code here should be wrapped inside bash functions, and it is
## recommended to have a separate file for each function.
##
## Subdirectories will also be scanned for *.sh, so you have no reason not
## Sub-directories will also be scanned for *.sh, so you have no reason not
## to organize your code neatly.
##
sample_function() {
Expand Down
8 changes: 7 additions & 1 deletion lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,16 @@ target_dir: .
# The path to use for common library files, relative to source_dir
lib_dir: lib

# An array or comma delimited string of additional directories to search for
# bash functions. Any bash script found in any of these directories
# (or sub-directories) will be merged into the final script.
# Note that this is relative to the working directory.
extra_lib_dirs: ~

# The path to use for command files, relative to source_dir
# When set to nil (~), command files will be placed directly under source_dir
# When set to any other string, command files will be placed under this
# directory, and each command will get its own subdirectory
# directory, and each command will get its own sub-directory
commands_dir: ~

# The extension to use when reading/writing partial script snippets
Expand Down
8 changes: 7 additions & 1 deletion lib/bashly/script/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ def base_usage_pattern
# This is meant to provide the user with the ability to add custom
# functions
def user_lib
@user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.#{Settings.partials_extension}"]
@user_lib ||= begin
result = Settings.all_lib_dirs.map do |dir|
Dir["#{dir}/**/*.#{Settings.partials_extension}"]
end

result.flatten
end
end

# Returns a mixed array of Argument and Flag objects that have validations
Expand Down
16 changes: 16 additions & 0 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class << self
:enable_inspect_args,
:enable_sourcing,
:enable_view_markers,
:extra_lib_dirs,
:formatter,
:function_names,
:lib_dir,
Expand Down Expand Up @@ -107,6 +108,21 @@ def lib_dir
@lib_dir ||= get :lib_dir
end

def extra_lib_dirs
@extra_lib_dirs ||= begin
dirs = get :extra_lib_dirs
case dirs
when Array then dirs
when String then dirs.split(',').map(&:strip)
else []
end
end
end

def all_lib_dirs
@all_lib_dirs = [full_lib_dir] + extra_lib_dirs
end

def partials_extension
@partials_extension ||= get :partials_extension
end
Expand Down
31 changes: 31 additions & 0 deletions schemas/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@
"minLength": 1,
"default": "lib"
},
"extra_lib_dirs": {
"title": "extra lib dirs",
"description": "One or more paths to use for common library files, relative to the working directory.\nMay be provided as an array or a comma delimited string.\nhttps://bashly.dev/usage/settings/#extra_lib_dirs",
"oneOf": [
{
"type": "null"
},
{
"type": "string",
"minLength": 1,
"examples": [
"common, org_lib",
"lib"
]
},
{
"type": "array",
"items": {
"type": "string",
"minLength": 1
},
"examples": [
[
"common",
"org_lib"
]
]
}
],
"default": null
},
"commands_dir": {
"title": "commands dir",
"description": "The path to use for command files, relative to source_dir\nhttps://bashly.dev/usage/settings/#commands_dir",
Expand Down
8 changes: 8 additions & 0 deletions spec/approvals/examples/multiple-lib-dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+ bashly generate
creating user files in src
skipped src/root_command.sh (exists)
created ./download
run ./download --help to test your bash script
+ ./download some_source
common_function called
cloud_function called
20 changes: 20 additions & 0 deletions spec/bashly/settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@
end
end

describe '::extra_lib_dirs' do
context 'when set using env var as a comma delimited string' do
before { ENV['BASHLY_EXTRA_LIB_DIRS'] = 'one,two' }
after { ENV['BASHLY_EXTRA_LIB_DIRS'] = nil }

it 'converts comma delimited string to an array' do
expect(subject.extra_lib_dirs).to match_array %w[one two]
end
end

context 'when provided as an array' do
let(:config) { Config.new({ 'extra_lib_dirs' => %w[item1 item2] }) }

it 'returns the array as is' do
allow(subject).to receive(:user_settings).and_return(config)
expect(subject.extra_lib_dirs).to match_array %w[item1 item2]
end
end
end

describe '::production?' do
it 'returns false by default' do
expect(subject.production?).to be false
Expand Down
21 changes: 21 additions & 0 deletions support/schema/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ properties:
type: string
minLength: 1
default: lib
extra_lib_dirs:
title: extra lib dirs
description: |-
One or more paths to use for common library files, relative to the working directory.
May be provided as an array or a comma delimited string.
https://bashly.dev/usage/settings/#extra_lib_dirs
oneOf:
- type: "null"
- type: string
minLength: 1
examples:
- "common, org_lib"
- "lib"
- type: array
items:
type: string
minLength: 1
examples:
- [common, org_lib]
default: null

commands_dir:
title: commands dir
description: |-
Expand Down