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",