diff --git a/lib/bashly/extensions/array.rb b/lib/bashly/extensions/array.rb index 92e0dfb2..739d6826 100644 --- a/lib/bashly/extensions/array.rb +++ b/lib/bashly/extensions/array.rb @@ -3,7 +3,17 @@ def indent(offset) return self unless offset.positive? indentation = ' ' * offset - map { |line| "#{indentation}#{line}" } + heredoc_marker = nil + + map do |line| + if heredoc_marker + heredoc_marker = nil if /^#{heredoc_marker}\n?$/.match?(line) + line + else + heredoc_marker = $1 if line =~ /<<-?(\w+)\n?$/ + "#{indentation}#{line}" + end + end end def nonuniq diff --git a/spec/approvals/fixtures/heredoc b/spec/approvals/fixtures/heredoc new file mode 100644 index 00000000..217a6115 --- /dev/null +++ b/spec/approvals/fixtures/heredoc @@ -0,0 +1,16 @@ ++ bundle exec bashly generate +creating user files in src +skipped src/root_command.sh (exists) +created ./cli +run ./cli --help to test your bash script ++ ./cli +unindented +multiline +heredoc text +also unindented + 1 indentation + 2 indentations + 3 indentations + + 3 indentations with empty line above +also unindented diff --git a/spec/bashly/extensions/array_spec.rb b/spec/bashly/extensions/array_spec.rb index abd8a98c..0d27246d 100644 --- a/spec/bashly/extensions/array_spec.rb +++ b/spec/bashly/extensions/array_spec.rb @@ -11,6 +11,40 @@ expect(subject.indent(0)).to eq subject end end + + context 'when the string contains heredoc block' do + subject do + result = <<~SUBJECT + function() { + cat <<-SOME_EOF_MARKER + not-indented + indented-once + indented-twice + SOME_EOF_MARKER + } # indented with function() start + SUBJECT + + result.lines + end + + let(:expected) do + result = <<~SUBJECT + function() { + cat <<-SOME_EOF_MARKER + not-indented + indented-once + indented-twice + SOME_EOF_MARKER + } # indented with function() start + SUBJECT + + result.lines + end + + it 'does not indent it but indents everything else' do + expect(subject.indent 2).to eq expected + end + end end describe '#nonuniq' do diff --git a/spec/fixtures/workspaces/heredoc/.gitignore b/spec/fixtures/workspaces/heredoc/.gitignore new file mode 100644 index 00000000..573c0c4f --- /dev/null +++ b/spec/fixtures/workspaces/heredoc/.gitignore @@ -0,0 +1 @@ +cli diff --git a/spec/fixtures/workspaces/heredoc/README.md b/spec/fixtures/workspaces/heredoc/README.md new file mode 100644 index 00000000..7575db38 --- /dev/null +++ b/spec/fixtures/workspaces/heredoc/README.md @@ -0,0 +1 @@ +This fixture tests that heredoc blocks are kept unindented \ No newline at end of file diff --git a/spec/fixtures/workspaces/heredoc/src/bashly.yml b/spec/fixtures/workspaces/heredoc/src/bashly.yml new file mode 100644 index 00000000..7d8d192f --- /dev/null +++ b/spec/fixtures/workspaces/heredoc/src/bashly.yml @@ -0,0 +1,3 @@ +name: cli +help: Sample application to test heredoc +version: 0.1.0 diff --git a/spec/fixtures/workspaces/heredoc/src/root_command.sh b/spec/fixtures/workspaces/heredoc/src/root_command.sh new file mode 100644 index 00000000..9c8db660 --- /dev/null +++ b/spec/fixtures/workspaces/heredoc/src/root_command.sh @@ -0,0 +1,18 @@ +echo unindented + +cat <<-FIRST_BLOCK +multiline +heredoc text +FIRST_BLOCK + +echo also unindented + +cat <<-SECOND_BLOCK + 1 indentation + 2 indentations + 3 indentations + + 3 indentations with empty line above +SECOND_BLOCK + +echo also unindented diff --git a/spec/fixtures/workspaces/heredoc/test.sh b/spec/fixtures/workspaces/heredoc/test.sh new file mode 100644 index 00000000..fade5ea5 --- /dev/null +++ b/spec/fixtures/workspaces/heredoc/test.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -x + +bundle exec bashly generate + +./cli +