Style auto format (#2557)

* Autoformat script

* Executable + package.json

* Fix pnpm format not exiting

* Fix exit handling

* Allow to only run fontend autoformat

* Rename script to autoformat.sh
 - Fix exit handling

* Add style check to CI
 - Replace deprecated actions-rs-plus/clippy-check@v2 with giraffate/clippy-action@v1

* Use HEAD as ancestor when running in CI

* Improve uncommmited erro message

* Increate timeout for Type and style check action

* Run Clippy when ci.yml file changes

* increase cypress timeout
This commit is contained in:
Vítor Vasconcellos 2024-06-19 21:04:43 -03:00 committed by GitHub
parent f1255f9e23
commit 8e8747a769
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 114 additions and 24 deletions

View file

@ -20,9 +20,9 @@ concurrency:
jobs:
typescript:
name: TypeScript
name: Type and style check
runs-on: ubuntu-22.04
timeout-minutes: 3
timeout-minutes: 7
permissions: {}
steps:
- name: Checkout repository
@ -36,6 +36,15 @@ jobs:
- name: Perform typechecks
run: pnpm typecheck
- name: Perform style check
run: |-
set -eux
pnpm autoformat only-frontend
if [ -n "$(git diff --name-only --cached)" ]; then
echo "Some files are not correctly formatted. Please run 'pnpm autoformat' and commit the changes." >&2
exit 1
fi
eslint:
name: ESLint
runs-on: ubuntu-22.04
@ -56,7 +65,7 @@ jobs:
cypress:
name: Cypress
runs-on: macos-14
timeout-minutes: 30
timeout-minutes: 45
permissions: {}
steps:
- name: Checkout repository
@ -174,11 +183,20 @@ jobs:
run: cargo fmt --all -- --check
clippy:
name: Clippy (${{ matrix.platform }})
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: true
matrix:
platform: [ubuntu-22.04, macos-14, windows-latest]
settings:
- host: macos-13
target: x86_64-apple-darwin
- host: macos-14
target: aarch64-apple-darwin
- host: windows-latest
target: x86_64-pc-windows-msvc
- host: ubuntu-22.04
target: x86_64-unknown-linux-gnu
name: Clippy (${{ matrix.settings.host }})
runs-on: ${{ matrix.settings.host }}
permissions:
contents: read
timeout-minutes: 45
@ -223,6 +241,7 @@ jobs:
- 'extensions/*/**'
- 'Cargo.toml'
- 'Cargo.lock'
- '.github/workflows/ci.yml'
- name: Setup System and Rust
if: steps.filter.outcome != 'success' || steps.filter.outputs.changes == 'true'
@ -232,22 +251,15 @@ jobs:
- name: Run Clippy
if: steps.filter.outcome != 'success' || steps.filter.outputs.changes == 'true'
uses: actions-rs-plus/clippy-check@v2
uses: giraffate/clippy-action@v1
with:
args: --workspace --all-features --locked
reporter: github-pr-review
tool_name: 'Clippy (${{ matrix.settings.host }})'
filter_mode: diff_context
github_token: ${{ secrets.GITHUB_TOKEN }}
clippy_flags: --workspace --all-features --locked
fail_on_error: true
# test:
# name: Test (${{ matrix.platform }})
# runs-on: ${{ matrix.platform }}
# strategy:
# matrix:
# platform: [ubuntu-22.04, macos-latest, windows-latest]
# steps:
# - name: Checkout repository
# uses: actions/checkout@v4
#
# - name: Setup
# uses: ./.github/actions/setup
#
# - name: Test
# run: cargo test --workspace --all-features
# - name: Run tests
# if: steps.filter.outcome != 'success' || steps.filter.outputs.changes == 'true'
# run: cargo test --workspace --all-features --locked --target ${{ matrix.settings.target }}

View file

@ -97,6 +97,8 @@ After cleaning out your build artifacts using `pnpm clean`, `git clean`, or `car
Make sure to read the [guidelines](https://spacedrive.com/docs/developers/prerequisites/guidelines) to ensure that your code follows a similar style to ours.
After you finish making your changes and committed them to your branch, make sure to execute `pnpm autoformat` to fix any style inconsistency in your code.
##### Mobile App
To run the mobile app:

View file

@ -31,7 +31,8 @@
"lint:fix": "turbo run lint -- --fix",
"clean": "cargo clean; git clean -qfX .",
"test-data": "./scripts/test-data.sh",
"i18n:sync": "npx i18next-locales-sync -p en -s $(find ./interface/locales -wholename '*/common.json' | awk -F'/' '$4 != \"en\" { ORS=\" \"; print $4 }') -l ./interface/locales"
"i18n:sync": "npx i18next-locales-sync -p en -s $(find ./interface/locales -wholename '*/common.json' | awk -F'/' '$4 != \"en\" { ORS=\" \"; print $4 }') -l ./interface/locales",
"autoformat": "./scripts/autoformat.sh"
},
"pnpm": {
"patchedDependencies": {

75
scripts/autoformat.sh Executable file
View file

@ -0,0 +1,75 @@
#!/usr/bin/env bash
set -Eeumo pipefail
has() {
command -v "$1" >/dev/null 2>&1
}
handle_exit() {
_exit=$?
set +e
trap '' SIGINT
trap - EXIT
if [ "$_exit" -ne 0 ]; then
git restore --staged .
git restore .
fi
exit "$_exit"
}
cleanup() {
set +e
trap '' SIGINT
trap - EXIT
jobs -p | xargs kill -SIGTERM
git restore --staged .
git restore .
kill -- -$$ 2>/dev/null
}
if ! has git pnpm; then
echo "Missing at on of the required dependencies: git, pnpm" >&2
exit 1
fi
__dirname="$(CDPATH='' cd "$(dirname "$0")" && pwd -P)"
# Change to the root directory of the repository
cd "$__dirname/.."
if [ -n "$(git diff --name-only HEAD)" ] || [ -n "$(git ls-files --others --exclude-standard)" ]; then
echo "Uncommitted changes found. Please commit or stash your changes before running this script." >&2
exit 1
fi
# Find the common ancestor of the current branch and main
if [ -n "${CI:-}" ]; then
ancestor="HEAD"
elif ! ancestor="$(git merge-base HEAD origin/main)"; then
echo "Failed to find the common ancestor of the current branch and main." >&2
exit 1
fi
# Handle errors and cleanup after formating has started
trap 'handle_exit' EXIT
trap 'cleanup' SIGINT
# Run the linter and formatter for frontend
# Use a background processes to avoid pnpm weird handling of CTRL+C
pnpm run -r lint --fix &
wait
pnpm run format &
wait
if [ "${1:-}" != "only-frontend" ]; then
# Run clippy and formatter for backend
cargo clippy --fix --all --all-targets --all-features --allow-dirty --allow-staged
cargo fmt --all
fi
# Add all fixes for changes made in this branch
git diff --cached --name-only "$ancestor" | xargs git add
# Restore unrelated changes
git restore .