Skip to content

Commit

Permalink
Merge pull request #633 from quarto-dev/feature/temp-part
Browse files Browse the repository at this point in the history
Templating Work
  • Loading branch information
dragonstyle committed Apr 14, 2022
2 parents 73a6e3f + 5572642 commit 7b53645
Show file tree
Hide file tree
Showing 85 changed files with 7,671 additions and 1,292 deletions.
5 changes: 5 additions & 0 deletions package/src/bld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
parseSwcLogCommand,
} from "./common/cyclic-dependencies.ts";
import { archiveBinaryDependencies } from "./common/archive-binary-dependencies.ts";
import { updatePandoc } from "./common/update-pandoc.ts";

// Core command dispatch
export async function quartoBld(args: string[]) {
Expand Down Expand Up @@ -102,5 +103,9 @@ function getCommands() {
commands.push(
parseSwcLogCommand(),
);
commands.push(
updatePandoc(),
);

return commands;
}
113 changes: 55 additions & 58 deletions package/src/common/archive-binary-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { join } from "path/mod.ts";
import { info } from "log/mod.ts";

import { execProcess } from "../../../src/core/process.ts";
import { Configuration } from "./config.ts";
import { Configuration, withWorkingDir } from "./config.ts";
import {
ArchitectureDependency,
Dependency,
Expand All @@ -33,68 +33,65 @@ export async function archiveBinaryDependencies(_config: Configuration) {
info(`Updating binary dependencies...\n`);

for (const dependency of kDependencies) {
info(`** ${dependency.name} ${dependency.version} **`);

const dependencyBucketPath = `${dependency.bucket}/${dependency.version}`;
info("Checking archive status...\n");

const archive = async (
architectureDependency: ArchitectureDependency,
) => {
const platformDeps = [
architectureDependency.darwin,
architectureDependency.linux,
architectureDependency.windows,
];
for (const platformDep of platformDeps) {
const dependencyAwsPath =
`${kBucket}/${dependencyBucketPath}/${platformDep.filename}`;
const response = await s3cmd("ls", [dependencyAwsPath]);
if (!response) {
// This dependency doesn't exist, archive it
info(
`Archiving ${dependencyBucketPath} - ${platformDep.filename}`,
);

// Download the file
const localPath = await download(
workingDir,
platformDep,
);

// Sync to S3
info(`Copying to ${dependencyAwsPath}\n`);
await s3cmd("cp", [
localPath,
dependencyAwsPath,
"--acl",
"public-read",
]);
} else {
info(`${dependencyAwsPath} already archived.`);
}
}
};

for (const arch of Object.keys(dependency.architectureDependencies)) {
info(`Archiving ${dependency.name}`);
const archDep = dependency.architectureDependencies[arch];
await archive(archDep);
}

info("");
await archiveBinaryDependency(dependency, workingDir);
}
});
}

// Utility that provides a working directory and cleans it up
async function withWorkingDir(fn: (wkDir: string) => Promise<void>) {
const workingDir = Deno.makeTempDirSync();
try {
await fn(workingDir);
} finally {
Deno.removeSync(workingDir, { recursive: true });
export async function archiveBinaryDependency(
dependency: Dependency,
workingDir: string,
) {
info(`** ${dependency.name} ${dependency.version} **`);

const dependencyBucketPath = `${dependency.bucket}/${dependency.version}`;
info("Checking archive status...\n");

const archive = async (
architectureDependency: ArchitectureDependency,
) => {
const platformDeps = [
architectureDependency.darwin,
architectureDependency.linux,
architectureDependency.windows,
];
for (const platformDep of platformDeps) {
const dependencyAwsPath =
`${kBucket}/${dependencyBucketPath}/${platformDep.filename}`;
const response = await s3cmd("ls", [dependencyAwsPath]);
if (!response) {
// This dependency doesn't exist, archive it
info(
`Archiving ${dependencyBucketPath} - ${platformDep.filename}`,
);

// Download the file
const localPath = await download(
workingDir,
platformDep,
);

// Sync to S3
info(`Copying to ${dependencyAwsPath}\n`);
await s3cmd("cp", [
localPath,
dependencyAwsPath,
"--acl",
"public-read",
]);
} else {
info(`${dependencyAwsPath} already archived.`);
}
}
};

for (const arch of Object.keys(dependency.architectureDependencies)) {
info(`Archiving ${dependency.name}`);
const archDep = dependency.architectureDependencies[arch];
await archive(archDep);
}

info("");
}

async function s3cmd(cmd: string, args: string[]) {
Expand Down
10 changes: 10 additions & 0 deletions package/src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ export function readConfiguration(
directoryInfo,
};
}

// Utility that provides a working directory and cleans it up
export async function withWorkingDir(fn: (wkDir: string) => Promise<void>) {
const workingDir = Deno.makeTempDirSync();
try {
await fn(workingDir);
} finally {
Deno.removeSync(workingDir, { recursive: true });
}
}
112 changes: 2 additions & 110 deletions package/src/common/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { dirname, join, SEP } from "path/mod.ts";
import { ensureDirSync, existsSync } from "fs/mod.ts";
import { info, warning } from "log/mod.ts";

import { execProcess } from "../../../src/core/process.ts";
import { expandPath } from "../../../src/core/path.ts";
import {
createDevConfig,
Expand All @@ -17,11 +16,9 @@ import {

import { Configuration } from "./config.ts";
import {
Dependency,
configureDependency,
kDependencies,
PlatformDependency,
} from "./dependencies/dependencies.ts";
import { archiveUrl } from "./archive-binary-dependencies.ts";
import { suggestUserBinPaths } from "../../../src/core/env.ts";

export async function configure(
Expand All @@ -39,37 +36,7 @@ export async function configure(

// Download dependencies
for (const dependency of kDependencies) {
info(`Preparing ${dependency.name}`);
const archDep = dependency.architectureDependencies[Deno.build.arch];
if (archDep) {
const platformDep = archDep[Deno.build.os];
info(`Downloading ${dependency.name}`);

let targetFile;
try {
targetFile = await downloadBinaryDependency(
dependency,
platformDep,
config,
);
} catch (error) {
const msg =
`Failed to Download ${dependency.name}\nAre you sure that version ${dependency.version} of ${dependency.bucket} has been archived using './quarto-bld archive-bin-deps'?\n${error.message}`;
throw new Error(msg);
}

info(`Configuring ${dependency.name}`);
await platformDep.configure(targetFile);

info(`Cleaning up`);
Deno.removeSync(targetFile);
} else {
throw new Error(
`The architecture ${Deno.build.arch} is missing the dependency ${dependency.name}`,
);
}

info(`${dependency.name} complete.\n`);
await configureDependency(dependency, config);
}

// Move the quarto script into place
Expand Down Expand Up @@ -160,78 +127,3 @@ export async function configure(
}
}
}

async function downloadBinaryDependency(
dependency: Dependency,
platformDependency: PlatformDependency,
configuration: Configuration,
) {
const targetFile = join(
configuration.directoryInfo.bin,
"tools",
platformDependency.filename,
);
const dlUrl = archiveUrl(dependency, platformDependency);

info("Downloading " + dlUrl);
info("to " + targetFile);
const response = await fetch(dlUrl);
if (response.status === 200) {
const blob = await response.blob();

const bytes = await blob.arrayBuffer();
const data = new Uint8Array(bytes);

Deno.writeFileSync(
targetFile,
data,
);
return targetFile;
} else {
throw new Error(response.statusText);
}
}

// note that this didn't actually work on windows (it froze and then deno was
// inoperable on the machine until reboot!) so we moved it to script/batch
// files on both platforms)
// deno-lint-ignore no-unused-vars
async function downloadDenoStdLibrary(config: Configuration) {
const denoBinary = join(config.directoryInfo.bin, "tools", "deno");
const denoStdTs = join(
config.directoryInfo.pkg,
"scripts",
"deno_std",
"deno_std.ts",
);

const denoCacheLock = join(
config.directoryInfo.pkg,
"scripts",
"deno_std",
"deno_std.lock",
);
const denoCacheDir = join(
config.directoryInfo.src,
"resources",
"deno_std",
"cache",
);
ensureDirSync(denoCacheDir);

info("Updating Deno Stdlib");
info("");
await execProcess({
cmd: [
denoBinary,
"cache",
"--unstable",
"--lock",
denoCacheLock,
denoStdTs,
],
env: {
DENO_DIR: denoCacheDir,
},
});
}
73 changes: 73 additions & 0 deletions package/src/common/dependencies/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*
*/

import { join } from "path/mod.ts";
import { info } from "log/mod.ts";
import { Configuration } from "../config.ts";

import { dartSass } from "./dartsass.ts";
import { deno_dom } from "./deno_dom.ts";
import { esBuild } from "./esbuild.ts";
import { pandoc } from "./pandoc.ts";
import { archiveUrl } from "../archive-binary-dependencies.ts";

// The list of binary dependencies for Quarto
export const kDependencies = [
Expand Down Expand Up @@ -53,3 +58,71 @@ function version(env: string) {
return version;
}
}

export async function configureDependency(
dependency: Dependency,
config: Configuration,
) {
info(`Preparing ${dependency.name}`);
const archDep = dependency.architectureDependencies[Deno.build.arch];
if (archDep) {
const platformDep = archDep[Deno.build.os];
info(`Downloading ${dependency.name}`);

let targetFile;
try {
targetFile = await downloadBinaryDependency(
dependency,
platformDep,
config,
);
} catch (error) {
const msg =
`Failed to Download ${dependency.name}\nAre you sure that version ${dependency.version} of ${dependency.bucket} has been archived using './quarto-bld archive-bin-deps'?\n${error.message}`;
throw new Error(msg);
}

info(`Configuring ${dependency.name}`);
await platformDep.configure(targetFile);

info(`Cleaning up`);
Deno.removeSync(targetFile);
} else {
throw new Error(
`The architecture ${Deno.build.arch} is missing the dependency ${dependency.name}`,
);
}

info(`${dependency.name} complete.\n`);
}

async function downloadBinaryDependency(
dependency: Dependency,
platformDependency: PlatformDependency,
configuration: Configuration,
) {
const targetFile = join(
configuration.directoryInfo.bin,
"tools",
platformDependency.filename,
);
const dlUrl = archiveUrl(dependency, platformDependency);

info("Downloading " + dlUrl);
info("to " + targetFile);
const response = await fetch(dlUrl);
if (response.status === 200) {
const blob = await response.blob();

const bytes = await blob.arrayBuffer();
const data = new Uint8Array(bytes);

Deno.writeFileSync(
targetFile,
data,
);
return targetFile;
} else {
throw new Error(response.statusText);
}
}
1 change: 1 addition & 0 deletions package/src/common/prepare-dist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function inlineFilters(config: Configuration) {
{ name: "crossref" },
{ name: "layout" },
{ name: "quarto-post" },
{ name: "authors" },
{ name: "pagebreak", dir: "rmarkdown" },
];

Expand Down

0 comments on commit 7b53645

Please sign in to comment.