Skip to content

Commit d70ef60

Browse files
committed
show code diff locally
1 parent 997cabd commit d70ef60

3 files changed

Lines changed: 89 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,4 @@ packages/samples/test/output/azure/templates-contoso/setup.*
225225
# python emitter
226226
packages/typespec-python/tests/generated/**
227227
packages/typespec-python/alpha
228+
.python-code-diff
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* eslint-disable no-console */
2+
/**
3+
* Build a side-by-side diff workspace comparing the locally generated Python
4+
* SDKs against the committed baseline in the azure-sdk-for-python repo.
5+
*
6+
* Steps:
7+
* 1. (Re)create `<repo-root>/.python-code-diff`, clearing any existing content.
8+
* 2. Sparse-checkout `eng/tools/azure-sdk-tools/emitter/generated` from the
9+
* `typespec-python-generated-tests` branch of Azure/azure-sdk-for-python.
10+
* 3. Remove the checked-out `azure` and `unbranded` baselines.
11+
* 4. Copy this package's freshly generated `tests/generated/azure` and
12+
* `tests/generated/unbranded` into their place.
13+
* 5. Open the diff folder in VS Code (or print its path if `code` is missing).
14+
*
15+
* Usage:
16+
* tsx eng/scripts/show-diff.ts
17+
*/
18+
import { execSync } from "child_process";
19+
import { cpSync, existsSync, rmSync } from "fs";
20+
import { dirname, join } from "path";
21+
import pc from "picocolors";
22+
import { fileURLToPath } from "url";
23+
24+
const here = dirname(fileURLToPath(import.meta.url));
25+
// eng/scripts/show-diff.ts -> package root is two levels up
26+
const packageRoot = join(here, "..", "..");
27+
// packageRoot == <repo-root>/packages/typespec-python, so the repo root is two
28+
// levels up from packageRoot.
29+
const repoRoot = join(packageRoot, "..", "..");
30+
31+
const REPO_URL = "https://github.com/Azure/azure-sdk-for-python.git";
32+
const BRANCH = "typespec-python-generated-tests";
33+
const SPARSE_PATH = "eng/tools/azure-sdk-tools/emitter/generated";
34+
35+
const diffDir = join(repoRoot, ".python-code-diff");
36+
const generatedDir = join(diffDir, ...SPARSE_PATH.split("/"));
37+
38+
const flavors = ["azure", "unbranded"] as const;
39+
40+
function run(command: string, cwd: string): void {
41+
console.log(pc.gray(`$ ${command}`));
42+
execSync(command, { cwd, stdio: "inherit" });
43+
}
44+
45+
// 1. (Re)create the diff folder, clearing any existing content.
46+
if (existsSync(diffDir)) {
47+
console.log(pc.yellow(`Clearing existing ${diffDir}`));
48+
rmSync(diffDir, { recursive: true, force: true });
49+
}
50+
51+
// 2. Sparse-checkout the baseline generated folder.
52+
console.log(pc.cyan(`Cloning ${REPO_URL} (${BRANCH})...`));
53+
run(
54+
`git clone --no-checkout --depth 1 --filter=blob:none --branch ${BRANCH} ${REPO_URL} "${diffDir}"`,
55+
repoRoot,
56+
);
57+
run("git sparse-checkout init --cone", diffDir);
58+
run(`git sparse-checkout set ${SPARSE_PATH}`, diffDir);
59+
run("git checkout", diffDir);
60+
61+
// 3. Remove the checked-out azure / unbranded baselines.
62+
for (const flavor of flavors) {
63+
const target = join(generatedDir, flavor);
64+
if (existsSync(target)) {
65+
console.log(pc.yellow(`Removing baseline ${target}`));
66+
rmSync(target, { recursive: true, force: true });
67+
}
68+
}
69+
70+
// 4. Copy locally generated azure / unbranded into place.
71+
for (const flavor of flavors) {
72+
const source = join(packageRoot, "tests", "generated", flavor);
73+
const target = join(generatedDir, flavor);
74+
if (!existsSync(source)) {
75+
console.warn(pc.yellow(`Skipping ${flavor}: ${source} does not exist`));
76+
continue;
77+
}
78+
console.log(pc.cyan(`Copying ${source} -> ${target}`));
79+
cpSync(source, target, { recursive: true });
80+
}
81+
82+
// 5. Open the diff folder in VS Code, or print its path on failure.
83+
try {
84+
execSync(`code "${diffDir}"`, { stdio: "inherit" });
85+
} catch {
86+
console.log(pc.green(`see code diff in ${diffDir}`));
87+
}

packages/typespec-python/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"format:extra:check": "tsx ./eng/scripts/ci/format.ts --python --check",
4040
"regenerate": "tsx ./eng/scripts/ci/regenerate.ts",
4141
"sync": "tsx ./eng/scripts/sync.ts",
42+
"show-diff": "pnpm regenerate && tsx ./eng/scripts/show-diff.ts",
4243
"sync:check": "tsx ./eng/scripts/sync.ts --check",
4344
"test:python:e2e": "tsx ./eng/scripts/ci/run-tests.ts",
4445
"regen-docs": "pnpm run build && tspd doc . --enable-experimental --output-dir ./website/src/content/docs/docs/emitters/clients/typespec-python/reference --skip-js"

0 commit comments

Comments
 (0)