Skip to content

Commit

Permalink
eat: Add script to update Cargo version and modify Tauri config
Browse files Browse the repository at this point in the history
- 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".
  • Loading branch information
takanotume24 committed Dec 4, 2024
1 parent 3a28e50 commit 4f84626
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 64 deletions.
86 changes: 86 additions & 0 deletions script/update_cargo_version.py
Original file line number Diff line number Diff line change
@@ -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 <new_version> <file_path>")
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)
97 changes: 35 additions & 62 deletions script/update_tauri_version.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -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 <new_version> <file_path>")
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()
3 changes: 2 additions & 1 deletion script/update_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 4f84626

Please sign in to comment.