Skip to content

Commit

Permalink
Make execute optional and pass in from the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored and agoose77 committed Feb 1, 2024
1 parent ea0d6ca commit a3c3de1
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 48 deletions.
1 change: 1 addition & 0 deletions packages/myst-cli/src/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type BuildOpts = {
output?: string;
checkLinks?: boolean;
ci?: boolean;
execute?: boolean;
};

export function hasAnyExplicitExportFormat(opts: BuildOpts): boolean {
Expand Down
14 changes: 10 additions & 4 deletions packages/myst-cli/src/build/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type CleanOptions = {
html?: boolean;
temp?: boolean;
exports?: boolean;
execute?: boolean;
templates?: boolean;
all?: boolean;
yes?: boolean;
Expand All @@ -43,6 +44,7 @@ const ALL_OPTS: CleanOptions = {
html: true,
temp: true,
exports: true,
execute: true,
templates: true,
};
const DEFAULT_OPTS: CleanOptions = {
Expand All @@ -56,10 +58,12 @@ const DEFAULT_OPTS: CleanOptions = {
html: true,
temp: true,
exports: true,
execute: true,
};

function coerceOpts(opts: CleanOptions) {
const { docx, pdf, tex, xml, md, meca, site, html, temp, exports, templates, all } = opts;
const { docx, pdf, tex, xml, md, meca, site, html, temp, exports, execute, templates, all } =
opts;
if (all) return { ...opts, ...ALL_OPTS };
if (
!docx &&
Expand All @@ -72,6 +76,7 @@ function coerceOpts(opts: CleanOptions) {
!html &&
!temp &&
!exports &&
!execute &&
!templates
) {
return { ...opts, ...DEFAULT_OPTS };
Expand Down Expand Up @@ -112,7 +117,7 @@ function deduplicatePaths(paths: string[]) {

export async function clean(session: ISession, files: string[], opts: CleanOptions) {
opts = coerceOpts(opts);
const { site, html, temp, exports, templates, yes } = opts;
const { site, html, temp, exports, execute, templates, yes } = opts;
let pathsToDelete: string[] = [];
const exportOptionsList = await collectAllBuildExportOptions(session, files, opts);
if (exports) {
Expand All @@ -125,7 +130,7 @@ export async function clean(session: ISession, files: string[], opts: CleanOptio
});
}
let buildFolders: string[] = [];
if (temp || exports || templates || html) {
if (temp || exports || execute || templates || html) {
const projectPaths = [
...getProjectPaths(session),
...exportOptionsList.map((exp) => exp.$project),
Expand All @@ -138,12 +143,13 @@ export async function clean(session: ISession, files: string[], opts: CleanOptio
buildFolders.push(session.buildPath());
}
buildFolders = [...new Set(buildFolders)].sort();
if (temp || exports || templates || html) {
if (temp || exports || templates || execute || html) {
buildFolders.forEach((folder) => {
if (temp) pathsToDelete.push(path.join(folder, 'temp'));
if (exports) pathsToDelete.push(path.join(folder, 'exports'));
if (templates) pathsToDelete.push(path.join(folder, 'templates'));
if (html) pathsToDelete.push(path.join(folder, 'html'));
if (execute) pathsToDelete.push(path.join(folder, 'execute'));
});
}
if (site) {
Expand Down
13 changes: 11 additions & 2 deletions packages/myst-cli/src/build/site/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Options = {
checkLinks?: boolean;
yes?: boolean;
port?: number;
execute?: boolean;
serverPort?: number;
writeToc?: boolean;
keepHost?: boolean;
Expand All @@ -38,8 +39,15 @@ export function ensureBuildFoldersExist(session: ISession): void {
}

export async function buildSite(session: ISession, opts: Options) {
const { writeToc, strict, checkLinks, extraLinkTransformers, extraTransforms, defaultTemplate } =
opts;
const {
writeToc,
strict,
checkLinks,
extraLinkTransformers,
extraTransforms,
defaultTemplate,
execute,
} = opts;
ensureBuildFoldersExist(session);
await processSite(session, {
writeToc,
Expand All @@ -48,5 +56,6 @@ export async function buildSite(session: ISession, opts: Options) {
extraLinkTransformers,
extraTransforms,
defaultTemplate,
execute,
});
}
3 changes: 2 additions & 1 deletion packages/myst-cli/src/build/site/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,13 @@ export async function startServer(
if (!opts.headless) await installSiteTemplate(session, mystTemplate);
await buildSite(session, opts);
const server = await startContentServer(session, opts);
const { extraLinkTransformers, extraTransforms, defaultTemplate } = opts;
const { extraLinkTransformers, extraTransforms, defaultTemplate, execute } = opts;
if (!opts.buildStatic) {
watchContent(session, server.reload, {
extraLinkTransformers,
extraTransforms,
defaultTemplate,
execute,
});
}
if (opts.headless) {
Expand Down
1 change: 1 addition & 0 deletions packages/myst-cli/src/build/site/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TransformOptions = {
extraTransforms?: TransformFn[];
defaultTemplate?: string;
reloadProject?: boolean;
execute?: boolean;
};

function watchConfigAndPublic(session: ISession, serverReload: () => void, opts: TransformOptions) {
Expand Down
22 changes: 14 additions & 8 deletions packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
enumerateTargetsPlugin,
keysTransform,
linksTransform,
MultiPageReferenceState,
MystTransformer,
WikiTransformer,
GithubTransformer,
Expand Down Expand Up @@ -115,6 +116,7 @@ export async function transformMdast(
pageSlug?: string;
imageExtensions?: ImageExtensions[];
watchMode?: boolean;
execute?: boolean;
extraTransforms?: TransformFn[];
minifyMaxCharacters?: number;
index?: string;
Expand All @@ -132,6 +134,7 @@ export async function transformMdast(
minifyMaxCharacters,
index,
titleDepth,
execute,
} = opts;
const toc = tic();
const { store, log } = session;
Expand Down Expand Up @@ -230,14 +233,17 @@ export async function transformMdast(
// Combine file-specific citation renderers with project renderers from bib files
const fileCitationRenderer = combineCitationRenderers(cache, ...rendererFiles);

const cachePath = path.join(session.buildPath(), 'execute');
await kernelExecutionTransform(mdast, vfile, {
cache: new LocalDiskCache<(IExpressionResult | IOutput[])[]>(cachePath),
sessionFactory: () => session.jupyterSessionManager(),
frontmatter: frontmatter,
ignoreCache: false,
errorIsFatal: false,
});
if (execute) {
const cachePath = path.join(session.buildPath(), 'execute');
await kernelExecutionTransform(mdast, vfile, {
cache: new LocalDiskCache<(IExpressionResult | IOutput[])[]>(cachePath),
sessionFactory: () => session.jupyterSessionManager(),
frontmatter: frontmatter,
ignoreCache: false,
errorIsFatal: false,
log: session.log,
});
}
transformRenderInlineExpressions(mdast, vfile);

transformFilterOutputStreams(mdast, vfile, frontmatter.settings);
Expand Down
11 changes: 8 additions & 3 deletions packages/myst-cli/src/process/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type ProcessOptions = {
defaultTemplate?: string;
reloadProject?: boolean;
minifyMaxCharacters?: number;
/** Execute flag for notebooks */
execute?: boolean;
};

/**
Expand Down Expand Up @@ -206,6 +208,7 @@ export async function fastProcessFile(
extraLinkTransformers,
extraTransforms,
defaultTemplate,
execute,
}: {
file: string;
pageSlug: string;
Expand All @@ -214,6 +217,7 @@ export async function fastProcessFile(
extraLinkTransformers?: LinkTransformer[];
extraTransforms?: TransformFn[];
defaultTemplate?: string;
execute?: boolean;
},
) {
const toc = tic();
Expand All @@ -228,6 +232,7 @@ export async function fastProcessFile(
watchMode: true,
extraTransforms,
index: project.index,
execute,
});
const pageReferenceStates = selectPageReferenceStates(session, pages);
await postProcessMdast(session, {
Expand Down Expand Up @@ -267,6 +272,7 @@ export async function processProject(
writeFiles = true,
reloadProject,
minifyMaxCharacters,
execute,
} = opts || {};
if (!siteProject.path) {
const slugSuffix = siteProject.slug ? `: ${siteProject.slug}` : '';
Expand All @@ -283,9 +289,7 @@ export async function processProject(
// Load all citations (.bib)
...project.bibliography.map((path) => loadFile(session, path, siteProject.path, '.bib')),
// Load all content (.md and .ipynb)
...pages.map((page) =>
loadFile(session, page.file, siteProject.path, undefined),
),
...pages.map((page) => loadFile(session, page.file, siteProject.path, undefined)),
// Load up all the intersphinx references
loadIntersphinx(session, { projectPath: siteProject.path }) as Promise<any>,
]);
Expand All @@ -306,6 +310,7 @@ export async function processProject(
watchMode,
extraTransforms: opts?.extraTransforms,
index: project.index,
execute,
}),
),
);
Expand Down
11 changes: 8 additions & 3 deletions packages/myst-cli/src/session/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,14 @@ export class Session implements ISession {
token: process.env.JUPYTER_TOKEN,
};
} else {
partialServerSettings =
(await findExistingJupyterServer()) ||
(await launchJupyterServer(this.contentPath(), this.log));
const existing = await findExistingJupyterServer();
if (existing) {
this.log.debug(`Found existing server on: ${existing.appUrl}`);
partialServerSettings = existing;
} else {
this.log.debug(`Launching jupyter server on ${this.contentPath()}`);
partialServerSettings = await launchJupyterServer(this.contentPath(), this.log);
}
}

const serverSettings = ServerConnection.makeSettings(partialServerSettings);
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/utils/addWarningForFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function addWarningForFile(
const formatted = `${message}${note}${url}`;
switch (kind) {
case 'info':
session.log.info(`ℹ️ ${prefix}${formatted}`);
session.log.info(`ℹ️ ${prefix}${formatted}`);
break;
case 'error':
session.log.error(`⛔️ ${prefix}${formatted}`);
Expand Down
1 change: 1 addition & 0 deletions packages/myst-execute/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"url": "https://github.com/executablebooks/mystmd/issues"
},
"dependencies": {
"chalk": "^5.2.0",
"myst-common": "^1.1.21",
"vfile": "^5.3.7",
"@jupyterlab/services": "^7.0.0",
Expand Down
Loading

0 comments on commit a3c3de1

Please sign in to comment.