|
| 1 | +import { Effect } from "effect" |
| 2 | + |
| 3 | +import type { PlatformError } from "@effect/platform/Error" |
| 4 | +import type { FileSystem as Fs } from "@effect/platform/FileSystem" |
| 5 | +import type { Path as PathService } from "@effect/platform/Path" |
| 6 | + |
| 7 | +import type { ScrapExportCommand, ScrapImportCommand } from "../core/domain.js" |
| 8 | +import { readProjectConfig } from "../shell/config.js" |
| 9 | +import { ScrapArchiveInvalidError, ScrapArchiveNotFoundError } from "../shell/errors.js" |
| 10 | +import { resolveBaseDir } from "../shell/paths.js" |
| 11 | +import { resolvePathFromCwd } from "./path-helpers.js" |
| 12 | +import { |
| 13 | + chunkManifestSuffix, |
| 14 | + decodeChunkManifest, |
| 15 | + listChunkParts, |
| 16 | + maxGitBlobBytes, |
| 17 | + removeChunkArtifacts, |
| 18 | + sumFileSizes, |
| 19 | + writeChunkManifest |
| 20 | +} from "./scrap-chunks.js" |
| 21 | +import { buildScrapTemplate, eitherToEffect, ensureSafeScrapImportWipe, runShell, shellEscape } from "./scrap-common.js" |
| 22 | +import { deriveScrapWorkspaceRelativePath } from "./scrap-path.js" |
| 23 | +import type { ScrapError, ScrapRequirements } from "./scrap-types.js" |
| 24 | + |
| 25 | +const scrapImage = "alpine:3.20" |
| 26 | + |
| 27 | +type CacheArchiveInput = { |
| 28 | + readonly baseAbs: string |
| 29 | + readonly partsAbs: ReadonlyArray<string> |
| 30 | +} |
| 31 | + |
| 32 | +const workspacePathFromRelative = (relative: string): string => |
| 33 | + relative.length === 0 ? "/volume" : `/volume/${relative}` |
| 34 | + |
| 35 | +const resolveCacheArchiveInput = ( |
| 36 | + fs: Fs, |
| 37 | + path: PathService, |
| 38 | + projectDir: string, |
| 39 | + archivePath: string |
| 40 | +): Effect.Effect<CacheArchiveInput, ScrapArchiveNotFoundError | ScrapArchiveInvalidError | PlatformError> => |
| 41 | + Effect.gen(function*(_) { |
| 42 | + const baseAbs = resolvePathFromCwd(path, projectDir, archivePath) |
| 43 | + const exists = yield* _(fs.exists(baseAbs)) |
| 44 | + if (exists) { |
| 45 | + const stat = yield* _(fs.stat(baseAbs)) |
| 46 | + if (stat.type === "File") { |
| 47 | + return { baseAbs, partsAbs: [baseAbs] } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + const manifestAbs = `${baseAbs}${chunkManifestSuffix}` |
| 52 | + const manifestExists = yield* _(fs.exists(manifestAbs)) |
| 53 | + if (!manifestExists) { |
| 54 | + return yield* _(Effect.fail(new ScrapArchiveNotFoundError({ path: baseAbs }))) |
| 55 | + } |
| 56 | + |
| 57 | + const manifestText = yield* _(fs.readFileString(manifestAbs)) |
| 58 | + const manifest = yield* _(decodeChunkManifest(manifestAbs, manifestText)) |
| 59 | + if (manifest.parts.length === 0) { |
| 60 | + return yield* _( |
| 61 | + Effect.fail(new ScrapArchiveInvalidError({ path: manifestAbs, message: "manifest.parts is empty" })) |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + const dir = path.dirname(baseAbs) |
| 66 | + const partsAbs = manifest.parts.map((part) => path.join(dir, part)) |
| 67 | + for (const partAbs of partsAbs) { |
| 68 | + const partExists = yield* _(fs.exists(partAbs)) |
| 69 | + if (!partExists) { |
| 70 | + return yield* _(Effect.fail(new ScrapArchiveNotFoundError({ path: partAbs }))) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return { baseAbs, partsAbs } |
| 75 | + }) |
| 76 | + |
| 77 | +const buildCacheExportScript = (volumeName: string, workspacePath: string, partsPrefix: string): string => { |
| 78 | + const volumeMount = `${volumeName}:/volume:ro` |
| 79 | + const innerScript = [ |
| 80 | + "set -e", |
| 81 | + `SRC=${shellEscape(workspacePath)}`, |
| 82 | + "if [ ! -d \"$SRC\" ]; then echo \"Workspace dir not found: $SRC\" >&2; exit 2; fi", |
| 83 | + "tar czf - -C \"$SRC\" ." |
| 84 | + ].join("; ") |
| 85 | + |
| 86 | + return [ |
| 87 | + "set -e", |
| 88 | + `docker run --rm --user 1000:1000 -v ${shellEscape(volumeMount)} ${scrapImage} sh -lc ${shellEscape(innerScript)}`, |
| 89 | + `| split -b ${maxGitBlobBytes} -d -a 5 - ${shellEscape(partsPrefix)}` |
| 90 | + ].join(" ") |
| 91 | +} |
| 92 | + |
| 93 | +export const exportScrapCache = ( |
| 94 | + command: ScrapExportCommand |
| 95 | +): Effect.Effect<void, ScrapError, ScrapRequirements> => |
| 96 | + Effect.gen(function*(_) { |
| 97 | + const { fs, path, resolved } = yield* _(resolveBaseDir(command.projectDir)) |
| 98 | + const config = yield* _(readProjectConfig(resolved)) |
| 99 | + const template = buildScrapTemplate(config) |
| 100 | + |
| 101 | + const relative = yield* _(eitherToEffect(deriveScrapWorkspaceRelativePath(template.sshUser, template.targetDir))) |
| 102 | + const workspacePath = workspacePathFromRelative(relative) |
| 103 | + |
| 104 | + const archiveAbs = resolvePathFromCwd(path, resolved, command.archivePath) |
| 105 | + const archiveDir = path.dirname(archiveAbs) |
| 106 | + const archiveBase = path.basename(archiveAbs) |
| 107 | + const partsPrefix = `${archiveAbs}.part` |
| 108 | + |
| 109 | + yield* _(fs.makeDirectory(archiveDir, { recursive: true })) |
| 110 | + yield* _(removeChunkArtifacts(fs, path, archiveAbs)) |
| 111 | + yield* _(fs.remove(archiveAbs, { force: true })) |
| 112 | + |
| 113 | + yield* _( |
| 114 | + Effect.log( |
| 115 | + [ |
| 116 | + `Project: ${resolved}`, |
| 117 | + "Mode: cache", |
| 118 | + `Volume: ${template.volumeName}`, |
| 119 | + `Workspace: ${template.targetDir}`, |
| 120 | + `Archive: ${archiveAbs} (+parts, max ${maxGitBlobBytes} bytes each)` |
| 121 | + ].join("\n") |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + const script = buildCacheExportScript(template.volumeName, workspacePath, partsPrefix) |
| 126 | + yield* _(runShell(resolved, "scrap export cache", script)) |
| 127 | + |
| 128 | + const partsAbs = yield* _(listChunkParts(fs, path, archiveAbs)) |
| 129 | + const totalSize = yield* _(sumFileSizes(fs, partsAbs)) |
| 130 | + yield* _(writeChunkManifest(fs, path, archiveAbs, totalSize, partsAbs)) |
| 131 | + |
| 132 | + yield* _(Effect.log(`Scrap cache export complete: ${archiveBase}${chunkManifestSuffix}`)) |
| 133 | + }).pipe(Effect.asVoid) |
| 134 | + |
| 135 | +const buildCacheImportScript = ( |
| 136 | + volumeName: string, |
| 137 | + workspacePath: string, |
| 138 | + wipe: boolean |
| 139 | +): { readonly dockerRun: string; readonly innerScript: string } => { |
| 140 | + const wipeLine = wipe ? "rm -rf \"$DST\"" : ":" |
| 141 | + const innerScript = [ |
| 142 | + "set -e", |
| 143 | + `DST=${shellEscape(workspacePath)}`, |
| 144 | + wipeLine, |
| 145 | + "mkdir -p \"$DST\"", |
| 146 | + "tar xzf - -C \"$DST\"" |
| 147 | + ].join("; ") |
| 148 | + |
| 149 | + const volumeMount = `${volumeName}:/volume` |
| 150 | + const dockerRun = [ |
| 151 | + "docker run --rm -i", |
| 152 | + "--user 1000:1000", |
| 153 | + `-v ${shellEscape(volumeMount)}`, |
| 154 | + scrapImage, |
| 155 | + "sh -lc", |
| 156 | + shellEscape(innerScript) |
| 157 | + ].join(" ") |
| 158 | + |
| 159 | + return { dockerRun, innerScript } |
| 160 | +} |
| 161 | + |
| 162 | +export const importScrapCache = ( |
| 163 | + command: ScrapImportCommand |
| 164 | +): Effect.Effect<void, ScrapError, ScrapRequirements> => |
| 165 | + Effect.gen(function*(_) { |
| 166 | + const { fs, path, resolved } = yield* _(resolveBaseDir(command.projectDir)) |
| 167 | + const config = yield* _(readProjectConfig(resolved)) |
| 168 | + const template = buildScrapTemplate(config) |
| 169 | + |
| 170 | + const relative = yield* _(eitherToEffect(deriveScrapWorkspaceRelativePath(template.sshUser, template.targetDir))) |
| 171 | + yield* _(ensureSafeScrapImportWipe(command.wipe, template, relative)) |
| 172 | + const workspacePath = workspacePathFromRelative(relative) |
| 173 | + |
| 174 | + const archiveInput = yield* _(resolveCacheArchiveInput(fs, path, resolved, command.archivePath)) |
| 175 | + |
| 176 | + yield* _( |
| 177 | + Effect.log( |
| 178 | + [ |
| 179 | + `Project: ${resolved}`, |
| 180 | + "Mode: cache", |
| 181 | + `Volume: ${template.volumeName}`, |
| 182 | + `Workspace: ${template.targetDir}`, |
| 183 | + `Archive: ${archiveInput.baseAbs}`, |
| 184 | + `Wipe: ${command.wipe ? "yes" : "no"}` |
| 185 | + ].join("\n") |
| 186 | + ) |
| 187 | + ) |
| 188 | + |
| 189 | + const { dockerRun } = buildCacheImportScript(template.volumeName, workspacePath, command.wipe) |
| 190 | + const catArgs = archiveInput.partsAbs.map((p) => shellEscape(p)).join(" ") |
| 191 | + const script = ["set -e", `cat ${catArgs} | ${dockerRun}`].join("; ") |
| 192 | + yield* _(runShell(resolved, "scrap import cache", script)) |
| 193 | + |
| 194 | + yield* _(Effect.log("Scrap cache import complete.")) |
| 195 | + }).pipe(Effect.asVoid) |
0 commit comments