Skip to content

Commit 02701df

Browse files
committed
- Add a 'deps' associative array to the generated bash script
1 parent c32ef47 commit 02701df

File tree

16 files changed

+205
-13
lines changed

16 files changed

+205
-13
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Alternate Dependencies Example
2+
3+
Demonstrates how to require your script's user to have at least one of a list
4+
of dependencies (commands) installed prior to using your script.
5+
6+
This example was generated with:
7+
8+
```bash
9+
$ bashly init
10+
$ bashly add colors
11+
# ... now edit src/bashly.yml to match the example ...
12+
$ bashly generate
13+
```
14+
15+
-----
16+
17+
## `bashly.yml`
18+
19+
```yaml
20+
name: cli
21+
help: Sample application that requires alternate dependencies
22+
version: 0.1.0
23+
24+
commands:
25+
- name: download
26+
help: Download something
27+
28+
# This is the explicit way of defining dependencies.
29+
# The generated script will be halted (with a friendly error message) if
30+
# any of these programs are not installed:
31+
dependencies:
32+
# Abort with just the default 'missing dependency: git' message if git
33+
# is not found
34+
git:
35+
36+
# Abort with an additional help message if ruby is not found
37+
ruby: visit $(blue_underlined https://www.ruby-lang.org) to install
38+
39+
# Abort if both curl and wget are not found, and show an additional help
40+
# message.
41+
# Note that in this case you will also have a `deps` hash in your
42+
# bash script, with the path to the first found program.
43+
http_client:
44+
command: [curl, wget]
45+
help: install with $(green sudo apt install curl)
46+
```
47+
48+
49+
50+
## Generated script output
51+
52+
### `$ ./cli download`
53+
54+
```shell
55+
# this file is located in 'src/download_command.sh'
56+
# code for 'cli download' goes here
57+
# you can edit it freely and regenerate (it will not be overwritten)
58+
args: none
59+
60+
deps:
61+
- ${deps[git]} = /usr/bin/git
62+
- ${deps[http_client]} = /usr/bin/curl
63+
- ${deps[ruby]} = /home/vagrant/.rbenv/versions/3.1.3/bin/ruby
64+
65+
66+
```
67+
68+
69+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: cli
2+
help: Sample application that requires alternate dependencies
3+
version: 0.1.0
4+
5+
commands:
6+
- name: download
7+
help: Download something
8+
9+
# This is the explicit way of defining dependencies.
10+
# The generated script will be halted (with a friendly error message) if
11+
# any of these programs are not installed:
12+
dependencies:
13+
# Abort with just the default 'missing dependency: git' message if git
14+
# is not found
15+
git:
16+
17+
# Abort with an additional help message if ruby is not found
18+
ruby: visit $(blue_underlined https://www.ruby-lang.org) to install
19+
20+
# Abort if both curl and wget are not found, and show an additional help
21+
# message.
22+
# Note that in this case you will also have a `deps` hash in your
23+
# bash script, with the path to the first found program.
24+
http_client:
25+
command: [curl, wget]
26+
help: install with $(green sudo apt install curl)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
echo "# this file is located in 'src/download_command.sh'"
2+
echo "# code for 'cli download' goes here"
3+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
4+
inspect_args
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Code here runs inside the initialize() function
2+
## Use it for anything that you need to run before any other function, like
3+
## setting environment variables:
4+
## CONFIG_FILE=settings.ini
5+
##
6+
## Feel free to empty (but not delete) this file.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Color functions [@bashly-upgrade colors]
2+
## This file is a part of Bashly standard library
3+
##
4+
## Usage:
5+
## Use any of the functions below to color or format a portion of a string.
6+
##
7+
## echo "before $(red this is red) after"
8+
## echo "before $(green_bold this is green_bold) after"
9+
##
10+
## Color output will be disabled if `NO_COLOR` environment variable is set
11+
## in compliance with https://no-color.org/
12+
##
13+
print_in_color() {
14+
local color="$1"
15+
shift
16+
if [[ -z ${NO_COLOR+x} ]]; then
17+
printf "$color%b\e[0m\n" "$*"
18+
else
19+
printf "%b\n" "$*"
20+
fi
21+
}
22+
23+
red() { print_in_color "\e[31m" "$*"; }
24+
green() { print_in_color "\e[32m" "$*"; }
25+
yellow() { print_in_color "\e[33m" "$*"; }
26+
blue() { print_in_color "\e[34m" "$*"; }
27+
magenta() { print_in_color "\e[35m" "$*"; }
28+
cyan() { print_in_color "\e[36m" "$*"; }
29+
bold() { print_in_color "\e[1m" "$*"; }
30+
underlined() { print_in_color "\e[4m" "$*"; }
31+
red_bold() { print_in_color "\e[1;31m" "$*"; }
32+
green_bold() { print_in_color "\e[1;32m" "$*"; }
33+
yellow_bold() { print_in_color "\e[1;33m" "$*"; }
34+
blue_bold() { print_in_color "\e[1;34m" "$*"; }
35+
magenta_bold() { print_in_color "\e[1;35m" "$*"; }
36+
cyan_bold() { print_in_color "\e[1;36m" "$*"; }
37+
red_underlined() { print_in_color "\e[4;31m" "$*"; }
38+
green_underlined() { print_in_color "\e[4;32m" "$*"; }
39+
yellow_underlined() { print_in_color "\e[4;33m" "$*"; }
40+
blue_underlined() { print_in_color "\e[4;34m" "$*"; }
41+
magenta_underlined() { print_in_color "\e[4;35m" "$*"; }
42+
cyan_underlined() { print_in_color "\e[4;36m" "$*"; }

examples/dependencies-alt/test.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
rm -f src/lib/colors.sh
4+
rm -f src/*.sh
5+
6+
set -x
7+
8+
bashly add colors
9+
bashly generate
10+
11+
### Try Me ###
12+
13+
./cli download

lib/bashly/config_validator.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ def assert_dependencies_hash(key, value)
6464
end
6565

6666
def assert_dependency(key, value)
67-
assert [String, Hash].include?(value.class),
68-
"#{key} must be a string or a hash"
67+
assert [String, Hash, NilClass].include?(value.class),
68+
"#{key} must be a string, a hash or nil"
6969

70-
return if value.is_a? String
70+
return unless value.is_a? Hash
7171

7272
assert_hash key, value, keys: Script::Dependency.option_keys
7373
assert_string_or_array "#{key}.command", value['command']

lib/bashly/docs/command.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ command.dependencies:
113113
- docker
114114
- curl
115115
116-
# Simple hash syntax, to provide additional help message
116+
# Simple hash syntax, to provide additional (optional) help message
117117
dependencies:
118118
docker: see https://docker.com for installation instructions
119119
git: "install by running: sudo apt install git"
120+
ruby:
120121
121122
# Explicit hash syntax, to provide additional help message and
122123
# look for "one of" a given list of dependencies

lib/bashly/script/dependency.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ def initialize(label: nil, commands: nil, help: nil)
3030
@help = help&.empty? ? nil : help
3131
end
3232

33+
def multi?
34+
@multi ||= commands.size > 1
35+
end
36+
3337
def name
34-
@name ||= if commands.size == 1
35-
commands.first
36-
else
37-
"#{label} (#{commands.join '/'})"
38-
end
38+
@name ||= multi? ? "#{label} (#{commands.join '/'})" : label
3939
end
4040
end
4141
end

0 commit comments

Comments
 (0)