Skip to content

Commit d8a9e2e

Browse files
authored
Merge pull request #120 from DannyBen/add/custom-script-header
Add support for a custom script header
2 parents 4a16a90 + 2078aa4 commit d8a9e2e

File tree

13 files changed

+185
-15
lines changed

13 files changed

+185
-15
lines changed

examples/completions/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,13 @@ Options:
172172
# Modifying it manually is not recommended
173173
_cli_completions() {
174174
local cur=${COMP_WORDS[COMP_CWORD]}
175+
local comp_line="${COMP_WORDS[*]:1}"
175176

176-
case "$COMP_LINE" in
177-
'cli completions'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;
178-
'cli download'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;
179-
'cli upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;
180-
'cli'*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;
177+
case "$comp_line" in
178+
'completions'*) COMPREPLY=($(compgen -W "--help -h" -- "$cur")) ;;
179+
'download'*) COMPREPLY=($(compgen -A file -W "--force --help -f -h" -- "$cur")) ;;
180+
'upload'*) COMPREPLY=($(compgen -A directory -A user -W "--help --password --user -h -p -u" -- "$cur")) ;;
181+
''*) COMPREPLY=($(compgen -W "--help --version -h -v completions download upload" -- "$cur")) ;;
181182
esac
182183
}
183184

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
download
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Custom Script Header Example
2+
3+
This example shows how to replace the script header by simply placing a
4+
file called `header.sh` in your `src` folder.
5+
6+
Under most circumstances, you should not do it, and instead put your
7+
initialization code in `src/initialize.sh`.
8+
9+
This example was generated with:
10+
11+
```bash
12+
$ bashly init --minimal
13+
$ bashly generate
14+
```
15+
16+
<!-- include: src/header.sh -->
17+
18+
-----
19+
20+
## `bashly.yml`
21+
22+
```yaml
23+
name: download
24+
help: Sample minimal application without commands
25+
version: 0.1.0
26+
27+
args:
28+
- name: source
29+
required: true
30+
help: URL to download from
31+
- name: target
32+
help: "Target filename (default: same as source)"
33+
34+
flags:
35+
- long: --force
36+
short: -f
37+
help: Overwrite existing files
38+
39+
examples:
40+
- download example.com
41+
- download example.com ./output -f
42+
```
43+
44+
## `src/header.sh`
45+
46+
```bash
47+
#!/usr/bin/env bash
48+
49+
# This is a custom header that will be injected to the very top of the
50+
# script, replacing the original header.
51+
# Note that in most cases you should use initialize.sh instead.
52+
53+
some_condition=true
54+
55+
if [ "$some_condition" = true ]; then
56+
echo "Some condition was met, aborting"
57+
exit
58+
fi
59+
60+
61+
```
62+
63+
64+
## Generated script output
65+
66+
### `$ ./download`
67+
68+
```shell
69+
Some condition was met, aborting
70+
71+
72+
```
73+
74+
75+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: download
2+
help: Sample minimal application without commands
3+
version: 0.1.0
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+
- long: --force
14+
short: -f
15+
help: Overwrite existing files
16+
17+
examples:
18+
- download example.com
19+
- download example.com ./output -f
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+
# This is a custom header that will be injected to the very top of the
4+
# script, replacing the original header.
5+
# Note that in most cases you should use initialize.sh instead.
6+
7+
some_condition=true
8+
9+
if [ "$some_condition" = true ]; then
10+
echo "Some condition was met, aborting"
11+
exit
12+
fi
13+
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 vairables:
4+
# CONFIG_FILE=settings.ini
5+
#
6+
# Feel free to empty (but not delete) this file.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
echo "# this file is located in 'src/root_command.sh'"
2+
echo "# you can edit it freely and regenerate (it will not be overwritten)"
3+
inspect_args
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bashly generate
6+
7+
### Try Me ###
8+
9+
./download

lib/bashly/models/script.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,24 @@ def code
2222
private
2323

2424
def header
25-
@header ||= render('header')
25+
@header ||= header!
26+
end
27+
28+
def header!
29+
if File.exist? custom_header_path
30+
File.read custom_header_path
31+
else
32+
render('header')
33+
end
2634
end
2735

2836
def body
2937
@body ||= command.render('master_script')
3038
end
39+
40+
def custom_header_path
41+
@custom_header_path ||= "#{Settings.source_dir}/header.sh"
42+
end
3143
end
3244
end
3345
end

spec/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ Run tests with either of these commands:
1010
$ [bundle exec] run spec
1111
$ [bundle exec] rspec
1212

13-
Also note that running the tests will regenerate all examples.
13+
## Notes about Example Tests
14+
15+
1. Running the tests will regenerate all examples.
16+
2. The generated example executables are .gitignored.
17+
3. Each example contains a `test.sh` file, which calls `bashly generate`
18+
(without `bundle exec`) before proceeding with the test. For this reason, you
19+
*might* need to install the local gem before running tests, you can do so by
20+
running `run gem build --install`).
1421

1522

1623
[1]: https://github.com/DannyBen/rspec_approvals

0 commit comments

Comments
 (0)