|
| 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