-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscrapeVersions.ts
More file actions
141 lines (120 loc) · 4.47 KB
/
Copy pathscrapeVersions.ts
File metadata and controls
141 lines (120 loc) · 4.47 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
133
134
135
136
137
138
139
140
141
import { EditorVersionInfo } from '../../model/editorVersionInfo';
import { searchChangesets, SearchMode } from 'unity-changeset';
import fetch from 'node-fetch';
const unity_version_regex = /^(\d+)\.(\d+)\.(\d+)([a-zA-Z]+)(-?\d+)$/;
const unity_whats_new_url = 'https://unity.com/releases/editor/whats-new';
type UnityChangesetVersion = {
version: string;
changeset: string;
};
const toEditorVersionInfo = (unityVersion: UnityChangesetVersion): EditorVersionInfo | null => {
const match = RegExp(unity_version_regex).exec(unityVersion.version);
if (!match) {
return null;
}
const [_, major, minor, patch, lifecycle] = match;
if (lifecycle !== 'f' || Number(major) < 2017) {
return null;
}
return {
version: unityVersion.version,
changeSet: unityVersion.changeset,
major: Number(major),
minor: Number(minor),
patch,
} as EditorVersionInfo;
};
export const scrapeRecentOfficialUnityVersions = async (): Promise<EditorVersionInfo[]> => {
const response = await fetch(unity_whats_new_url, {
redirect: 'follow',
headers: {
'User-Agent': 'game-ci-versioning-backend/1.0',
},
});
if (!response.ok) {
throw new Error(`Unity release page returned ${response.status}`);
}
const html = await response.text();
const versions: UnityChangesetVersion[] = [];
const processedVersions = new Set<string>();
const versionRegex = /(\d+\.\d+\.\d+f\d+)/g;
let match;
while ((match = versionRegex.exec(html)) !== null) {
const version = match[1];
if (processedVersions.has(version)) continue;
processedVersions.add(version);
const escapedVersion = version.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let changeset: string | undefined;
const changesetMatch =
new RegExp(`unityhub://${escapedVersion}/([a-f0-9]{12})`, 'i').exec(html) ||
new RegExp(`${escapedVersion}[^a-f0-9]*([a-f0-9]{12})`, 'i').exec(html);
if (changesetMatch?.[1]) {
changeset = changesetMatch[1];
} else {
// As a last resort, search for changeset within a context window near the version
// Extract ~500 chars around the version match for local context search
const versionIndex = html.indexOf(version);
if (versionIndex !== -1) {
const contextStart = Math.max(0, versionIndex - 200);
const contextEnd = Math.min(html.length, versionIndex + 300);
const context = html.substring(contextStart, contextEnd);
const contextChangesetMatch = /[Cc]hangeset:\s*([a-f0-9]{12})/i.exec(context);
if (contextChangesetMatch?.[1]) {
changeset = contextChangesetMatch[1];
}
}
}
if (changeset) {
versions.push({
version,
changeset,
});
}
}
return versions
.map(toEditorVersionInfo)
.filter((versionInfo): versionInfo is EditorVersionInfo => versionInfo !== null);
};
export const scrapeLatestOfficialUnityVersion = async (): Promise<EditorVersionInfo | null> => {
const recentVersions = await scrapeRecentOfficialUnityVersions();
return recentVersions.length > 0 ? recentVersions[0] : null;
};
export const scrapeVersions = async (): Promise<EditorVersionInfo[]> => {
const unityVersions: UnityChangesetVersion[] = (await searchChangesets(SearchMode.Default)).map(
({ version, changeset }) => ({
version,
changeset,
}),
);
const unityXltsVersions: UnityChangesetVersion[] = (await searchChangesets(SearchMode.XLTS)).map(
({ version, changeset }) => ({
version,
changeset,
}),
);
const recentOfficialVersions = await scrapeRecentOfficialUnityVersions();
// Merge XLTS versions into main list, avoiding duplicates
const existingVersions = new Set(unityVersions.map((v) => v.version));
for (const xltsVersion of unityXltsVersions) {
if (!existingVersions.has(xltsVersion.version)) {
unityVersions.push(xltsVersion);
existingVersions.add(xltsVersion.version);
}
}
// Merge recent official versions discovered from Unity releases page
for (const officialVersion of recentOfficialVersions) {
if (!existingVersions.has(officialVersion.version)) {
unityVersions.push({
version: officialVersion.version,
changeset: officialVersion.changeSet,
});
existingVersions.add(officialVersion.version);
}
}
if (unityVersions?.length > 0) {
return unityVersions
.map(toEditorVersionInfo)
.filter((versionInfo): versionInfo is EditorVersionInfo => versionInfo !== null);
}
throw new Error('No Unity versions found!');
};