-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupdate-google-java-format.js
More file actions
151 lines (123 loc) · 3.82 KB
/
update-google-java-format.js
File metadata and controls
151 lines (123 loc) · 3.82 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
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
"use strict";
const fs = require("fs");
const path = require("path");
const REPO_ROOT = path.resolve(__dirname, "..");
const LIB_DIR = path.join(REPO_ROOT, "lib");
const INDEX_PATH = path.join(REPO_ROOT, "index.js");
const RELEASE_URL =
"https://api.github.com/repos/google/google-java-format/releases/latest";
const JAR_PATTERN = /^google-java-format-(\d+\.\d+\.\d+)-all-deps\.jar$/;
function setOutput(name, value) {
if (!process.env.GITHUB_OUTPUT) {
return;
}
fs.appendFileSync(process.env.GITHUB_OUTPUT, `${name}=${String(value)}\n`);
}
function getCurrentJar() {
const jarFiles = fs.readdirSync(LIB_DIR).filter((fileName) => {
return JAR_PATTERN.test(fileName);
});
if (jarFiles.length !== 1) {
throw new Error(
`Expected exactly one google-java-format jar in lib/, found ${jarFiles.length}.`
);
}
const jarName = jarFiles[0];
const match = jarName.match(JAR_PATTERN);
return {
name: jarName,
version: match[1],
path: path.join(LIB_DIR, jarName),
};
}
async function fetchLatestRelease() {
const response = await fetch(RELEASE_URL, {
headers: {
Accept: "application/vnd.github+json",
"User-Agent": "nodejs-google-java-format-updater",
},
});
if (!response.ok) {
throw new Error(
`Failed to fetch latest release: ${response.status} ${response.statusText}`
);
}
return response.json();
}
function getLatestJarAsset(release) {
const asset = (release.assets || []).find((entry) => {
return JAR_PATTERN.test(entry.name);
});
if (!asset) {
throw new Error("Unable to find google-java-format all-deps jar asset.");
}
const match = asset.name.match(JAR_PATTERN);
return {
name: asset.name,
version: match[1],
downloadUrl: asset.browser_download_url,
};
}
async function downloadJar(downloadUrl, destinationPath) {
const response = await fetch(downloadUrl, {
headers: {
"User-Agent": "nodejs-google-java-format-updater",
},
});
if (!response.ok) {
throw new Error(
`Failed to download jar: ${response.status} ${response.statusText}`
);
}
const arrayBuffer = await response.arrayBuffer();
fs.writeFileSync(destinationPath, Buffer.from(arrayBuffer));
}
function updateIndexJarPath(nextVersion) {
const source = fs.readFileSync(INDEX_PATH, "utf8");
const updatedSource = source.replace(
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar/g,
`google-java-format-${nextVersion}-all-deps.jar`
);
if (source === updatedSource) {
throw new Error("Failed to update google-java-format jar path in index.js.");
}
fs.writeFileSync(INDEX_PATH, updatedSource);
}
async function main() {
const currentJar = getCurrentJar();
const latestRelease = await fetchLatestRelease();
const latestJar = getLatestJarAsset(latestRelease);
setOutput("current_version", currentJar.version);
setOutput("latest_version", latestJar.version);
setOutput("download_url", latestJar.downloadUrl);
setOutput(
"update_available",
currentJar.version !== latestJar.version ? "true" : "false"
);
if (currentJar.version === latestJar.version) {
setOutput("updated", "false");
console.log(
`google-java-format is already up to date at ${currentJar.version}.`
);
return;
}
const targetJarPath = path.join(LIB_DIR, latestJar.name);
const tempJarPath = `${targetJarPath}.download`;
try {
await downloadJar(latestJar.downloadUrl, tempJarPath);
fs.rmSync(currentJar.path, { force: true });
fs.renameSync(tempJarPath, targetJarPath);
updateIndexJarPath(latestJar.version);
} finally {
fs.rmSync(tempJarPath, { force: true });
}
setOutput("updated", "true");
console.log(
`Updated google-java-format from ${currentJar.version} to ${latestJar.version}.`
);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});