Path: blob/main/package/src/common/compile-quarto-latexmk.ts
6450 views
/*1* compile-quarto-latexmk.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*5*/6import { Command } from "cliffy/command/mod.ts";7import { basename, join } from "../../../src/deno_ral/path.ts";8import { ensureDirSync } from "../../../src/deno_ral/fs.ts";9import { info } from "../../../src/deno_ral/log.ts";1011import { Configuration, readConfiguration } from "../common/config.ts";12import { compile, install, updateDenoPath } from "../util/deno.ts";1314export function compileQuartoLatexmkCommand() {15return new Command()16.name("compile-quarto-latexmk")17.description("Builds binary for quarto-latexmk")18.option(19"-d, --development",20"Install for local development",21)22.option(23"-t, --target <target:string>",24"The target architecture for the binary (e.g. x86_64-unknown-linux-gnu, x86_64-pc-windows-msvc, x86_64-apple-darwin, aarch64-apple-darwin)",25{26collect: true,27},28)29.option(30"-v, --version <version:string>",31"The version number of the compiled executable",32)33.option(34"-n, --name <name:string>",35"The name of the compiled executable",36)37.option(38"--description <description...:string>",39"The description of the compiled executable",40)41// deno-lint-ignore no-explicit-any42.action(async (args: Record<string, any>) => {43const configuration = readConfiguration();44info("Using configuration:");45info(configuration);46info("");4748if (args.development) {49await installQuartoLatexmk(configuration);50} else {51const description = Array.isArray(args.description)52? args.description.join(" ")53: args.description || "Quarto Latexmk Engine";5455const version = args.version || configuration.version;56const name = args.name || "quarto-latexmk";57const targets = (args.targets || [Deno.build.target]) as string[];5859await compileQuartoLatexmk(60configuration,61targets,62version,63name,64description,65);66}67});68}6970// Using --allow-all as there is otherwise an issue in Deno 1.46.3 with --allow-read and --allow-write with network drives71// https://github.com/quarto-dev/quarto-cli/issues/1133272/*73const kFlags = [74"--allow-read",75"--allow-write",76"--allow-run",77"--allow-env",78"--allow-net",79];80*/81const kFlags = ["--allow-all"];8283export async function installQuartoLatexmk(84config: Configuration,85) {86const installPath = await install(87entryPointPath(config),88["-f", ...kFlags],89config,90);91if (installPath) {92updateDenoPath(installPath, config);93}94}9596export async function compileQuartoLatexmk(97config: Configuration,98targets: string[],99version: string,100name: string,101description: string,102) {103const workingTempDir = Deno.makeTempDirSync();104try {105// If target isn't specified, build for whatever the current architecture is106targets = targets || [Deno.build.target];107108// temporarily update the constants to reflect the provided information109const metadataPath = metadataFilePath(config);110info("Using executable info:");111info(`version: ${version}`);112info(`name: ${name}`);113info(`description: ${description}`);114info("");115116// Backup a copy of the source117Deno.copyFileSync(118metadataPath,119join(workingTempDir, basename(metadataPath)),120);121122// Generate the proper constants and compile123const verRegex = /^(export const kExeVersion = ").*(";)$/gm;124const nameRegex = /^(export const kExeName = ").*(";)$/gm;125const descRegex = /^(export const kExeDescription = ").*(";)$/gm;126127let contents = Deno.readTextFileSync(metadataPath);128contents = contents.replace(verRegex, `$1${version}$2`);129contents = contents.replace(nameRegex, `$1${name}$2`);130contents = contents.replace(descRegex, `$1${description}$2`);131132Deno.writeTextFileSync(metadataPath, contents);133134for (const target of targets) {135const outputName = name || "quarto-latexmk";136info(`Compiling for ${target}:`);137const outputDir = join(138config.directoryInfo.bin,139outputName,140target,141);142ensureDirSync(outputDir);143const output = join(outputDir, filename(outputName, target));144145await compile(146entryPointPath(config),147output,148[...kFlags],149config,150);151info(output + "\n");152}153154// Restore the previously backed up file155Deno.copyFileSync(156join(workingTempDir, basename(metadataPath)),157metadataPath,158);159} finally {160Deno.removeSync(workingTempDir, { recursive: true });161}162}163164function filename(name: string, target: string) {165if (target.match(/.*windows.*/)) {166return `${name}.exe`;167} else {168return name;169}170}171172function metadataFilePath(config: Configuration) {173return join(174config.directoryInfo.src,175"command",176"render",177"latexmk",178"quarto-latexmk-metadata.ts",179);180}181182function entryPointPath(config: Configuration) {183return join(184config.directoryInfo.src,185"command",186"render",187"latexmk",188"quarto-latexmk.ts",189);190}191192193