Skip to content

Commit 9ff5551

Browse files
committed
- Add support for generating tab-indented scripts using BASHLY_TAB_INDENT
1 parent a654720 commit 9ff5551

File tree

11 files changed

+90
-46
lines changed

11 files changed

+90
-46
lines changed

examples/heredoc/README.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# Heredoc Example
22

3-
This example shows how (and where) you can use *Here document* (heredoc)
4-
constructs.
3+
This example shows how you can use *Here document* (heredoc) constructs.
54

6-
Since bashly applies indentation to most of your code, it is not possible to
7-
use heredoc everywhere. Code in the `src/lib` directory is injected to your
8-
script as is, so in cases where you must use heredocs, this is where you can.
5+
By default, bashly indents all code using spaces. In order for heredoc to
6+
properly work in bash, all leading spaces within the heredoc document must use
7+
the tab character.
8+
9+
Setting the environment variable `BASHLY_TAB_INDENT` to any value will make
10+
bashly replace the space indentation in the result, with tabs.
911

1012
This example was generated with:
1113

1214
```bash
1315
$ bashly init
14-
$ bashly add lib
1516
# ... now edit src/bashly.yml to match the example ...
1617
# ... now edit src/root_command.sh to match the example ...
17-
# ... now edit src/lib/heredocs.sh to match the example ...
18-
$ bashly generate
18+
$ BASHLY_TAB_INDENT=1 bashly generate
1919
```
2020

21-
<!-- include: src/root_command.sh src/lib/heredocs.sh -->
21+
<!-- include: src/root_command.sh -->
2222

2323
-----
2424

@@ -33,20 +33,19 @@ version: 0.1.0
3333
## `src/root_command.sh`
3434

3535
```bash
36-
text="$(message1)"
37-
echo "$text"
38-
```
39-
40-
## `src/lib/heredocs.sh`
36+
cat <<-EOF
37+
multiline
38+
heredoc text
39+
EOF
4140
42-
```bash
43-
message1() {
44-
cat << EOF
45-
this is a
46-
multiline
47-
heredoc text
41+
# In case an inner indentation is needed, use a whitespace that is not a space
42+
# and not a tab character. For example, Unicode U+3000 [ ]
43+
cat <<-EOF
44+
this is
45+
 an indented
46+
  multiline text
4847
EOF
49-
}
48+
5049
5150
```
5251

@@ -56,9 +55,11 @@ EOF
5655
### `$ ./cli`
5756

5857
```shell
59-
this is a
60-
multiline
61-
heredoc text
58+
multiline
59+
heredoc text
60+
this is
61+
 an indented
62+
  multiline text
6263
6364
6465
```

examples/heredoc/src/lib/heredocs.sh

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
text="$(message1)"
2-
echo "$text"
1+
cat <<-EOF
2+
multiline
3+
heredoc text
4+
EOF
5+
6+
# In case an inner indentation is needed, use a whitespace that is not a space
7+
# and not a tab character. For example, Unicode U+3000 [ ]
8+
cat <<-EOF
9+
this is
10+
 an indented
11+
  multiline text
12+
EOF
13+

examples/heredoc/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -x
44

5-
bashly generate
5+
BASHLY_TAB_INDENT=1 bashly generate
66

77
### Try Me ###
88

lib/bashly/commands/generate.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Generate < Base
1616
environment "BASHLY_TARGET_DIR", "The path to use for creating the bash script [default: .]"
1717
environment "BASHLY_LIB_DIR", "The path to use for upgrading library files, relative to the source dir [default: lib]"
1818
environment "BASHLY_STRICT", "When not empty, enable bash strict mode (set -euo pipefail)"
19+
environment "BASHLY_TAB_INDENT", "When not empty, the generated script will use tab indentation instead of spaces (every 2 leading spaces will be converted to a tab character)"
1920
environment "BASHLY_ENV", <<~EOF
2021
Set to 'production' or 'development':
2122
- production generate a smaller script, without file markers
@@ -114,7 +115,7 @@ def create_file(file, content)
114115
end
115116

116117
def create_master_script
117-
File.write master_script_path, script.code
118+
File.write master_script_path, script.code(tab_indent: Settings.tab_indent)
118119
FileUtils.chmod "+x", master_script_path
119120
quiet_say "!txtgrn!created!txtrst! #{master_script_path}"
120121
end

lib/bashly/extensions/string.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ def remove_front_matter
3131
split(/^---\s*/).last
3232
end
3333

34+
def expand_tabs(tabstop = 2)
35+
gsub(/^( {#{tabstop}}+)/) do
36+
"\t" * ($1.size / tabstop)
37+
end
38+
end
39+
3440
end

lib/bashly/script/wrapper.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ def initialize(command, function_name = nil)
99
@command, @function_name = command, function_name
1010
end
1111

12-
def code
13-
if function_name
14-
result = [header, render('wrapper')].join "\n"
12+
def code(tab_indent: false)
13+
tab_indent ? base_code.expand_tabs : base_code
14+
end
15+
16+
private
17+
18+
def base_code
19+
result = if function_name
20+
[header, render('wrapper')]
1521
else
16-
result = [header, body].join "\n"
22+
[header, body]
1723
end
1824

19-
result.lint
25+
result.join("\n").lint
2026
end
2127

22-
private
23-
2428
def header
2529
@header ||= header!
2630
end

lib/bashly/settings.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Bashly
22
class Settings
33
class << self
4-
attr_writer :source_dir, :target_dir, :lib_dir, :strict
4+
attr_writer :source_dir, :target_dir, :lib_dir, :strict, :tab_indent
55

66
def source_dir
77
@source_dir ||= ENV['BASHLY_SOURCE_DIR'] || 'src'
@@ -22,6 +22,10 @@ def strict
2222
def full_lib_dir
2323
"#{source_dir}/#{lib_dir}"
2424
end
25+
26+
def tab_indent
27+
@tab_indent ||= ENV['BASHLY_TAB_INDENT']
28+
end
2529
end
2630
end
2731
end

spec/approvals/cli/generate/help

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Environment Variables:
3737
BASHLY_STRICT
3838
When not empty, enable bash strict mode (set -euo pipefail)
3939

40+
BASHLY_TAB_INDENT
41+
When not empty, the generated script will use tab indentation instead of
42+
spaces (every 2 leading spaces will be converted to a tab character)
43+
4044
BASHLY_ENV
4145
Set to 'production' or 'development':
4246
- production generate a smaller script, without file markers

spec/approvals/examples/heredoc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
+ BASHLY_TAB_INDENT=1
12
+ bashly generate
23
creating user files in src
34
skipped src/initialize.sh (exists)
45
skipped src/root_command.sh (exists)
56
created ./cli
67
run ./cli --help to test your bash script
78
+ ./cli
8-
this is a
9-
multiline
10-
heredoc text
9+
multiline
10+
heredoc text
11+
this is
12+
 an indented
13+
  multiline text

0 commit comments

Comments
 (0)