Skip to content

Commit 19ae603

Browse files
authored
Merge pull request #219 from DannyBen/add/env-to-settings
Support env value in settings.yml as an alternative to BASHLY_ENV
2 parents 68c35b0 + d9681c1 commit 19ae603

File tree

9 files changed

+37
-22
lines changed

9 files changed

+37
-22
lines changed

lib/bashly.rb

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,3 @@
1515
requires 'bashly/commands/base'
1616
requires 'bashly/libraries/base'
1717
requires 'bashly'
18-
19-
module Bashly
20-
class << self
21-
def env
22-
ENV['BASHLY_ENV']&.to_sym
23-
end
24-
25-
def production?
26-
env == :production
27-
end
28-
end
29-
end

lib/bashly/commands/generate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Generate < Base
3030

3131
def run
3232
validate_config
33-
ENV['BASHLY_ENV'] = args['--env'] if args['--env']
34-
quiet_say "creating !txtgrn!production!txtrst! version" if Bashly.production?
33+
Settings.env = args['--env'] if args['--env']
34+
quiet_say "creating !txtgrn!production!txtrst! version" if Settings.production?
3535
create_user_files
3636
upgrade_libs if args['--upgrade']
3737
create_master_script

lib/bashly/concerns/renderable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def strings
1515

1616
def view_marker(id = nil)
1717
id ||= ":#{caller_locations.first.path}"
18-
"# #{id}" unless Bashly.production?
18+
"# #{id}" unless Settings.production?
1919
end
2020

2121
private

lib/bashly/script/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def load_user_file(file, placeholder: true)
103103
''
104104
end
105105

106-
Bashly.production? ? content : "#{view_marker path}\n#{content}"
106+
Settings.production? ? content : "#{view_marker path}\n#{content}"
107107
end
108108

109109
# Returns an array of all parents. For example, the command

lib/bashly/settings.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ def tab_indent
2323
@tab_indent ||= get :tab_indent
2424
end
2525

26+
def env
27+
@env ||= get(:env, :development)&.to_sym
28+
end
29+
30+
def env=(value)
31+
@env = value&.to_sym
32+
end
33+
34+
def production?
35+
env == :production
36+
end
37+
2638
def full_lib_dir
2739
"#{source_dir}/#{lib_dir}"
2840
end

lib/bashly/templates/settings.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22
# can also be set with an environment variable with the same name, capitalized
33
# and prefixed by `BASHLY_` - for example: BASHLY_SOURCE_DIR
44

5+
# The path containing the bashly configuration and source files
56
source_dir: src
7+
8+
# The path to use for creating the bash script
69
target_dir: .
10+
11+
# The path to use for upgrading library files, relative to the source dir
712
lib_dir: lib
13+
14+
# When true, enable bash strict mode (set -euo pipefail)
815
strict: false
16+
17+
# When true, the generated script will use tab indentation instead of spaces
18+
# (every 2 leading spaces will be converted to a tab character)
919
tab_indent: false
20+
21+
# Set to 'production' or 'development':
22+
# - production generate a smaller script, without file markers
23+
# - development generate with file markers
24+
env: development

spec/bashly/commands/add_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292

9393
context "with settings command" do
9494
let(:settings_file) { "#{target_dir}/settings.yml" }
95+
let(:template_file) { "lib/bashly/templates/settings.yml" }
9596

9697
before do
9798
reset_tmp_dir
@@ -104,6 +105,7 @@
104105
end
105106
end.to output_approval('cli/add/settings')
106107
expect(File).to exist(settings_file)
108+
expect(File.read settings_file).to eq File.read(template_file)
107109
end
108110

109111
context "when the file exists" do

spec/bashly/commands/generate_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
end
2828

2929
context "when BASHLY_ENV=production" do
30-
before { ENV['BASHLY_ENV'] = 'production' }
31-
after { ENV['BASHLY_ENV'] = nil }
30+
before { Settings.env = :production }
31+
after { Settings.env = nil }
3232

3333
it "generates a script without view markers" do
3434
expect { subject.run %w[generate] }.to output_approval('cli/generate/production-env-var')
@@ -118,7 +118,7 @@
118118
expect(success).to be true
119119
end
120120

121-
after { ENV['BASHLY_ENV'] = nil }
121+
after { Settings.env = nil }
122122

123123
it "generates a script without view markers" do
124124
expect { subject.run %w[generate --env production] }.to output_approval('cli/generate/production')

spec/spec_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,14 @@
2020
Settings.source_dir = 'spec/tmp/src'
2121
Settings.target_dir = 'spec/tmp'
2222
Settings.strict = '1' # generate scripts with `set -euo pipefile`
23+
Settings.env = :development
2324
reset_tmp_dir
2425

2526
# Consistent Colsole output (for rspec_fixtures)
2627
ENV['TTY'] = 'off'
2728
ENV['COLUMNS'] = '80'
2829
ENV['LINES'] = '30'
2930

30-
# Unset any user env settings
31-
ENV['BASHLY_ENV'] = nil
32-
3331
RSpec.configure do |c|
3432
c.include SpecMixin
3533
c.strip_ansi_escape = true

0 commit comments

Comments
 (0)