diff --git a/lib/bashly/libraries/settings/settings.yml b/lib/bashly/libraries/settings/settings.yml index 772cb0806..781766942 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 078dcf106..389badcdd 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 09b894d55..cae7faaf9 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 +> diff --git a/schemas/settings.json b/schemas/settings.json index 9d0e57837..d6477f188 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 3b5dfcbf4..d42463e5d 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 2a0db8623..89f43d10b 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 c7cd79887..74484a430 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 66bd91d74..f7bb33d01 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: |-