|
| 1 | +/* |
| 2 | + * verapdf.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2020-2022 Posit Software, PBC |
| 5 | + */ |
| 6 | + |
| 7 | +import { existsSync, safeRemoveSync } from "../../deno_ral/fs.ts"; |
| 8 | +import { basename, join } from "../../deno_ral/path.ts"; |
| 9 | + |
| 10 | +import { unzip } from "../../core/zip.ts"; |
| 11 | +import { execProcess } from "../../core/process.ts"; |
| 12 | +import { quartoDataDir } from "../../core/appdirs.ts"; |
| 13 | + |
| 14 | +import { |
| 15 | + InstallableTool, |
| 16 | + InstallContext, |
| 17 | + kUpdatePath, |
| 18 | + PackageInfo, |
| 19 | + RemotePackageInfo, |
| 20 | +} from "../types.ts"; |
| 21 | +import { createToolSymlink, removeToolSymlink } from "../tools.ts"; |
| 22 | +import { isWindows } from "../../deno_ral/platform.ts"; |
| 23 | + |
| 24 | +// veraPDF version API |
| 25 | +const kVersionApiUrl = "https://software.verapdf.org/get-latest-version.php"; |
| 26 | + |
| 27 | +// Supported Java versions for veraPDF |
| 28 | +const kSupportedJavaVersions = [8, 11, 17, 21]; |
| 29 | + |
| 30 | +// The name of the file that we use to store the installed version |
| 31 | +const kVersionFileName = "version"; |
| 32 | + |
| 33 | +export const verapdfInstallable: InstallableTool = { |
| 34 | + name: "VeraPDF", |
| 35 | + prereqs: [{ |
| 36 | + check: async () => { |
| 37 | + const javaVersion = await getJavaVersion(); |
| 38 | + return javaVersion !== undefined && |
| 39 | + kSupportedJavaVersions.includes(javaVersion); |
| 40 | + }, |
| 41 | + os: ["darwin", "linux", "windows"], |
| 42 | + message: |
| 43 | + `Java is not installed or version is not supported. veraPDF requires Java 8, 11, 17, or 21. |
| 44 | +Please install a compatible Java version from https://adoptium.net/`, |
| 45 | + }], |
| 46 | + installed, |
| 47 | + installDir, |
| 48 | + binDir, |
| 49 | + installedVersion, |
| 50 | + latestRelease, |
| 51 | + preparePackage, |
| 52 | + install, |
| 53 | + afterInstall, |
| 54 | + uninstall, |
| 55 | +}; |
| 56 | + |
| 57 | +async function getJavaVersion(): Promise<number | undefined> { |
| 58 | + try { |
| 59 | + const result = await execProcess({ |
| 60 | + cmd: "java", |
| 61 | + args: ["-version"], |
| 62 | + stderr: "piped", |
| 63 | + }); |
| 64 | + if (!result.success) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + // Java outputs version to stderr |
| 68 | + // Parse: openjdk version "17.0.1" or java version "1.8.0_301" |
| 69 | + const match = result.stderr?.match(/version "(\d+)(?:\.(\d+))?/); |
| 70 | + if (match) { |
| 71 | + const major = parseInt(match[1]); |
| 72 | + // Java 8 reports as "1.8", newer versions report as "11", "17", etc. |
| 73 | + return major === 1 ? parseInt(match[2]) : major; |
| 74 | + } |
| 75 | + } catch { |
| 76 | + return undefined; |
| 77 | + } |
| 78 | + return undefined; |
| 79 | +} |
| 80 | + |
| 81 | +function verapdfInstallDir(): string { |
| 82 | + return quartoDataDir("verapdf"); |
| 83 | +} |
| 84 | + |
| 85 | +async function installed(): Promise<boolean> { |
| 86 | + const dir = verapdfInstallDir(); |
| 87 | + const verapdfBin = isWindows |
| 88 | + ? join(dir, "verapdf.bat") |
| 89 | + : join(dir, "verapdf"); |
| 90 | + return existsSync(verapdfBin); |
| 91 | +} |
| 92 | + |
| 93 | +async function installDir(): Promise<string | undefined> { |
| 94 | + if (await installed()) { |
| 95 | + return verapdfInstallDir(); |
| 96 | + } |
| 97 | + return undefined; |
| 98 | +} |
| 99 | + |
| 100 | +async function binDir(): Promise<string | undefined> { |
| 101 | + if (await installed()) { |
| 102 | + return verapdfInstallDir(); |
| 103 | + } |
| 104 | + return undefined; |
| 105 | +} |
| 106 | + |
| 107 | +async function installedVersion(): Promise<string | undefined> { |
| 108 | + const dir = verapdfInstallDir(); |
| 109 | + const versionFile = join(dir, kVersionFileName); |
| 110 | + if (existsSync(versionFile)) { |
| 111 | + return await Deno.readTextFile(versionFile); |
| 112 | + } |
| 113 | + return undefined; |
| 114 | +} |
| 115 | + |
| 116 | +function noteInstalledVersion(version: string): void { |
| 117 | + const dir = verapdfInstallDir(); |
| 118 | + const versionFile = join(dir, kVersionFileName); |
| 119 | + Deno.writeTextFileSync(versionFile, version); |
| 120 | +} |
| 121 | + |
| 122 | +async function latestRelease(): Promise<RemotePackageInfo> { |
| 123 | + const response = await fetch(kVersionApiUrl); |
| 124 | + if (!response.ok) { |
| 125 | + throw new Error( |
| 126 | + `Failed to fetch veraPDF version info: ${response.statusText}`, |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + const data = await response.json(); |
| 131 | + if (!data.success) { |
| 132 | + throw new Error("Failed to fetch veraPDF version info from API"); |
| 133 | + } |
| 134 | + |
| 135 | + const greenfield = data.releases.greenfield; |
| 136 | + const versionMatch = greenfield.filename.match(/(\d+\.\d+\.\d+)/); |
| 137 | + const version = versionMatch ? versionMatch[1] : "unknown"; |
| 138 | + const downloadUrl = `https://software.verapdf.org${greenfield.path}`; |
| 139 | + |
| 140 | + return { |
| 141 | + url: downloadUrl, |
| 142 | + version, |
| 143 | + assets: [{ name: greenfield.filename, url: downloadUrl }], |
| 144 | + }; |
| 145 | +} |
| 146 | + |
| 147 | +async function preparePackage(context: InstallContext): Promise<PackageInfo> { |
| 148 | + const pkgInfo = await latestRelease(); |
| 149 | + const version = pkgInfo.version; |
| 150 | + const asset = pkgInfo.assets[0]; |
| 151 | + const filePath = join(context.workingDir, asset.name); |
| 152 | + |
| 153 | + await context.download(`VeraPDF ${version}`, asset.url, filePath); |
| 154 | + return { filePath, version }; |
| 155 | +} |
| 156 | + |
| 157 | +async function install( |
| 158 | + pkgInfo: PackageInfo, |
| 159 | + context: InstallContext, |
| 160 | +): Promise<void> { |
| 161 | + const targetDir = verapdfInstallDir(); |
| 162 | + |
| 163 | + // Extract the downloaded ZIP |
| 164 | + await context.withSpinner( |
| 165 | + { message: `Extracting ${basename(pkgInfo.filePath)}` }, |
| 166 | + async () => { |
| 167 | + await unzip(pkgInfo.filePath); |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + // Find the installer JAR in the extracted files |
| 172 | + // The ZIP extracts to a subdirectory like "verapdf-greenfield-1.28.2/" |
| 173 | + const extractedDir = context.workingDir; |
| 174 | + let installerJar: string | undefined; |
| 175 | + |
| 176 | + for await (const entry of Deno.readDir(extractedDir)) { |
| 177 | + if (entry.isDirectory && entry.name.startsWith("verapdf-")) { |
| 178 | + // Look inside the extracted subdirectory for the JAR |
| 179 | + const subDir = join(extractedDir, entry.name); |
| 180 | + for await (const subEntry of Deno.readDir(subDir)) { |
| 181 | + if (subEntry.isFile && subEntry.name.endsWith(".jar")) { |
| 182 | + installerJar = join(subDir, subEntry.name); |
| 183 | + break; |
| 184 | + } |
| 185 | + } |
| 186 | + if (installerJar) break; |
| 187 | + } else if (entry.isFile && entry.name.endsWith(".jar")) { |
| 188 | + // JAR might be at the root level |
| 189 | + installerJar = join(extractedDir, entry.name); |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + if (!installerJar) { |
| 195 | + context.error( |
| 196 | + "Could not find veraPDF installer JAR in the downloaded package", |
| 197 | + ); |
| 198 | + return Promise.reject(); |
| 199 | + } |
| 200 | + |
| 201 | + // Create auto-install.xml for headless installation |
| 202 | + // Panel IDs and pack names must match the IzPack installer configuration |
| 203 | + const autoInstallXml = `<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 204 | +<AutomatedInstallation langpack="eng"> |
| 205 | + <com.izforge.izpack.panels.htmlhello.HTMLHelloPanel id="welcome"/> |
| 206 | + <com.izforge.izpack.panels.target.TargetPanel id="install_dir"> |
| 207 | + <installpath>${targetDir}</installpath> |
| 208 | + </com.izforge.izpack.panels.target.TargetPanel> |
| 209 | + <com.izforge.izpack.panels.packs.PacksPanel id="sdk_pack_select"> |
| 210 | + <pack index="0" name="veraPDF GUI" selected="false"/> |
| 211 | + <pack index="1" name="veraPDF Mac and *nix Scripts" selected="true"/> |
| 212 | + <pack index="2" name="veraPDF Batch files" selected="true"/> |
| 213 | + <pack index="3" name="veraPDF Validation model" selected="true"/> |
| 214 | + <pack index="4" name="veraPDF Documentation" selected="false"/> |
| 215 | + <pack index="5" name="veraPDF Sample Plugins" selected="false"/> |
| 216 | + </com.izforge.izpack.panels.packs.PacksPanel> |
| 217 | + <com.izforge.izpack.panels.install.InstallPanel id="install"/> |
| 218 | + <com.izforge.izpack.panels.finish.FinishPanel id="finish"/> |
| 219 | +</AutomatedInstallation>`; |
| 220 | + |
| 221 | + const autoInstallPath = join(extractedDir, "auto-install.xml"); |
| 222 | + await Deno.writeTextFile(autoInstallPath, autoInstallXml); |
| 223 | + |
| 224 | + // Run the installer in headless mode |
| 225 | + // Pass the auto-install XML path directly to the installer |
| 226 | + await context.withSpinner( |
| 227 | + { message: "Installing veraPDF" }, |
| 228 | + async () => { |
| 229 | + const result = await execProcess({ |
| 230 | + cmd: "java", |
| 231 | + args: ["-jar", installerJar!, autoInstallPath], |
| 232 | + stdout: "piped", |
| 233 | + stderr: "piped", |
| 234 | + }); |
| 235 | + |
| 236 | + if (!result.success) { |
| 237 | + const errorMsg = result.stderr || "Unknown error"; |
| 238 | + throw new Error(`veraPDF installation failed: ${errorMsg}`); |
| 239 | + } |
| 240 | + }, |
| 241 | + ); |
| 242 | + |
| 243 | + // Note the installed version |
| 244 | + noteInstalledVersion(pkgInfo.version); |
| 245 | +} |
| 246 | + |
| 247 | +async function afterInstall(context: InstallContext): Promise<boolean> { |
| 248 | + if (context.flags[kUpdatePath]) { |
| 249 | + const dir = verapdfInstallDir(); |
| 250 | + const verapdfBin = isWindows |
| 251 | + ? join(dir, "verapdf.bat") |
| 252 | + : join(dir, "verapdf"); |
| 253 | + |
| 254 | + await context.withSpinner( |
| 255 | + { message: "Updating PATH" }, |
| 256 | + async () => { |
| 257 | + await createToolSymlink(verapdfBin, "verapdf", context); |
| 258 | + }, |
| 259 | + ); |
| 260 | + |
| 261 | + // On Windows, a restart may be needed |
| 262 | + return isWindows; |
| 263 | + } |
| 264 | + |
| 265 | + return false; |
| 266 | +} |
| 267 | + |
| 268 | +async function uninstall(context: InstallContext): Promise<void> { |
| 269 | + // Remove symlinks if they exist |
| 270 | + if (context.flags[kUpdatePath]) { |
| 271 | + await removeToolSymlink("verapdf"); |
| 272 | + } |
| 273 | + |
| 274 | + await context.withSpinner( |
| 275 | + { message: "Removing veraPDF" }, |
| 276 | + async () => { |
| 277 | + const dir = verapdfInstallDir(); |
| 278 | + if (existsSync(dir)) { |
| 279 | + safeRemoveSync(dir, { recursive: true }); |
| 280 | + } |
| 281 | + }, |
| 282 | + ); |
| 283 | +} |
0 commit comments