Skip to content

Commit

Permalink
Merge pull request #8769 from quarto-dev/improve/refactor-project-con…
Browse files Browse the repository at this point in the history
…text

Improve/refactor project context
  • Loading branch information
cscheid authored Feb 17, 2024
2 parents 626cebc + 60d4c56 commit 9d91693
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/command/render/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ export async function runPandoc(
) {
const projectExtras = options.project?.formatExtras
? (await options.project.formatExtras(
options.project,
options.source,
options.flags || {},
options.format,
Expand Down
2 changes: 1 addition & 1 deletion src/command/use/commands/binder/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const useBinderCommand = new Command()
doneMessage: "Detected Project configuration:\n",
},
() => {
return context.environment(context);
return context.environment();
},
);

Expand Down
6 changes: 3 additions & 3 deletions src/format/html/format-html-bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ async function processOtherLinks(
context: ProjectContext,
): Promise<OtherLink | undefined> => {
if (link === "repo") {
const env = await context.environment(context);
const env = await context.environment();
if (env.github.repoUrl) {
return {
icon: "github",
Expand All @@ -685,7 +685,7 @@ async function processOtherLinks(
);
}
} else if (link === "devcontainer") {
const env = await context.environment(context);
const env = await context.environment();
if (
env.github.organization && env.github.repository && env.github.repoUrl
) {
Expand All @@ -703,7 +703,7 @@ async function processOtherLinks(
);
}
} else if (link === "binder") {
const env = await context.environment(context);
const env = await context.environment();
if (env.github.organization && env.github.repository) {
const containerUrl = binderUrl(
env.github.organization,
Expand Down
25 changes: 17 additions & 8 deletions src/project/project-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { existsSync, walkSync } from "fs/mod.ts";
import * as ld from "../core/lodash.ts";

import { ProjectType } from "./types/types.ts";
import { Metadata } from "../config/types.ts";
import { Format, Metadata, PandocFlags } from "../config/types.ts";
import {
kProjectLibDir,
kProjectOutputDir,
Expand Down Expand Up @@ -67,7 +67,7 @@ import {
projectConfigFile,
projectVarsFile,
} from "./project-shared.ts";
import { RenderFlags } from "../command/render/types.ts";
import { RenderFlags, RenderServices } from "../command/render/types.ts";
import { kWebsite } from "./types/website/website-constants.ts";

import { readAndValidateYamlFromFile } from "../core/schema/validated-yaml.ts";
Expand Down Expand Up @@ -257,7 +257,7 @@ export async function projectContext(
}

debug(`projectContext: Found Quarto project in ${dir}`);
return {
const result: ProjectContext = {
dir,
engines,
files: {
Expand All @@ -267,17 +267,25 @@ export async function projectContext(
configResources: projectConfigResources(dir, projectConfig, type),
},
config: projectConfig,
formatExtras: type.formatExtras,
// this is a relatively ugly hack to avoid a circular import chain
// that causes a deno bundler bug;
renderFormats,
environment,
environment: () => environment(result),
notebookContext,
};
if (type.formatExtras) {
result.formatExtras = async (
source: string,
flags: PandocFlags,
format: Format,
services: RenderServices,
) => type.formatExtras!(result, source, flags, format, services);
}
return result;
} else {
const { files, engines } = projectInputFiles(dir);
debug(`projectContext: Found Quarto project in ${dir}`);
return {
const result = {
dir,
engines,
config: projectConfig,
Expand All @@ -288,9 +296,10 @@ export async function projectContext(
configResources: projectConfigResources(dir, projectConfig),
},
renderFormats,
environment,
environment: () => environment(result),
notebookContext,
};
return result;
}
} else {
const nextDir = dirname(dir);
Expand All @@ -312,7 +321,7 @@ export async function projectContext(
input: [],
},
renderFormats,
environment,
environment: () => environment(context),
notebookContext,
};
if (Deno.statSync(path).isDirectory) {
Expand Down
9 changes: 4 additions & 5 deletions src/project/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export interface ProjectContext {
engines: string[];
files: ProjectFiles;
config?: ProjectConfig;
notebookContext: NotebookContext;
outputNameIndex?: Map<string, { file: string; format: Format } | undefined>;

formatExtras?: (
project: ProjectContext,
source: string,
flags: PandocFlags,
format: Format,
Expand All @@ -56,10 +58,7 @@ export interface ProjectContext {
project?: ProjectContext,
) => Promise<Record<string, Format>>;

notebookContext: NotebookContext;

outputNameIndex?: Map<string, { file: string; format: Format } | undefined>;
environment: (project: ProjectContext) => Promise<ProjectEnvironment>;
environment: () => Promise<ProjectEnvironment>;
}

export interface ProjectFiles {
Expand Down

0 comments on commit 9d91693

Please sign in to comment.