From 3004879575bffbd85dbbef2396b0eec4c2d7f191 Mon Sep 17 00:00:00 2001 From: MATSUNAGA Takuya Date: Wed, 4 Dec 2024 14:05:40 +0900 Subject: [PATCH 1/3] feat: Automate version management and update Tauri application configuration - Updated version numbers in package.json, package-lock.json, Cargo.toml, and Cargo.lock to 0.2.1. - Added GitHub Actions workflow for publishing automatic releases. - Extracted version number from the branch name and updated the application's version accordingly. - Introduced script to update the Tauri version in the Cargo.toml file. - Configured automatic commits and pushes of version changes back to the repository. - Removed hardcoded version in tauri.conf.json to allow automatic versioning. --- .github/workflows/publish-to-auto-release.yml | 34 ++++++++++ package-lock.json | 4 +- package.json | 2 +- script/update_tauri_version.py | 67 +++++++++++++++++++ script/update_version.sh | 7 ++ src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- 8 files changed, 114 insertions(+), 6 deletions(-) create mode 100755 script/update_tauri_version.py create mode 100755 script/update_version.sh diff --git a/.github/workflows/publish-to-auto-release.yml b/.github/workflows/publish-to-auto-release.yml index b7caada..1e6bfcb 100644 --- a/.github/workflows/publish-to-auto-release.yml +++ b/.github/workflows/publish-to-auto-release.yml @@ -50,6 +50,40 @@ jobs: - name: install frontend dependencies run: npm install # change this to npm, pnpm or bun depending on which one you use. + + - uses: tauri-apps/tauri-action@v0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + args: ${{ matrix.args }} + + - name: Extract version from branch name + id: extract_version + run: | + VERSION=${GITHUB_REF#refs/heads/release/} + if [[ -z "$VERSION" ]]; then + echo "Error: Version could not be extracted from the branch name." + exit 1 + fi + echo "VERSION=$VERSION" >> $GITHUB_ENV + + - name: Update version number + run: ./script/update_version $VERSION + + - name: Set up Git + run: | + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + + - name: Commit changes + run: | + git add . + git commit -m "Automated commit from GitHub Actions" + + - name: Push changes + uses: ad-m/github-push-action@v0.8.0 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} - uses: tauri-apps/tauri-action@v0 env: diff --git a/package-lock.json b/package-lock.json index 9feeb43..5a5860b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cuuri", - "version": "0.2.0", + "version": "0.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cuuri", - "version": "0.2.0", + "version": "0.2.1", "dependencies": { "@tauri-apps/api": "^2", "@tauri-apps/plugin-shell": "^2", diff --git a/package.json b/package.json index 4295aae..4b97288 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cuuri", "private": true, - "version": "0.2.0", + "version": "0.2.1", "type": "module", "scripts": { "dev": "vite", diff --git a/script/update_tauri_version.py b/script/update_tauri_version.py new file mode 100755 index 0000000..a52b45f --- /dev/null +++ b/script/update_tauri_version.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +import sys +import re +from typing import List +from pathlib import Path + +def read_file(file_path: Path) -> List[str]: + try: + with open(file_path, 'r', encoding='utf-8') as file: + return file.readlines() + except FileNotFoundError: + print(f"Error: The file {file_path} was not found.") + sys.exit(1) + except IOError: + print(f"Error: Could not read the file {file_path}.") + sys.exit(1) + +def write_file(file_path: Path, lines: List[str]) -> None: + try: + with open(file_path, 'w', encoding='utf-8') as file: + file.writelines(lines) + except IOError: + print(f"Error: Could not write to the file {file_path}.") + sys.exit(1) + +def update_version(lines: List[str], new_version: str) -> List[str]: + in_package_section = False + version_updated = False + pattern = re.compile(r'^version\s*=\s*".*"$') + + def process_line(line: str) -> str: + nonlocal in_package_section, version_updated + + if line.strip() == "[package]": + in_package_section = True + elif line.strip().startswith("[") and in_package_section: + in_package_section = False + + if in_package_section and pattern.search(line): + version_updated = True + return f'version = "{new_version}"\n' + + return line + + updated_lines = list(map(process_line, lines)) + + if not version_updated: + print("Error: No version entry was updated. Please ensure the [package] section exists and contains a version entry.") + sys.exit(1) + + return updated_lines + +def main(new_version: str, file_path: Path) -> None: + lines = read_file(file_path) + updated_lines = update_version(lines, new_version) + write_file(file_path, updated_lines) + print(f"Version updated successfully to {new_version}") + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("Usage: python update_version.py ") + sys.exit(1) + + new_version = sys.argv[1] + file_path = Path(sys.argv[2]).absolute().resolve(strict=True) + main(new_version, file_path) diff --git a/script/update_version.sh b/script/update_version.sh new file mode 100755 index 0000000..eff45f3 --- /dev/null +++ b/script/update_version.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -euxo pipefail +cd "$(dirname "$0")/../" + +version="$1" +npm version "${version}" --git-tag-version false +python3 ./script/update_tauri_version.py "${version}" "./src-tauri/Cargo.toml" diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 63004c6..68cd3b9 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "Cuuri" -version = "0.2.0" +version = "0.2.1" dependencies = [ "chrono", "diesel", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 0ffcc36..81e52a3 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "Cuuri" -version = "0.2.0" +version = "0.2.1" description = "Cuuri is a GUI client for ChatGPT built with Tauri, Vue, and TypeScript. " authors = ["takanotume24 "] edition = "2021" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 4f44046..19d4cdf 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Cuuri", - "version": "0.2.0", + "version": "", "identifier": "com.takanotume24.cuuri", "build": { "beforeDevCommand": "npm run dev", From 3a28e50d3221f3870e57b592f45045b7df523bd8 Mon Sep 17 00:00:00 2001 From: MATSUNAGA Takuya Date: Wed, 4 Dec 2024 14:07:36 +0900 Subject: [PATCH 2/3] I shouldn't have changed the version number manually, because Actions updates the version number. --- package-lock.json | 4 ++-- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5a5860b..9feeb43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cuuri", - "version": "0.2.1", + "version": "0.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "cuuri", - "version": "0.2.1", + "version": "0.2.0", "dependencies": { "@tauri-apps/api": "^2", "@tauri-apps/plugin-shell": "^2", diff --git a/package.json b/package.json index 4b97288..4295aae 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cuuri", "private": true, - "version": "0.2.1", + "version": "0.2.0", "type": "module", "scripts": { "dev": "vite", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 68cd3b9..63004c6 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "Cuuri" -version = "0.2.1" +version = "0.2.0" dependencies = [ "chrono", "diesel", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 81e52a3..0ffcc36 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "Cuuri" -version = "0.2.1" +version = "0.2.0" description = "Cuuri is a GUI client for ChatGPT built with Tauri, Vue, and TypeScript. " authors = ["takanotume24 "] edition = "2021" From 4f846267fb8f46fb9687b36ac13814f4425116e5 Mon Sep 17 00:00:00 2001 From: MATSUNAGA Takuya Date: Wed, 4 Dec 2024 14:16:22 +0900 Subject: [PATCH 3/3] eat: Add script to update Cargo version and modify Tauri config - New script `update_cargo_version.py` added to update the version in Cargo.toml. - Refactored `update_tauri_version.py` to modify the version in `tauri.conf.json` instead of Cargo.toml. - Updated `update_version.sh` to call the new `update_cargo_version.py` and the modified `update_tauri_version.py`. - Set initial version in `tauri.conf.json` to "0.2.0". --- script/update_cargo_version.py | 86 ++++++++++++++++++++++++++++++ script/update_tauri_version.py | 97 ++++++++++++---------------------- script/update_version.sh | 3 +- src-tauri/tauri.conf.json | 2 +- 4 files changed, 124 insertions(+), 64 deletions(-) create mode 100755 script/update_cargo_version.py mode change 100755 => 100644 script/update_tauri_version.py diff --git a/script/update_cargo_version.py b/script/update_cargo_version.py new file mode 100755 index 0000000..0b291e5 --- /dev/null +++ b/script/update_cargo_version.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +import sys +import re +from typing import List +from pathlib import Path + +def read_file(file_path: Path) -> List[str]: + """Reads the file and returns its lines.""" + try: + with open(file_path, 'r', encoding='utf-8') as file: + return file.readlines() + except FileNotFoundError: + print(f"Error: The file {file_path} was not found.") + sys.exit(1) + except IOError: + print(f"Error: Could not read the file {file_path}.") + sys.exit(1) + +def write_file(file_path: Path, lines: List[str]) -> None: + """Writes the given lines to the file.""" + try: + with open(file_path, 'w', encoding='utf-8') as file: + file.writelines(lines) + except IOError: + print(f"Error: Could not write to the file {file_path}.") + sys.exit(1) + +def update_version(lines: List[str], new_version: str) -> List[str]: + """Updates the version in the specified lines and returns updated lines.""" + in_package_section = False + version_updated = False + pattern = re.compile(r'^version\s*=\s*".*"$') + current_version = None + + def process_line(line: str) -> str: + nonlocal in_package_section, version_updated, current_version + + if line.strip() == "[package]": + in_package_section = True + elif line.strip().startswith("[") and in_package_section: + in_package_section = False + + if in_package_section and pattern.search(line): + current_version_match = re.search(r'"([^"]+)"', line) + if current_version_match: + current_version = current_version_match.group(1) + + if current_version == new_version: + print(f"No change in version. The current version is already {new_version}.") + sys.exit(1) + + version_updated = True + return f'version = "{new_version}"\n' + + return line + + updated_lines = list(map(process_line, lines)) + + if not version_updated: + print("Error: No version entry was updated. Please ensure the [package] section exists and contains a version entry.") + sys.exit(1) + + print(f"Updated version from {current_version} to {new_version}") + return updated_lines + +def main(new_version: str, file_path: Path) -> None: + """Main function to update the version in the given file.""" + lines = read_file(file_path) + updated_lines = update_version(lines, new_version) + write_file(file_path, updated_lines) + +if __name__ == '__main__': + if len(sys.argv) != 3: + print("Usage: python update_version.py ") + sys.exit(1) + + new_version = sys.argv[1] + file_path = Path(sys.argv[2]).absolute() + + # Ensure the file exists + if not file_path.exists(): + print(f"Error: The file {file_path} does not exist.") + sys.exit(1) + + main(new_version, file_path) diff --git a/script/update_tauri_version.py b/script/update_tauri_version.py old mode 100755 new mode 100644 index a52b45f..18ed6a9 --- a/script/update_tauri_version.py +++ b/script/update_tauri_version.py @@ -1,67 +1,40 @@ -#!/usr/bin/env python3 - +import json +import argparse import sys -import re -from typing import List from pathlib import Path -def read_file(file_path: Path) -> List[str]: - try: - with open(file_path, 'r', encoding='utf-8') as file: - return file.readlines() - except FileNotFoundError: - print(f"Error: The file {file_path} was not found.") - sys.exit(1) - except IOError: - print(f"Error: Could not read the file {file_path}.") - sys.exit(1) - -def write_file(file_path: Path, lines: List[str]) -> None: - try: - with open(file_path, 'w', encoding='utf-8') as file: - file.writelines(lines) - except IOError: - print(f"Error: Could not write to the file {file_path}.") - sys.exit(1) - -def update_version(lines: List[str], new_version: str) -> List[str]: - in_package_section = False - version_updated = False - pattern = re.compile(r'^version\s*=\s*".*"$') - - def process_line(line: str) -> str: - nonlocal in_package_section, version_updated - - if line.strip() == "[package]": - in_package_section = True - elif line.strip().startswith("[") and in_package_section: - in_package_section = False - - if in_package_section and pattern.search(line): - version_updated = True - return f'version = "{new_version}"\n' - - return line - - updated_lines = list(map(process_line, lines)) - - if not version_updated: - print("Error: No version entry was updated. Please ensure the [package] section exists and contains a version entry.") - sys.exit(1) - - return updated_lines - -def main(new_version: str, file_path: Path) -> None: - lines = read_file(file_path) - updated_lines = update_version(lines, new_version) - write_file(file_path, updated_lines) - print(f"Version updated successfully to {new_version}") - -if __name__ == '__main__': - if len(sys.argv) != 3: - print("Usage: python update_version.py ") +def update_version_in_config(file_path: Path, new_version: str) -> None: + # Load the existing JSON configuration + with open(file_path, 'r', encoding='utf-8') as file: + config = json.load(file) + + # Update the version number + current_version = config.get("version", "0.0.0") + + if new_version == current_version: + print(f"No change in version. The current version is already {new_version}.") sys.exit(1) - new_version = sys.argv[1] - file_path = Path(sys.argv[2]).absolute().resolve(strict=True) - main(new_version, file_path) + config["version"] = new_version + + # Write the updated configuration back to the file + with open(file_path, 'w', encoding='utf-8') as file: + json.dump(config, file, indent=2) + + print(f"Updated version from {current_version} to {new_version}") + +def main() -> None: + # Set up argument parser + parser = argparse.ArgumentParser(description="Update version in Tauri configuration file.") + parser.add_argument("new_version", help="New version to set.") + parser.add_argument("file_path", help="Path to the Tauri configuration JSON file.") + + # Parse arguments + args = parser.parse_args() + + file_path = Path(args.file_path).absolute().resolve(strict=True) + # Update version in the configuration file + update_version_in_config(file_path, args.new_version) + +if __name__ == "__main__": + main() diff --git a/script/update_version.sh b/script/update_version.sh index eff45f3..8aa1708 100755 --- a/script/update_version.sh +++ b/script/update_version.sh @@ -4,4 +4,5 @@ cd "$(dirname "$0")/../" version="$1" npm version "${version}" --git-tag-version false -python3 ./script/update_tauri_version.py "${version}" "./src-tauri/Cargo.toml" +python3 ./script/update_tauri_version.py "${version}" "./src-tauri/tauri.conf.json" +python3 ./script/update_cargo_version.py "${version}" "./src-tauri/Cargo.toml" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 19d4cdf..4f44046 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "$schema": "https://schema.tauri.app/config/2", "productName": "Cuuri", - "version": "", + "version": "0.2.0", "identifier": "com.takanotume24.cuuri", "build": { "beforeDevCommand": "npm run dev",