/** * A workspace's packages, as `zena pkg add` and `zena pkg update` need them * (docs/design/package-manifest.md): the root's manifest, the packages its * `workspace.packages` lists, and every package any of them depends on by * path. Versions are selected for all of them at once, and pinned in the * one `zena-lock.json` beside the root's manifest. * * Files are read through `WorkspaceFiles`, so that tests can hold a * workspace in memory; `DiskFiles` reads them with `zena:fs`. */ import {Future} from 'zena:async'; import {HashMap, OrderedHashMap} from 'zena:collections'; import {isDirectory, isFile, listDir, readFile} from 'zena:fs'; import {join2, normalize} from 'zena:path'; import {Manifest, ManifestError, readManifest} from './manifest.zena'; import {Requirement} from './resolve.zena'; import {Resolution, ResolutionError} from './resolution.zena'; /** The manifest's file name. */ export let MANIFEST = 'zena-package.json'; /** The lockfile's file name. */ export let LOCKFILE = 'zena-lock.json'; /** How a workspace's files are read. */ export interface WorkspaceFiles { /** The text of the file `path`, or null when there is none. */ read(path: String): Future; /** The names in the directory `path`, or null when it is not one. */ list(path: String): Future | null>; } /** A workspace's files on disk. */ export final class DiskFiles implements WorkspaceFiles { async read(path: String): Future { return if (await isFile(path)) await readFile(path) else null; } async list(path: String): Future | null> { if (!(await isDirectory(path))) { return null; } let names = new GrowableArray(); for (let entry in await listDir(path)) { names.push(entry.name); } return names; } } /** A package of a workspace, and its manifest. */ export final class WorkspacePackage { /** The package's directory. */ dir: String; /** Its manifest's text; null for a root that has no manifest yet. */ text: String | null; manifest: Manifest; new(dir: String, text: String | null, manifest: Manifest) : dir = dir, text = text, manifest = manifest; /** The path of its manifest. */ manifestPath(): String { return join2(this.dir, MANIFEST); } } /** A workspace: its root and the packages it is made of. */ export final class Workspace { /** The root, whose manifest has the workspace's packages and scopes. */ root: WorkspacePackage; /** * Every package, the root first, then each in the order it was reached: * the ones `workspace.packages` lists, and the ones they depend on by path. */ packages: GrowableArray; new(root: WorkspacePackage, packages: GrowableArray) : root = root, packages = packages; /** The path of the lockfile. */ lockfilePath(): String { return join2(this.root.dir, LOCKFILE); } /** What every package requires of published packages. */ requirements(): GrowableArray { let all = new GrowableArray(); for (let pkg in this.packages) { pkg.manifest.requirements.forEach((name, requirement) => { all.push(requirement); }); } return all; } /** * The lockfile's `workspace` section for `resolution`: each package's * published dependencies, by the package's directory relative to the * root (`.` for the root), each name mapped to the key of the version * selected for it. Throws `ResolutionError` for a dependency the * resolution has no version for. */ pinsByPackage(resolution: Resolution): OrderedHashMap> { let out = new OrderedHashMap>(); for (let pkg in this.packages) { let pins = new OrderedHashMap(); pkg.manifest.requirements.forEach((name, requirement) => { let key = resolution.keyOf(requirement.did, requirement.range); if (key == null) { throw new ResolutionError(`${pkg.manifestPath()}: no version of '${name}' was selected`); } pins[name] = key; }); if (pins.size > 0) { out[relativeDir(this.root.dir, pkg.dir)] = pins; } } return out; } /** * What the workspace, and `pkg`'s own tests, call `pkg`: its `name`, * with its scope when it has one, `:`, and null when it has * no name. The compiler names it the same way * (docs/design/import-names.md, "Workspace packages"). */ localNameOf(pkg: WorkspacePackage): String | null { let name = pkg.manifest.name; if (name == null) { return null; } let scope = pkg.manifest.scope; return if (scope != null) `${scope}:${name}` else name; } /** * The package whose directory holds `dir`, the innermost one if several * do, and the root if no other does. */ packageAt(dir: String): WorkspacePackage { let path = normalize(dir); var found = this.root; for (let pkg in this.packages) { if ((path == pkg.dir || path.startsWith(`${pkg.dir}/`)) && pkg.dir.length > found.dir.length) { found = pkg; } } return found; } } /** * Reads the workspace whose root is `root`. The root need not have a * manifest yet. Throws `ManifestError`, its message naming the manifest, * for one that cannot be read, and for a path dependency on a directory * with no manifest. */ export async function readWorkspace(root: String, files: WorkspaceFiles): Future { let reader = new WorkspaceReader(files); let rootPackage = await reader.read(normalize(root), true); for (let entry in rootPackage.manifest.workspacePackages) { if (entry.endsWith('/*')) { let parent = join2(rootPackage.dir, entry.sliceBytes(0, entry.length - 2)); let names = await files.list(parent); if (names == null) { throw new ManifestError( `${rootPackage.manifestPath()}: workspace.packages names ${parent}, which is not a directory`); } for (let name in names) { let dir = join2(parent, name); if ((await files.read(join2(dir, MANIFEST))) != null) { await reader.read(dir, false); } } } else { await reader.read(join2(rootPackage.dir, entry), false); } } return new Workspace(rootPackage, reader.packages); } /** Reads packages once each, following their path dependencies. */ final class WorkspaceReader { #files: WorkspaceFiles; #byDir = new HashMap(); packages = new GrowableArray(); new(files: WorkspaceFiles) : #files = files; async read(dir: String, isRoot: boolean): Future { if (let (true, pkg) = this.#byDir.get(dir)) { return pkg; } let path = join2(dir, MANIFEST); let text = await this.#files.read(path); if (text == null && !isRoot) { throw new ManifestError(`${dir} has no ${MANIFEST}`); } var manifest: Manifest | null = null; try { manifest = readManifest(text); } catch (e) { throw new ManifestError(`${dir}/${e.message}`); } let pkg = new WorkspacePackage(dir, text, manifest); this.#byDir[dir] = pkg; this.packages.push(pkg); for (let name in pkg.manifest.paths.keys()) { await this.read(join2(dir, pkg.manifest.paths[name]), false); } return pkg; } } /** * `dir` relative to the workspace's root `root`, as the lockfile's * `workspace` section and the compiler name packages: `.` for the root, * and a directory outside it as it is. */ export function relativeDir(root: String, dir: String): String { if (dir == root) { return '.'; } if (root == '.') { return if (dir.startsWith('./')) dir.sliceBytes(2, dir.length) else dir; } let prefix = `${root}/`; return if (dir.startsWith(prefix)) dir.sliceBytes(prefix.length, dir.length) else dir; }