Skip to content

Commit

Permalink
Merge pull request #597 from DannyBen/add/enable-sourcing
Browse files Browse the repository at this point in the history
Add support for avoiding execution if the script is sourced
  • Loading branch information
DannyBen authored Dec 30, 2024
2 parents 2073e83 + e71071a commit 59432f0
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 16 deletions.
1 change: 1 addition & 0 deletions lib/bashly/libraries/settings/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ enable_view_markers: development
enable_inspect_args: development
enable_deps_array: always
enable_env_var_names_array: always
enable_sourcing: development


#-------------------------------------------------------------------------------
Expand Down
25 changes: 15 additions & 10 deletions lib/bashly/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class << self
:enable_env_var_names_array,
:enable_header_comment,
:enable_inspect_args,
:enable_sourcing,
:enable_view_markers,
:lib_dir,
:partials_extension,
Expand Down Expand Up @@ -48,28 +49,32 @@ def enabled?(feature)
(send(:"enable_#{feature}") == 'development' && !production?)
end

def enable_header_comment
@enable_header_comment ||= get :enable_header_comment
end

def enable_bash3_bouncer
@enable_bash3_bouncer ||= get :enable_bash3_bouncer
end

def enable_view_markers
@enable_view_markers ||= get :enable_view_markers
def enable_deps_array
@enable_deps_array ||= get :enable_deps_array
end

def enable_env_var_names_array
@enable_env_var_names_array ||= get :enable_env_var_names_array
end

def enable_header_comment
@enable_header_comment ||= get :enable_header_comment
end

def enable_inspect_args
@enable_inspect_args ||= get :enable_inspect_args
end

def enable_deps_array
@enable_deps_array ||= get :enable_deps_array
def enable_sourcing
@enable_sourcing ||= get :enable_sourcing
end

def enable_env_var_names_array
@enable_env_var_names_array ||= get :enable_env_var_names_array
def enable_view_markers
@enable_view_markers ||= get :enable_view_markers
end

def env
Expand Down
13 changes: 10 additions & 3 deletions lib/bashly/views/command/master_script.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
= render :run

>
> initialize
> run "$@"
>
if Settings.enabled? :sourcing
> if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
> initialize
> run "$@"
> fi
else
> initialize
> run "$@"
end
>
12 changes: 12 additions & 0 deletions schemas/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@
],
"default": "always"
},
"enable_sourcing": {
"title": "enable_sourcing",
"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",
"type": "string",
"enum": [
"development",
"production",
"always",
"never"
],
"default": "development"
},
"partials_extension": {
"title": "partials extension",
"description": "The extension to use when reading/writing partial script snippets\nhttps://bashly.dannyb.co/usage/settings/#partials_extension",
Expand Down
2 changes: 1 addition & 1 deletion spec/approvals/cli/preview/no-args
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
...
run "$@"
run "$@"
2 changes: 1 addition & 1 deletion spec/bashly/commands/preview_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

it 'prints the generated cli script' do
expect { subject.execute %w[preview] }.to output_approval('cli/preview/no-args')
.except(/env bash\n.*\nrun "\$@"/m, "env bash\n...\nrun \"$@\"")
.except(/env bash\n.*\n\s*run "\$@"\n.*/m, "env bash\n...\nrun \"$@\"")
end
end
end
2 changes: 1 addition & 1 deletion spec/bashly/script/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
lines = subject.code.split "\n"
expect(lines[0..13].join("\n")).to match_approval('script/wrapper/code')
.except(/\d+\.\d+\.\d+(\.rc\d)?/)
expect(lines[-1]).to eq 'run "$@"'
expect(lines[-2]).to eq ' run "$@"'
end
end

Expand Down
8 changes: 8 additions & 0 deletions support/schema/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ properties:
type: string
enum: *feature_toggles
default: always
enable_sourcing:
title: enable_sourcing
description: |-
Whether to wrap the script execution in a condition that checks if the script is sourced
https://bashly.dannyb.co/usage/settings/#enable_sourcing
type: string
enum: *feature_toggles
default: development
partials_extension:
title: partials extension
description: |-
Expand Down

0 comments on commit 59432f0

Please sign in to comment.