Skip to content

Commit cedaced

Browse files
committed
install splitish-lite
1 parent fa993be commit cedaced

2 files changed

Lines changed: 77 additions & 10 deletions

File tree

‎.github/scripts/monorepo-split-plan.php‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@
99
'head::',
1010
'origin::',
1111
'tag::',
12+
'calculate-splits',
1213
'help',
1314
]);
1415

1516
if (isset($options['help'])) {
1617
fwrite(STDOUT, <<<'HELP'
1718
Usage:
18-
php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>]
19+
php .github/scripts/monorepo-split-plan.php [--config=.github/monorepo-split.json] [--base=<git-ref>] [--head=<git-ref>] [--origin=<git-ref>] [--tag=<tag>] [--calculate-splits]
1920
2021
Validates the monorepo split map and prints the read-only repository split plan.
21-
This is a dry-run planner only. It prints splitsh-lite commands but never runs
22-
them and never pushes.
22+
This is a dry-run planner only. By default it prints splitsh-lite commands but
23+
does not run them. With --calculate-splits it runs splitsh-lite and prints split
24+
SHAs, but still never pushes.
2325

2426
HELP);
2527
exit(0);
@@ -32,6 +34,7 @@
3234
$head = isset($options['head']) ? (string) $options['head'] : null;
3335
$origin = isset($options['origin']) ? (string) $options['origin'] : 'HEAD';
3436
$tag = isset($options['tag']) ? (string) $options['tag'] : null;
37+
$calculateSplits = isset($options['calculate-splits']);
3538

3639
$errors = [];
3740
$warnings = [];
@@ -149,6 +152,10 @@
149152
$errors[] = 'Origin cannot be empty when --origin is provided.';
150153
}
151154

155+
if ($calculateSplits && !commandExists('splitsh-lite')) {
156+
$errors[] = 'splitsh-lite is required when --calculate-splits is provided.';
157+
}
158+
152159
if (null !== $base || null !== $head) {
153160
if (null === $base || null === $head) {
154161
$errors[] = 'Both --base and --head are required when checking changed paths.';
@@ -158,12 +165,17 @@
158165
}
159166

160167
if ([] !== $errors) {
161-
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, [], [], $origin, $tag, $root);
168+
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, [], [], $origin, $tag, $root, $calculateSplits, $errors);
162169
fail($errors);
163170
}
164171

165172
[$plannedPackages, $skippedPackages] = planPackages($packages, $changedFiles, null !== $tag);
166-
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, $plannedPackages, $skippedPackages, $origin, $tag, $root);
173+
$splitErrors = [];
174+
printSummary($configFile, $packages, $statusCounts, $warnings, $changedFiles, $plannedPackages, $skippedPackages, $origin, $tag, $root, $calculateSplits, $splitErrors);
175+
176+
if ([] !== $splitErrors) {
177+
fail($splitErrors);
178+
}
167179

168180
exit(0);
169181

@@ -307,7 +319,7 @@ function packageChanged(string $path, array $changedFiles): bool
307319
* @param list<array<string, mixed>> $plannedPackages
308320
* @param list<array{name: string, reason: string}> $skippedPackages
309321
*/
310-
function printSummary(string $configFile, array $packages, array $statusCounts, array $warnings, array $changedFiles, array $plannedPackages, array $skippedPackages, string $origin, ?string $tag, string $root): void
322+
function printSummary(string $configFile, array $packages, array $statusCounts, array $warnings, array $changedFiles, array $plannedPackages, array $skippedPackages, string $origin, ?string $tag, string $root, bool $calculateSplits, array &$splitErrors): void
311323
{
312324
fwrite(STDOUT, 'Monorepo split dry-run plan' . PHP_EOL);
313325
fwrite(STDOUT, 'Config: ' . $configFile . PHP_EOL);
@@ -352,7 +364,15 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
352364
}
353365

354366
fwrite(STDOUT, $line . PHP_EOL);
355-
fwrite(STDOUT, ' split: ' . splitshCommand((string) $package['path'], $origin, $root) . PHP_EOL);
367+
$command = splitshCommand((string) $package['path'], $origin, $root);
368+
fwrite(STDOUT, ' split: ' . $command . PHP_EOL);
369+
370+
if ($calculateSplits) {
371+
$sha = splitshSha((string) $package['path'], $origin, $root, $splitErrors);
372+
if (null !== $sha) {
373+
fwrite(STDOUT, ' sha: ' . $sha . PHP_EOL);
374+
}
375+
}
356376
}
357377

358378
$skipCounts = [];
@@ -369,6 +389,12 @@ function printSummary(string $configFile, array $packages, array $statusCounts,
369389
}
370390
}
371391

392+
if ($calculateSplits) {
393+
fwrite(STDOUT, PHP_EOL . 'Dry run only. splitsh-lite calculated split SHAs, but nothing was pushed.' . PHP_EOL);
394+
395+
return;
396+
}
397+
372398
fwrite(STDOUT, PHP_EOL . 'Dry run only. splitsh-lite commands were printed but not run, and nothing was pushed.' . PHP_EOL);
373399
}
374400

@@ -381,3 +407,30 @@ function splitshCommand(string $path, string $origin, string $root): string
381407
escapeshellarg($root),
382408
);
383409
}
410+
411+
function splitshSha(string $path, string $origin, string $root, array &$errors): ?string
412+
{
413+
$command = splitshCommand($path, $origin, $root) . ' 2>&1';
414+
exec($command, $output, $exitCode);
415+
if (0 !== $exitCode) {
416+
$errors[] = sprintf('splitsh-lite failed for %s: %s', $path, implode(PHP_EOL, $output));
417+
418+
return null;
419+
}
420+
421+
$sha = trim((string) end($output));
422+
if (!preg_match('/^[a-f0-9]{40}$/', $sha)) {
423+
$errors[] = sprintf('splitsh-lite returned an invalid SHA for %s: %s', $path, $sha);
424+
425+
return null;
426+
}
427+
428+
return $sha;
429+
}
430+
431+
function commandExists(string $command): bool
432+
{
433+
exec(sprintf('command -v %s >/dev/null 2>&1', escapeshellarg($command)), $output, $exitCode);
434+
435+
return 0 === $exitCode;
436+
}

‎.github/workflows/monorepo-split-plan.yml‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ jobs:
3838
with:
3939
php-version: '8.3'
4040
coverage: none
41+
- uses: actions/setup-go@v5
42+
with:
43+
go-version: '1.22'
44+
- name: Install splitsh-lite
45+
run: |
46+
sudo apt-get update
47+
sudo apt-get install -y libgit2-dev pkg-config
48+
go install github.com/splitsh/lite@master
49+
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
50+
export PATH="$(go env GOPATH)/bin:$PATH"
51+
splitsh-lite --help
4152
- name: Validate split config
4253
run: php .github/scripts/monorepo-split-plan.php
4354
- name: Plan pull request package splits
@@ -46,17 +57,20 @@ jobs:
4657
php .github/scripts/monorepo-split-plan.php \
4758
--base='${{ github.event.pull_request.base.sha }}' \
4859
--head='${{ github.event.pull_request.head.sha }}' \
49-
--origin='${{ github.event.pull_request.head.sha }}'
60+
--origin='${{ github.event.pull_request.head.sha }}' \
61+
--calculate-splits
5062
- name: Plan mainline package splits
5163
if: github.event_name == 'push' && !startsWith(github.ref, 'refs/tags/')
5264
run: |
5365
php .github/scripts/monorepo-split-plan.php \
5466
--base='${{ github.event.before }}' \
5567
--head='${{ github.sha }}' \
56-
--origin='${{ github.sha }}'
68+
--origin='${{ github.sha }}' \
69+
--calculate-splits
5770
- name: Plan release tag package splits
5871
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
5972
run: |
6073
php .github/scripts/monorepo-split-plan.php \
6174
--tag='${{ github.ref_name }}' \
62-
--origin='${{ github.ref }}'
75+
--origin='${{ github.ref }}' \
76+
--calculate-splits

0 commit comments

Comments
 (0)