Add native mobile review diff rendering and static checks
- Add iOS review diff module, parser, and highlighter support - Wire mobile native lint tooling into CI and local docs - Include fixtures and tests for the new diff pipeline
This commit is contained in:
1 parent
da56bd50d9
commit
37908166ed
26 files changed
+4836
-192
No files matched your search
@@ -0,0 +1,208 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { Console, Data, Effect, FileSystem, Logger, Path } from "effect";
|
||||
import { Command } from "effect/unstable/cli";
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
interface NativeStaticTool {
|
||||
readonly command: string;
|
||||
readonly installHint: string;
|
||||
}
|
||||
|
||||
class NativeStaticCheckError extends Data.TaggedError("NativeStaticCheckError")<{
|
||||
readonly message: string;
|
||||
}> {}
|
||||
|
||||
const tools = [
|
||||
{
|
||||
command: "swiftlint",
|
||||
installHint: "brew install swiftlint",
|
||||
},
|
||||
{
|
||||
command: "ktlint",
|
||||
installHint: "brew install ktlint",
|
||||
},
|
||||
{
|
||||
command: "detekt",
|
||||
installHint: "brew install detekt",
|
||||
},
|
||||
] as const satisfies ReadonlyArray<NativeStaticTool>;
|
||||
|
||||
const sourceExtensions = new Set([".swift", ".kt", ".kts"]);
|
||||
const excludedDirectories = new Set([
|
||||
".expo",
|
||||
".git",
|
||||
"build",
|
||||
"DerivedData",
|
||||
"node_modules",
|
||||
"Pods",
|
||||
"Vendor",
|
||||
]);
|
||||
|
||||
const appRoot = Effect.service(Path.Path).pipe(
|
||||
Effect.flatMap((path) => path.fromFileUrl(new URL("../apps/mobile", import.meta.url))),
|
||||
);
|
||||
|
||||
const commandOutputOptions = {
|
||||
stdout: "inherit",
|
||||
stderr: "inherit",
|
||||
} as const;
|
||||
|
||||
const commandExists = Effect.fn("commandExists")(function* (command: string) {
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const lookupCommand =
|
||||
process.platform === "win32"
|
||||
? ChildProcess.make("where", [command], {
|
||||
stdout: "ignore",
|
||||
stderr: "ignore",
|
||||
})
|
||||
: ChildProcess.make("/bin/sh", ["-c", `command -v ${command}`], {
|
||||
stdout: "ignore",
|
||||
stderr: "ignore",
|
||||
});
|
||||
|
||||
return yield* spawner.spawn(lookupCommand).pipe(
|
||||
Effect.flatMap((child) => child.exitCode),
|
||||
Effect.map((exitCode) => exitCode === 0),
|
||||
Effect.catch(() => Effect.succeed(false)),
|
||||
);
|
||||
});
|
||||
|
||||
const warnMissingTool = (tool: NativeStaticTool, checkName: string) =>
|
||||
Effect.logWarning(
|
||||
`${tool.command} is not installed; skipping ${checkName}. Install it with '${tool.installHint}' or run 'brew bundle install --file apps/mobile/Brewfile'.`,
|
||||
);
|
||||
|
||||
const runCommand = Effect.fn("runCommand")(function* (
|
||||
command: string,
|
||||
args: ReadonlyArray<string>,
|
||||
cwd: string,
|
||||
) {
|
||||
yield* Console.log(`$ ${[command, ...args].join(" ")}`);
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const child = yield* spawner.spawn(
|
||||
ChildProcess.make(command, [...args], {
|
||||
cwd,
|
||||
...commandOutputOptions,
|
||||
shell: process.platform === "win32",
|
||||
}),
|
||||
);
|
||||
const exitCode = Number(yield* child.exitCode);
|
||||
|
||||
if (exitCode !== 0) {
|
||||
return yield* new NativeStaticCheckError({
|
||||
message: `Command exited with non-zero exit code (${exitCode})`,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function collectSources(
|
||||
directory: string,
|
||||
): Effect.Effect<ReadonlyArray<string>, unknown, FileSystem.FileSystem | Path.Path> {
|
||||
return Effect.gen(function* () {
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const entries = yield* fs.readDirectory(directory);
|
||||
const sources: Array<string> = [];
|
||||
|
||||
for (const entry of entries) {
|
||||
if (excludedDirectories.has(entry)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const entryPath = path.join(directory, entry);
|
||||
const stat = yield* fs.stat(entryPath);
|
||||
|
||||
if (stat.type === "Directory") {
|
||||
sources.push(...(yield* collectSources(entryPath)));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stat.type === "File" && sourceExtensions.has(path.extname(entry))) {
|
||||
sources.push(entryPath);
|
||||
}
|
||||
}
|
||||
|
||||
return sources;
|
||||
});
|
||||
}
|
||||
|
||||
const runNativeStaticChecks = Effect.fn("runNativeStaticChecks")(function* () {
|
||||
const path = yield* Path.Path;
|
||||
const root = yield* appRoot;
|
||||
const sources = yield* collectSources(root);
|
||||
const swiftSources = sources.filter((source) => path.extname(source) === ".swift");
|
||||
const kotlinSources = sources.filter((source) => {
|
||||
const extension = path.extname(source);
|
||||
return extension === ".kt" || extension === ".kts";
|
||||
});
|
||||
const availableTools = new Map<string, boolean>();
|
||||
|
||||
for (const tool of tools) {
|
||||
availableTools.set(tool.command, yield* commandExists(tool.command));
|
||||
}
|
||||
|
||||
if (swiftSources.length > 0) {
|
||||
if (availableTools.get("swiftlint")) {
|
||||
yield* runCommand("swiftlint", ["lint", "--config", ".swiftlint.yml", "--strict"], root);
|
||||
} else {
|
||||
yield* warnMissingTool(tools[0], "SwiftLint");
|
||||
}
|
||||
}
|
||||
|
||||
if (kotlinSources.length > 0) {
|
||||
const relativeKotlinSources = kotlinSources.map((source) => path.relative(root, source));
|
||||
|
||||
if (availableTools.get("ktlint")) {
|
||||
yield* runCommand("ktlint", relativeKotlinSources, root);
|
||||
} else {
|
||||
yield* warnMissingTool(tools[1], "ktlint");
|
||||
}
|
||||
|
||||
if (availableTools.get("detekt")) {
|
||||
yield* runCommand(
|
||||
"detekt",
|
||||
[
|
||||
"--config",
|
||||
"detekt.yml",
|
||||
"--input",
|
||||
relativeKotlinSources.join(","),
|
||||
"--build-upon-default-config",
|
||||
],
|
||||
root,
|
||||
);
|
||||
} else {
|
||||
yield* warnMissingTool(tools[2], "detekt");
|
||||
}
|
||||
}
|
||||
|
||||
const androidProject = path.join(root, "android");
|
||||
const gradlew = path.join(
|
||||
androidProject,
|
||||
process.platform === "win32" ? "gradlew.bat" : "gradlew",
|
||||
);
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
|
||||
if (yield* fs.exists(gradlew)) {
|
||||
const stat = yield* fs.stat(gradlew);
|
||||
if (stat.type === "File") {
|
||||
yield* runCommand(gradlew, ["lint"], androidProject);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const mobileNativeStaticCheckCommand = Command.make("mobile-native-static-check", {}, () =>
|
||||
runNativeStaticChecks(),
|
||||
).pipe(
|
||||
Command.withDescription("Run mobile native static analysis when native tools are available."),
|
||||
);
|
||||
|
||||
if (import.meta.main) {
|
||||
Command.run(mobileNativeStaticCheckCommand, { version: "0.0.0" }).pipe(
|
||||
Effect.scoped,
|
||||
Effect.provide([Logger.layer([Logger.consolePretty()]), NodeServices.layer]),
|
||||
NodeRuntime.runMain,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user