Skip to content

Commit 71aa1d6

Browse files
perf: faster code formatting
1 parent fbc0f7e commit 71aa1d6

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

Rakefile

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ tapioca = "sorbet/tapioca"
1212
examples = "examples"
1313
ignore_file = ".ignore"
1414

15+
FILES_ENV = "FORMAT_FILE"
16+
1517
CLEAN.push(*%w[.idea/ .ruby-lsp/ .yardoc/ doc/], *FileList["*.gem"], ignore_file)
1618

1719
CLOBBER.push(*%w[sorbet/rbi/annotations/ sorbet/rbi/gems/], tapioca)
@@ -55,21 +57,21 @@ end
5557
desc("Format `*.rb`")
5658
multitask(:"format:rb") do
5759
# while `syntax_tree` is much faster than `rubocop`, `rubocop` is the only formatter with full syntax support
58-
find = %w[find ./lib ./test ./examples -type f -and -name *.rb -print0]
60+
files = ENV.key?(FILES_ENV) ? %w[sed -E -n -e /\.rb$/p --] << ENV.fetch(FILES_ENV) : %w[find ./lib ./test ./examples -type f -and -name *.rb -print0]
5961
fmt = xargs + %w[rubocop --fail-level F --autocorrect --format simple --]
60-
sh("#{find.shelljoin} | #{fmt.shelljoin}")
62+
sh("#{files.shelljoin} | #{fmt.shelljoin}")
6163
end
6264

6365
desc("Format `*.rbi`")
6466
multitask(:"format:rbi") do
65-
find = %w[find ./rbi -type f -and -name *.rbi -print0]
67+
files = ENV.key?(FILES_ENV) ? %w[sed -E -n -e /\.rbi$/p --] << ENV.fetch(FILES_ENV) : %w[find ./rbi -type f -and -name *.rbi -print0]
6668
fmt = xargs + %w[stree write --]
67-
sh(ruby_opt, "#{find.shelljoin} | #{fmt.shelljoin}")
69+
sh(ruby_opt, "#{files.shelljoin} | #{fmt.shelljoin}")
6870
end
6971

7072
desc("Format `*.rbs`")
7173
multitask(:"format:rbs") do
72-
find = %w[find ./sig -type f -name *.rbs -print0]
74+
files = ENV.key?(FILES_ENV) ? %w[sed -E -n -e /\.rbs$/p --] << ENV.fetch(FILES_ENV) : %w[find ./sig -type f -name *.rbs -print0]
7375
inplace = /darwin|bsd/ =~ RUBY_PLATFORM ? ["-i", ""] : %w[-i]
7476
uuid = SecureRandom.uuid
7577

@@ -98,13 +100,13 @@ multitask(:"format:rbs") do
98100
success = false
99101

100102
# transform class aliases to type aliases, which syntax tree has no trouble with
101-
sh("#{find.shelljoin} | #{pre.shelljoin}")
103+
sh("#{files.shelljoin} | #{pre.shelljoin}")
102104
# run syntax tree to format `*.rbs` files
103-
sh(ruby_opt, "#{find.shelljoin} | #{fmt.shelljoin}") do
105+
sh(ruby_opt, "#{files.shelljoin} | #{fmt.shelljoin}") do
104106
success = _1
105107
end
106108
# transform type aliases back to class aliases
107-
sh("#{find.shelljoin} | #{pst.shelljoin}")
109+
sh("#{files.shelljoin} | #{pst.shelljoin}")
108110

109111
# always run post-processing to remove comment marker
110112
fail unless success

scripts/fast-format

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
echo "Script started with $# arguments"
6+
echo "Arguments: $*"
7+
echo "Script location: $(dirname "$0")"
8+
9+
cd "$(dirname "$0")/.."
10+
echo "Changed to directory: $(pwd)"
11+
12+
if [ $# -eq 0 ]; then
13+
echo "Usage: $0 <file-with-paths> [additional-formatter-args...]"
14+
echo "The file should contain one file path per line"
15+
exit 1
16+
fi
17+
18+
FORMAT_FILE="$1" exec -- bundle exec rake format

0 commit comments

Comments
 (0)