Skip to content

Commit 93c9446

Browse files
committed
- Add BASHLY_ENV and bashly generate --env
1 parent 4529298 commit 93c9446

File tree

9 files changed

+84
-4
lines changed

9 files changed

+84
-4
lines changed

lib/bashly.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@
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: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ module Commands
33
class Generate < Base
44
help "Generate the bash script and required files"
55

6-
usage "bashly generate [--force --quiet --upgrade --wrap FUNCTION]"
6+
usage "bashly generate [options]"
77
usage "bashly generate (-h|--help)"
88

99
option "-f --force", "Overwrite existing files"
1010
option "-q --quiet", "Disable on-screen progress report"
1111
option "-u --upgrade", "Upgrade all added library functions"
1212
option "-w --wrap FUNCTION", "Wrap the entire script in a function so it can also be sourced"
13+
option "-e --env ENV", "Force the generation environment (see BASHLY_ENV)"
1314

1415
environment "BASHLY_SOURCE_DIR", "The path containing the bashly configuration and source files [default: src]"
1516
environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
1617
environment "BASHLY_LIB_DIR", "The path to use for upgrading library files, relative to the source dir [default: lib]"
1718
environment "BASHLY_STRICT", "When not empty, enable bash strict mode (set -euo pipefail)"
19+
environment "BASHLY_ENV", <<~EOF
20+
Set to 'production' or 'development':
21+
- production generate a smaller script, without file markers
22+
- development generate with file markers
23+
24+
Can be overridden with --env [default: development]
25+
EOF
1826

1927
example "bashly generate --force"
2028
example "bashly generate --wrap my_function"
2129

2230
def run
2331
validate_config
32+
ENV['BASHLY_ENV'] = args['--env'] if args['--env']
33+
quiet_say "creating !txtgrn!production!txtrst! version" if Bashly.production?
2434
create_user_files
2535
upgrade_libs if args['--upgrade']
2636
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 ENV['BASHLY_PRODUCTION']
18+
"# #{id}" unless Bashly.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
@@ -102,7 +102,7 @@ def load_user_file(file, placeholder: true)
102102
default_content
103103
end
104104

105-
ENV['BASHLY_PRODUCTION'] ? content : "#{view_marker path}\n#{content}"
105+
Bashly.production? ? content : "#{view_marker path}\n#{content}"
106106
end
107107

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

spec/approvals/cli/generate/help

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Generate the bash script and required files
22

33
Usage:
4-
bashly generate [--force --quiet --upgrade --wrap FUNCTION]
4+
bashly generate [options]
55
bashly generate (-h|--help)
66

77
Options:
@@ -17,6 +17,9 @@ Options:
1717
-w --wrap FUNCTION
1818
Wrap the entire script in a function so it can also be sourced
1919

20+
-e --env ENV
21+
Force the generation environment (see BASHLY_ENV)
22+
2023
-h --help
2124
Show this help
2225

@@ -34,6 +37,13 @@ Environment Variables:
3437
BASHLY_STRICT
3538
When not empty, enable bash strict mode (set -euo pipefail)
3639

40+
BASHLY_ENV
41+
Set to 'production' or 'development':
42+
- production generate a smaller script, without file markers
43+
- development generate with file markers
44+
45+
Can be overridden with --env [default: development]
46+
3747
Examples:
3848
bashly generate --force
3949
bashly generate --wrap my_function
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
creating production version
2+
creating user files in spec/tmp/src
3+
created spec/tmp/src/initialize.sh
4+
created spec/tmp/src/download_command.sh
5+
created spec/tmp/src/upload_command.sh
6+
created spec/tmp/cli
7+
run spec/tmp/cli --help to test your bash script
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
creating production version
2+
creating user files in spec/tmp/src
3+
created spec/tmp/src/initialize.sh
4+
created spec/tmp/src/download_command.sh
5+
created spec/tmp/src/upload_command.sh
6+
created spec/tmp/cli
7+
run spec/tmp/cli --help to test your bash script

spec/bashly/commands/generate_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
describe Commands::Generate do
44
let(:source_dir) { Settings.source_dir }
55
let(:target_dir) { Settings.target_dir }
6+
let(:cli_script_content) { File.read cli_script }
67
subject { CLI.runner }
78

89
context "with --help" do
@@ -25,6 +26,17 @@
2526
expect(File).to exist(cli_script)
2627
end
2728

29+
context "when BASHLY_ENV=production" do
30+
before { ENV['BASHLY_ENV'] = 'production' }
31+
after { ENV['BASHLY_ENV'] = nil }
32+
33+
it "generates a script without view markers" do
34+
expect { subject.run %w[generate] }.to output_approval('cli/generate/production-env-var')
35+
expect(File).to exist(cli_script)
36+
expect(cli_script_content).to_not include '# :'
37+
end
38+
end
39+
2840
context "when source files already exist" do
2941
before do
3042
expect { subject.run %w[generate] }.to output_approval('cli/generate/no-args')
@@ -83,6 +95,25 @@
8395
end
8496
end
8597

98+
context "with --env production" do
99+
let(:cli_script) { "#{target_dir}/cli" }
100+
let(:cli_script_content) { File.read cli_script }
101+
102+
before do
103+
reset_tmp_dir
104+
success = system "mkdir -p #{source_dir} && cp lib/bashly/templates/bashly.yml #{source_dir}/bashly.yml"
105+
expect(success).to be true
106+
end
107+
108+
after { ENV['BASHLY_ENV'] = nil }
109+
110+
it "generates a script without view markers" do
111+
expect { subject.run %w[generate --env production] }.to output_approval('cli/generate/production')
112+
expect(File).to exist(cli_script)
113+
expect(cli_script_content).to_not include '# :'
114+
end
115+
end
116+
86117
context "with --upgrade" do
87118
let(:lib_files) { Dir["spec/tmp/src/lib/**/*.sh"].sort }
88119
let(:outdated_text) { "OUTDATED TEXT" }

spec/spec_helper.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
ENV['COLUMNS'] = '80'
2323
ENV['LINES'] = '30'
2424

25+
# Unset any user env settings
26+
ENV['BASHLY_ENV'] = nil
27+
2528
RSpec.configure do |c|
2629
c.include SpecMixin
2730
c.strip_ansi_escape = true

0 commit comments

Comments
 (0)