From 9d1eebddbe3077c6b61ad8e82e65e02b17576a1d Mon Sep 17 00:00:00 2001 From: Zimo Li <7163127+lzm0@users.noreply.github.com> Date: Mon, 24 Jun 2024 13:17:54 -0400 Subject: [PATCH] Infer scalafmt version from `.scalafmt.conf` instead of using arg (#5) * Infer scalafmt version from `.scalafmt.conf` instead of using arg * Add GitHub step summary messages for failed and successful runs --- .github/workflows/test.yml | 3 ++- action.yml | 17 ++++++++++------- run-scalafmt.sh | 9 +-------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1010c85..34e6a1b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -8,5 +8,6 @@ jobs: - uses: actions/checkout@v4 - name: Checking testdata (pass) uses: ./ + working-directory: .github/testdata with: - args: "-c .github/testdata/.scalafmt.conf --exclude .github/testdata/Bad.scala --list" + args: "--exclude Bad.scala --list" diff --git a/action.yml b/action.yml index 4def6f9..4edec3d 100644 --- a/action.yml +++ b/action.yml @@ -9,25 +9,28 @@ inputs: description: "Args to pass to scalafmt" default: "--list" required: false - version: - description: "Version of scalafmt" - default: "3.7.15" - required: false runs: using: "composite" steps: + - shell: bash + run: | + version=$(perl -ne 'next unless /^\s*version\s*=\s*(\d+\.\d+\.\d+)/; print "$1"; last' .scalafmt.conf) + if [ -z "$version" ]; then + echo "Failed to extract scalafmt version from .scalafmt.conf" >> "$GITHUB_STEP_SUMMARY" + exit 1 + fi + echo "scalafmt_version=$version" >> "$GITHUB_ENV" - shell: bash run: | "$action_path/set-path.sh" env: - scalafmt_version: ${{ inputs.version }} action_path: ${{ github.action_path }} - uses: actions/cache/restore@v4 id: cache with: path: ${{ env.SCALAFMT_PATH }} - key: scalafmt-${{ inputs.version }} + key: scalafmt-${{ env.scalafmt_version }} - shell: bash id: retrieve-scalafmt if: ${{ !steps.cache.outputs.cache-hit }} @@ -45,4 +48,4 @@ runs: uses: actions/cache/save@v4 with: path: ${{ env.SCALAFMT_PATH }} - key: scalafmt-${{ inputs.version }} + key: scalafmt-${{ env.scalafmt_version }} diff --git a/run-scalafmt.sh b/run-scalafmt.sh index 6f538b1..2fd3269 100755 --- a/run-scalafmt.sh +++ b/run-scalafmt.sh @@ -3,18 +3,11 @@ output=$(mktemp) warnings=$(mktemp) errors=$(mktemp) -enforce_scala_format_version() { - if [ -e .scalafmt.conf ]; then - perl -pi -e 'next unless s/^\s*(version)=.*/$1=$ENV{scalafmt_version}/' .scalafmt.conf - fi -} - -enforce_scala_format_version scalafmt --non-interactive $args > "$output" 2> "$warnings" result=$? cat "$output" cat "$warnings" >&2 -[ $result -eq 0 ] && exit +[ $result -eq 0 ] && echo "All files well-formatted ✨" >> "$GITHUB_STEP_SUMMARY" && exit report() { file="$1"