Skip to content

Commit 42015fe

Browse files
authored
build: support structured toml patches to reduce build breakage (microsoft#184201)
Currently the Cargo.toml patches are git patches, but these can break easily when surrounding lines are changed. Instead this lets us make "Cargo.patch.toml" which gets a structured merge.
1 parent 5504c4b commit 42015fe

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

build/azure-pipelines/distro/apply-cli-patches.js

Lines changed: 29 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/azure-pipelines/distro/apply-cli-patches.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import * as fs from 'fs';
77
import * as cp from 'child_process';
8+
import * as toml from '@iarna/toml';
89

910
function log(...args: any[]): void {
1011
console.log(`[${new Date().toLocaleTimeString('en', { hour12: false })}]`, '[distro]', ...args);
@@ -13,8 +14,32 @@ function log(...args: any[]): void {
1314
log(`Applying CLI patches...`);
1415

1516
const basePath = `.build/distro/cli-patches`;
17+
const patchTomlSuffix = '.patch.toml';
18+
19+
function deepMerge(target: any, source: any): any {
20+
for (const [key, value] of Object.entries(source)) {
21+
if (value && typeof value === 'object' && !Array.isArray(value)) {
22+
if (!target.hasOwnProperty(key)) {
23+
target[key] = value;
24+
} else {
25+
deepMerge(target[key], value);
26+
}
27+
} else {
28+
target[key] = value;
29+
}
30+
}
31+
return target;
32+
}
1633

1734
for (const patch of fs.readdirSync(basePath)) {
18-
cp.execSync(`git apply --ignore-whitespace --ignore-space-change ${basePath}/${patch}`, { stdio: 'inherit' });
35+
if (patch.endsWith(patchTomlSuffix)) {
36+
// this does not support nested filepaths, but that's fine for now...
37+
const originalPath = `cli/${patch.slice(0, -patchTomlSuffix.length)}.toml`;
38+
const contents = toml.parse(fs.readFileSync(originalPath, 'utf8'));
39+
deepMerge(contents, toml.parse(fs.readFileSync(`${basePath}/${patch}`, 'utf8')));
40+
fs.writeFileSync(originalPath, toml.stringify(contents));
41+
} else {
42+
cp.execSync(`git apply --ignore-whitespace --ignore-space-change ${basePath}/${patch}`, { stdio: 'inherit' });
43+
}
1944
log('Applied CLI patch:', patch, '✔︎');
2045
}

build/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@azure/identity": "^3.1.3",
88
"@azure/storage-blob": "^12.13.0",
99
"@electron/get": "^1.12.4",
10+
"@iarna/toml": "^2.2.5",
1011
"@types/ansi-colors": "^3.2.0",
1112
"@types/byline": "^4.2.32",
1213
"@types/cssnano": "^4.0.0",
@@ -23,6 +24,7 @@
2324
"@types/gulp-postcss": "^8.0.0",
2425
"@types/gulp-rename": "^0.0.33",
2526
"@types/gulp-sourcemaps": "^0.0.32",
27+
"@types/iarna__toml": "^2.0.2",
2628
"@types/mime": "0.0.29",
2729
"@types/minimatch": "^3.0.3",
2830
"@types/minimist": "^1.2.1",

build/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@
339339
resolved "https://registry.yarnpkg.com/@esbuild/win32-x64/-/win32-x64-0.17.14.tgz#e81fb49de05fed91bf74251c9ca0343f4fc77d31"
340340
integrity sha512-gPQmsi2DKTaEgG14hc3CHXHp62k8g6qr0Pas+I4lUxRMugGSATh/Bi8Dgusoz9IQ0IfdrvLpco6kujEIBoaogA==
341341

342+
"@iarna/toml@^2.2.5":
343+
version "2.2.5"
344+
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
345+
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==
346+
342347
"@malept/cross-spawn-promise@^1.1.0":
343348
version "1.1.1"
344349
resolved "https://registry.yarnpkg.com/@malept/cross-spawn-promise/-/cross-spawn-promise-1.1.1.tgz#504af200af6b98e198bce768bc1730c6936ae01d"
@@ -501,6 +506,13 @@
501506
"@types/undertaker" "*"
502507
"@types/vinyl-fs" "*"
503508

509+
"@types/iarna__toml@^2.0.2":
510+
version "2.0.2"
511+
resolved "https://registry.yarnpkg.com/@types/iarna__toml/-/iarna__toml-2.0.2.tgz#2e61b079e50760b477bc70e4df1fe5b633ef6c63"
512+
integrity sha512-Q3obxKhBLVVbEQ8zsAmsQVobAAZhi8dFFFjF0q5xKXiaHvH8IkSxcbM27e46M9feUMieR03SPpmp5CtaNzpdBg==
513+
dependencies:
514+
"@types/node" "*"
515+
504516
"@types/js-beautify@*":
505517
version "1.8.0"
506518
resolved "https://registry.yarnpkg.com/@types/js-beautify/-/js-beautify-1.8.0.tgz#0369d3d0e1f35a6aec07cb4da2ee2bcda111367c"

0 commit comments

Comments
 (0)