Skip to content

Commit 59432f0

Browse files
authored
Merge pull request #597 from DannyBen/add/enable-sourcing
Add support for avoiding execution if the script is sourced
2 parents 2073e83 + e71071a commit 59432f0

File tree

8 files changed

+49
-16
lines changed

8 files changed

+49
-16
lines changed

lib/bashly/libraries/settings/settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ enable_view_markers: development
110110
enable_inspect_args: development
111111
enable_deps_array: always
112112
enable_env_var_names_array: always
113+
enable_sourcing: development
113114

114115

115116
#-------------------------------------------------------------------------------

lib/bashly/settings.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class << self
1313
:enable_env_var_names_array,
1414
:enable_header_comment,
1515
:enable_inspect_args,
16+
:enable_sourcing,
1617
:enable_view_markers,
1718
:lib_dir,
1819
:partials_extension,
@@ -48,28 +49,32 @@ def enabled?(feature)
4849
(send(:"enable_#{feature}") == 'development' && !production?)
4950
end
5051

51-
def enable_header_comment
52-
@enable_header_comment ||= get :enable_header_comment
53-
end
54-
5552
def enable_bash3_bouncer
5653
@enable_bash3_bouncer ||= get :enable_bash3_bouncer
5754
end
5855

59-
def enable_view_markers
60-
@enable_view_markers ||= get :enable_view_markers
56+
def enable_deps_array
57+
@enable_deps_array ||= get :enable_deps_array
58+
end
59+
60+
def enable_env_var_names_array
61+
@enable_env_var_names_array ||= get :enable_env_var_names_array
62+
end
63+
64+
def enable_header_comment
65+
@enable_header_comment ||= get :enable_header_comment
6166
end
6267

6368
def enable_inspect_args
6469
@enable_inspect_args ||= get :enable_inspect_args
6570
end
6671

67-
def enable_deps_array
68-
@enable_deps_array ||= get :enable_deps_array
72+
def enable_sourcing
73+
@enable_sourcing ||= get :enable_sourcing
6974
end
7075

71-
def enable_env_var_names_array
72-
@enable_env_var_names_array ||= get :enable_env_var_names_array
76+
def enable_view_markers
77+
@enable_view_markers ||= get :enable_view_markers
7378
end
7479

7580
def env

lib/bashly/views/command/master_script.gtx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
= render :run
1414

1515
>
16-
> initialize
17-
> run "$@"
18-
>
16+
if Settings.enabled? :sourcing
17+
> if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
18+
> initialize
19+
> run "$@"
20+
> fi
21+
else
22+
> initialize
23+
> run "$@"
24+
end
25+
>

schemas/settings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@
201201
],
202202
"default": "always"
203203
},
204+
"enable_sourcing": {
205+
"title": "enable_sourcing",
206+
"description": "Whether to wrap the script execution in a condition that checks if the script is sourced\nhttps://bashly.dannyb.co/usage/settings/#enable_sourcing",
207+
"type": "string",
208+
"enum": [
209+
"development",
210+
"production",
211+
"always",
212+
"never"
213+
],
214+
"default": "development"
215+
},
204216
"partials_extension": {
205217
"title": "partials extension",
206218
"description": "The extension to use when reading/writing partial script snippets\nhttps://bashly.dannyb.co/usage/settings/#partials_extension",

spec/approvals/cli/preview/no-args

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#!/usr/bin/env bash
22
...
3-
run "$@"
3+
run "$@"

spec/bashly/commands/preview_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
it 'prints the generated cli script' do
1919
expect { subject.execute %w[preview] }.to output_approval('cli/preview/no-args')
20-
.except(/env bash\n.*\nrun "\$@"/m, "env bash\n...\nrun \"$@\"")
20+
.except(/env bash\n.*\n\s*run "\$@"\n.*/m, "env bash\n...\nrun \"$@\"")
2121
end
2222
end
2323
end

spec/bashly/script/wrapper_spec.rb

Lines changed: 1 addition & 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[-1]).to eq 'run "$@"'
14+
expect(lines[-2]).to eq ' run "$@"'
1515
end
1616
end
1717

support/schema/settings.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ properties:
173173
type: string
174174
enum: *feature_toggles
175175
default: always
176+
enable_sourcing:
177+
title: enable_sourcing
178+
description: |-
179+
Whether to wrap the script execution in a condition that checks if the script is sourced
180+
https://bashly.dannyb.co/usage/settings/#enable_sourcing
181+
type: string
182+
enum: *feature_toggles
183+
default: development
176184
partials_extension:
177185
title: partials extension
178186
description: |-

0 commit comments

Comments
 (0)