-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprelease.py
135 lines (96 loc) · 4.66 KB
/
preprelease.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
129
130
131
132
133
134
135
import os
import json
import shutil
import zipfile
import subprocess
from urllib.parse import urlparse
def zip_files(source_folder, output_zip_path, files_to_zip):
with zipfile.ZipFile(output_zip_path, 'w') as zip_file:
for file_to_zip in files_to_zip:
file_path = os.path.join(source_folder, file_to_zip)
zip_file.write(file_path, arcname=file_to_zip)
def get_current_version_from_link(content):
link_start = content.find('https://github.com/saertna/obsidian-gamified-pkm/releases/tag/')
if link_start == -1:
return None
link_end = content.find(')', link_start)
if link_end == -1:
link_end = content.find(']', link_start)
if link_end == -1:
link_end = len(content)
link = content[link_start:link_end]
parsed_url = urlparse(link)
path_parts = parsed_url.path.split('/')
current_version = path_parts[-1]
return current_version
def update_readme_version(readme_path, new_version):
with open(readme_path, 'r', encoding='utf-8') as readme_file:
readme_content = readme_file.read()
old_version = get_current_version_from_link(readme_content)
if old_version is None:
print("Unable to extract the current version from the README.md file.")
return
old_version_link = f'https://github.com/saertna/obsidian-gamified-pkm/releases/tag/{old_version}'
new_version_link = f'https://github.com/saertna/obsidian-gamified-pkm/releases/tag/{new_version}'
updated_readme_content = readme_content.replace(old_version_link, new_version_link)
with open(readme_path, 'w', encoding='utf-8') as readme_file:
readme_file.write(updated_readme_content)
def update_json_version(file_path, new_version):
with open(file_path, 'r') as json_file:
data = json.load(json_file)
data['version'] = new_version
with open(file_path, 'w') as json_file:
json.dump(data, json_file, indent=2)
def update_constants_false(constants_path):
with open(constants_path, 'r', encoding='utf-8') as constants_file:
constants_content = constants_file.read()
updated_constants_content = constants_content.replace('export const debugLogs = true;', 'export const debugLogs = false;')
with open(constants_path, 'w', encoding='utf-8') as constants_file:
constants_file.write(updated_constants_content)
def update_constants(constants_path, new_version):
with open(constants_path, 'r', encoding='utf-8') as constants_file:
constants_lines = constants_file.readlines()
# Find the line containing the version string
for i, line in enumerate(constants_lines):
if 'export const PLUGIN_VERSION' in line:
old_version = line.split('=')[1].strip().strip(';').strip("'")
constants_lines[i] = line.replace(old_version, new_version)
# Write the updated content back to the file
with open(constants_path, 'w', encoding='utf-8') as constants_file:
constants_file.writelines(constants_lines)
def main(new_version):
plugin_folder = '.'
readme_path = os.path.join(plugin_folder, 'README.md')
manifest_path = os.path.join(plugin_folder, 'manifest.json')
package_path = os.path.join(plugin_folder, 'package.json')
constants_path = os.path.join(plugin_folder, 'src', 'constants.ts')
# Update README.md
update_readme_version(readme_path, new_version)
# Update manifest.json
update_json_version(manifest_path, new_version)
# Update package.json
update_json_version(package_path, new_version)
# Update constants.ts
print(constants_path)
update_constants(constants_path, new_version)
update_constants_false(constants_path)
# Run npm build commands
try:
subprocess.run(["npm", "install"], check=True, cwd=plugin_folder, shell=True)
subprocess.run(["npm", "run", "build"], check=True, cwd=plugin_folder, shell=True)
except subprocess.CalledProcessError as e:
print(f"Error running npm commands: {e}")
# Zip files
# files_to_zip = ['main.js', 'manifest.json', 'styles.css']
# zip_folder = os.path.join(plugin_folder, f'obsidian-gamified-pkm.zip')
# zip_files(plugin_folder, zip_folder, files_to_zip)
# Move the zip archive to the 'install-zip' subfolder
# install_zip_folder = os.path.join(plugin_folder, 'obsidian-gamified-pkm')
# os.makedirs(install_zip_folder, exist_ok=True)
# Create the new path for the zip archive in the 'install-zip' folder
# new_zip_path = os.path.join(install_zip_folder, f'obsidian-gamified-pkm.zip')
# Move the zip archive
# shutil.move(zip_folder, new_zip_path)
if __name__ == "__main__":
new_version = input("Enter the new version: ")
main(new_version)