Skip to content

Commit 1546863

Browse files
committed
- Add start() bash function to wrap __complete, initialize and run`
1 parent d1deb7e commit 1546863

8 files changed

Lines changed: 54 additions & 14 deletions

File tree

‎lib/bashly/libraries/settings/settings.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,6 @@ var_aliases:
200200

201201
# Choose different names for some of the internal functions.
202202
function_names:
203+
start: ~
203204
run: ~
204205
initialize: ~

‎lib/bashly/views/command/master_script.gtx‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
= render :initialize
1414
= render :run
1515

16-
>
16+
>
17+
= render :start
18+
>
1719
if Settings.enabled? :sourcing
1820
> if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
19-
= render(:start).indent 2
21+
> {{ Settings.function_name :start }} "$@"
2022
> fi
2123
else
22-
= render :start
24+
> {{ Settings.function_name :start }} "$@"
2325
end
24-
>
26+
>

‎lib/bashly/views/command/start.gtx‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
= view_marker
22

3-
> command_line_args=("$@")
3+
> {{ Settings.function_name :start }}() {
44
if Settings.completions?
5-
> if [[ "${command_line_args[0]:-}" == "__complete" ]]; then
6-
> completion_run "${command_line_args[@]:1}"
7-
> else
5+
> if [[ ${1:-} == "__complete" ]]; then
6+
> completion_run "${@:2}"
7+
> return
8+
> fi
9+
end
10+
>
11+
> command_line_args=("$@")
812
> {{ Settings.function_name :initialize }}
913
> {{ Settings.function_name :run }} "${command_line_args[@]}"
10-
> fi
11-
else
12-
> {{ Settings.function_name :initialize }}
13-
> {{ Settings.function_name :run }} "${command_line_args[@]}"
14-
end
14+
> }

‎schemas/settings.json‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,19 @@
423423
"description": "Choose different names for some of the internal functions.\nhttps://bashly.dev/usage/settings/#function_names",
424424
"type": "object",
425425
"properties": {
426+
"start": {
427+
"title": "start",
428+
"description": "Name for the start() function\nhttps://bashly.dev/usage/settings/#function_names",
429+
"oneOf": [
430+
{
431+
"type": "string",
432+
"minLength": 1
433+
},
434+
{
435+
"type": "null"
436+
}
437+
]
438+
},
426439
"run": {
427440
"title": "run",
428441
"description": "Name for the run() function\nhttps://bashly.dev/usage/settings/#function_names",

‎spec/approvals/examples/stacktrace‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ Examples:
5252
Stack trace:
5353
from ./download:<line> in `root_command`
5454
from ./download:<line> in `run`
55+
from ./download:<line> in `start`
5556
from ./download:<line> in `main`

‎spec/bashly/script/wrapper_spec.rb‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
lines = subject.code.split "\n"
1212
expect(lines[0..13].join("\n")).to match_approval('script/wrapper/code')
1313
.except(/\d+\.\d+\.\d+(\.rc\d)?/)
14-
expect(lines[-3]).to eq ' run "${command_line_args[@]}"'
14+
expect(lines).to include ' start "$@"'
1515
end
1616
end
1717

@@ -42,5 +42,20 @@
4242
expect(lines[2]).to eq '# :command.root_command'
4343
end
4444
end
45+
46+
context 'with a custom start function name' do
47+
around do |example|
48+
original_function_names = Settings.function_names
49+
Settings.function_names = { 'start' => 'custom_start' }
50+
example.run
51+
ensure
52+
Settings.function_names = original_function_names
53+
end
54+
55+
it 'uses the configured name for the definition and invocation' do
56+
expect(subject.code).to include "custom_start() {\n"
57+
expect(subject.code).to include "\n custom_start \"$@\"\n"
58+
end
59+
end
4560
end
4661
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printf "initialize unexpectedly called\n"
2+
return 1

‎support/schema/settings.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ properties:
348348
https://bashly.dev/usage/settings/#function_names
349349
type: object
350350
properties:
351+
start:
352+
title: start
353+
description: |-
354+
Name for the start() function
355+
https://bashly.dev/usage/settings/#function_names
356+
oneOf: *optional_string
351357
run:
352358
title: run
353359
description: |-

0 commit comments

Comments
 (0)