-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathupdate_node_version.py
More file actions
executable file
·132 lines (102 loc) · 3.73 KB
/
Copy pathupdate_node_version.py
File metadata and controls
executable file
·132 lines (102 loc) · 3.73 KB
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
#!/usr/bin/env python3
"""
Script to update Node.js version in node-version.h based on GitHub releases.
This script:
1. Fetches Node.js version tags from GitHub
2. Finds the maximum even major version
3. Looks for the latest version in the (max-2) series
4. Updates the node-version.h file if needed
"""
import json
import re
import sys
import urllib.request
from pathlib import Path
def fetch_nodejs_tags():
"""Fetch Node.js version tags from GitHub API."""
url = "https://api.github.com/repos/nodejs/node/tags"
headers = {
"User-Agent": "workerd-node-version-updater",
"Accept": "application/vnd.github.v3+json",
}
all_tags = []
page = 1
while True:
req = urllib.request.Request(f"{url}?page={page}&per_page=100", headers=headers)
try:
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode())
if not data:
break
all_tags.extend(data)
page += 1
except Exception as e:
print(f"Error fetching tags: {e}", file=sys.stderr)
sys.exit(1)
return all_tags
def parse_version(version_str):
"""Parse a version string like 'v22.17.0' into (major, minor, patch)."""
match = re.match(r"^v(\d+)\.(\d+)\.(\d+)$", version_str)
if match:
return tuple(map(int, match.groups()))
return None
def find_target_version(tags):
"""Find the target Node.js version based on the algorithm."""
# Extract valid versions
versions = []
for tag in tags:
version = parse_version(tag["name"])
if version:
versions.append((version, tag["name"]))
if not versions:
print("No valid versions found", file=sys.stderr)
sys.exit(1)
# Sort versions
versions.sort(reverse=True)
# Find maximum even major version
max_even_major = None
for (major, _minor, _patch), _ in versions:
if major % 2 == 0:
max_even_major = major
break
if max_even_major is None:
print("No even major version found", file=sys.stderr)
sys.exit(1)
# Calculate target major version (max_even - 2)
target_major = max_even_major - 2
# Find latest version in target major series
for (major, _minor, _patch), tag_name in versions:
if major == target_major:
return tag_name[1:] # Remove 'v' prefix
print(f"No version found for major version {target_major}", file=sys.stderr)
sys.exit(1)
def update_header_file(file_path, new_version):
"""Update the node-version.h file with the new version."""
path = Path(file_path)
content = path.read_text()
# Replace the version string
# Match: static constexpr kj::StringPtr nodeVersion = "X.Y.Z"_kj;
pattern = r'static constexpr kj::StringPtr nodeVersion = "[^"]+"_kj;'
replacement = f'static constexpr kj::StringPtr nodeVersion = "{new_version}"_kj;'
new_content = re.sub(pattern, replacement, content)
if new_content != content:
path.write_text(new_content)
return True
return False
def main():
if len(sys.argv) != 2:
print("Usage: update_node_version.py <output_file>", file=sys.stderr)
sys.exit(1)
output_file = sys.argv[1]
print("Fetching Node.js versions from GitHub...")
tags = fetch_nodejs_tags()
print("Finding target version...")
target_version = find_target_version(tags)
print(f"Target version: {target_version}")
# Update the header file
if update_header_file(output_file, target_version):
print(f"Updated {output_file} with version {target_version}")
else:
print(f"No changes needed - already at version {target_version}")
if __name__ == "__main__":
main()