|
| 1 | +#!/usr/bin/env node |
| 2 | +import fs from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const UNIT_TEST_CONDITION = 'AGENT_DEVICE_RUNNER_UNIT_TESTS'; |
| 7 | +const SOURCE_DIR = path.join('apple-runner'); |
| 8 | +const OUTPUT_DIR = path.join('dist', 'apple-runner'); |
| 9 | +const SKIPPED_DIR_NAMES = new Set(['.build', '.swiftpm', 'xcuserdata']); |
| 10 | +const SKIPPED_ROOT_FILES = new Set(['README.md', 'RUNNER_PROTOCOL.md']); |
| 11 | + |
| 12 | +export function packageAppleRunnerSource(options = {}) { |
| 13 | + const root = path.resolve(options.root ?? process.cwd()); |
| 14 | + const sourceRoot = path.join(root, SOURCE_DIR); |
| 15 | + const outputRoot = path.join(root, OUTPUT_DIR); |
| 16 | + if (!fs.existsSync(sourceRoot)) { |
| 17 | + throw new Error(`Apple runner source not found at ${sourceRoot}`); |
| 18 | + } |
| 19 | + |
| 20 | + fs.rmSync(outputRoot, { recursive: true, force: true }); |
| 21 | + const summary = { |
| 22 | + outputRoot, |
| 23 | + copiedFiles: 0, |
| 24 | + strippedFiles: 0, |
| 25 | + strippedBlocks: 0, |
| 26 | + }; |
| 27 | + |
| 28 | + copyDirectory(sourceRoot, outputRoot, '', summary); |
| 29 | + return summary; |
| 30 | +} |
| 31 | + |
| 32 | +export function stripRunnerUnitTestBlocks(source, filePath = '<swift source>') { |
| 33 | + const lines = source.match(/[^\n]*\n|[^\n]+/g) ?? []; |
| 34 | + const state = { |
| 35 | + output: [], |
| 36 | + strippedBlocks: 0, |
| 37 | + skippedDepth: 0, |
| 38 | + }; |
| 39 | + |
| 40 | + for (const line of lines) { |
| 41 | + consumeSwiftLine(state, line); |
| 42 | + } |
| 43 | + |
| 44 | + if (state.skippedDepth !== 0) { |
| 45 | + throw new Error(`Unterminated ${UNIT_TEST_CONDITION} block in ${filePath}`); |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + contents: state.output.join(''), |
| 50 | + strippedBlocks: state.strippedBlocks, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function consumeSwiftLine(state, line) { |
| 55 | + if (state.skippedDepth > 0) { |
| 56 | + consumeSkippedConditionalLine(state, line); |
| 57 | + return; |
| 58 | + } |
| 59 | + if (isRunnerUnitTestBlockStart(line)) { |
| 60 | + state.skippedDepth = 1; |
| 61 | + state.strippedBlocks += 1; |
| 62 | + return; |
| 63 | + } |
| 64 | + state.output.push(line); |
| 65 | +} |
| 66 | + |
| 67 | +function consumeSkippedConditionalLine(state, line) { |
| 68 | + if (isConditionalStart(line)) { |
| 69 | + state.skippedDepth += 1; |
| 70 | + } |
| 71 | + if (isConditionalEnd(line)) { |
| 72 | + state.skippedDepth -= 1; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function copyDirectory(sourceDir, outputDir, relativeDir, summary) { |
| 77 | + fs.mkdirSync(outputDir, { recursive: true }); |
| 78 | + const entries = fs.readdirSync(sourceDir, { withFileTypes: true }); |
| 79 | + |
| 80 | + for (const entry of entries) { |
| 81 | + copyDirectoryEntry(entry, sourceDir, outputDir, relativeDir, summary); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +function copyDirectoryEntry(entry, sourceDir, outputDir, relativeDir, summary) { |
| 86 | + const relativePath = path.join(relativeDir, entry.name); |
| 87 | + if (shouldSkipEntry(entry, relativePath)) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const sourcePath = path.join(sourceDir, entry.name); |
| 92 | + const outputPath = path.join(outputDir, entry.name); |
| 93 | + if (entry.isDirectory()) { |
| 94 | + copyDirectory(sourcePath, outputPath, relativePath, summary); |
| 95 | + return; |
| 96 | + } |
| 97 | + if (entry.isFile()) { |
| 98 | + copyFile(sourcePath, outputPath, summary); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +function copyFile(sourcePath, outputPath, summary) { |
| 103 | + if (path.extname(sourcePath) !== '.swift') { |
| 104 | + fs.copyFileSync(sourcePath, outputPath); |
| 105 | + summary.copiedFiles += 1; |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const source = fs.readFileSync(sourcePath, 'utf8'); |
| 110 | + const stripped = stripRunnerUnitTestBlocks(source, sourcePath); |
| 111 | + fs.writeFileSync(outputPath, stripped.contents); |
| 112 | + summary.copiedFiles += 1; |
| 113 | + if (stripped.strippedBlocks > 0) { |
| 114 | + summary.strippedFiles += 1; |
| 115 | + summary.strippedBlocks += stripped.strippedBlocks; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +function shouldSkipEntry(entry, relativePath) { |
| 120 | + return shouldSkipDirectory(entry) || shouldSkipFile(entry, relativePath); |
| 121 | +} |
| 122 | + |
| 123 | +function shouldSkipDirectory(entry) { |
| 124 | + return entry.isDirectory() && SKIPPED_DIR_NAMES.has(entry.name); |
| 125 | +} |
| 126 | + |
| 127 | +function shouldSkipFile(entry, relativePath) { |
| 128 | + return entry.isFile() && (isXcodeUserStateFile(entry) || isSkippedRootFile(entry, relativePath)); |
| 129 | +} |
| 130 | + |
| 131 | +function isXcodeUserStateFile(entry) { |
| 132 | + return entry.name.endsWith('.xcuserstate'); |
| 133 | +} |
| 134 | + |
| 135 | +function isSkippedRootFile(entry, relativePath) { |
| 136 | + return !relativePath.includes(path.sep) && SKIPPED_ROOT_FILES.has(entry.name); |
| 137 | +} |
| 138 | + |
| 139 | +function isRunnerUnitTestBlockStart(line) { |
| 140 | + return new RegExp(`^\\s*#if\\s+${UNIT_TEST_CONDITION}(?:\\b|$)`).test(line); |
| 141 | +} |
| 142 | + |
| 143 | +function isConditionalStart(line) { |
| 144 | + return /^\s*#if\b/.test(line); |
| 145 | +} |
| 146 | + |
| 147 | +function isConditionalEnd(line) { |
| 148 | + return /^\s*#endif\b/.test(line); |
| 149 | +} |
| 150 | + |
| 151 | +function parseArgs(argv) { |
| 152 | + const parsed = { root: process.cwd(), quiet: false }; |
| 153 | + let index = 0; |
| 154 | + while (index < argv.length) { |
| 155 | + index = parseArg(argv, index, parsed); |
| 156 | + } |
| 157 | + return parsed; |
| 158 | +} |
| 159 | + |
| 160 | +function parseArg(argv, index, parsed) { |
| 161 | + const arg = argv[index]; |
| 162 | + if (arg === '--quiet') { |
| 163 | + parsed.quiet = true; |
| 164 | + return index + 1; |
| 165 | + } |
| 166 | + if (arg === '--root') { |
| 167 | + return parseRootArg(argv, index, parsed); |
| 168 | + } |
| 169 | + throw new Error(`Unknown argument: ${arg}`); |
| 170 | +} |
| 171 | + |
| 172 | +function parseRootArg(argv, index, parsed) { |
| 173 | + const value = argv[index + 1]; |
| 174 | + if (!value || value.startsWith('--')) { |
| 175 | + throw new Error('--root requires a path'); |
| 176 | + } |
| 177 | + parsed.root = value; |
| 178 | + return index + 2; |
| 179 | +} |
| 180 | + |
| 181 | +function isMainModule() { |
| 182 | + return process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url); |
| 183 | +} |
| 184 | + |
| 185 | +if (isMainModule()) { |
| 186 | + const options = parseArgs(process.argv.slice(2)); |
| 187 | + const summary = packageAppleRunnerSource(options); |
| 188 | + if (!options.quiet) { |
| 189 | + const relativeOutput = path.relative(path.resolve(options.root), summary.outputRoot); |
| 190 | + console.log( |
| 191 | + `Packaged Apple runner source at ${relativeOutput} ` + |
| 192 | + `(${summary.copiedFiles} files, stripped ${summary.strippedBlocks} unit-test blocks).`, |
| 193 | + ); |
| 194 | + } |
| 195 | +} |
0 commit comments