Skip to content

Commit 7153ce9

Browse files
authored
Add support for using heredoc (#496)
- Add support for using heredoc
1 parent 530cb05 commit 7153ce9

File tree

8 files changed

+92
-1
lines changed

8 files changed

+92
-1
lines changed

lib/bashly/extensions/array.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ def indent(offset)
33
return self unless offset.positive?
44

55
indentation = ' ' * offset
6-
map { |line| "#{indentation}#{line}" }
6+
heredoc_marker = nil
7+
8+
map do |line|
9+
if heredoc_marker
10+
heredoc_marker = nil if /^#{heredoc_marker}\n?$/.match?(line)
11+
line
12+
else
13+
heredoc_marker = $1 if line =~ /<<-?(\w+)\n?$/
14+
"#{indentation}#{line}"
15+
end
16+
end
717
end
818

919
def nonuniq

spec/approvals/fixtures/heredoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
+ bundle exec bashly generate
2+
creating user files in src
3+
skipped src/root_command.sh (exists)
4+
created ./cli
5+
run ./cli --help to test your bash script
6+
+ ./cli
7+
unindented
8+
multiline
9+
heredoc text
10+
also unindented
11+
1 indentation
12+
2 indentations
13+
3 indentations
14+
15+
3 indentations with empty line above
16+
also unindented

spec/bashly/extensions/array_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,40 @@
1111
expect(subject.indent(0)).to eq subject
1212
end
1313
end
14+
15+
context 'when the string contains heredoc block' do
16+
subject do
17+
result = <<~SUBJECT
18+
function() {
19+
cat <<-SOME_EOF_MARKER
20+
not-indented
21+
indented-once
22+
indented-twice
23+
SOME_EOF_MARKER
24+
} # indented with function() start
25+
SUBJECT
26+
27+
result.lines
28+
end
29+
30+
let(:expected) do
31+
result = <<~SUBJECT
32+
function() {
33+
cat <<-SOME_EOF_MARKER
34+
not-indented
35+
indented-once
36+
indented-twice
37+
SOME_EOF_MARKER
38+
} # indented with function() start
39+
SUBJECT
40+
41+
result.lines
42+
end
43+
44+
it 'does not indent it but indents everything else' do
45+
expect(subject.indent 2).to eq expected
46+
end
47+
end
1448
end
1549

1650
describe '#nonuniq' do
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cli
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This fixture tests that heredoc blocks are kept unindented
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name: cli
2+
help: Sample application to test heredoc
3+
version: 0.1.0
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
echo unindented
2+
3+
cat <<-FIRST_BLOCK
4+
multiline
5+
heredoc text
6+
FIRST_BLOCK
7+
8+
echo also unindented
9+
10+
cat <<-SECOND_BLOCK
11+
1 indentation
12+
2 indentations
13+
3 indentations
14+
15+
3 indentations with empty line above
16+
SECOND_BLOCK
17+
18+
echo also unindented
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -x
4+
5+
bundle exec bashly generate
6+
7+
./cli
8+

0 commit comments

Comments
 (0)