40
40
xargs = %w[ xargs --no-run-if-empty --null --max-procs=0 --max-args=300 -- ]
41
41
ruby_opt = { "RUBYOPT" => [ ENV [ "RUBYOPT" ] , "--encoding=UTF-8" ] . compact . join ( " " ) }
42
42
43
+ filtered = -> ( ext , dirs ) do
44
+ if ENV . key? ( FILES_ENV )
45
+ %w[ sed -E -n -e ] << "/\\ .#{ ext } $/p" << "--" << ENV . fetch ( FILES_ENV )
46
+ else
47
+ ( %w[ find ] + dirs + %w[ -type f -and -name ] ) << "*.#{ ext } " << "-print0"
48
+ end
49
+ end
50
+
43
51
desc ( "Lint `*.rb(i)`" )
44
52
multitask ( :"lint:rubocop" ) do
45
53
find = %w[ find ./lib ./test ./rbi ./examples -type f -and ( -name *.rb -or -name *.rbi ) -print0 ]
@@ -54,24 +62,26 @@ multitask(:"lint:rubocop") do
54
62
sh ( "#{ find . shelljoin } | #{ lint . shelljoin } " )
55
63
end
56
64
65
+ norm_lines = %w[ tr -- \n \0 ] . shelljoin
66
+
57
67
desc ( "Format `*.rb`" )
58
68
multitask ( :"format:rb" ) do
59
69
# while `syntax_tree` is much faster than `rubocop`, `rubocop` is the only formatter with full syntax support
60
- files = ENV . key? ( FILES_ENV ) ? %w[ sed -E -z -n -e /\.rb$/p -- ] << ENV . fetch ( FILES_ENV ) : %w[ find ./lib ./test ./examples -type f -and -name *.rb -print0 ]
70
+ files = filtered [ "rb" , %w[ ./lib ./test ./examples ] ]
61
71
fmt = xargs + %w[ rubocop --fail-level F --autocorrect --format simple -- ]
62
- sh ( "#{ files . shelljoin } | #{ fmt . shelljoin } " )
72
+ sh ( "#{ files . shelljoin } | #{ norm_lines } | #{ fmt . shelljoin } " )
63
73
end
64
74
65
75
desc ( "Format `*.rbi`" )
66
76
multitask ( :"format:rbi" ) do
67
- files = ENV . key? ( FILES_ENV ) ? %w[ sed -E -z -n -e /\. rbi$/p -- ] << ENV . fetch ( FILES_ENV ) : %w[ find ./rbi -type f -and -name *.rbi -print0 ]
77
+ files = filtered [ " rbi" , %w[ ./rbi ] ]
68
78
fmt = xargs + %w[ stree write -- ]
69
- sh ( ruby_opt , "#{ files . shelljoin } | #{ fmt . shelljoin } " )
79
+ sh ( ruby_opt , "#{ files . shelljoin } | #{ norm_lines } | #{ fmt . shelljoin } " )
70
80
end
71
81
72
82
desc ( "Format `*.rbs`" )
73
83
multitask ( :"format:rbs" ) do
74
- files = ENV . key? ( FILES_ENV ) ? %w[ sed -E -z -n -e /\. rbs$/p -- ] << ENV . fetch ( FILES_ENV ) : %w[ find ./sig -type f -name *.rbs -print0 ]
84
+ files = filtered [ " rbs" , %w[ ./sig ] ]
75
85
inplace = /darwin|bsd/ =~ RUBY_PLATFORM ? [ "-i" , "" ] : %w[ -i ]
76
86
uuid = SecureRandom . uuid
77
87
@@ -100,13 +110,13 @@ multitask(:"format:rbs") do
100
110
success = false
101
111
102
112
# transform class aliases to type aliases, which syntax tree has no trouble with
103
- sh ( "#{ files . shelljoin } | #{ pre . shelljoin } " )
113
+ sh ( "#{ files . shelljoin } | #{ norm_lines } | #{ pre . shelljoin } " )
104
114
# run syntax tree to format `*.rbs` files
105
- sh ( ruby_opt , "#{ files . shelljoin } | #{ fmt . shelljoin } " ) do
115
+ sh ( ruby_opt , "#{ files . shelljoin } | #{ norm_lines } | #{ fmt . shelljoin } " ) do
106
116
success = _1
107
117
end
108
118
# transform type aliases back to class aliases
109
- sh ( "#{ files . shelljoin } | #{ pst . shelljoin } " )
119
+ sh ( "#{ files . shelljoin } | #{ norm_lines } | #{ pst . shelljoin } " )
110
120
111
121
# always run post-processing to remove comment marker
112
122
fail unless success
0 commit comments