From 4d83222bab827d16aa88239bf22c5ce8cb5ff002 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 30 Dec 2024 17:22:38 +0000 Subject: [PATCH 1/2] - Add support for avoiding execution if the script is sourced --- lib/bashly/libraries/settings/settings.yml | 1 + lib/bashly/settings.rb | 25 +++++++++++++--------- lib/bashly/views/command/master_script.gtx | 13 ++++++++--- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/bashly/libraries/settings/settings.yml b/lib/bashly/libraries/settings/settings.yml index 772cb080..78176694 100644 --- a/lib/bashly/libraries/settings/settings.yml +++ b/lib/bashly/libraries/settings/settings.yml @@ -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 #------------------------------------------------------------------------------- diff --git a/lib/bashly/settings.rb b/lib/bashly/settings.rb index 078dcf10..389badcd 100644 --- a/lib/bashly/settings.rb +++ b/lib/bashly/settings.rb @@ -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, @@ -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 diff --git a/lib/bashly/views/command/master_script.gtx b/lib/bashly/views/command/master_script.gtx index 09b894d5..cae7faaf 100644 --- a/lib/bashly/views/command/master_script.gtx +++ b/lib/bashly/views/command/master_script.gtx @@ -13,6 +13,13 @@ = render :run > -> initialize -> run "$@" -> \ No newline at end of file +if Settings.enabled? :sourcing + > if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + > initialize + > run "$@" + > fi +else + > initialize + > run "$@" +end +> From e71071a309af507cba24bb0a98c84659a76e0eba Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 30 Dec 2024 17:42:20 +0000 Subject: [PATCH 2/2] fix tests --- schemas/settings.json | 12 ++++++++++++ spec/approvals/cli/preview/no-args | 2 +- spec/bashly/commands/preview_spec.rb | 2 +- spec/bashly/script/wrapper_spec.rb | 2 +- support/schema/settings.yml | 8 ++++++++ 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/schemas/settings.json b/schemas/settings.json index 9d0e5783..d6477f18 100644 --- a/schemas/settings.json +++ b/schemas/settings.json @@ -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", diff --git a/spec/approvals/cli/preview/no-args b/spec/approvals/cli/preview/no-args index 3b5dfcbf..d42463e5 100644 --- a/spec/approvals/cli/preview/no-args +++ b/spec/approvals/cli/preview/no-args @@ -1,3 +1,3 @@ #!/usr/bin/env bash ... -run "$@" +run "$@" \ No newline at end of file diff --git a/spec/bashly/commands/preview_spec.rb b/spec/bashly/commands/preview_spec.rb index 2a0db862..89f43d10 100644 --- a/spec/bashly/commands/preview_spec.rb +++ b/spec/bashly/commands/preview_spec.rb @@ -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 diff --git a/spec/bashly/script/wrapper_spec.rb b/spec/bashly/script/wrapper_spec.rb index c7cd7988..74484a43 100644 --- a/spec/bashly/script/wrapper_spec.rb +++ b/spec/bashly/script/wrapper_spec.rb @@ -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 diff --git a/support/schema/settings.yml b/support/schema/settings.yml index 66bd91d7..f7bb33d0 100644 --- a/support/schema/settings.yml +++ b/support/schema/settings.yml @@ -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: |-