diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3edab7d..2fe15d6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -28,9 +28,44 @@ jobs: - name: Run tests run: pytest tests/ -v + + get-next-version: + needs: test + uses: semantic-release-action/next-release-version/.github/workflows/next-release-version.yml@v4 + update-version: + needs: get-next-version + runs-on: ubuntu-latest + if: needs.get-next-version.outputs.new-release-published == 'true' + steps: + - uses: actions/checkout@v4 + with: + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Update version + run: | + echo "Updating version..." + python scripts/update_version.py --next-version ${{ needs.get-next-version.outputs.new-release-version }} + + - name: Upload updated version file + uses: actions/upload-artifact@v4 + with: + name: version-file + path: casbin_cli/__version__.py + build-binaries: - needs: test + needs: update-version + if: needs.get-next-version.outputs.new-release-published == 'true' runs-on: ${{ matrix.os }} strategy: matrix: @@ -54,6 +89,17 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt pip install pyinstaller + + - name: Download version file + uses: actions/download-artifact@v4 + with: + name: version-file + path: casbin_cli/ + + - name: Verify file content + run: | + echo "Content of __version__.py after update:" + cat casbin_cli/__version__.py - name: Build binary run: python scripts/build_binaries.py --platform ${{ matrix.platform }} @@ -85,8 +131,14 @@ jobs: python -m pip install --upgrade pip pip install -r requirements.txt pip install wheel twine - npm install -g semantic-release @semantic-release/changelog @semantic-release/exec @semantic-release/github - + npm install -g semantic-release @semantic-release/changelog @semantic-release/exec @semantic-release/github @semantic-release/git + + - name: Download version file + uses: actions/download-artifact@v4 + with: + name: version-file + path: casbin_cli/ + - name: Download all artifacts uses: actions/download-artifact@v4 with: diff --git a/.releaserc.json b/.releaserc.json index ddaed9d..3f97ed6 100644 --- a/.releaserc.json +++ b/.releaserc.json @@ -10,12 +10,13 @@ "changelogFile": "CHANGELOG.md" } ], - [ - "@semantic-release/exec", - { - "prepareCmd": "python scripts/update_version.py ${nextRelease.version}" - } - ], + [ + "@semantic-release/git", + { + "assets": ["casbin_cli/__version__.py"], + "message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}" + } + ], [ "@semantic-release/github", { diff --git a/casbin_cli/__version__.py b/casbin_cli/__version__.py index 5b2615d..0063ea1 100644 --- a/casbin_cli/__version__.py +++ b/casbin_cli/__version__.py @@ -1,21 +1,2 @@ -import subprocess - - -def get_base_tag(): - try: - command = ["git", "describe", "--tags", "--abbrev=0"] - - tag = subprocess.check_output( - command, text=True, stderr=subprocess.PIPE - ).strip() - - if tag.startswith("v"): - return tag[1:] - return tag - - except subprocess.CalledProcessError: - print("Error: Failed to find any git tags.") - return None - - -__version__ = get_base_tag() +__version__ = "1.6.0" +__pycasbin_version__ = "2.3.0" diff --git a/casbin_cli/client.py b/casbin_cli/client.py index e1cdea0..1701fdb 100644 --- a/casbin_cli/client.py +++ b/casbin_cli/client.py @@ -5,6 +5,8 @@ from casbin_cli.enforcer_factory import EnforcerFactory from casbin_cli.utils import process_line_breaks from casbin_cli.__version__ import __version__ +from casbin_cli.__version__ import __pycasbin_version__ + class Client: @staticmethod @@ -26,18 +28,7 @@ def run(args=None): return "" elif command_name in ['-v', '--version']: print(f"casbin-python-cli {__version__}") - try: - from importlib.metadata import version - - pycasbin_version = version("pycasbin") - except ImportError: - try: - from importlib_metadata import version - - pycasbin_version = version("pycasbin") - except (ImportError, Exception): - pycasbin_version = "unknown" - print(f"pycasbin {pycasbin_version}") + print(f"pycasbin {__pycasbin_version__}") return "" elif command_name == 'completion': if len(args) < 2: diff --git a/scripts/update_version.py b/scripts/update_version.py index 14bff6e..1b9d553 100644 --- a/scripts/update_version.py +++ b/scripts/update_version.py @@ -15,31 +15,33 @@ import sys import re import os +import argparse -def update_version(new_version): +def update_version(): """Update version in all relevant files""" + parser = argparse.ArgumentParser() + parser.add_argument('--next-version', help='next release version(x.x.x)') + args = parser.parse_args() - # Update setup.py - setup_py_path = "setup.py" - with open(setup_py_path, 'r') as f: - content = f.read() - - # Update client.py version display - client_py_path = "casbin_cli/client.py" - with open(client_py_path, 'r') as f: - content = f.read() - + new_version = args.next_version + try: + from importlib.metadata import version + + pycasbin_version = version("pycasbin") + except ImportError: + try: + from importlib_metadata import version + + pycasbin_version = version("pycasbin") + except (ImportError, Exception): + pycasbin_version = "unknown" # Create __version__.py version_py_path = "casbin_cli/__version__.py" with open(version_py_path, 'w') as f: f.write(f'__version__ = "{new_version}"\n') + f.write(f'__pycasbin_version__ = "{pycasbin_version}"\n') print(f"Updated version to {new_version}") -if __name__ == "__main__": - if len(sys.argv) != 2: - print("Usage: python update_version.py ") - sys.exit(1) - - new_version = sys.argv[1] - update_version(new_version) \ No newline at end of file +if __name__ == "__main__": + update_version() \ No newline at end of file