Skip to content

Commit 4adf299

Browse files
committed
🎉 chore: init project with ast-grep v0.38.0
0 parents  commit 4adf299

File tree

7 files changed

+201
-0
lines changed

7 files changed

+201
-0
lines changed

.github/workflows/mirror.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Mirror
2+
on:
3+
push:
4+
branches: [main]
5+
workflow_dispatch:
6+
schedule:
7+
- cron: "0 12 * * *"
8+
jobs:
9+
build:
10+
permissions:
11+
contents: write
12+
name: Mirror
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout
16+
uses: actions/checkout@v4
17+
- name: Install uv
18+
uses: astral-sh/setup-uv@v5
19+
- name: Install python
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: "3.13"
23+
- name: Setup git
24+
run: |
25+
git config --global user.name 'Github Actions'
26+
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com'
27+
- name: Run mirror
28+
run: |
29+
uv run --script mirror.py
30+
- name: Push changes
31+
run: |
32+
git remote set-url origin https://x-access-token:[email protected]/$GITHUB_REPOSITORY
33+
git push origin HEAD:refs/heads/main --tags
34+
env:
35+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/release.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10
6+
workflow_dispatch:
7+
jobs:
8+
publish-release:
9+
runs-on: ubuntu-latest
10+
name: Publish to GitHub
11+
if: startsWith(github.ref, 'refs/tags/')
12+
permissions:
13+
contents: write
14+
steps:
15+
- name: Get tag name
16+
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
17+
- name: Publish to GitHub
18+
uses: softprops/action-gh-release@v2
19+
with:
20+
tag_name: ${{ env.RELEASE_VERSION }}

.pre-commit-hooks.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- id: ast-grep
2+
name: ast-grep
3+
description: "A CLI tool for code structural search, lint and rewriting. Written in Rust"
4+
entry: ast-grep scan
5+
stages: [commit, merge-commit, push, manual]
6+
language: python
7+
minimum_pre_commit_version: 2.9.2
8+
require_serial: false
9+
types: [text]

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2025-present, Nyakku Shigure
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# ast-grep-pre-commit-mirror
2+
3+
[ast-grep](https://ast-grep.github.io/) pre-commit hook distributed via PyPI for seamless integration with Python projects.
4+
5+
## Usage
6+
7+
Add this to your `.pre-commit-config.yaml`:
8+
9+
```yaml
10+
- repo: https://github.com/PFCCLab/ast-grep-pre-commit-mirror
11+
rev: v0.38.0
12+
hooks:
13+
- id: ast-grep
14+
```
15+
16+
### Auto-fix code issues
17+
18+
```yaml
19+
- id: ast-grep
20+
args: ["--update-all"]
21+
```
22+
23+
### Custom configuration
24+
25+
```yaml
26+
- id: ast-grep
27+
args: ["--config", "ast-grep/sgconfig.yml"]
28+
```
29+
30+
## Writing Rules
31+
32+
ast-grep uses AST pattern matching to search and rewrite code:
33+
34+
- [Documentation](https://ast-grep.github.io/guide/introduction.html) - Learn the fundamentals
35+
- [Playground](https://ast-grep.github.io/playground.html) - Test patterns interactively
36+
- [Rule Examples](https://ast-grep.github.io/catalog/) - Browse community rules

mirror.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# /// script
2+
# requires-python = ">=3.9"
3+
# dependencies = [
4+
# "tomli>=2.2.1",
5+
# "tomli-w>=1.2.0",
6+
# "urllib3>=2",
7+
# "packaging>=21.0",
8+
# ]
9+
# ///
10+
11+
import subprocess
12+
from pathlib import Path
13+
14+
import tomli
15+
import tomli_w
16+
import urllib3
17+
from packaging.requirements import Requirement
18+
from packaging.version import Version
19+
20+
21+
def main():
22+
# Load pyproject.toml
23+
with open(Path(__file__).parent / "pyproject.toml", "rb") as f:
24+
pyproject = tomli.load(f)
25+
# Load README.md
26+
with open(Path(__file__).parent / "README.md", "r") as f:
27+
readme = f.read()
28+
29+
# 获取当前版本的 ast-grep
30+
deps = pyproject["project"]["dependencies"]
31+
assert len(deps) == 1
32+
ast_grep_dep = Requirement(deps[0])
33+
assert ast_grep_dep.name == "ast-grep-cli"
34+
ast_grep_specs = list(ast_grep_dep.specifier)
35+
assert len(ast_grep_specs) == 1
36+
assert ast_grep_specs[0].operator == "=="
37+
current_version = Version(ast_grep_specs[0].version)
38+
39+
# Get ast-grep versions from PyPI
40+
http = urllib3.PoolManager()
41+
resp = http.request("GET", "https://pypi.org/pypi/ast-grep-cli/json")
42+
if resp.status != 200:
43+
raise RuntimeError("Failed to fetch data from PyPI")
44+
45+
versions = [Version(release) for release in resp.json()["releases"]]
46+
versions = [v for v in versions if v > current_version and not v.is_prerelease]
47+
versions.sort()
48+
49+
# Update ast-grep for each version
50+
for version in versions:
51+
# Update pyproject.toml
52+
pyproject["project"]["version"] = str(version)
53+
pyproject["project"]["dependencies"] = [f"ast-grep-cli=={version}"]
54+
# Update README.md
55+
updated_readme = readme.replace(str(current_version), str(version))
56+
57+
# Write pyproject.toml and README.md
58+
with open(Path(__file__).parent / "pyproject.toml", "wb") as f:
59+
tomli_w.dump(pyproject, f)
60+
with open(Path(__file__).parent / "README.md", "w") as f:
61+
f.write(updated_readme)
62+
63+
# Commit and tag
64+
subprocess.run(["git", "add", "pyproject.toml", "README.md"])
65+
subprocess.run(
66+
[
67+
"git",
68+
"commit",
69+
"-m",
70+
f":arrow_up: bump ast-grep version to {version}",
71+
]
72+
)
73+
subprocess.run(["git", "tag", f"v{version}"])
74+
75+
76+
if __name__ == "__main__":
77+
main()

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[project]
2+
name = "ast-grep-mirror"
3+
version = "0.38.0"
4+
dependencies = ["ast-grep-cli==0.38.0"]

0 commit comments

Comments
 (0)