Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dart fix --dry-run での確認フローを追加 #113

Merged
merged 9 commits into from
Feb 13, 2024
38 changes: 36 additions & 2 deletions scripts/check-format-ci.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
#!/usr/bin/env bash

# for terminate any remaining background jobs when the script exits.
warahiko marked this conversation as resolved.
Show resolved Hide resolved
trap '[ 0 -lt $(jobs | wc -l) ] && kill -SIGKILL $(jobs -p)' EXIT
warahiko marked this conversation as resolved.
Show resolved Hide resolved

# wait for all child processes to finish and returns a non-zero status if any of them fail.
warahiko marked this conversation as resolved.
Show resolved Hide resolved
wait_for_all(){
for pid in $@; do
wait ${pid} || return 1
done
}
warahiko marked this conversation as resolved.
Show resolved Hide resolved

find_files() {
find . -name "*.dart" -not \( -name "*.freezed.dart" -o -name "*.g.dart" -o -name "*.gen.dart" -o -path "*/gen/*.dart" -o -path "./.dart_tool/*" \) -print0
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment
このあたり format.sh でもありますし、管理上漏れが出そうな見た目してるので共通化したほうがいいかもしれないですね

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4c024f6にて共通化しました。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

common.sh というのが一般的すぎる気もしますが、今のところ問題ないかなと思いました 👍
( $0 より適当な書き方があるんですね......!勉強なります)


files=()
while IFS= read -r -d $'\0' file; do
files+=("$file")
done < <(find . -name "*.dart" -not \( -name "*.freezed.dart" -o -name "*.g.dart" -o -name "*.gen.dart" -o -path "*/gen/*.dart" -o -path "./.dart_tool/*" \) -print0)
files+=("$file")
done < <(find_files)

PIDS=()
warahiko marked this conversation as resolved.
Show resolved Hide resolved
for file in "${files[@]}"; do
# limit jobs to 5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if [ "$(jobs -r | wc -l)" -ge 5 ]; then
wait "$(jobs -r -p | head -1)"
fi

(
# Note: The script relies on the 'Nothing to fix!' output from 'dart fix --dry-run'.
# This dependency might be an issue if the output format changes in future Dart versions.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge
dart fix --dry-run の出力の最後が 「Nothing to fix!」で終了しているかで判定

背景

dart fix --applyを実行し、git diff による差分で比較しない理由

実行速度において、dart fix --dry-rundart fix --applyより遥かに速いため

「Nothing to fix!」という文言で判定している理由

dart fix --dry-runは修正必要なファイルを検出してもステータスコード 0 を返すため

FYI: dart fix --dry-run の実行結果

修正必要なファイルなし

fvm dart fix --dry-run  
Computing fixes in flutter-mobile-project-template (dry run)...
Nothing to fix!

修正必要なファイルを検出

fvm dart fix --dry-run
Computing fixes in flutter-mobile-project-template (dry run)...

1 proposed fix in 1 file.

apps/app/lib/main.dart
  directives_ordering - 1 fix

To fix an individual diagnostic, run one of:
  dart fix --apply --code=directives_ordering 

To fix all diagnostics, run:
  dart fix --apply 

trm11tkr marked this conversation as resolved.
Show resolved Hide resolved
if ! dart fix --dry-run "$file" | grep -q "Nothing to fix!"; then
Kotaro666-dev marked this conversation as resolved.
Show resolved Hide resolved
echo "Fixes might be needed for $file"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memo-badge
dart fix --dry-runの実行ログが表示されないようになるため、修正必要ファイルが検出された際にはメッセージを表示

exit 1
fi
) &
PIDS+=($!)
done

dart format -o none --set-exit-if-changed "${files[@]}"

wait_for_all ${PIDS[@]}
Loading