4040xargs = %w[ xargs --no-run-if-empty --null --max-procs=0 --max-args=300 -- ]
4141ruby_opt = { "RUBYOPT" => [ ENV [ "RUBYOPT" ] , "--encoding=UTF-8" ] . compact . join ( " " ) }
4242
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+
4351desc ( "Lint `*.rb(i)`" )
4452multitask ( :"lint:rubocop" ) do
4553 find = %w[ find ./lib ./test ./rbi ./examples -type f -and ( -name *.rb -or -name *.rbi ) -print0 ]
@@ -54,24 +62,26 @@ multitask(:"lint:rubocop") do
5462 sh ( "#{ find . shelljoin } | #{ lint . shelljoin } " )
5563end
5664
65+ norm_lines = %w[ tr -- \n \0 ] . shelljoin
66+
5767desc ( "Format `*.rb`" )
5868multitask ( :"format:rb" ) do
5969 # 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 ] ]
6171 fmt = xargs + %w[ rubocop --fail-level F --autocorrect --format simple -- ]
62- sh ( "#{ files . shelljoin } | #{ fmt . shelljoin } " )
72+ sh ( "#{ files . shelljoin } | #{ norm_lines } | #{ fmt . shelljoin } " )
6373end
6474
6575desc ( "Format `*.rbi`" )
6676multitask ( :"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 ] ]
6878 fmt = xargs + %w[ stree write -- ]
69- sh ( ruby_opt , "#{ files . shelljoin } | #{ fmt . shelljoin } " )
79+ sh ( ruby_opt , "#{ files . shelljoin } | #{ norm_lines } | #{ fmt . shelljoin } " )
7080end
7181
7282desc ( "Format `*.rbs`" )
7383multitask ( :"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 ] ]
7585 inplace = /darwin|bsd/ =~ RUBY_PLATFORM ? [ "-i" , "" ] : %w[ -i ]
7686 uuid = SecureRandom . uuid
7787
@@ -100,13 +110,13 @@ multitask(:"format:rbs") do
100110 success = false
101111
102112 # 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 } " )
104114 # 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
106116 success = _1
107117 end
108118 # transform type aliases back to class aliases
109- sh ( "#{ files . shelljoin } | #{ pst . shelljoin } " )
119+ sh ( "#{ files . shelljoin } | #{ norm_lines } | #{ pst . shelljoin } " )
110120
111121 # always run post-processing to remove comment marker
112122 fail unless success
0 commit comments