Skip to content

Commit 601e3a8

Browse files
claude: support brand extensions in quarto use brand
When `quarto use brand` encounters a brand extension (has _extension.yml with contributes.metadata.project.brand), it now: - Detects the extension in the staged directory (root, _extensions/*, or _extensions/*/*) - Uses the extension directory as the source - Excludes _extension.yml from being copied - Renames the brand file (e.g., brand.yml) to _brand.yml Also updates tests: - Scenario 8 now tests single-file mode (no _quarto.yml) - Adds Scenario 15 for brand extension installation - Adds Scenario 16 for brand extension dry-run Fixes #13863 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 878eb5c commit 601e3a8

5 files changed

Lines changed: 227 additions & 9 deletions

File tree

‎src/command/use/commands/brand.ts‎

Lines changed: 121 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,92 @@ import { InternalError } from "../../../core/lib/error.ts";
2424
import { notebookContext } from "../../../render/notebook/notebook-context.ts";
2525
import { projectContext } from "../../../project/project-context.ts";
2626
import { afterConfirm } from "../../../tools/tools-console.ts";
27+
import { readYaml } from "../../../core/yaml.ts";
28+
import { Metadata } from "../../../config/types.ts";
2729

2830
const kRootTemplateName = "template.qmd";
2931

32+
// Brand extension detection result
33+
interface BrandExtensionInfo {
34+
isBrandExtension: boolean;
35+
extensionDir?: string; // Directory containing the brand extension
36+
brandFileName?: string; // The original brand file name (e.g., "brand.yml")
37+
}
38+
39+
// Check if a directory contains a brand extension
40+
function checkForBrandExtension(dir: string): BrandExtensionInfo {
41+
const extensionFiles = ["_extension.yml", "_extension.yaml"];
42+
43+
for (const file of extensionFiles) {
44+
const path = join(dir, file);
45+
if (existsSync(path)) {
46+
try {
47+
const yaml = readYaml(path) as Metadata;
48+
// Check for contributes.metadata.project.brand
49+
const contributes = yaml?.contributes as Metadata | undefined;
50+
const metadata = contributes?.metadata as Metadata | undefined;
51+
const project = metadata?.project as Metadata | undefined;
52+
const brandFile = project?.brand as string | undefined;
53+
54+
if (brandFile && typeof brandFile === "string") {
55+
return {
56+
isBrandExtension: true,
57+
extensionDir: dir,
58+
brandFileName: brandFile,
59+
};
60+
}
61+
} catch {
62+
// If we can't read/parse the extension file, continue searching
63+
}
64+
}
65+
}
66+
67+
return { isBrandExtension: false };
68+
}
69+
70+
// Search for a brand extension in the staged directory
71+
// Searches: root, _extensions/*, _extensions/*/*
72+
function findBrandExtension(stagedDir: string): BrandExtensionInfo {
73+
// First check the root directory
74+
const rootCheck = checkForBrandExtension(stagedDir);
75+
if (rootCheck.isBrandExtension) {
76+
return rootCheck;
77+
}
78+
79+
// Check _extensions directory
80+
const extensionsDir = join(stagedDir, "_extensions");
81+
if (!existsSync(extensionsDir)) {
82+
return { isBrandExtension: false };
83+
}
84+
85+
try {
86+
// Check direct children: _extensions/extension-name/
87+
for (const entry of Deno.readDirSync(extensionsDir)) {
88+
if (!entry.isDirectory) continue;
89+
90+
const extPath = join(extensionsDir, entry.name);
91+
const check = checkForBrandExtension(extPath);
92+
if (check.isBrandExtension) {
93+
return check;
94+
}
95+
96+
// Check nested: _extensions/org/extension-name/
97+
for (const nested of Deno.readDirSync(extPath)) {
98+
if (!nested.isDirectory) continue;
99+
const nestedPath = join(extPath, nested.name);
100+
const nestedCheck = checkForBrandExtension(nestedPath);
101+
if (nestedCheck.isBrandExtension) {
102+
return nestedCheck;
103+
}
104+
}
105+
}
106+
} catch {
107+
// Directory read error, return not found
108+
}
109+
110+
return { isBrandExtension: false };
111+
}
112+
30113
export const useBrandCommand = new Command()
31114
.name("brand")
32115
.arguments("<target:string>")
@@ -100,8 +183,24 @@ async function useBrand(
100183
// Extract and move the template into place
101184
const stagedDir = await stageBrand(source, tempContext);
102185

186+
// Check if this is a brand extension
187+
const brandExtInfo = findBrandExtension(stagedDir);
188+
189+
// Determine the actual source directory and file mapping
190+
const sourceDir = brandExtInfo.isBrandExtension
191+
? brandExtInfo.extensionDir!
192+
: stagedDir;
193+
103194
// Filter the list to template files
104-
const filesToCopy = templateFiles(stagedDir);
195+
let filesToCopy = templateFiles(sourceDir);
196+
197+
// For brand extensions, exclude _extension.yml/_extension.yaml
198+
if (brandExtInfo.isBrandExtension) {
199+
filesToCopy = filesToCopy.filter((f) => {
200+
const name = basename(f);
201+
return name !== "_extension.yml" && name !== "_extension.yaml";
202+
});
203+
}
105204

106205
// Confirm changes to brand directory (skip for dry-run or force)
107206
if (!options.dryRun && !options.force) {
@@ -125,10 +224,20 @@ async function useBrand(
125224
}
126225

127226
// Build set of source file paths for comparison
227+
// For brand extensions, we need to account for the brand file rename
128228
const sourceFiles = new Set(
129229
filesToCopy
130230
.filter((f) => !Deno.statSync(f).isDirectory)
131-
.map((f) => relative(stagedDir, f)),
231+
.map((f) => {
232+
const rel = relative(sourceDir, f);
233+
// If this is a brand extension and this is the brand file, it will become _brand.yml
234+
if (
235+
brandExtInfo.isBrandExtension && rel === brandExtInfo.brandFileName
236+
) {
237+
return "_brand.yml";
238+
}
239+
return rel;
240+
}),
132241
);
133242

134243
// Find extra files in target that aren't in source
@@ -147,13 +256,20 @@ async function useBrand(
147256

148257
for (const fileToCopy of filesToCopy) {
149258
const isDir = Deno.statSync(fileToCopy).isDirectory;
150-
const rel = relative(stagedDir, fileToCopy);
259+
const rel = relative(sourceDir, fileToCopy);
151260
if (isDir) {
152261
continue;
153262
}
263+
264+
// For brand extensions, rename the brand file to _brand.yml
265+
let targetRel = rel;
266+
if (brandExtInfo.isBrandExtension && rel === brandExtInfo.brandFileName) {
267+
targetRel = "_brand.yml";
268+
}
269+
154270
// Compute the paths
155-
const targetPath = join(brandDir, rel);
156-
const displayName = rel;
271+
const targetPath = join(brandDir, targetRel);
272+
const displayName = targetRel;
157273
const targetDir = dirname(targetPath);
158274
const copyAction = {
159275
file: displayName,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: Test Brand Extension
2+
author: Test Author
3+
version: 1.0.0
4+
quarto-required: ">=1.4.0"
5+
contributes:
6+
metadata:
7+
project:
8+
brand: brand.yml
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
meta:
2+
name: Test Brand Extension
3+
color:
4+
primary: "#007bff"
5+
secondary: "#6c757d"
1.82 KB
Loading

‎tests/smoke/use/brand.test.ts‎

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,18 +327,22 @@ testQuartoCmd(
327327
"quarto use brand - nested directory structure"
328328
);
329329

330-
// Scenario 8: Error - no project directory
330+
// Scenario 8: Single-file mode (no _quarto.yml) - should work, using current directory
331331
const noProjectDir = join(tempDir, "no-project");
332332
ensureDirSync(noProjectDir);
333333
testQuartoCmd(
334334
"use",
335335
["brand", join(fixtureDir, "basic-brand"), "--force"],
336336
[
337-
printsMessage({ level: "ERROR", regex: /Could not find project dir/ }),
337+
noErrorsOrWarnings,
338+
// Should create _brand/ in the current directory even without _quarto.yml
339+
folderExists(join(noProjectDir, "_brand")),
340+
fileExists(join(noProjectDir, "_brand", "_brand.yml")),
341+
fileExists(join(noProjectDir, "_brand", "logo.png")),
338342
],
339343
{
340344
setup: () => {
341-
// No _quarto.yml created - this should cause an error
345+
// No _quarto.yml created - single-file mode should work
342346
return Promise.resolve();
343347
},
344348
cwd: () => noProjectDir,
@@ -347,7 +351,7 @@ testQuartoCmd(
347351
return Promise.resolve();
348352
}
349353
},
350-
"quarto use brand - error on no project"
354+
"quarto use brand - single-file mode (no _quarto.yml)"
351355
);
352356

353357
// Scenario 9: Nested directory - overwrite files in subdirectories, remove extra
@@ -650,3 +654,88 @@ testQuartoCmd(
650654
},
651655
"quarto use brand - deeply nested directories recursively cleaned up"
652656
);
657+
658+
// Scenario 15: Brand extension - basic installation
659+
// Tests that brand extensions are detected and the brand file is renamed to _brand.yml
660+
const brandExtDir = join(tempDir, "brand-ext");
661+
ensureDirSync(brandExtDir);
662+
testQuartoCmd(
663+
"use",
664+
["brand", join(fixtureDir, "brand-extension"), "--force"],
665+
[
666+
noErrorsOrWarnings,
667+
folderExists(join(brandExtDir, "_brand")),
668+
// brand.yml should be renamed to _brand.yml
669+
fileExists(join(brandExtDir, "_brand", "_brand.yml")),
670+
// logo.png should be copied
671+
fileExists(join(brandExtDir, "_brand", "logo.png")),
672+
// _extension.yml should NOT be copied
673+
{
674+
name: "_extension.yml should not be copied",
675+
verify: () => {
676+
if (existsSync(join(brandExtDir, "_brand", "_extension.yml"))) {
677+
throw new Error("_extension.yml should not be copied from brand extension");
678+
}
679+
return Promise.resolve();
680+
}
681+
},
682+
// Verify the content is correct (from brand.yml, not some other file)
683+
{
684+
name: "_brand.yml should contain brand extension content",
685+
verify: () => {
686+
const content = Deno.readTextFileSync(join(brandExtDir, "_brand", "_brand.yml"));
687+
if (!content.includes("Test Brand Extension")) {
688+
throw new Error("_brand.yml should contain content from brand.yml");
689+
}
690+
return Promise.resolve();
691+
}
692+
},
693+
],
694+
{
695+
setup: () => {
696+
Deno.writeTextFileSync(join(brandExtDir, "_quarto.yml"), "project:\n type: default\n");
697+
return Promise.resolve();
698+
},
699+
cwd: () => brandExtDir,
700+
teardown: () => {
701+
try { Deno.removeSync(brandExtDir, { recursive: true }); } catch { /* ignore */ }
702+
return Promise.resolve();
703+
}
704+
},
705+
"quarto use brand - brand extension installation"
706+
);
707+
708+
// Scenario 16: Brand extension - dry-run shows correct file names
709+
const brandExtDryRunDir = join(tempDir, "brand-ext-dry-run");
710+
ensureDirSync(brandExtDryRunDir);
711+
testQuartoCmd(
712+
"use",
713+
["brand", join(fixtureDir, "brand-extension"), "--dry-run"],
714+
[
715+
noErrorsOrWarnings,
716+
// Should show _brand.yml (renamed from brand.yml), not brand.yml
717+
filesInSections({ create: ["_brand.yml", "logo.png"] }, true),
718+
// _brand directory should not exist in dry-run mode
719+
{
720+
name: "_brand directory should not exist in dry-run mode",
721+
verify: () => {
722+
if (existsSync(join(brandExtDryRunDir, "_brand"))) {
723+
throw new Error("_brand directory should not exist in dry-run mode");
724+
}
725+
return Promise.resolve();
726+
}
727+
}
728+
],
729+
{
730+
setup: () => {
731+
Deno.writeTextFileSync(join(brandExtDryRunDir, "_quarto.yml"), "project:\n type: default\n");
732+
return Promise.resolve();
733+
},
734+
cwd: () => brandExtDryRunDir,
735+
teardown: () => {
736+
try { Deno.removeSync(brandExtDryRunDir, { recursive: true }); } catch { /* ignore */ }
737+
return Promise.resolve();
738+
}
739+
},
740+
"quarto use brand - brand extension dry-run shows renamed file"
741+
);

0 commit comments

Comments
 (0)