-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.py
131 lines (100 loc) · 4.35 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import sys
import toml
import re
from datetime import datetime
####################
# Modify TOML
####################
# Check if a new version number was provided as a command-line argument
if len(sys.argv) < 2:
print("Usage: python script.py <new_version>")
sys.exit(1)
new_version = sys.argv[1] # Get the new version number from the command-line arguments
# Define the TOML content
toml_content = """
[package]
name = "modernpro-coverletter"
version = "0.0.1"
entrypoint = "modernpro-coverletter.typ"
authors = [ "jxpeng98"]
repository = "https://github.com/jxpeng98/typst-coverletter"
license = "MIT"
description = "A cover letter template with modern Sans font for job applications and other formal letters."
keywords = ["coverletter", "CV", "Job"]
categories = ["cv", "utility"]
[template]
path = "template"
entrypoint = "coverletter.typ"
thumbnail = "thumbnail.png"
"""
# Parse the TOML content
parsed_toml = toml.loads(toml_content)
# Update the version
parsed_toml["package"]["version"] = new_version
# Define a file path to save the updated TOML content
file_path = "typst.toml"
# Write the updated TOML content to a file
with open(file_path, "w") as file:
toml.dump(parsed_toml, file)
print(f"Updated TOML file saved to {file_path}")
####################
# Update .typ file
####################
new_year = datetime.now().year
new_date = datetime.now().strftime("%Y-%m-%d")
# Define the path to your .typ file
typ_file_path = 'modernpro-coverletter.typ'
# Define the content to prepend, ensure no leading newline before the first comment line
new_content_to_prepend = f"""///////////////////////////////
// modernpro-coverletter.typ
// A cover letter template with modern Sans font for job applications and other formal letters.
// Copyright (c) {new_year}
// Author: Jiaxin Peng
// License: MIT
// Version: {new_version}
// Date: {new_date}
// Email: jiaxin.peng@outlook.com
///////////////////////////////
""" # Ensuring only the intended newlines are included
# Read the existing content of the file
with open(typ_file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# Pattern to match the existing header, assuming it's always at the start of the file
header_pattern = re.compile(r'^\/\/+[\s\S]*?\/\/+\n\n', re.MULTILINE)
# Check if the existing header is present and replace it
if header_pattern.search(original_content):
updated_content = header_pattern.sub(new_content_to_prepend, original_content, count=1)
else:
# If no header is found, prepend new header without adding an extra newline at the beginning
updated_content = new_content_to_prepend.rstrip('\n') + original_content
# Write the updated content back to the file
with open(typ_file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print("Template File has been updated with the new version and date.")
# update the main.typ file
def update_main(main_typ_file_path, new_version):
# define the content to prepend
new_content_to_prepend = f'#import "@preview/modernpro-coverletter:{new_version}": *\n\n'# Ensuring only the intended newlines are included
# Read the existing content of the file
with open(main_typ_file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# Pattern to match the existing header, assuming it's always at the start of the file
header_pattern = re.compile(
r'^#import\s+"@preview/modernpro-coverletter:([^"]+)"\s*:\s*\*\s*\n',
re.MULTILINE
)
# Check if the existing header is present and replace it
if header_pattern.search(original_content):
updated_content = header_pattern.sub(new_content_to_prepend, original_content, count=1)
else:
# If no header is found, prepend new header without adding an extra newline at the beginning
updated_content = new_content_to_prepend.rstrip('\n') + original_content
# Write the updated content back to the file
with open(main_typ_file_path, 'w', encoding='utf-8') as file:
file.write(updated_content)
print(f"Main Typ File {main_typ_file_path} has been updated with the new version.")
# define the path to main.typ file
main_typ_file_coverletter_path = 'template/coverletter.typ'
main_typ_file_statement_path = 'template/statement.typ'
update_main(main_typ_file_coverletter_path, new_version)
update_main(main_typ_file_statement_path, new_version)