Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/bashly/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/approvals/fixtures/heredoc
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions spec/bashly/extensions/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/heredoc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cli
1 change: 1 addition & 0 deletions spec/fixtures/workspaces/heredoc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This fixture tests that heredoc blocks are kept unindented
3 changes: 3 additions & 0 deletions spec/fixtures/workspaces/heredoc/src/bashly.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: cli
help: Sample application to test heredoc
version: 0.1.0
18 changes: 18 additions & 0 deletions spec/fixtures/workspaces/heredoc/src/root_command.sh
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions spec/fixtures/workspaces/heredoc/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

set -x

bundle exec bashly generate

./cli