-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-snippets.ts
More file actions
581 lines (537 loc) · 17.6 KB
/
Copy pathextract-snippets.ts
File metadata and controls
581 lines (537 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import { readdirSync, readFileSync, writeFileSync } from "fs";
import { glob } from "glob";
import { load } from "js-yaml";
import path from "path";
import { fileURLToPath } from "url";
interface PlaceholderMapping {
pattern: string;
placeholder: string;
}
interface PlaceholderMap {
[lang: string]: PlaceholderMapping[];
}
interface Step {
step: number;
description: string;
code: string;
file: string;
lang: string;
lines: string;
}
interface FrameworkSnippet {
steps: Step[];
framework: string;
lib: string;
lib_version: string;
docs_url: string;
install: string;
repo_path: string;
run_command?: string;
callout?: string;
}
interface ScenarioMeta {
run_command?: string;
lib?: string;
docs_url?: string;
callout?: string;
[key: string]: unknown;
}
interface FrameworkManifest {
framework: string;
label: string;
lang: string;
lib: string;
docs_url: string;
scenarios: Record<string, ScenarioMeta>;
}
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const SAMPLES = path.join(ROOT, "samples");
const TAG_START = /(?:\/\/|#)\s*@snippet:step(\d+):start/;
const TAG_END = /(?:\/\/|#)\s*@snippet:step(\d+):end/;
const DESCRIPTION = /(?:\/\/|#)\s*@description\s+(.+)/;
function getPlaceholderMap(): PlaceholderMap {
const content = readFileSync(
path.join(ROOT, "placeholder-map.yaml"),
"utf-8",
);
return load(content) as PlaceholderMap;
}
function applyPlaceholders(
code: string,
lang: string,
placeholderMap: PlaceholderMap,
): string {
// Try language-specific mappings first, then fall back to js
const mappings = placeholderMap[lang] || placeholderMap["js"] || [];
let result = code;
for (const { pattern, placeholder } of mappings) {
result = result.replaceAll(pattern, `"${placeholder}"`);
}
return result;
}
function getLibVersion(
scenarioDir: string,
manifestDir: string,
libOverride?: string,
): string {
const manifest = load(
readFileSync(path.join(manifestDir, "manifest.yaml"), "utf-8"),
) as FrameworkManifest;
const lib = libOverride ?? manifest.lib;
// npm — package.json
try {
const pkg = JSON.parse(
readFileSync(path.join(scenarioDir, "package.json"), "utf-8"),
);
const v = pkg.dependencies?.[lib] || pkg.devDependencies?.[lib];
if (v) return String(v).replace(/^[\^~>=<\s]+/, "");
} catch {
// no package.json — try next
}
// .NET — *.csproj under src/
try {
const srcDir = path.join(scenarioDir, "src");
const csprojFiles = readdirSync(srcDir).filter((f) =>
f.endsWith(".csproj"),
);
if (csprojFiles.length > 0) {
const csprojContent = readFileSync(
path.join(srcDir, csprojFiles[0]),
"utf-8",
);
const libEscaped = lib.replace(/[.]/g, "\\.");
const regex = new RegExp(
`<PackageReference\\s+Include="${libEscaped}"\\s+Version="([^"]+)"`,
);
const match = csprojContent.match(regex);
if (match) return match[1];
}
} catch {
// no src/ dir or no .csproj — fall through
}
// Java — pom.xml (Spring Boot starters inherit version from parent POM)
try {
const pomContent = readFileSync(path.join(scenarioDir, "pom.xml"), "utf-8");
const parentMatch = pomContent.match(
/<parent>[\s\S]*?<artifactId>spring-boot-starter-parent<\/artifactId>[\s\S]*?<version>([^<]+)<\/version>[\s\S]*?<\/parent>/,
);
if (parentMatch) return parentMatch[1];
} catch {
// no pom.xml — fall through
}
// Flutter — pubspec.yaml dependencies
try {
const pubspec = load(
readFileSync(path.join(scenarioDir, "pubspec.yaml"), "utf-8"),
) as { dependencies?: Record<string, unknown> };
const dep = pubspec?.dependencies?.[lib];
if (typeof dep === "string") {
return dep.replace(/^[\^~>=<\s]+/, "");
}
if (
dep &&
typeof dep === "object" &&
typeof (dep as { version?: unknown }).version === "string"
) {
return (dep as { version: string }).version.replace(/^[\^~>=<\s]+/, "");
}
} catch {
// no pubspec.yaml — fall through
}
// iOS — Package.resolved (preferred) or Package.swift literal
try {
// Look for Package.resolved at the SPM root and inside any *.xcworkspace or
// *.xcodeproj/project.xcworkspace under the scenario dir. globSync is
// synchronous — fine here since this whole function is sync.
const swiftpmGlobs = glob
.sync(
[
"**/*.xcworkspace/**/swiftpm/Package.resolved",
"**/*.xcodeproj/**/swiftpm/Package.resolved",
],
{ cwd: scenarioDir, absolute: true, nodir: true },
)
// glob may include both the .xcworkspace inside a .xcodeproj and the
// .xcodeproj's own embedded workspace — dedupe.
.filter((p, i, arr) => arr.indexOf(p) === i);
const resolvedPaths = [
path.join(scenarioDir, "Package.resolved"),
...swiftpmGlobs,
];
for (const p of resolvedPaths) {
try {
// SwiftPM v1 puts pins at the top level; v2 (used by modern Xcode)
// wraps them under `object.pins`. Handle both.
const resolved = JSON.parse(readFileSync(p, "utf-8")) as {
pins?: Array<{
identity?: string;
location?: string;
state?: { version?: string };
}>;
object?: {
pins?: Array<{
identity?: string;
location?: string;
state?: { version?: string };
}>;
};
};
const pins = resolved.pins ?? resolved.object?.pins;
const libLower = lib.toLowerCase();
const pin = pins?.find((p) => {
const id = (p.identity || "").toLowerCase();
const loc = (p.location || "").toLowerCase();
return id === libLower || loc.includes(`/${libLower}`);
});
if (pin?.state?.version) return pin.state.version;
} catch {
// try next path
}
}
// Fall back to scanning Package.swift for `.upToNextMajor(from: "X.Y.Z")` literals.
const swiftContent = readFileSync(
path.join(scenarioDir, "Package.swift"),
"utf-8",
);
const libEscapedSwift = lib.replace(/[.+*?^${}()|[\]\\]/g, "\\$&");
const literalMatch = swiftContent.match(
new RegExp(
`${libEscapedSwift}[\\s\\S]{0,200}?\\.upToNextMajor\\(from:\\s*"([0-9][0-9A-Za-z.\\-]*)"\\)`,
),
);
if (literalMatch) return literalMatch[1];
} catch {
// no Package.swift / no Package.resolved — fall through
}
// iOS — xcodegen project.yml `packages:` block. The Package.resolved lockfile
// lives inside *.xcworkspace/, which is typically gitignored, so CI checkouts
// can't see it; project.yml is the committed source of truth for SPM pins.
try {
const projectYml = load(
readFileSync(path.join(scenarioDir, "project.yml"), "utf-8"),
) as {
packages?: Record<string, { url?: string; from?: string | number }>;
};
const libLower = lib.toLowerCase();
for (const [name, pkg] of Object.entries(projectYml?.packages ?? {})) {
const url = (pkg?.url || "").toLowerCase();
if (
name.toLowerCase() === libLower ||
url.endsWith(`/${libLower}`) ||
url.endsWith(`/${libLower}.git`)
) {
if (pkg.from != null) return String(pkg.from);
}
}
} catch {
// no project.yml — fall through
}
// Android — build.gradle.kts (Kotlin DSL) or build.gradle (Groovy DSL)
try {
const gradlePaths = [
path.join(scenarioDir, "app", "build.gradle.kts"),
path.join(scenarioDir, "app", "build.gradle"),
path.join(scenarioDir, "build.gradle.kts"),
path.join(scenarioDir, "build.gradle"),
];
for (const p of gradlePaths) {
try {
const gradle = readFileSync(p, "utf-8");
// Match e.g. `implementation("net.openid:appauth:0.11.1")`.
// The `lib` value is expected to be `group:artifact` (e.g. `net.openid:appauth`).
const libEscaped = lib.replace(/[.+*?^${}()|[\]\\]/g, "\\$&");
const regex = new RegExp(
`["']${libEscaped}:([0-9][0-9A-Za-z.\\-]*)["']`,
);
const match = gradle.match(regex);
if (match) return match[1];
} catch {
// try next path
}
}
} catch {
// no Gradle file — fall through
}
// Android — Gradle version catalog (gradle/libs.versions.toml).
// When build.gradle.kts uses `libs.<alias>` instead of inline coordinates,
// the version is in the TOML catalog. Strategy: find an alias whose `module`
// matches the lib coordinate, then resolve its `version.ref` in [versions].
try {
const tomlPath = path.join(scenarioDir, "gradle", "libs.versions.toml");
const tomlContent = readFileSync(tomlPath, "utf-8");
// Parse [versions] section: key = "value"
const versionsMatch = tomlContent.match(/\[versions\]([\s\S]*?)(?=\[|$)/);
const librariesMatch = tomlContent.match(/\[libraries\]([\s\S]*?)(?=\[|$)/);
if (versionsMatch && librariesMatch) {
const versionsBlock = versionsMatch[1];
const librariesBlock = librariesMatch[1];
const libLower = lib.toLowerCase();
// Find alias whose module matches the lib coordinate
const libLineRegex =
/^\s*[\w-]+\s*=\s*\{[^}]*module\s*=\s*["']([^"']+)["'][^}]*\}/gm;
let libMatch;
while ((libMatch = libLineRegex.exec(librariesBlock)) !== null) {
const module = libMatch[1].toLowerCase();
if (module === libLower) {
// Extract version.ref or version from this entry
const entry = libMatch[0];
const versionRefMatch = entry.match(
/version\.ref\s*=\s*["']([^"']+)["']/,
);
if (versionRefMatch) {
const ref = versionRefMatch[1];
const versionLineRegex = new RegExp(
`^\\s*${ref}\\s*=\\s*["']([^"']+)["']`,
"m",
);
const vMatch = versionsBlock.match(versionLineRegex);
if (vMatch) return vMatch[1];
}
const versionMatch = entry.match(
/version\s*=\s*["']([0-9][^"']+)["']/,
);
if (versionMatch) return versionMatch[1];
}
}
}
} catch {
// no TOML catalog — fall through
}
return "unknown";
}
function getInstallCommand(scenarioDir: string): string {
try {
const pkg = JSON.parse(
readFileSync(path.join(scenarioDir, "package.json"), "utf-8"),
);
const deps = Object.keys(pkg.dependencies || {}).filter(
(d) =>
![
"react",
"react-dom",
"react-native",
"vue",
"express",
"express-session",
"dotenv",
"selfsigned",
"@angular/animations",
"@angular/common",
"@angular/compiler",
"@angular/core",
"@angular/forms",
"@angular/platform-browser",
"@angular/platform-browser-dynamic",
"@angular/router",
"rxjs",
"tslib",
"zone.js",
].includes(d),
);
return deps.join(" ");
} catch {
// non-npm projects (e.g. .NET) declare packages elsewhere (.csproj) and install via run_command
return "";
}
}
function extractStepsFromFile(
filePath: string,
lang: string,
scenarioDir: string,
placeholderMap: PlaceholderMap,
): Step[] {
const content = readFileSync(filePath, "utf-8");
const lines = content.split("\n");
const steps: Step[] = [];
let currentStep: number | null = null;
let currentDescription = "";
let currentLines: string[] = [];
let startLine = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const startMatch = line.match(TAG_START);
if (startMatch) {
currentStep = parseInt(startMatch[1], 10);
startLine = i + 1;
currentLines = [];
currentDescription = "";
continue;
}
const endMatch = line.match(TAG_END);
if (endMatch && currentStep !== null) {
const endStep = parseInt(endMatch[1], 10);
if (endStep !== currentStep) {
throw new Error(
`Mismatched snippet end tag in ${filePath}:${i + 1}. Expected @snippet:step${currentStep}:end but found @snippet:step${endStep}:end.`,
);
}
const code = applyPlaceholders(
currentLines.join("\n"),
lang,
placeholderMap,
);
steps.push({
step: currentStep,
description: currentDescription,
code,
file: path.relative(scenarioDir, filePath),
lang,
lines: `${startLine}-${i}`,
});
currentStep = null;
continue;
}
if (currentStep !== null) {
const descMatch = line.match(DESCRIPTION);
if (descMatch) {
currentDescription = descMatch[1].trim();
continue;
}
currentLines.push(line);
}
}
return steps;
}
const EXT_LANG_OVERRIDES: Record<string, string> = {
".java": "java",
".yml": "yaml",
".yaml": "yaml",
".swift": "swift",
".kt": "kotlin",
".kts": "kotlin",
".dart": "dart",
".gradle": "groovy",
".xcconfig": "xcconfig",
};
function langFor(file: string, manifestLang: string): string {
const ext = path.extname(file).toLowerCase();
return EXT_LANG_OVERRIDES[ext] ?? manifestLang;
}
async function main() {
const placeholderMap = getPlaceholderMap();
const manifestFiles = await glob("*/manifest.yaml", { cwd: SAMPLES });
const snippets: Record<string, Record<string, FrameworkSnippet>> = {};
for (const manifestFile of manifestFiles.sort()) {
const frameworkDir = path.join(SAMPLES, path.dirname(manifestFile));
const content = readFileSync(path.join(SAMPLES, manifestFile), "utf-8");
const manifest = load(content) as FrameworkManifest;
const fw = manifest.framework;
const lang = manifest.lang;
for (const scenarioId of Object.keys(manifest.scenarios)) {
const dirName = scenarioId.replace(/^[^_]+_/, "").replaceAll("_", "-");
const scenarioDir = path.join(frameworkDir, dirName);
let hasPkg = false;
const projectMarkers = [
"package.json", // Node / RN
"pom.xml", // Java
"pubspec.yaml", // Flutter
"Package.swift", // iOS (SPM)
"build.gradle.kts", // Android (Kotlin DSL)
"build.gradle", // Android (Groovy DSL)
];
for (const marker of projectMarkers) {
try {
readFileSync(path.join(scenarioDir, marker), "utf-8");
hasPkg = true;
break;
} catch {
// marker not found — try next
}
}
if (!hasPkg) {
// .NET projects keep their .csproj inside src/ — fall back to "any src/ content".
try {
const srcFiles = await glob("src/**/*", { cwd: scenarioDir });
hasPkg = srcFiles.length > 0;
} catch {
// src/ doesn't exist
}
}
if (!hasPkg) {
// iOS Xcode projects (xcodegen-generated or hand-rolled) — any *.xcodeproj/ folder.
try {
const xcodeprojDirs = await glob("*.xcodeproj", { cwd: scenarioDir });
hasPkg = xcodeprojDirs.length > 0;
} catch {
// no .xcodeproj
}
}
if (!hasPkg) {
console.warn(
`Warning: no project found for scenario ${scenarioId} in samples/${fw}/${dirName}/`,
);
continue;
}
const sourceFiles = await glob(
"**/*.{ts,tsx,js,jsx,vue,cs,java,yml,yaml,swift,kt,kts,dart,gradle,xcconfig}",
{
cwd: scenarioDir,
ignore: [
"**/node_modules/**",
"**/.yarn/**",
"**/dist/**",
"**/build/**",
"**/target/**",
"**/bin/**",
"**/obj/**",
"**/.gradle/**",
"**/.dart_tool/**",
"**/.flutter-plugins-dependencies",
"**/Pods/**",
"**/DerivedData/**",
"**/.build/**",
"**/coverage/**",
],
},
);
const allSteps: Step[] = [];
for (const sourceFile of sourceFiles.sort()) {
const fullPath = path.join(scenarioDir, sourceFile);
const fileLang = langFor(sourceFile, lang);
const steps = extractStepsFromFile(
fullPath,
fileLang,
scenarioDir,
placeholderMap,
);
allSteps.push(...steps);
}
if (allSteps.length === 0) {
console.warn(
`Warning: no @snippet tags found in samples/${fw}/${dirName}/`,
);
continue;
}
allSteps.sort((a, b) => a.step - b.step);
if (!snippets[scenarioId]) {
snippets[scenarioId] = {};
}
const runCommand = manifest.scenarios[scenarioId]?.run_command;
const scenarioLib = manifest.scenarios[scenarioId]?.lib ?? manifest.lib;
const scenarioDocsUrl =
manifest.scenarios[scenarioId]?.docs_url ?? manifest.docs_url;
const scenarioCallout = manifest.scenarios[scenarioId]?.callout;
snippets[scenarioId][fw] = {
steps: allSteps,
framework: fw,
lib: scenarioLib,
lib_version: getLibVersion(scenarioDir, frameworkDir, scenarioLib),
docs_url: scenarioDocsUrl,
install: getInstallCommand(scenarioDir),
repo_path: `samples/${path.relative(SAMPLES, scenarioDir)}`,
...(runCommand ? { run_command: runCommand } : {}),
...(scenarioCallout ? { callout: scenarioCallout } : {}),
};
}
}
const outputPath = path.join(ROOT, "snippets.json");
writeFileSync(outputPath, JSON.stringify(snippets, null, 2) + "\n");
const totalSnippets = Object.values(snippets).reduce(
(sum, fw) => sum + Object.keys(fw).length,
0,
);
console.log(
`Wrote ${outputPath} (${Object.keys(snippets).length} scenarios, ${totalSnippets} framework snippets)`,
);
}
main();