Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ permissions:
contents: read

jobs:
# Decides whether a pull request can affect the iOS build, so build-ios (macOS: scarce, slow
# runners) runs only when it can. Conservative: see scripts/ios-affected.ts. Every push to main
# builds iOS, so main's status, a run cancelled by a later push, and the release gate (CI passed
# for the tagged commit, which is on main) never rest on a skipped build.
changes:
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
ios: ${{ steps.ios.outputs.ios }}
steps:
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: .nvmrc

- name: Can the change affect the iOS build?
id: ios
env:
EVENT: ${{ github.event_name }}
PR_BASE: ${{ github.event.pull_request.base.sha }}
PR_HEAD: ${{ github.event.pull_request.head.sha }}
run: |
if [ "$EVENT" = pull_request ]; then
node scripts/ios-affected.ts "$PR_BASE" "$PR_HEAD"
else
echo "ios=true" | tee -a "$GITHUB_OUTPUT" # push to main, merge queue: always build
fi

lint:
runs-on: ubuntu-latest

Expand Down Expand Up @@ -343,6 +377,8 @@ jobs:
yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --concurrency=1

build-ios:
needs: changes
if: needs.changes.outputs.ios == 'true'
runs-on: macos-latest

env:
Expand Down
129 changes: 129 additions & 0 deletions scripts/__tests__/ios-affected.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { test } from 'node:test';
import { changedFiles, iosAffected } from '../ios-affected.ts';
import { ROOT } from '../lib.ts';

const git = (cwd: string, ...args: string[]) =>
execFileSync('git', args, { cwd, encoding: 'utf8' }).trim();

/** A throwaway repository: one commit per `{path: text}` map; returns each commit's SHA. */
function scratchRepo(...commits: Record<string, string | null>[]): {
dir: string;
shas: string[];
} {
const dir = mkdtempSync(join(tmpdir(), 'ios-affected-'));
git(dir, 'init', '-q', '-b', 'main');
git(dir, 'config', 'user.email', 'test@example.com');
git(dir, 'config', 'user.name', 'test');
git(dir, 'config', 'commit.gpgsign', 'false');
const shas: string[] = [];
for (const files of commits) {
for (const [path, text] of Object.entries(files)) {
if (text === null) {
git(dir, 'rm', '-q', path);
} else {
mkdirSync(dirname(join(dir, path)), { recursive: true });
writeFileSync(join(dir, path), text);
git(dir, 'add', path);
}
}
git(dir, 'commit', '-q', '--no-verify', '-m', 'c');
shas.push(git(dir, 'rev-parse', 'HEAD'));
}
return { dir, shas };
}

await test('only documentation, Android and tooling changes skip the iOS build', () => {
assert.equal(
iosAffected([
'README.md',
'CONTRIBUTING.md',
'docs/preview.jpg',
'android/src/main/java/com/remotecompose/RemoteComposeViewManager.kt',
'example/android/app/build.gradle',
'example-expo/android/app/build.gradle',
'tools/remote-compose-json/build.gradle.kts',
'e2e/flows/host-values.yaml',
'.github/dependabot.yml',
]),
false
);
});

await test('any file the iOS build may read runs it, including near misses', () => {
for (const path of [
'ios/HostValue.swift',
'ios/RemoteComposeView.mm',
'RemoteCompose.podspec',
'src/index.tsx',
'package.json',
'yarn.lock',
'example/ios/Podfile.lock',
'example-expo/ios/Podfile', // only example-expo/android is irrelevant
'example/README.md', // a turbo build:ios input; only top-level Markdown is skipped
'ios/Tests/NOTES.md',
'example/src/App.tsx',
'example-shared/src/screens/Host.tsx',
'.github/workflows/ci.yml', // a CI change must run the job it changes
'.github/actions/setup/action.yml',
'app.plugin.js',
'turbo.json',
]) {
assert.equal(iosAffected(['README.md', path]), true, path);
}
});

await test('an empty change runs the build rather than skipping it', () => {
assert.equal(iosAffected([]), true);
});

await test('no tracked file that turbo counts as a build:ios input is ever skipped', () => {
// turbo.json build:ios: the example apps' own files except android/, plus these root paths.
const turboInput =
/^(example|example-expo)\/(?!android\/)|^(package\.json|yarn\.lock|app\.plugin\.js|react-native\.config\.js|[^/]+\.podspec)$|^(src|plugin|example-shared|ios)\//;
const tracked = git(ROOT, 'ls-files').split('\n');
const wronglySkipped = tracked.filter(
(path) => turboInput.test(path) && !iosAffected([path])
);
assert.deepEqual(wronglySkipped, []);
});

await test('a real range: docs only skips, a moved iOS file and an iOS edit build', () => {
const { dir, shas } = scratchRepo(
{ 'README.md': 'a', 'ios/fixture.json': '{}' },
{ 'README.md': 'b' }, // docs only
{ 'ios/fixture.json': null, 'docs/fixture.json': '{}' }, // moved out of ios/
{ 'ios/HostValue.swift': 'let x = 1' } // iOS edit
);
const [first, docs, moved, edit] = shas as [string, string, string, string];
assert.deepEqual(changedFiles(first, docs, dir), ['README.md']);
assert.equal(iosAffected(changedFiles(first, docs, dir) ?? []), false);
const movedFiles = changedFiles(docs, moved, dir) ?? [];
assert.ok(movedFiles.includes('ios/fixture.json'), movedFiles.join(','));
assert.equal(iosAffected(movedFiles), true);
assert.equal(iosAffected(changedFiles(moved, edit, dir) ?? []), true);
assert.equal(changedFiles('0'.repeat(40), edit, dir), null);
});

await test('the CLI writes its decision to GITHUB_OUTPUT', () => {
const { dir, shas } = scratchRepo({ 'README.md': 'a' }, { 'README.md': 'b' });
const output = join(dir, 'github-output');
writeFileSync(output, '');
const run = (...args: string[]) =>
execFileSync(
process.execPath,
[join(ROOT, 'scripts', 'ios-affected.ts'), ...args],
{
cwd: dir,
encoding: 'utf8',
env: { ...process.env, GITHUB_OUTPUT: output },
}
);
assert.match(run(shas[0] ?? '', shas[1] ?? ''), /^ios=false \(1 files\)/);
assert.match(run('0'.repeat(40), 'HEAD'), /^ios=true \(range/);
assert.equal(readFileSync(output, 'utf8'), 'ios=false\nios=true\n');
});
75 changes: 75 additions & 0 deletions scripts/ios-affected.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Whether a pull request can affect the iOS build, so CI can skip the macOS job (scarce, slow
* runners) when it cannot. Conservative: the job is skipped only when EVERY changed file matches a
* path that the iOS build provably does not read; anything else, an empty list, or an unknown range
* builds. Pushes to main always build (see ci.yml), so main and every release are always proven.
*
* node scripts/ios-affected.ts <base> <head> # prints ios=true|false, also to $GITHUB_OUTPUT
*
* The iOS build reads what turbo.json's `build:ios` inputs list; a test checks that no tracked file
* those inputs cover is ever treated as irrelevant here.
*/
import { execFileSync } from 'node:child_process';
import { appendFileSync } from 'node:fs';

/** Paths the iOS example build and the Swift unit tests never read. */
export const IOS_IRRELEVANT: readonly RegExp[] = [
/^[^/]+\.md$/, // top-level documentation (README, CONTRIBUTING, ...)
/^docs\//,
/^android\//, // the library's Android module
/^example\/android\//,
/^example-expo\/android\//,
/^tools\//, // the Kotlin JSON -> .rc converter; generated documents are committed separately
/^e2e\//, // Maestro flows, run on devices, not in CI's iOS build
/^detekt\.yml$/,
/^LICENSE$/,
/^\.github\/dependabot\.yml$/,
/^\.github\/ISSUE_TEMPLATE\//,
];

/** True unless every path is known not to affect the iOS build. An empty list runs the build. */
export function iosAffected(paths: readonly string[]): boolean {
if (paths.length === 0) return true;
return paths.some(
(path) => !IOS_IRRELEVANT.some((pattern) => pattern.test(path))
);
}

/**
* The files changed between the merge base of `base` and `head`, with both sides of a rename
* (`--no-renames`), so moving an iOS file into an ignored folder still counts. A range git cannot
* resolve (a new branch's all-zero `before`, history not fetched) means "run the build".
*/
export function changedFiles(
base: string,
head: string,
cwd?: string
): string[] | null {
if (!base || !head || /^0+$/.test(base)) return null;
try {
const out = execFileSync(
'git',
['diff', '--name-only', '--no-renames', `${base}...${head}`],
{ cwd, encoding: 'utf8' }
);
return out.split('\n').filter(Boolean);
} catch {
return null;
}
}

if (import.meta.main) {
const [base = '', head = ''] = process.argv.slice(2);
const files = changedFiles(base, head);
const affected = files === null ? true : iosAffected(files);
const line = `ios=${String(affected)}`;
console.log(
files === null
? `${line} (range ${base}...${head} unknown)`
: `${line} (${files.length} files)`
);
// unknown: process.env's values are `any` in this repository's TypeScript program.
const output: unknown = process.env.GITHUB_OUTPUT;
if (typeof output === 'string' && output !== '')
appendFileSync(output, `${line}\n`);
}