This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 105
/
propagateSdkVersions.js
172 lines (154 loc) · 5.48 KB
/
propagateSdkVersions.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import fs from "fs";
import path from "path";
import { promisify } from "util";
import { fileURLToPath } from "url";
// Setup
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const sdkVersionsPath = path.resolve(__dirname, "sdk-versions.json");
const directoriesToUpdate = [
__dirname,
path.resolve(__dirname, "./site/testsuites"),
];
// Grab SDK versions from sdk-versions.json
const getSdkVersions = async () => {
try {
const content = await fs.promises.readFile(sdkVersionsPath, "utf8");
return JSON.parse(content);
} catch (error) {
console.error("Error reading sdk-versions.json:", error);
throw error; // Rethrow to handle it in propagateVersions
}
};
// Update package.json dependencies
const updatePackageJsonDependencies = async (dirPath, sdkVersions) => {
const packageJsonPath = path.join(dirPath, "package.json");
if (fs.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(
await fs.promises.readFile(packageJsonPath, "utf8")
);
let updated = false;
for (const sdk in sdkVersions.js) {
if (packageJson.dependencies && packageJson.dependencies[sdk]) {
packageJson.dependencies[sdk] = sdkVersions.js[sdk];
updated = true;
}
if (packageJson.devDependencies && packageJson.devDependencies[sdk]) {
packageJson.devDependencies[sdk] = sdkVersions.js[sdk];
updated = true;
}
}
if (updated) {
await fs.promises.writeFile(
packageJsonPath,
JSON.stringify(packageJson, null, 2)
);
console.log(`Updated ${packageJsonPath}`);
}
} catch (error) {
console.error(`Error updating ${packageJsonPath}:`, error);
}
}
if (fs.lstatSync(dirPath).isDirectory()) {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (const dirent of entries) {
if (dirent.name === "node_modules") continue;
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await updatePackageJsonDependencies(fullPath, sdkVersions);
}
}
}
};
const updatePomXmlVersion = async (filePath, sdkVersions) => {
try {
const pomContent = await fs.promises.readFile(filePath, "utf8");
let updatedPomContent = pomContent;
Object.entries(sdkVersions.jvm).forEach(([artifactId, version]) => {
const versionTagRegex = new RegExp(
`(<version.xyz.block.${artifactId}>)(.*?)(<\/version.xyz.block.${artifactId}>)`,
"g"
);
if (versionTagRegex.test(pomContent)) {
console.log(
`Found matches for ${artifactId}, updating to version ${version}.`
);
} else {
console.log(`No matches found for ${artifactId}.`);
}
updatedPomContent = updatedPomContent.replace(
versionTagRegex,
`$1${version}$3`
);
});
if (updatedPomContent !== pomContent) {
await fs.promises.writeFile(filePath, updatedPomContent);
}
} catch (error) {
console.error(`Failed to update ${filePath}:`, error);
}
};
const findAndUpdatePomFiles = async (dirPath, sdkVersions) => {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (const dirent of entries) {
if (dirent.name === "node_modules") continue;
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await findAndUpdatePomFiles(fullPath, sdkVersions);
} else if (dirent.name === "pom.xml") {
await updatePomXmlVersion(fullPath, sdkVersions);
}
}
};
// Function to update Package.swift dependencies
const updatePackageSwiftDependencies = async (dirPath, sdkVersions) => {
const packageSwiftPath = path.join(dirPath, "Package.swift");
try {
const packageSwiftContent = await fs.promises.readFile(
packageSwiftPath,
"utf8"
);
let updatedPackageSwiftContent = packageSwiftContent;
Object.keys(sdkVersions.swift).forEach((dependency) => {
const { url, branch } = sdkVersions.swift[dependency];
const regex = new RegExp(
`\\.package\\(url: "${url}", branch: ".*?"\\)`,
"g"
);
updatedPackageSwiftContent = updatedPackageSwiftContent.replace(
regex,
`.package(url: "${url}", branch: "${branch}")`
);
});
await fs.promises.writeFile(packageSwiftPath, updatedPackageSwiftContent);
} catch (error) {
console.error(`Failed to update Package.swift in ${dirPath}:`, error);
}
};
// Function to recursively find and update Package.swift files
const findAndUpdatePackageSwift = async (dirPath, sdkVersions) => {
const entries = await fs.promises.readdir(dirPath, { withFileTypes: true });
for (const dirent of entries) {
if (dirent.name === "node_modules") continue;
const fullPath = path.join(dirPath, dirent.name);
if (dirent.isDirectory()) {
await findAndUpdatePackageSwift(fullPath, sdkVersions);
} else if (dirent.name === "Package.swift") {
await updatePackageSwiftDependencies(dirPath, sdkVersions);
}
}
};
// Main function to initiate the version propagation process
const propagateVersions = async () => {
try {
const sdkVersions = await getSdkVersions();
for (const dirPath of directoriesToUpdate) {
await updatePackageJsonDependencies(dirPath, sdkVersions);
await findAndUpdatePomFiles(dirPath, sdkVersions);
await findAndUpdatePackageSwift(dirPath, sdkVersions);
}
} catch (error) {
console.error("Failed to propagate versions:", error);
}
};
propagateVersions();