Skip to content

Commit

Permalink
Merge pull request #11098 from quarto-dev/bugfix/10828
Browse files Browse the repository at this point in the history
pre,postrender - use QUARTO_USE_* environment when requested
  • Loading branch information
cscheid authored Oct 17, 2024
2 parents fb98c5d + 2e498bd commit cf81582
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 23 deletions.
76 changes: 63 additions & 13 deletions src/command/render/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,34 @@ export async function renderProject(

// run pre-render step if we are rendering all files
if (preRenderScripts.length) {
// https://github.com/quarto-dev/quarto-cli/issues/10828
// some environments limit the length of environment variables.
// It's hard to know in advance what the limit is, so we will
// instead ask users to configure their environment with
// the names of the files we will write the list of files to.

const filesToRender = projectRenderConfig.filesToRender
.map((fileToRender) => fileToRender.path)
.map((file) => relative(projDir, file));
const env: Record<string, string> = {
...prePostEnv,
};

if (Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES")) {
Deno.writeTextFileSync(
Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES")!,
filesToRender.join("\n"),
);
} else {
env.QUARTO_PROJECT_INPUT_FILES = filesToRender.join("\n");
}

await runPreRender(
projDir,
preRenderScripts,
progress,
!!projectRenderConfig.options.flags?.quiet,
{
...prePostEnv,
QUARTO_PROJECT_INPUT_FILES: projectRenderConfig.filesToRender
.map((fileToRender) => fileToRender.path)
.map((file) => relative(projDir, file))
.join("\n"),
},
env,
);

// re-initialize project context
Expand Down Expand Up @@ -814,17 +830,34 @@ export async function renderProject(

// run post-render if this isn't incremental
if (postRenderScripts.length) {
// https://github.com/quarto-dev/quarto-cli/issues/10828
// some environments limit the length of environment variables.
// It's hard to know in advance what the limit is, so we will
// instead ask users to configure their environment with
// the names of the files we will write the list of files to.

const env: Record<string, string> = {
...prePostEnv,
};

if (Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES")) {
Deno.writeTextFileSync(
Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES")!,
outputFiles.map((outputFile) => relative(projDir, outputFile.file))
.join("\n"),
);
} else {
env.QUARTO_PROJECT_OUTPUT_FILES = outputFiles
.map((outputFile) => relative(projDir, outputFile.file))
.join("\n");
}

await runPostRender(
projDir,
postRenderScripts,
progress,
!!projectRenderConfig.options.flags?.quiet,
{
...prePostEnv,
QUARTO_PROJECT_OUTPUT_FILES: outputFiles
.map((outputFile) => relative(projDir, outputFile.file))
.join("\n"),
},
env,
);
}
}
Expand Down Expand Up @@ -908,6 +941,23 @@ async function runScripts(

const handler = handlerForScript(script);
if (handler) {
if (env) {
env = {
...env,
};
} else {
env = {};
}
if (!env) throw new Error("should never get here");
const input = Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES");
const output = Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES");
if (input) {
env["QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES"] = input;
}
if (output) {
env["QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES"] = output;
}

const result = await handler.run(script, args.splice(1), undefined, {
cwd: projDir,
stdout: quiet ? "piped" : "inherit",
Expand Down
16 changes: 8 additions & 8 deletions src/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function quarto(

// passthrough to run handlers
if (args[0] === "run" && args[1] !== "help" && args[1] !== "--help") {
const result = await runScript(args.slice(1));
const result = await runScript(args.slice(1), env);
Deno.exit(result.code);
}

Expand Down Expand Up @@ -151,16 +151,16 @@ export async function quarto(

const promise = quartoCommand.command("help", new HelpCommand().global())
.command("completions", new CompletionsCommand()).hidden().parse(args);
for (const [key, value] of Object.entries(oldEnv)) {
if (value === undefined) {
Deno.env.delete(key);
} else {
Deno.env.set(key, value);
}
}

try {
await promise;
for (const [key, value] of Object.entries(oldEnv)) {
if (value === undefined) {
Deno.env.delete(key);
} else {
Deno.env.set(key, value);
}
}
} catch (e) {
if (e instanceof CommandError) {
logError(e, false);
Expand Down
1 change: 1 addition & 0 deletions tests/docs/project/prepost/issue-10828/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
21 changes: 21 additions & 0 deletions tests/docs/project/prepost/issue-10828/_quarto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
project:
type: website
pre-render: check-input.ts
post-render: check-output.ts

website:
title: "issue-10828"
navbar:
left:
- href: index.qmd
text: Home
- about.qmd

format:
html:
theme: cosmo
css: styles.css
toc: true



5 changes: 5 additions & 0 deletions tests/docs/project/prepost/issue-10828/about.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: "About"
---

About this site
1 change: 1 addition & 0 deletions tests/docs/project/prepost/issue-10828/check-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deno.readTextFileSync(Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES"));
1 change: 1 addition & 0 deletions tests/docs/project/prepost/issue-10828/check-output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deno.readTextFileSync(Deno.env.get("QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES"));
7 changes: 7 additions & 0 deletions tests/docs/project/prepost/issue-10828/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: "issue-10828"
---

This is a Quarto website.

To learn more about Quarto websites visit <https://quarto.org/docs/websites>.
1 change: 1 addition & 0 deletions tests/docs/project/prepost/issue-10828/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* css styles */
24 changes: 22 additions & 2 deletions tests/smoke/project/project-prepost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { join } from "../../../src/deno_ral/path.ts";
import { existsSync } from "../../../src/deno_ral/fs.ts";
import { testQuartoCmd } from "../../test.ts";
import { fileExists, noErrors, printsMessage, verifyNoPath, verifyPath } from "../../verify.ts";
import { safeRemoveIfExists } from "../../../src/core/path.ts";
import { normalizePath, safeRemoveIfExists } from "../../../src/core/path.ts";

const renderDir = docs("project/prepost/mutate-render-list");
const dir = join(Deno.cwd(), renderDir);
Expand Down Expand Up @@ -76,4 +76,24 @@ testQuartoCmd(
verifyPath(path);
safeRemoveIfExists(path);
}
});
});

testQuartoCmd(
"render",
[docs("project/prepost/issue-10828")],
[],
{
env: {
"QUARTO_USE_FILE_FOR_PROJECT_INPUT_FILES": normalizePath(docs("project/prepost/issue-10828/input-files.txt")),
"QUARTO_USE_FILE_FOR_PROJECT_OUTPUT_FILES": normalizePath(docs("project/prepost/issue-10828/output-files.txt"))
},
teardown: async () => {
const inputPath = normalizePath(docs("project/prepost/issue-10828/input-files.txt"));
const outputPath = normalizePath(docs("project/prepost/issue-10828/output-files.txt"));
verifyPath(inputPath);
safeRemoveIfExists(inputPath);
verifyPath(outputPath);
safeRemoveIfExists(outputPath);
}
}
)

0 comments on commit cf81582

Please sign in to comment.