@hia-doc/dotnetdoc-runner 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -22,3 +22,36 @@ files.
22
22
 
23
23
  `dotnet-markup-comments` input 会为 `.aspx`、`.ascx`、`.cshtml`、`.razor`
24
24
  文件输出 `dotnetdoc-markup-comment-extraction` 与对应 HIA document。
25
+
26
+ Config inputs support the original single `path`, explicit `paths`, and
27
+ workspace-relative `glob` / `globs` patterns. Glob expansion is performed inside
28
+ the workspace and never accepts absolute paths or `..` traversal.
29
+
30
+ 配置输入支持原有单个 `path`、显式 `paths`,以及工作区相对的 `glob` /
31
+ `globs`。glob 展开只在工作区内执行,不接受绝对路径或 `..` 路径穿越。
32
+
33
+ For `dotnet-csharp-source`, config inputs can also provide `projectPath` to let
34
+ the runner resolve `.cs` files from a `.csproj` before invoking the lightweight
35
+ Roslyn source extractor.
36
+
37
+ 对 `dotnet-csharp-source`,配置也可以提供 `projectPath`,由 runner 先从
38
+ `.csproj` 推导源码文件集合,再调用轻量 Roslyn source extractor。
39
+
40
+ ```json
41
+ {
42
+ "kind": "dotnet-csharp-source",
43
+ "projectPath": "src/Portal.Components/Portal.Components.csproj",
44
+ "artifactBasePath": "source/Portal.Components",
45
+ "title": "Portal Components Source API"
46
+ }
47
+ ```
48
+
49
+ ```json
50
+ {
51
+ "kind": "dotnet-markup-comments",
52
+ "globs": ["src/Web/**/*.{aspx,ascx,master,cshtml,razor}"],
53
+ "excludeGlobs": ["src/Web/**/bin/**", "src/Web/**/obj/**"],
54
+ "artifactBasePath": "web/markup-comments",
55
+ "title": "Web Markup Comments"
56
+ }
57
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hia-doc/dotnetdoc-runner",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Standalone .NET XML documentation runner and CLI.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -36,9 +36,9 @@
36
36
  "LICENSE"
37
37
  ],
38
38
  "dependencies": {
39
- "@hia-doc/dotnet-source-extractor": "0.1.3",
40
- "@hia-doc/dotnet-xml-doc-extractor": "0.1.3",
41
- "@hia-doc/dotnetdoc-adapter": "0.1.3",
42
- "@hia-doc/dotnetdoc-spec": "0.1.3"
39
+ "@hia-doc/dotnet-source-extractor": "0.1.5",
40
+ "@hia-doc/dotnet-xml-doc-extractor": "0.1.5",
41
+ "@hia-doc/dotnetdoc-adapter": "0.1.5",
42
+ "@hia-doc/dotnetdoc-spec": "0.1.5"
43
43
  }
44
44
  }
package/src/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { readFile, mkdir, writeFile } from "node:fs/promises";
1
+ import { readFile, mkdir, readdir, writeFile } from "node:fs/promises";
2
2
  import path from "node:path";
3
3
 
4
4
  import { extractAspNetEndpoints, extractDotnetMarkupComments, extractDotnetProjectDiscovery, extractDotnetSourceFiles } from "@hia-doc/dotnet-source-extractor";
@@ -21,7 +21,7 @@ export {
21
21
  } from "./schema.mjs";
22
22
  import { DOTNETDOC_CONFIG_SCHEMA_ID, DOTNETDOC_CONFIG_SCHEMA_VERSION } from "./schema.mjs";
23
23
 
24
- export const DOTNETDOC_RUNNER_VERSION = "0.1.3";
24
+ export const DOTNETDOC_RUNNER_VERSION = "0.1.5";
25
25
  export const DOTNETDOC_INPUT_KINDS = Object.freeze(["dotnet-xml-doc", "dotnet-csharp-source", "dotnet-aspnet-surface", "dotnet-markup-comments", "dotnet-project"]);
26
26
  export const DOTNETDOC_OUTPUT_KINDS = Object.freeze(["dotnetdoc-extraction", "hia-document", "dotnetdoc-source-relation", "dotnetdoc-aspnet-endpoint-extraction", "dotnetdoc-markup-comment-extraction", "dotnetdoc-project-discovery"]);
27
27
 
@@ -40,7 +40,11 @@ const SAFE_ID_PATTERN = /^[a-z0-9][a-z0-9._-]*$/;
40
40
  * @lang zh-CN 执行 .NET XML documentation 构建。
41
41
  */
42
42
  export async function runDotnetDoc(request, context = {}) {
43
- const normalized = normalizeRequest(request);
43
+ const normalizedRequest = normalizeRequest(request);
44
+ const normalized = {
45
+ ...normalizedRequest,
46
+ inputs: await expandInputs(normalizedRequest)
47
+ };
44
48
  await mkdir(normalized.outputDirectory, { recursive: true });
45
49
 
46
50
  const artifacts = [];
@@ -178,19 +182,39 @@ function normalizeRequest(request) {
178
182
 
179
183
  function normalizeInput(input, index) {
180
184
  assertRecord(input, `inputs[${index}] must be an object.`);
181
- assertKnownKeys(input, ["kind", "path", "applicationRoot", "artifactBasePath", "hiaDocumentId", "title"], `inputs[${index}]`);
185
+ assertKnownKeys(input, ["kind", "path", "paths", "projectPath", "glob", "globs", "excludeGlobs", "globPatterns", "excludeGlobPatterns", "applicationRoot", "artifactBasePath", "hiaDocumentId", "title"], `inputs[${index}]`);
182
186
  if (!DOTNETDOC_INPUT_KINDS.includes(input.kind)) {
183
187
  throw new TypeError(`inputs[${index}].kind must be one of: ${DOTNETDOC_INPUT_KINDS.join(", ")}.`);
184
188
  }
185
189
 
186
- const inputPath = normalizeSafeRelativePath(input.path, `inputs[${index}].path`);
190
+ const explicitPaths = normalizeInputPathList(input, index);
191
+ const projectPath = input.projectPath == null
192
+ ? null
193
+ : normalizeSafeProjectPath(input.projectPath, `inputs[${index}].projectPath`);
194
+ const globPatterns = uniqueStrings([
195
+ ...normalizeInputGlobList(input, index, "glob", "globs"),
196
+ ...normalizeInternalGlobList(input.globPatterns, index, "globPatterns")
197
+ ]);
198
+ const excludeGlobPatterns = uniqueStrings([
199
+ ...normalizeInputGlobList(input, index, null, "excludeGlobs"),
200
+ ...normalizeInternalGlobList(input.excludeGlobPatterns, index, "excludeGlobPatterns")
201
+ ]);
202
+ if (explicitPaths.length === 0 && globPatterns.length === 0 && !projectPath) {
203
+ throw new TypeError(`inputs[${index}] must define path, paths, projectPath, glob or globs.`);
204
+ }
205
+
206
+ const inputPath = explicitPaths[0] ?? projectPath ?? `${input.kind}-group`;
187
207
  const artifactBasePath = input.artifactBasePath
188
208
  ? normalizeSafeRelativePath(input.artifactBasePath, `inputs[${index}].artifactBasePath`)
189
- : stripKnownInputExtension(inputPath);
209
+ : (explicitPaths[0] ? stripKnownInputExtension(explicitPaths[0]) : projectPath ? stripKnownInputExtension(projectPath) : `${input.kind}-group`);
190
210
 
191
211
  return {
192
212
  kind: input.kind,
193
213
  path: inputPath,
214
+ paths: explicitPaths,
215
+ projectPath,
216
+ globPatterns,
217
+ excludeGlobPatterns,
194
218
  applicationRoot: input.applicationRoot ? normalizeSafeRelativePath(input.applicationRoot, `inputs[${index}].applicationRoot`) : ".",
195
219
  artifactBasePath,
196
220
  hiaDocumentId: input.hiaDocumentId,
@@ -421,31 +445,35 @@ async function processXmlDocInput(input, request) {
421
445
  }
422
446
 
423
447
  async function processCSharpSourceInput(input, request) {
424
- return extractDotnetSourceFiles({
448
+ const sourceRequest = {
425
449
  workspaceRoot: request.workspaceRoot,
426
- paths: [input.path]
427
- });
450
+ paths: input.paths
451
+ };
452
+ if (input.projectPath) {
453
+ sourceRequest.projectPath = input.projectPath;
454
+ }
455
+ return extractDotnetSourceFiles(sourceRequest);
428
456
  }
429
457
 
430
458
  async function processAspNetEndpointInput(input, request) {
431
459
  return extractAspNetEndpoints({
432
460
  workspaceRoot: request.workspaceRoot,
433
461
  applicationRoot: input.applicationRoot,
434
- paths: [input.path]
462
+ paths: input.paths
435
463
  });
436
464
  }
437
465
 
438
466
  async function processMarkupCommentInput(input, request) {
439
467
  return extractDotnetMarkupComments({
440
468
  workspaceRoot: request.workspaceRoot,
441
- paths: [input.path]
469
+ paths: input.paths
442
470
  });
443
471
  }
444
472
 
445
473
  async function processProjectDiscoveryInput(input, request) {
446
474
  return extractDotnetProjectDiscovery({
447
475
  workspaceRoot: request.workspaceRoot,
448
- path: input.path
476
+ paths: input.paths
449
477
  });
450
478
  }
451
479
 
@@ -476,6 +504,121 @@ function normalizeConfigDirectory(value, label) {
476
504
  return normalizeSafeRelativePath(value, label);
477
505
  }
478
506
 
507
+ async function expandInputs(request) {
508
+ const expandedInputs = [];
509
+ for (const input of request.inputs) {
510
+ const expandedPaths = await expandInputPaths(request.workspaceRoot, input);
511
+ expandedInputs.push({
512
+ ...input,
513
+ path: expandedPaths[0] ?? input.path,
514
+ paths: expandedPaths,
515
+ globPatterns: undefined,
516
+ excludeGlobPatterns: undefined
517
+ });
518
+ }
519
+ return expandedInputs;
520
+ }
521
+
522
+ async function expandInputPaths(workspaceRoot, input) {
523
+ const matchedPaths = input.globPatterns.length === 0
524
+ ? []
525
+ : await expandGlobPatterns(workspaceRoot, input.globPatterns, input.excludeGlobPatterns);
526
+ const paths = uniqueStrings([...input.paths, ...matchedPaths]);
527
+ if (paths.length === 0 && input.projectPath) {
528
+ return [];
529
+ }
530
+ if (paths.length === 0) {
531
+ throw new TypeError(`Input ${input.kind} did not resolve any paths.`);
532
+ }
533
+ if (input.kind === "dotnet-xml-doc" && paths.length !== 1) {
534
+ throw new TypeError("dotnet-xml-doc inputs currently require exactly one XML documentation path.");
535
+ }
536
+ return paths;
537
+ }
538
+
539
+ async function expandGlobPatterns(workspaceRoot, patterns, excludePatterns) {
540
+ const includeRegexes = expandBracePatterns(patterns).map(globPatternToRegExp);
541
+ const excludeRegexes = expandBracePatterns(excludePatterns).map(globPatternToRegExp);
542
+ const matched = new Set();
543
+ for (const pattern of patterns) {
544
+ const searchRoot = globSearchRoot(pattern);
545
+ const files = await listWorkspaceFiles(workspaceRoot, searchRoot);
546
+ for (const filePath of files) {
547
+ if (includeRegexes.some((regex) => regex.test(filePath)) && !excludeRegexes.some((regex) => regex.test(filePath))) {
548
+ matched.add(filePath);
549
+ }
550
+ }
551
+ }
552
+ return [...matched].sort((left, right) => left.localeCompare(right));
553
+ }
554
+
555
+ async function listWorkspaceFiles(workspaceRoot, relativeDirectory) {
556
+ const root = path.join(workspaceRoot, relativeDirectory);
557
+ const result = [];
558
+
559
+ async function visit(absoluteDirectory, relativeDirectoryPath) {
560
+ let entries;
561
+ try {
562
+ entries = await readdir(absoluteDirectory, { withFileTypes: true });
563
+ } catch (error) {
564
+ if (error?.code === "ENOENT") {
565
+ return;
566
+ }
567
+ throw error;
568
+ }
569
+ for (const entry of entries) {
570
+ const relativePath = relativeDirectoryPath === "." ? entry.name : `${relativeDirectoryPath}/${entry.name}`;
571
+ const absolutePath = path.join(absoluteDirectory, entry.name);
572
+ if (entry.isDirectory()) {
573
+ await visit(absolutePath, relativePath);
574
+ } else if (entry.isFile()) {
575
+ result.push(relativePath.replaceAll("\\", "/"));
576
+ }
577
+ }
578
+ }
579
+
580
+ await visit(root, relativeDirectory);
581
+ return result;
582
+ }
583
+
584
+ function normalizeInputPathList(input, index) {
585
+ const paths = [];
586
+ if (input.path !== undefined) {
587
+ paths.push(normalizeSafeRelativePath(input.path, `inputs[${index}].path`));
588
+ }
589
+ if (input.paths !== undefined) {
590
+ if (!Array.isArray(input.paths) || input.paths.length === 0) {
591
+ throw new TypeError(`inputs[${index}].paths must be a non-empty array.`);
592
+ }
593
+ paths.push(...input.paths.map((value, pathIndex) => normalizeSafeRelativePath(value, `inputs[${index}].paths[${pathIndex}]`)));
594
+ }
595
+ return uniqueStrings(paths);
596
+ }
597
+
598
+ function normalizeInputGlobList(input, index, singularKey, pluralKey) {
599
+ const patterns = [];
600
+ if (singularKey && input[singularKey] !== undefined) {
601
+ patterns.push(normalizeSafeGlobPattern(input[singularKey], `inputs[${index}].${singularKey}`));
602
+ }
603
+ if (input[pluralKey] !== undefined) {
604
+ if (!Array.isArray(input[pluralKey]) || input[pluralKey].length === 0) {
605
+ throw new TypeError(`inputs[${index}].${pluralKey} must be a non-empty array.`);
606
+ }
607
+ patterns.push(...input[pluralKey].map((value, patternIndex) => normalizeSafeGlobPattern(value, `inputs[${index}].${pluralKey}[${patternIndex}]`)));
608
+ }
609
+ return uniqueStrings(patterns);
610
+ }
611
+
612
+ function normalizeInternalGlobList(values, index, key) {
613
+ if (values === undefined) {
614
+ return [];
615
+ }
616
+ if (!Array.isArray(values)) {
617
+ throw new TypeError(`inputs[${index}].${key} must be an array.`);
618
+ }
619
+ return values.map((value, patternIndex) => normalizeSafeGlobPattern(value, `inputs[${index}].${key}[${patternIndex}]`));
620
+ }
621
+
479
622
  function normalizeSafeRelativePath(value, label) {
480
623
  if (typeof value !== "string" || value.trim().length === 0) {
481
624
  throw new TypeError(`${label} must be a non-empty relative path.`);
@@ -487,6 +630,84 @@ function normalizeSafeRelativePath(value, label) {
487
630
  return normalized;
488
631
  }
489
632
 
633
+ function normalizeSafeGlobPattern(value, label) {
634
+ if (typeof value !== "string" || value.trim().length === 0) {
635
+ throw new TypeError(`${label} must be a non-empty relative glob pattern.`);
636
+ }
637
+ const normalized = value.replaceAll("\\", "/").replace(/^\.\//, "");
638
+ if (!normalized || normalized.startsWith("/") || /^[a-zA-Z]:\//.test(normalized) || normalized.split("/").includes("..")) {
639
+ throw new TypeError(`${label} must be a safe relative glob pattern.`);
640
+ }
641
+ return normalized;
642
+ }
643
+
644
+ function normalizeSafeProjectPath(value, label) {
645
+ const normalized = normalizeSafeRelativePath(value, label);
646
+ if (path.extname(normalized).toLowerCase() !== ".csproj") {
647
+ throw new TypeError(`${label} must reference a .csproj file.`);
648
+ }
649
+ return normalized;
650
+ }
651
+
652
+ function uniqueStrings(values) {
653
+ return [...new Set(values)];
654
+ }
655
+
656
+ function expandBracePatterns(patterns) {
657
+ return patterns.flatMap(expandBracePattern);
658
+ }
659
+
660
+ function expandBracePattern(pattern) {
661
+ const match = /\{([^{}]+)\}/.exec(pattern);
662
+ if (!match) {
663
+ return [pattern];
664
+ }
665
+ const before = pattern.slice(0, match.index);
666
+ const after = pattern.slice(match.index + match[0].length);
667
+ return match[1].split(",").flatMap((part) => expandBracePattern(`${before}${part.trim()}${after}`));
668
+ }
669
+
670
+ function globSearchRoot(pattern) {
671
+ const segments = pattern.split("/");
672
+ const staticSegments = [];
673
+ for (const segment of segments) {
674
+ if (/[*{}]/.test(segment)) {
675
+ break;
676
+ }
677
+ staticSegments.push(segment);
678
+ }
679
+ return staticSegments.length === 0 ? "." : staticSegments.join("/");
680
+ }
681
+
682
+ function globPatternToRegExp(pattern) {
683
+ let source = "^";
684
+ for (let index = 0; index < pattern.length;) {
685
+ const char = pattern[index];
686
+ if (char === "*") {
687
+ if (pattern[index + 1] === "*") {
688
+ if (pattern[index + 2] === "/") {
689
+ source += "(?:.*/)?";
690
+ index += 3;
691
+ } else {
692
+ source += ".*";
693
+ index += 2;
694
+ }
695
+ } else {
696
+ source += "[^/]*";
697
+ index += 1;
698
+ }
699
+ continue;
700
+ }
701
+ source += escapeRegExp(char);
702
+ index += 1;
703
+ }
704
+ return new RegExp(`${source}$`);
705
+ }
706
+
707
+ function escapeRegExp(value) {
708
+ return value.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
709
+ }
710
+
490
711
  function stripKnownInputExtension(value) {
491
712
  return value.replace(/\.(?:xml|cs|csproj|sln|aspx|ascx|master|cshtml|razor)$/i, "");
492
713
  }
package/src/schema.mjs CHANGED
@@ -12,6 +12,12 @@ const relativePath = {
12
12
  }
13
13
  };
14
14
 
15
+ const relativePathArray = {
16
+ type: "array",
17
+ minItems: 1,
18
+ items: relativePath
19
+ };
20
+
15
21
  export const DOTNETDOC_CONFIG_JSON_SCHEMA = Object.freeze({
16
22
  $schema: "https://json-schema.org/draft/2020-12/schema",
17
23
  $id: DOTNETDOC_CONFIG_SCHEMA_ID,
@@ -30,10 +36,22 @@ export const DOTNETDOC_CONFIG_JSON_SCHEMA = Object.freeze({
30
36
  items: {
31
37
  type: "object",
32
38
  additionalProperties: false,
33
- required: ["kind", "path"],
39
+ required: ["kind"],
40
+ anyOf: [
41
+ { required: ["path"] },
42
+ { required: ["paths"] },
43
+ { required: ["projectPath"] },
44
+ { required: ["glob"] },
45
+ { required: ["globs"] }
46
+ ],
34
47
  properties: {
35
48
  kind: { enum: ["dotnet-xml-doc", "dotnet-csharp-source", "dotnet-aspnet-surface", "dotnet-markup-comments", "dotnet-project"] },
36
49
  path: relativePath,
50
+ paths: relativePathArray,
51
+ projectPath: relativePath,
52
+ glob: relativePath,
53
+ globs: relativePathArray,
54
+ excludeGlobs: relativePathArray,
37
55
  applicationRoot: relativePath,
38
56
  artifactBasePath: relativePath,
39
57
  hiaDocumentId: { type: "string", minLength: 1 },