Skip to content

Commit 50fff58

Browse files
chore: allow running scripts manually
1 parent 3145571 commit 50fff58

File tree

3 files changed

+162
-92
lines changed

3 files changed

+162
-92
lines changed

.github/workflows/update-google-java-format.yml

Lines changed: 10 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -50,114 +50,32 @@ jobs:
5050
max_attempts: 3
5151
command: yarn
5252

53-
- name: Check latest upstream version
54-
id: check
55-
shell: bash
56-
run: |
57-
set -euo pipefail
58-
59-
current_jar="$(ls lib/google-java-format-*-all-deps.jar)"
60-
current_version="${current_jar##*/google-java-format-}"
61-
current_version="${current_version%-all-deps.jar}"
62-
63-
release_json="$(curl -fsSL -H "Accept: application/vnd.github+json" "https://api.github.com/repos/google/google-java-format/releases/latest")"
64-
65-
latest_version="$(
66-
printf '%s' "$release_json" | node -e '
67-
const fs = require("fs");
68-
const release = JSON.parse(fs.readFileSync(0, "utf8"));
69-
const asset = (release.assets || []).find((entry) =>
70-
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar$/.test(entry.name)
71-
);
72-
if (!asset) {
73-
throw new Error("Unable to find all-deps jar asset in latest release");
74-
}
75-
process.stdout.write(asset.name.replace(/^google-java-format-/, "").replace(/-all-deps\.jar$/, ""));
76-
'
77-
)"
78-
download_url="$(
79-
printf '%s' "$release_json" | node -e '
80-
const fs = require("fs");
81-
const release = JSON.parse(fs.readFileSync(0, "utf8"));
82-
const asset = (release.assets || []).find((entry) =>
83-
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar$/.test(entry.name)
84-
);
85-
if (!asset) {
86-
throw new Error("Unable to find all-deps jar asset in latest release");
87-
}
88-
process.stdout.write(asset.browser_download_url);
89-
'
90-
)"
91-
92-
echo "current_version=$current_version" >> "$GITHUB_OUTPUT"
93-
echo "latest_version=$latest_version" >> "$GITHUB_OUTPUT"
94-
echo "download_url=$download_url" >> "$GITHUB_OUTPUT"
95-
96-
if [[ "$current_version" == "$latest_version" ]]; then
97-
echo "update_available=false" >> "$GITHUB_OUTPUT"
98-
exit 0
99-
fi
100-
101-
echo "update_available=true" >> "$GITHUB_OUTPUT"
102-
103-
- name: Download new formatter jar
104-
if: steps.check.outputs.update_available == 'true'
105-
shell: bash
106-
run: |
107-
set -euo pipefail
108-
109-
rm -f lib/google-java-format-*-all-deps.jar
110-
curl -fsSL \
111-
"${{ steps.check.outputs.download_url }}" \
112-
-o "lib/google-java-format-${{ steps.check.outputs.latest_version }}-all-deps.jar"
113-
114-
- name: Update hardcoded jar path
115-
if: steps.check.outputs.update_available == 'true'
116-
shell: bash
117-
run: |
118-
set -euo pipefail
119-
120-
node - <<'EOF'
121-
const fs = require("fs");
122-
const indexPath = "index.js";
123-
const latestVersion = process.env.LATEST_VERSION;
124-
const oldSource = fs.readFileSync(indexPath, "utf8");
125-
const newSource = oldSource.replace(
126-
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar/g,
127-
`google-java-format-${latestVersion}-all-deps.jar`
128-
);
129-
130-
if (oldSource === newSource) {
131-
throw new Error("Failed to update google-java-format jar path in index.js");
132-
}
133-
134-
fs.writeFileSync(indexPath, newSource);
135-
EOF
136-
env:
137-
LATEST_VERSION: ${{ steps.check.outputs.latest_version }}
53+
- name: Update formatter files
54+
id: update
55+
run: node ./scripts/update-google-java-format.js
13856

13957
- name: Run tests
140-
if: steps.check.outputs.update_available == 'true'
58+
if: steps.update.outputs.updated == 'true'
14159
run: ./test.sh
14260

14361
- uses: actions/cache/save@v5
14462
name: Yarn Cache Save
145-
if: ${{ steps.check.outputs.update_available == 'true' && github.ref == 'refs/heads/main' }}
63+
if: ${{ steps.update.outputs.updated == 'true' && github.ref == 'refs/heads/main' }}
14664
with:
14765
path: .yarn/cache
14866
key: ${{ runner.os }}-yarn-v1-${{ hashFiles('yarn.lock') }}
14967

15068
- name: Create pull request
151-
if: steps.check.outputs.update_available == 'true'
69+
if: steps.update.outputs.updated == 'true'
15270
uses: peter-evans/create-pull-request@v7
15371
with:
15472
token: ${{ github.token }}
155-
commit-message: "feat: adopt upstream google-java-format [${{ steps.check.outputs.latest_version }}]"
156-
branch: ci/google-java-format-update-${{ steps.check.outputs.latest_version }}
73+
commit-message: "feat: adopt upstream google-java-format [${{ steps.update.outputs.latest_version }}]"
74+
branch: ci/google-java-format-update-${{ steps.update.outputs.latest_version }}
15775
delete-branch: true
158-
title: "feat: adopt upstream google-java-format [${{ steps.check.outputs.latest_version }}]"
76+
title: "feat: adopt upstream google-java-format [${{ steps.update.outputs.latest_version }}]"
15977
body: |
160-
Updates bundled google-java-format from `${{ steps.check.outputs.current_version }}` to `${{ steps.check.outputs.latest_version }}`.
78+
Updates bundled google-java-format from `${{ steps.update.outputs.current_version }}` to `${{ steps.update.outputs.latest_version }}`.
16179
16280
- Replaces the jar in `lib/`
16381
- Updates the hardcoded jar path in `index.js`

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"scripts": {
1919
"test": "bash ./test.sh",
20+
"update-google-java-format": "node ./scripts/update-google-java-format.js",
2021
"shipit": "release-it"
2122
},
2223
"contributors": [
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env node
2+
"use strict";
3+
4+
const fs = require("fs");
5+
const path = require("path");
6+
7+
const REPO_ROOT = path.resolve(__dirname, "..");
8+
const LIB_DIR = path.join(REPO_ROOT, "lib");
9+
const INDEX_PATH = path.join(REPO_ROOT, "index.js");
10+
const RELEASE_URL =
11+
"https://api.github.com/repos/google/google-java-format/releases/latest";
12+
const JAR_PATTERN = /^google-java-format-(\d+\.\d+\.\d+)-all-deps\.jar$/;
13+
14+
function setOutput(name, value) {
15+
if (!process.env.GITHUB_OUTPUT) {
16+
return;
17+
}
18+
19+
fs.appendFileSync(process.env.GITHUB_OUTPUT, `${name}=${String(value)}\n`);
20+
}
21+
22+
function getCurrentJar() {
23+
const jarFiles = fs.readdirSync(LIB_DIR).filter((fileName) => {
24+
return JAR_PATTERN.test(fileName);
25+
});
26+
27+
if (jarFiles.length !== 1) {
28+
throw new Error(
29+
`Expected exactly one google-java-format jar in lib/, found ${jarFiles.length}.`
30+
);
31+
}
32+
33+
const jarName = jarFiles[0];
34+
const match = jarName.match(JAR_PATTERN);
35+
36+
return {
37+
name: jarName,
38+
version: match[1],
39+
path: path.join(LIB_DIR, jarName),
40+
};
41+
}
42+
43+
async function fetchLatestRelease() {
44+
const response = await fetch(RELEASE_URL, {
45+
headers: {
46+
Accept: "application/vnd.github+json",
47+
"User-Agent": "nodejs-google-java-format-updater",
48+
},
49+
});
50+
51+
if (!response.ok) {
52+
throw new Error(
53+
`Failed to fetch latest release: ${response.status} ${response.statusText}`
54+
);
55+
}
56+
57+
return response.json();
58+
}
59+
60+
function getLatestJarAsset(release) {
61+
const asset = (release.assets || []).find((entry) => {
62+
return JAR_PATTERN.test(entry.name);
63+
});
64+
65+
if (!asset) {
66+
throw new Error("Unable to find google-java-format all-deps jar asset.");
67+
}
68+
69+
const match = asset.name.match(JAR_PATTERN);
70+
71+
return {
72+
name: asset.name,
73+
version: match[1],
74+
downloadUrl: asset.browser_download_url,
75+
};
76+
}
77+
78+
async function downloadJar(downloadUrl, destinationPath) {
79+
const response = await fetch(downloadUrl, {
80+
headers: {
81+
"User-Agent": "nodejs-google-java-format-updater",
82+
},
83+
});
84+
85+
if (!response.ok) {
86+
throw new Error(
87+
`Failed to download jar: ${response.status} ${response.statusText}`
88+
);
89+
}
90+
91+
const arrayBuffer = await response.arrayBuffer();
92+
fs.writeFileSync(destinationPath, Buffer.from(arrayBuffer));
93+
}
94+
95+
function updateIndexJarPath(nextVersion) {
96+
const source = fs.readFileSync(INDEX_PATH, "utf8");
97+
const updatedSource = source.replace(
98+
/google-java-format-\d+\.\d+\.\d+-all-deps\.jar/g,
99+
`google-java-format-${nextVersion}-all-deps.jar`
100+
);
101+
102+
if (source === updatedSource) {
103+
throw new Error("Failed to update google-java-format jar path in index.js.");
104+
}
105+
106+
fs.writeFileSync(INDEX_PATH, updatedSource);
107+
}
108+
109+
async function main() {
110+
const currentJar = getCurrentJar();
111+
const latestRelease = await fetchLatestRelease();
112+
const latestJar = getLatestJarAsset(latestRelease);
113+
114+
setOutput("current_version", currentJar.version);
115+
setOutput("latest_version", latestJar.version);
116+
setOutput("download_url", latestJar.downloadUrl);
117+
setOutput(
118+
"update_available",
119+
currentJar.version !== latestJar.version ? "true" : "false"
120+
);
121+
122+
if (currentJar.version === latestJar.version) {
123+
setOutput("updated", "false");
124+
console.log(
125+
`google-java-format is already up to date at ${currentJar.version}.`
126+
);
127+
return;
128+
}
129+
130+
const targetJarPath = path.join(LIB_DIR, latestJar.name);
131+
const tempJarPath = `${targetJarPath}.download`;
132+
133+
try {
134+
await downloadJar(latestJar.downloadUrl, tempJarPath);
135+
fs.rmSync(currentJar.path, { force: true });
136+
fs.renameSync(tempJarPath, targetJarPath);
137+
updateIndexJarPath(latestJar.version);
138+
} finally {
139+
fs.rmSync(tempJarPath, { force: true });
140+
}
141+
142+
setOutput("updated", "true");
143+
console.log(
144+
`Updated google-java-format from ${currentJar.version} to ${latestJar.version}.`
145+
);
146+
}
147+
148+
main().catch((error) => {
149+
console.error(error);
150+
process.exit(1);
151+
});

0 commit comments

Comments
 (0)