Skip to content

Commit 7c20a40

Browse files
committed
- Improve friendly error on bash 3
1 parent c5b8c1e commit 7c20a40

File tree

9 files changed

+55
-15
lines changed

9 files changed

+55
-15
lines changed

lib/bashly/models/script.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def header!
2929
if File.exist? custom_header_path
3030
File.read custom_header_path
3131
else
32-
render('header')
32+
default_header
3333
end
3434
end
3535

36+
def default_header
37+
result = render('header')
38+
result += render('bash3_bouncer') unless function_name
39+
result
40+
end
41+
3642
def body
3743
@body ||= command.render('master_script')
3844
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# :script.bash3_bouncer
2+
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
3+
printf "<%= strings[:unsupported_bash_version] -%>\n"
4+
exit 1
5+
fi

lib/bashly/views/script/header.erb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,3 @@
22
# This script was generated by bashly (https://github.com/DannyBen/bashly)
33
# Modifying it manually is not recommended
44

5-
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
6-
printf "<%= strings[:unsupported_bash_version] -%>\n"
7-
exit 1
8-
fi
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :script.wrapper
6+
function() {
7+
8+
# :command.version_command
9+
version_command() {
10+
echo "$version"
11+
}

spec/approvals/models/script/code

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :script.bash3_bouncer
6+
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
7+
printf "bash version 4 or higher is required\n"
8+
exit 1
9+
fi
10+
11+
# :command.root_command
12+
root_command() {
13+
# :spec/tmp/src/root_command.sh
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# This script was generated by bashly (https://github.com/DannyBen/bashly)
3+
# Modifying it manually is not recommended
4+
5+
# :script.wrapper
6+
my_super_function() {
7+
# :command.root_command
8+
root_command() {
9+
# :spec/tmp/src/root_command.sh
10+
echo "error: cannot load file"
11+
}
12+
13+
# :command.version_command

spec/bashly/commands/generate_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,11 @@
7575
expect(success).to be true
7676
end
7777

78-
it "generates the cli script wrapped in a function" do
78+
it "generates the cli script wrapped in a function without bash3 bouncer" do
7979
expect { subject.run %w[generate -w function] }.to output_approval('cli/generate/wrap-function')
8080
expect(File).to exist(cli_script)
8181
lines = File.readlines cli_script
82-
expect(lines[10]).to eq "function() {\n"
82+
expect(lines[0..10].join).to match_approval('cli/generate/wrap-script')
8383
end
8484
end
8585

spec/bashly/models/script_spec.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,17 @@
1010
context "without function name" do
1111
it "returns the complete script" do
1212
lines = subject.code.split "\n"
13-
expect(lines[0]).to eq '#!/usr/bin/env bash'
14-
expect(lines[1]).to start_with '# This script was generated by'
15-
expect(lines[10]).to eq 'root_command() {'
13+
expect(lines[0..12].join("\n")).to match_approval('models/script/code')
1614
expect(lines[-1]).to eq 'run "$@"'
1715
end
1816
end
1917

2018
context "with function name" do
2119
subject { described_class.new command, 'my_super_function' }
2220

23-
it "returns the complete script wrapped in a function" do
21+
it "returns the complete script wrapped in a function without a bash3 bouncer" do
2422
lines = subject.code.split "\n"
25-
expect(lines[0]).to eq '#!/usr/bin/env bash'
26-
expect(lines[1]).to start_with '# This script was generated by'
27-
expect(lines[10]).to eq 'my_super_function() {'
28-
expect(lines[11]).to eq ' # :command.root_command'
23+
expect(lines[0..12].join("\n")).to match_approval('models/script/code-wrapped')
2924
expect(lines[-1]).to eq '(return 0 2>/dev/null) || my_super_function "$@"'
3025
end
3126
end

spec/fixtures/workspaces/flag-args-with-dash/argflag

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# This script was generated by bashly (https://github.com/DannyBen/bashly)
33
# Modifying it manually is not recommended
44

5+
# :script.bash3_bouncer
56
if [[ "${BASH_VERSINFO:-0}" -lt 4 ]]; then
67
printf "bash version 4 or higher is required\n"
78
exit 1

0 commit comments

Comments
 (0)