Skip to content

Commit 522d2b8

Browse files
committed
- Add support for per-environment settings
1 parent 6c8c697 commit 522d2b8

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

lib/bashly/libraries/settings/settings.yml

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
# All settings are optional (with their default values provided below), and
2-
# can also be set with an environment variable with the same name, capitalized
3-
# and prefixed by `BASHLY_` - for example: BASHLY_SOURCE_DIR
1+
#-------------------------------------------------------------------------------
2+
# BASHLY SETTINGS
3+
#-------------------------------------------------------------------------------
44
#
5-
# When setting environment variables, you can use:
6-
# - "0", "false" or "no" to represent false
7-
# - "1", "true" or "yes" to represent true
5+
### Default Values
6+
#
7+
# All settings are optional, with their default values provided below
8+
#
9+
### Environment Variables
10+
#
11+
# Values can also be set using an environment variable with the same name,
12+
# capitalized and prefixed by `BASHLY_` - for example: `BASHLY_SOURCE_DIR`
13+
#
14+
# When setting environment variables, you can use:
15+
# - "0", "false" or "no" to represent false
16+
# - "1", "true" or "yes" to represent true
17+
#
18+
# Environment variables take precedence over values in the config file.
19+
#
20+
### File Location:
21+
#
22+
# Bashly looks for the settings file in these locations.
23+
# - The path defined in the `BASHLY_SETTINGS_PATH` environment variable.
24+
# - A file named `bashly-settings.yml` in the working directory.
25+
# - A file named `settings.yml` in the working directory.
26+
#
27+
### Environment Overrides:
28+
#
29+
# All options (except `env`) may be specified with an environment suffix in
30+
# order to override its value for a given environment.
31+
#
32+
# For example, when defining `formatter_production: shfmt --minify`, then
33+
# this will be the formatter used when generating the script with
34+
# `bashly generate --env production`
35+
#
36+
# Since these values take precedence over the standard values, you can define
37+
# both (i.e. `formatter: shfmt` and `formatter_production: shfmt --minify`).
838
#
9-
# If you wish to change the path to this file, set the environment variable
10-
# BASHLY_SETTINGS_PATH.
11-
1239

1340
#-------------------------------------------------------------------------------
1441
# PATH OPTIONS
@@ -52,12 +79,12 @@ strict: false
5279
tab_indent: false
5380

5481
# Choose a post-processor for the generated script:
55-
# formatter: internal # Use Bashly's internal formatter (compacts newlines)
82+
# formatter: internal # Use Bashly’s built-in formatter (removes extra newlines)
5683
# formatter: external # Run the external command `shfmt --case-indent --indent 2`
5784
# formatter: none # Disable formatting entirely
58-
# formatter: <string> # Use a custom shell command to format the script.
59-
# # The command will receive the script via stdin and
60-
# # must output the result to stdout.
85+
# formatter: <string> # Provide a custom shell command to format the script.
86+
# # The command receives the script via stdin and must
87+
# # write the result to stdout.
6188
# # Example: shfmt --minify
6289
formatter: internal
6390

@@ -102,7 +129,7 @@ usage_colors:
102129
#-------------------------------------------------------------------------------
103130

104131
# Set to 'production' or 'development'.
105-
# Determines which features are enabled in the rendered script.
132+
# Determines which features are enabled in the generated script.
106133
# Use the `enable_*` options below to adjust settings for each environment.
107134
# It is recommended to leave this set to 'development' and run
108135
# `bashly generate --env production` when the production version is needed.

lib/bashly/settings.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,22 @@ def var_aliases
160160
private
161161

162162
def get(key)
163+
value_from_env(key) || value_from_config(key)
164+
end
165+
166+
def value_from_config(key)
167+
if key != :env
168+
config["#{key}_#{env}"] || config["#{key}"]
169+
else
170+
config["#{key}"]
171+
end
172+
end
173+
174+
def value_from_env(key)
163175
case env_value key
164-
when nil then config[key.to_s]
165176
when '0', 'false', 'no' then false
166177
when '1', 'true', 'yes' then true
167-
else env_value key
178+
else env_value key
168179
end
169180
end
170181

spec/bashly/settings_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,28 @@
7474
expect(subject.tab_indent).to eq ENV['BASHLY_TAB_INDENT']
7575
end
7676
end
77+
78+
context 'when using env suffix overrides' do
79+
before do
80+
reset_tmp_dir
81+
File.write 'spec/tmp/settings.yml', 'formatter_production: shfmt --minify'
82+
subject.formatter = nil
83+
end
84+
85+
it 'returns the default value when it is not the specified environment' do
86+
Dir.chdir 'spec/tmp' do
87+
expect(subject.formatter).to eq 'internal'
88+
end
89+
end
90+
91+
it 'returns the config value when it is the specified environment' do
92+
Dir.chdir 'spec/tmp' do
93+
subject.env = :production
94+
expect(subject.formatter).to eq 'shfmt --minify'
95+
end
96+
end
97+
end
98+
7799
end
78100

79101
describe '::env' do

0 commit comments

Comments
 (0)