Skip to content

Commit 06ba731

Browse files
authored
ci: fail spider formatters and skip deleted prettier files (#5408)
1 parent a2c86cb commit 06ba731

3 files changed

Lines changed: 29 additions & 66 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ jobs:
110110
printf '%s\n' '---' 'comments: true' '---' '' '# LeetCode Contest' '' 'The contest list is not included in this build.' > docs-en/contest.md
111111
fi
112112
113-
- name: Set MKDOCS_API_KEYS
114-
run: echo "MKDOCS_API_KEYS=${{ secrets.MKDOCS_API_KEYS }}" >> $GITHUB_ENV
115-
116113
- name: Build ${{ matrix.lang }} site
114+
env:
115+
MKDOCS_API_KEYS: ${{ secrets.MKDOCS_API_KEYS }}
117116
run: |
118117
python3 build_site.py
119118
mkdocs build -f ${{ matrix.config }}

.github/workflows/prettier.yml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request_target:
55
types: [opened, edited, reopened, synchronize]
66

7-
concurrency:
7+
concurrency:
88
group: ${{github.workflow}} - ${{github.ref}}
99
cancel-in-progress: true
1010

@@ -33,17 +33,19 @@ jobs:
3333
with:
3434
node-version: 22
3535
- name: Install Dependencies
36-
run: pnpm install --frozen-lockfile
36+
run: pnpm install --frozen-lockfile --ignore-scripts
3737
- name: Run prettier
3838
run: |
39+
set -euo pipefail
3940
git config --global core.quotepath off
40-
changed_files=$(git diff --name-only "${{ github.event.pull_request.base.sha }}" | grep -E '\.js$|\.ts$|\.php$|\.sql$|\.md$' || true)
41-
if [ -n "$changed_files" ]; then
42-
echo "Running prettier on the changed files"
43-
echo "$changed_files" | xargs -d '\n' pnpm exec prettier --write
44-
else
41+
git fetch --depth=1 origin "${{ github.event.pull_request.base.sha }}"
42+
mapfile -t files < <(git diff --name-only --diff-filter=ACMR "${{ github.event.pull_request.base.sha }}" -- '*.js' '*.ts' '*.php' '*.sql' '*.md' || true)
43+
if [ ${#files[@]} -eq 0 ]; then
4544
echo "No matching files to run prettier on."
45+
exit 0
4646
fi
47+
echo "Running prettier on the changed files"
48+
pnpm exec prettier --write "${files[@]}"
4749
- name: Commit changes
4850
uses: stefanzweifel/git-auto-commit-action@v7
4951
with:

solution/main.py

Lines changed: 18 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import platform
1+
import os
22
import subprocess
33
import time
44
from datetime import timezone, timedelta, datetime
5+
from pathlib import Path
56

67
import requests
78
import urllib3
@@ -414,54 +415,19 @@ def get_contests(fetch_new=True) -> List:
414415

415416
########################################################################################
416417

418+
ROOT = Path(__file__).resolve().parents[1]
417419

418-
def format_rust_files_linux():
419-
# The find command to locate and format all .rs files in Linux
420-
find_command = 'find . -name "*.rs" -exec rustfmt {} \\;'
421420

422-
# Execute the command
423-
process = subprocess.Popen(
424-
find_command,
425-
shell=True,
426-
stdout=subprocess.PIPE,
427-
stderr=subprocess.PIPE,
428-
text=True,
429-
)
430-
431-
# Get the output and errors
432-
stdout, stderr = process.communicate()
433-
434-
if process.returncode == 0:
435-
print("Rust files formatted successfully on Linux!")
436-
print(stdout)
437-
else:
438-
print("Error formatting Rust files on Linux:")
439-
print(stderr)
440-
441-
442-
def format_rust_files_windows():
443-
# PowerShell command to format all .rs files recursively in Windows
444-
ps_command = (
445-
"Get-ChildItem -Recurse -Filter *.rs | ForEach-Object { rustfmt $_.FullName }"
446-
)
447-
448-
# Execute the PowerShell command
449-
process = subprocess.Popen(
450-
["powershell", "-Command", ps_command],
451-
stdout=subprocess.PIPE,
452-
stderr=subprocess.PIPE,
453-
text=True,
454-
)
455-
456-
# Get the output and errors
457-
stdout, stderr = process.communicate()
458-
459-
if process.returncode == 0:
460-
print("Rust files formatted successfully on Windows!")
461-
print(stdout)
462-
else:
463-
print("Error formatting Rust files on Windows:")
464-
print(stderr)
421+
def format_rust_files() -> None:
422+
skip = {"node_modules", "__pycache__", ".git"}
423+
rs_files = []
424+
for dirpath, dirnames, filenames in os.walk(ROOT):
425+
dirnames[:] = [name for name in dirnames if name not in skip]
426+
for name in filenames:
427+
if name.endswith(".rs"):
428+
rs_files.append(os.path.join(dirpath, name))
429+
for i in range(0, len(rs_files), 64):
430+
subprocess.check_call(["rustfmt", *rs_files[i : i + 64]])
465431

466432

467433
def run():
@@ -540,15 +506,11 @@ def run():
540506
generate_category_readme(ls, "JavaScript")
541507

542508
refresh(ls)
543-
# 格式化
544-
os.system('cd .. && npx prettier --write "**/*.{md,js,ts,php,sql}"')
545-
546-
# 格式化 rust 代码
547-
# 判断当前是 windows 还是 linux
548-
if platform.system() == "Linux":
549-
format_rust_files_linux()
550-
elif platform.system() == "Windows":
551-
format_rust_files_windows()
509+
subprocess.check_call(
510+
["npx", "prettier", "--write", "**/*.{md,js,ts,php,sql}"],
511+
cwd=ROOT,
512+
)
513+
format_rust_files()
552514

553515

554516
if __name__ == "__main__":

0 commit comments

Comments
 (0)