Skip to content

Commit 2d3e7ae

Browse files
claude: Add quarto install verapdf command
Adds veraPDF as an installable tool for PDF/A and PDF/UA validation. Features: - Java version check (requires 8, 11, 17, or 21) - Downloads from veraPDF version API - Headless IzPack installer with auto-install XML - --update-path support with shared symlink utilities - Full install/uninstall/update support Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 95a741c commit 2d3e7ae

2 files changed

Lines changed: 362 additions & 1 deletion

File tree

‎src/tools/impl/verapdf.ts‎

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
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+
}

‎src/tools/tools.ts‎

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ import {
1818
} from "./types.ts";
1919
import { tinyTexInstallable } from "./impl/tinytex.ts";
2020
import { chromiumInstallable } from "./impl/chromium.ts";
21+
import { verapdfInstallable } from "./impl/verapdf.ts";
2122
import { downloadWithProgress } from "../core/download.ts";
2223
import { Confirm } from "cliffy/prompt/mod.ts";
2324
import { isWSL } from "../core/platform.ts";
24-
import { safeRemoveSync } from "../deno_ral/fs.ts";
25+
import { ensureDirSync, existsSync, safeRemoveSync } from "../deno_ral/fs.ts";
26+
import { join } from "../deno_ral/path.ts";
27+
import { expandPath, suggestUserBinPaths } from "../core/path.ts";
28+
import { isWindows } from "../deno_ral/platform.ts";
2529

2630
// The tools that are available to install
2731
const kInstallableTools: { [key: string]: InstallableTool } = {
2832
tinytex: tinyTexInstallable,
2933
// temporarily disabled until deno 1.28.* gets puppeteer support
3034
chromium: chromiumInstallable,
35+
verapdf: verapdfInstallable,
3136
};
3237

3338
export async function allTools(): Promise<{
@@ -315,3 +320,76 @@ const installContext = (
315320
},
316321
};
317322
};
323+
324+
// Shared utility functions for --update-path functionality
325+
326+
/**
327+
* Creates a symlink for a tool binary in a user bin directory.
328+
* Returns true if successful, false otherwise.
329+
*/
330+
export async function createToolSymlink(
331+
binaryPath: string,
332+
symlinkName: string,
333+
context: InstallContext,
334+
): Promise<boolean> {
335+
if (isWindows) {
336+
context.info(
337+
`Add the tool's directory to your PATH to use ${symlinkName} from anywhere.`,
338+
);
339+
return false;
340+
}
341+
342+
const binPaths = suggestUserBinPaths();
343+
if (binPaths.length === 0) {
344+
context.info(
345+
`No suitable bin directory found in PATH. Add the tool's directory to your PATH manually.`,
346+
);
347+
return false;
348+
}
349+
350+
for (const binPath of binPaths) {
351+
const expandedBinPath = expandPath(binPath);
352+
ensureDirSync(expandedBinPath);
353+
const symlinkPath = join(expandedBinPath, symlinkName);
354+
355+
try {
356+
// Remove existing symlink if present
357+
if (existsSync(symlinkPath)) {
358+
await Deno.remove(symlinkPath);
359+
}
360+
// Create new symlink
361+
await Deno.symlink(binaryPath, symlinkPath);
362+
return true;
363+
} catch {
364+
// Try next path
365+
continue;
366+
}
367+
}
368+
369+
context.info(
370+
`Could not create symlink. Add the tool's directory to your PATH manually.`,
371+
);
372+
return false;
373+
}
374+
375+
/**
376+
* Removes a tool's symlink from user bin directories.
377+
*/
378+
export async function removeToolSymlink(symlinkName: string): Promise<void> {
379+
if (isWindows) {
380+
return;
381+
}
382+
383+
const binPaths = suggestUserBinPaths();
384+
for (const binPath of binPaths) {
385+
const symlinkPath = join(expandPath(binPath), symlinkName);
386+
try {
387+
const stat = await Deno.lstat(symlinkPath);
388+
if (stat.isSymlink) {
389+
await Deno.remove(symlinkPath);
390+
}
391+
} catch {
392+
// Symlink doesn't exist, continue
393+
}
394+
}
395+
}

0 commit comments

Comments
 (0)