Skip to content

Commit cfce5c4

Browse files
yanglbmecursoragent
andcommitted
ci: fail spider formatters and skip deleted prettier files
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a2c86cb commit cfce5c4

3 files changed

Lines changed: 27 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: 16 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import platform
21
import subprocess
32
import time
43
from datetime import timezone, timedelta, datetime
4+
from pathlib import Path
55

66
import requests
77
import urllib3
@@ -414,54 +414,18 @@ def get_contests(fetch_new=True) -> List:
414414

415415
########################################################################################
416416

417+
ROOT = Path(__file__).resolve().parents[1]
417418

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 {} \\;'
421419

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)
420+
def format_rust_files() -> None:
421+
skip = {"node_modules", "__pycache__", ".git"}
422+
rs_files = [
423+
str(path)
424+
for path in ROOT.rglob("*.rs")
425+
if path.is_file() and skip.isdisjoint(path.parts)
426+
]
427+
for i in range(0, len(rs_files), 64):
428+
subprocess.check_call(["rustfmt", *rs_files[i : i + 64]])
465429

466430

467431
def run():
@@ -540,15 +504,11 @@ def run():
540504
generate_category_readme(ls, "JavaScript")
541505

542506
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()
507+
subprocess.check_call(
508+
["npx", "prettier", "--write", "**/*.{md,js,ts,php,sql}"],
509+
cwd=ROOT,
510+
)
511+
format_rust_files()
552512

553513

554514
if __name__ == "__main__":

0 commit comments

Comments
 (0)