8000 fix(tooling): format staged Dart per package by shroominic · Pull Request #82 · shroominic/OpenGlucose · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ pre-commit:
parallel: true
commands:
format:
glob: "*.dart"
run: dart format --output=none --set-exit-if-changed {staged_files}
# Run for every commit. The helper exits quickly when no Dart source is
# staged, and rejects a staged Dart file outside a configured project.
# A glob here could otherwise hide an unrecognized staged Dart file.
run: ./scripts/flutter-workspace.sh format-staged
analyze:
glob: "{openhealth,packages}/**/*.{dart,yaml}"
run: make lint
Expand Down
1 change: 1 addition & 0 deletions scripts/check-tooling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ command -v ruby >/dev/null 2>&1 || {
exit 1
}
ruby "$repo_root/scripts/test-android-release-workflow-contract.rb"
ruby "$repo_root/scripts/test-lefthook-format-contract.rb"
ruby "$repo_root/scripts/test-macos-preview-workflow-contract.rb"
ruby "$repo_root/scripts/test-testflight-release-workflow-contract.rb"
ruby "$repo_root/scripts/test-testflight-release-tag-contract.rb"
Expand Down
88 changes: 78 additions & 10 deletions scripts/flutter-workspace.sh
7438
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ packages/cgm_aidex
openhealth
"

# Git hooks export worktree-local variables such as GIT_INDEX_FILE. Flutter's
# launcher runs Git inside its own SDK checkout; inheriting those values makes
# it inspect this repository instead and can recursively rebuild the tool.
if command -v git >/dev/null 2>&1; then
for git_local_variable in $(git rev-parse --local-env-vars 2>/dev/null); do
unset "$git_local_variable"
done
fi

die() {
printf 'error: %s\n' "$*" >&2
exit 1
Expand All @@ -35,7 +26,23 @@ uses_flutter() {
grep -q 'sdk: flutter' "$repo_root/$1/pubspec.yaml"
}

check_dart_format() {
dart format --output=none --set-exit-if-changed "$@"
}

clear_git_local_environment() {
# Git hooks export worktree-local variables such as GIT_INDEX_FILE. Flutter's
# launcher runs Git inside its own SDK checkout; inheriting those values makes
# it inspect this repository instead and can recursively rebuild the tool.
if command -v git >/dev/null 2>&1; then
for git_local_variable in $(git rev-parse --local-env-vars 2>/dev/null); do
unset "$git_local_variable"
done
fi
}

verify_runtime() {
clear_git_local_environment
require_tool flutter
require_tool dart

Expand Down Expand Up @@ -75,7 +82,7 @@ for_project() {
set -- lib
[ ! -d test ] || set -- "$@" test
[ ! -d integration_test ] || set -- "$@" integration_test
dart format --output=none --set-exit-if-changed "$@"
check_dart_format "$@"
;;
analyze)
if uses_flutter "$project_dir"; then
Expand Down Expand Up @@ -112,6 +119,64 @@ for_project() {
done
}

staged_dart_files() {
require_tool git
(
cd "$repo_root"
# Deleted files do not need formatting. Additions, copies, modifications,
# and renames must all be checked before they enter the index.
git diff --cached --name-only --diff-filter=ACMR -- '*.dart'
)
}

format_staged_files() {
staged_files=$(staged_dart_files)
[ -n "$staged_files" ] || return 0

# Do not let a newly added Dart project or an unexpected staged path bypass
# the formatter. Keep project_dirs as the single source of package roots.
while IFS= read -r staged_file; do
known_project=false
for project_dir in $project_dirs; do
case "$staged_file" in
"$project_dir"/*.dart)
known_project=true
break
;;
esac
done
[ "$known_project" = true ] ||
die "staged Dart file is outside a configured project: $staged_file"
done <<EOF
$staged_files
EOF

verify_runtime
for project_dir in $project_dirs; do
(
cd "$repo_root/$project_dir"
set --
while IFS= read -r staged_file; do
case "$staged_file" in
"$project_dir"/*.dart)
relative_file=${staged_file#"$project_dir"/}
[ -f "$relative_file" ] ||
die "staged Dart file is missing from the working tree: $staged_file"
set -- "$@" "$relative_file"
;;
esac
done <<EOF
$staged_files
EOF

[ "$#" -eq 0 ] || {
printf '\n==> %s: staged format-check\n' "$project_dir"
check_dart_format "$@"
}
)
done
}

integration_test_files() {
project_path=$1
for test_root in test integration_test; do
Expand Down Expand Up @@ -256,6 +321,9 @@ case "$command_name" in
verify_runtime
for_project "$command_name"
;;
format-staged)
format_staged_files
;;
outdated)
outdated_dependencies
;;
Expand Down
39 changes: 39 additions & 0 deletions scripts/test-lefthook-format-contract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/env ruby

root = File.expand_path("..", __dir__)
hook = File.read(File.join(root, "lefthook.yml"))
workspace = File.read(File.join(root, "scripts/flutter-workspace.sh"))
tooling_check = File.read(File.join(root, "scripts/check-tooling.sh"))

def assert(condition, message)
raise "Lefthook format contract failed: #{message}" unless condition
end

format_command = hook[/^ format:\n(?:(?: .*| #.*)\n)*/]
assert(format_command, "pre-commit format command must exist")
assert(
format_command.include?("run: ./scripts/flutter-workspace.sh format-staged"),
"format command must use the workspace formatter",
)
assert(!format_command.include?("glob:"), "format command must inspect every staged Dart path")
assert(!format_command.include?("dart format"), "format command must not format from the repository root")

assert(
workspace.include?("git diff --cached --name-only --diff-filter=ACMR -- '*.dart'"),
"staged additions, copies, modifications, and renames must be discovered",
)
assert(
workspace.include?("staged Dart file is outside a configured project"),
"an unknown staged Dart path must fail instead of being skipped",
)
assert(
workspace.include?("check_dart_format \"$@\"") &&
workspace.include?("format-staged)\n format_staged_files"),
"staged files must use the same formatter command as format-check",
)
assert(
tooling_check.include?("test-lefthook-format-contract.rb"),
"the hook contract must run with tooling checks",
)

puts "Lefthook staged-format contract passed."
0