Skip to content

Commit 2c2f51c

Browse files
chore: allow fast-format to use bsd sed as well
1 parent 73459f2 commit 2c2f51c

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Rakefile

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ end
4040
xargs = %w[xargs --no-run-if-empty --null --max-procs=0 --max-args=300 --]
4141
ruby_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+
4351
desc("Lint `*.rb(i)`")
4452
multitask(:"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}")
5563
end
5664

65+
norm_lines = %w[tr -- \n \0].shelljoin
66+
5767
desc("Format `*.rb`")
5868
multitask(:"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}")
6373
end
6474

6575
desc("Format `*.rbi`")
6676
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]]
6878
fmt = xargs + %w[stree write --]
69-
sh(ruby_opt, "#{files.shelljoin} | #{fmt.shelljoin}")
79+
sh(ruby_opt, "#{files.shelljoin} | #{norm_lines} | #{fmt.shelljoin}")
7080
end
7181

7282
desc("Format `*.rbs`")
7383
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]]
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

scripts/fast-format

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,4 @@ if [ $# -eq 0 ]; then
1515
exit 1
1616
fi
1717

18-
FILE="$(mktemp)"
19-
tr -- '\n' '\0' < "$1" > "$FILE"
20-
21-
exec -- bundle exec rake format FORMAT_FILE="$FILE"
18+
exec -- bundle exec rake format FORMAT_FILE="$1"

0 commit comments

Comments
 (0)