File tree Expand file tree Collapse file tree 2 files changed +16
-15
lines changed Expand file tree Collapse file tree 2 files changed +16
-15
lines changed Original file line number Diff line number Diff line change @@ -52,11 +52,12 @@ strict: false
52
52
tab_indent : false
53
53
54
54
# Choose a post-processor for the generated script:
55
- # formatter: internal # Use Bashly's internal formatter (compacts newlines)
56
- # formatter: none # Disable all post-processing
57
- # formatter: shfmt # Use shfmt if installed
58
- formatter : internal
59
-
55
+ # formatter: :internal # Use Bashly's internal formatter (compacts newlines)
56
+ # formatter: :none # Disable all post-processing
57
+ # formatter: :shfmt # Use shfmt if installed
58
+ # formatter: shfmt -mn # Use any other string, assuming the command accepts
59
+ # # the script as stdin and outputs it to stdout
60
+ formatter : :internal
60
61
61
62
# -------------------------------------------------------------------------------
62
63
# INTERFACE OPTIONS
Original file line number Diff line number Diff line change 1
1
require 'open3'
2
+ require 'shellwords'
2
3
3
4
module Bashly
4
5
module Script
@@ -9,29 +10,28 @@ class Formatter
9
10
10
11
def initialize ( script , mode : nil )
11
12
@script = script
12
- @mode = MODES . include? ( mode &. to_sym ) ? mode . to_sym : MODES . first
13
+ @mode = mode
13
14
end
14
15
15
16
def formatted_script
16
17
case mode
17
18
when :internal then script . gsub ( /\s +\n /m , "\n \n " )
18
19
when :shfmt then shfmt_result
19
- else script
20
+ when :none then script
21
+ else custom_formatter_result mode
20
22
end
21
23
end
22
24
23
25
private
24
26
25
27
def shfmt_result
26
- unless system 'command -v shfmt > /dev/null 2>&1'
27
- raise DependencyError ,
28
- 'Cannot find g`shfmt`.\nEither install it or change to the g`internal` formatter.'
29
- end
30
-
31
- output , error , status =
32
- Open3 . capture3 %w[ shfmt --case-indent --indent 2 ] , stdin_data : script
28
+ custom_formatter_result %w[ shfmt --case-indent --indent 2 ]
29
+ end
33
30
34
- raise DependencyError , "Failed running g`shfmt`:\n \n #{ error } " unless status . success?
31
+ def custom_formatter_result ( command )
32
+ command = Shellwords . split command if command . is_a? String
33
+ output , error , status = Open3 . capture3 *command , stdin_data : script
34
+ raise DependencyError , "Failed running g`#{ command } `:\n \n #{ error } " unless status . success?
35
35
36
36
output
37
37
end
You can’t perform that action at this time.
0 commit comments