Skip to content

Commit d6f192b

Browse files
authored
Merge pull request #154 from DannyBen/add/yaml-compose
Add ability to import external snippets in bashly.yml
2 parents c019766 + 36aefb5 commit d6f192b

File tree

19 files changed

+433
-3
lines changed

19 files changed

+433
-3
lines changed

examples/split-config/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src/download_command.sh
2+
src/initialize.sh
3+
cli

examples/split-config/README.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Split Config Example
2+
3+
Demonstrates how to separate your bashly.yml into several files, and load
4+
configuration data from other YAML files, or directly from a YAML front matter
5+
in your code.
6+
7+
This example was generated with:
8+
9+
```bash
10+
$ bashly init
11+
$ bashly generate
12+
# ... now edit all files in the src folder ...
13+
$ bashly generate
14+
```
15+
16+
<!-- include: src/download_command.yml src/upload_command.sh src/common_flags.yml -->
17+
18+
-----
19+
20+
## `bashly.yml`
21+
22+
```yaml
23+
name: cli
24+
help: Configuration splitting example
25+
version: 0.1.0
26+
27+
commands:
28+
# Import a command that is defined in another YAML file
29+
- import: src/download_command.yml
30+
31+
# Import a command that is defined in the front matter of its own shell
32+
# function.
33+
- import: src/upload_command.sh
34+
```
35+
36+
## `src/download_command.yml`
37+
38+
```yaml
39+
name: download
40+
short: d
41+
help: Download a file
42+
43+
args:
44+
- name: source
45+
required: true
46+
help: URL to download from
47+
- name: target
48+
help: "Target filename (default: same as source)"
49+
50+
flags:
51+
import: src/common_flags.yml
52+
53+
```
54+
55+
## `src/upload_command.sh`
56+
57+
```bash
58+
# This is a YAML front matter describing the command
59+
# It is imported to bashly.yml when running "bashly generate"
60+
61+
name: upload
62+
short: u
63+
help: Upload a file
64+
args:
65+
- name: source
66+
required: true
67+
help: File to upload
68+
69+
flags:
70+
import: src/common_flags.yml
71+
72+
---
73+
# Shell script starts here
74+
inspect_args
75+
76+
```
77+
78+
## `src/common_flags.yml`
79+
80+
```yaml
81+
- long: --force
82+
short: -f
83+
help: Overwrite existing files
84+
85+
```
86+
87+
88+
## Generated script output
89+
90+
### `$ ./cli`
91+
92+
```shell
93+
cli - Configuration splitting example
94+
95+
Usage:
96+
cli [command]
97+
cli [command] --help | -h
98+
cli --version | -v
99+
100+
Commands:
101+
download Download a file
102+
upload Upload a file
103+
104+
105+
106+
```
107+
108+
### `$ ./cli download -h`
109+
110+
```shell
111+
cli download - Download a file
112+
113+
Shortcut: d
114+
115+
Usage:
116+
cli download SOURCE [TARGET] [options]
117+
cli download --help | -h
118+
119+
Options:
120+
--help, -h
121+
Show this help
122+
123+
--force, -f
124+
Overwrite existing files
125+
126+
Arguments:
127+
SOURCE
128+
URL to download from
129+
130+
TARGET
131+
Target filename (default: same as source)
132+
133+
134+
135+
```
136+
137+
### `$ ./cli upload -h`
138+
139+
```shell
140+
cli upload - Upload a file
141+
142+
Shortcut: u
143+
144+
Usage:
145+
cli upload SOURCE [options]
146+
cli upload --help | -h
147+
148+
Options:
149+
--help, -h
150+
Show this help
151+
152+
--force, -f
153+
Overwrite existing files
154+
155+
Arguments:
156+
SOURCE
157+
File to upload
158+
159+
160+
161+
```
162+
163+
164+

examples/split-config/src/bashly.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: cli
2+
help: Configuration splitting example
3+
version: 0.1.0
4+
5+
commands:
6+
# Import a command that is defined in another YAML file
7+
- import: src/download_command.yml
8+
9+
# Import a command that is defined in the front matter of its own shell
10+
# function.
11+
- import: src/upload_command.sh
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- long: --force
2+
short: -f
3+
help: Overwrite existing files
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: download
2+
short: d
3+
help: Download a file
4+
5+
args:
6+
- name: source
7+
required: true
8+
help: URL to download from
9+
- name: target
10+
help: "Target filename (default: same as source)"
11+
12+
flags:
13+
import: src/common_flags.yml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This is a YAML front matter describing the command
2+
# It is imported to bashly.yml when running "bashly generate"
3+
4+
name: upload
5+
short: u
6+
help: Upload a file
7+
args:
8+
- name: source
9+
required: true
10+
help: File to upload
11+
12+
flags:
13+
import: src/common_flags.yml
14+
15+
---
16+
# Shell script starts here
17+
inspect_args

examples/split-config/test.sh

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

lib/bashly.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
requires 'bashly/settings'
1111
requires 'bashly/exceptions'
12+
requires 'bashly/refinements'
1213
requires 'bashly/script/base'
1314
requires 'bashly/commands/base'
1415
requires 'bashly/libraries/base'

lib/bashly/commands/generate.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Bashly
22
module Commands
33
class Generate < Base
4+
using ComposeRefinements
5+
46
help "Generate the bash script and required files"
57

68
usage "bashly generate [--force --quiet --upgrade --wrap FUNCTION]"
@@ -116,7 +118,7 @@ def master_script_path
116118
end
117119

118120
def config
119-
@config ||= Config.new "#{Settings.source_dir}/bashly.yml"
121+
@config ||= Config.new("#{Settings.source_dir}/bashly.yml").compose
120122
end
121123

122124
def command

lib/bashly/extensions/string.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ def lint
2727
gsub(/\s+\n/m, "\n\n").lines.reject { |l| l =~ /^\s*##/ }.join ""
2828
end
2929

30+
def remove_front_matter
31+
split(/^---\s*/).last
32+
end
33+
3034
end

0 commit comments

Comments
 (0)