Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions .github/workflows/auto_rules_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Auto Rules Version

on:
push:
branches: [main]
paths:
- 'rostran/rules/**'
- '!rostran/rules/VERSION'
- '!rostran/rules/VERSIONS.json'
pull_request:
branches: [main]
paths:
- 'rostran/rules/**'
- '!rostran/rules/VERSION'
- '!rostran/rules/VERSIONS.json'

permissions:
contents: write

jobs:
auto-rules-version:
runs-on: ubuntu-22.04
if: "!contains(github.event.head_commit.message, 'chore: bump rules version')"
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.pull_request.head.ref || '' }}

- name: Check if rule files actually changed
id: check_changes
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
else
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD)
fi

RULES_CHANGED=false
while IFS= read -r file; do
[ -z "$file" ] && continue
case "$file" in
rostran/rules/VERSION) continue ;;
rostran/rules/VERSIONS.json) continue ;;
rostran/rules/*)
RULES_CHANGED=true
echo "Rule file changed: $file"
;;
esac
done <<< "$CHANGED_FILES"

echo "rules_changed=$RULES_CHANGED" >> "$GITHUB_OUTPUT"

- name: Set up Python
if: steps.check_changes.outputs.rules_changed == 'true'
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Bump VERSION and update VERSIONS.json
if: steps.check_changes.outputs.rules_changed == 'true'
id: bump
run: |
VERSION_FILE="rostran/rules/VERSION"
VERSIONS_JSON="rostran/rules/VERSIONS.json"

CURRENT_VERSION=$(cat "$VERSION_FILE" | tr -d '[:space:]')
echo "Current rules version: $CURRENT_VERSION"

# Bump patch version
NEW_VERSION=$(python3 -c "
parts = '$CURRENT_VERSION'.split('.')
parts[-1] = str(int(parts[-1]) + 1)
print('.'.join(parts))
")
echo "New rules version: $NEW_VERSION"

# Write new VERSION file
echo "$NEW_VERSION" > "$VERSION_FILE"

# Get commit info (use current HEAD; this commit will be
# amended after we add our changes, but the SHA recorded
# here will be updated by the commit step below)
COMMIT_SHA=$(git rev-parse HEAD)
TODAY=$(date -u +%Y-%m-%d)

# Update VERSIONS.json
python3 << PYEOF
import json

version = "$NEW_VERSION"
commit_sha = "$COMMIT_SHA"
today = "$TODAY"

with open("$VERSIONS_JSON", "r") as f:
data = json.load(f)

data["latest"] = version
if "versions" not in data:
data["versions"] = {}

new_versions = {
version: {
"commit": commit_sha,
"date": today,
"description": ""
}
}
new_versions.update(data["versions"])
data["versions"] = new_versions

with open("$VERSIONS_JSON", "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
PYEOF

echo "old_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT"

- name: Commit version bump
if: steps.check_changes.outputs.rules_changed == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add rostran/rules/VERSION rostran/rules/VERSIONS.json
git diff --cached --quiet && echo "No changes to commit" && exit 0
git commit -m "chore: bump rules version ${{ steps.bump.outputs.old_version }} -> ${{ steps.bump.outputs.new_version }}"
git push

- name: Update VERSIONS.json with final commit SHA
if: steps.check_changes.outputs.rules_changed == 'true'
run: |
VERSIONS_JSON="rostran/rules/VERSIONS.json"
NEW_VERSION="${{ steps.bump.outputs.new_version }}"
FINAL_SHA=$(git rev-parse HEAD)
TODAY=$(date -u +%Y-%m-%d)

python3 << PYEOF
import json

with open("$VERSIONS_JSON", "r") as f:
data = json.load(f)

data["versions"]["$NEW_VERSION"]["commit"] = "$FINAL_SHA"
data["versions"]["$NEW_VERSION"]["date"] = "$TODAY"

with open("$VERSIONS_JSON", "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
PYEOF

git add rostran/rules/VERSIONS.json
git diff --cached --quiet && echo "SHA already correct, skipping." && exit 0
git commit -m "chore: update VERSIONS.json commit SHA for rules version $NEW_VERSION"
git push
119 changes: 119 additions & 0 deletions .github/workflows/auto_version_bump.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Auto Version Bump

on:
push:
branches: [main]
paths-ignore:
- 'rostran/rules/**'
- 'docs/**'
- '*.md'
- 'LICENSE'
pull_request:
branches: [main]
paths-ignore:
- 'rostran/rules/**'
- 'docs/**'
- '*.md'
- 'LICENSE'

permissions:
contents: write

jobs:
bump-version:
runs-on: ubuntu-22.04
# Skip commits made by this workflow to avoid infinite loops
if: "!contains(github.event.head_commit.message, 'chore: bump version')"
steps:
- name: Check out repository
uses: actions/checkout@v4
with:
# For push: need HEAD~1 diff; for PR: need full branch history
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
# For PRs, check out the PR head branch so we can push back to it
ref: ${{ github.event.pull_request.head.ref || '' }}

- name: Check if code files changed (excluding rules)
id: check_changes
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
CHANGED_FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD)
else
CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD)
fi

echo "Changed files:"
echo "$CHANGED_FILES"

CODE_CHANGED=false
while IFS= read -r file; do
[ -z "$file" ] && continue
case "$file" in
rostran/rules/*) continue ;;
docs/*) continue ;;
*.md) continue ;;
LICENSE) continue ;;
.github/workflows/auto_version_bump.yml) continue ;;
.github/workflows/auto_rules_version.yml) continue ;;
*)
CODE_CHANGED=true
echo "Code change detected: $file"
break
;;
esac
done <<< "$CHANGED_FILES"

echo "code_changed=$CODE_CHANGED" >> "$GITHUB_OUTPUT"

- name: Set up Python
if: steps.check_changes.outputs.code_changed == 'true'
uses: actions/setup-python@v5
with:
python-version: '3.12'

- name: Bump patch version
if: steps.check_changes.outputs.code_changed == 'true'
id: bump
run: |
INIT_FILE="rostran/__init__.py"
CURRENT_VERSION=$(python -c "
import re
with open('$INIT_FILE') as f:
match = re.search(r\"__version__\s*=\s*['\\\"]([^'\\\"]+)['\\\"]\", f.read())
print(match.group(1))
")
echo "Current version: $CURRENT_VERSION"

NEW_VERSION=$(python -c "
parts = '$CURRENT_VERSION'.split('.')
parts[-1] = str(int(parts[-1]) + 1)
print('.'.join(parts))
")
echo "New version: $NEW_VERSION"

python -c "
import re
with open('$INIT_FILE', 'r') as f:
content = f.read()
content = re.sub(
r\"__version__\s*=\s*['\\\"][^'\\\"]+['\\\"]\",
f'__version__ = \\\"$NEW_VERSION\\\"',
content,
)
with open('$INIT_FILE', 'w') as f:
f.write(content)
"

echo "old_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
echo "new_version=$NEW_VERSION" >> "$GITHUB_OUTPUT"

- name: Commit version bump
if: steps.check_changes.outputs.code_changed == 'true'
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add rostran/__init__.py
git diff --cached --quiet && echo "No changes to commit" && exit 0
git commit -m "chore: bump version ${{ steps.bump.outputs.old_version }} -> ${{ steps.bump.outputs.new_version }}"
git push
64 changes: 64 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,70 @@ The following options are supported:
- `--with-link`: Append a link when showing rules in markdown format.
- `--no-with-link`: [Default] No link is attached when showing rules in markdown format.

## Update Transform Rules

Update transform rules from the remote repository without upgrading the package.
Downloaded rules are cached locally at `~/.rostran/rules/` and take precedence over the built-in rules shipped with the package.

### Command

```bash
rostran update-rules [OPTIONS]
```

#### OPTIONS

The following options are supported:

- `--version`/`-v`: Specific rules version to install (e.g. `1.2.0`). Defaults to the latest version on the main branch.
- `--list`/`-l`: List all available rules versions from remote.
- `--force`: Force re-download even if the local rules are already up-to-date.
- `--clean`: Remove the local rules cache and revert to built-in rules.

#### Examples

```bash
# Update to the latest rules version
rostran update-rules

# Install a specific rules version
rostran update-rules --version 1.2.0

# List all available rules versions
rostran update-rules --list

# Force re-download
rostran update-rules --force

# Remove local cache and revert to built-in rules
rostran update-rules --clean
```

## Show Rules Version

Show the version and source of the currently active rules.

### Command

```bash
rostran rules-version
```

When using locally cached rules (after running `update-rules`):

```
Rules source : local cache (~/.rostran/rules/)
Rules version: 1.2.0
Built-in ver : 1.0.0
```

When using built-in rules (default):

```
Rules source : built-in (shipped with package)
Rules version: 1.0.0
```

## View Help Information

### Command
Expand Down
Loading
Loading