Skip to content

Commit c431f8c

Browse files
authored
Add resolved to logger (#15)
* add resolved to logger * extend the logger in porcelain
1 parent ce202a3 commit c431f8c

4 files changed

Lines changed: 44 additions & 19 deletions

File tree

src/plumbing/install.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
2323
const tarball = useCache().path(stowage)
2424
const shelf = tea_prefix.join(pkg.project)
2525

26-
logger?.locking(pkg)
26+
logger?.locking?.(pkg)
2727
const { rid: fd } = await Deno.open(shelf.mkdir('p').string)
2828
await flock(fd, 'ex')
2929

@@ -32,11 +32,11 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
3232
if (already_installed) {
3333
// some other tea instance installed us while we were waiting for the lock
3434
// or potentially we were already installed and the caller is naughty
35-
logger?.installed(already_installed)
35+
logger?.installed?.(already_installed)
3636
return already_installed
3737
}
3838

39-
logger?.downloading({pkg})
39+
logger?.downloading?.({pkg})
4040

4141
const tmpdir = Path.mktemp({
4242
dir: tea_prefix.join("local/tmp").join(pkg.project),
@@ -58,13 +58,13 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
5858
src: url,
5959
dst: tarball,
6060
logger: info => {
61-
logger?.downloading({ pkg, ...info })
61+
logger?.downloading?.({ pkg, ...info })
6262
total ??= info.total
6363
}
6464
}, blob => {
6565
n += blob.length
6666
hasher.update(blob)
67-
logger?.installing({ pkg, progress: total ? n / total : total })
67+
logger?.installing?.({ pkg, progress: total ? n / total : total })
6868
return writeAll(untar.stdin, blob)
6969
})
7070

@@ -89,14 +89,14 @@ export default async function install(pkg: Package, logger?: Logger): Promise<In
8989
const path = tmpdir.mv({ to: shelf.join(`v${pkg.version}`) })
9090
const install = { pkg, path }
9191

92-
logger?.installed(install)
92+
logger?.installed?.(install)
9393

9494
return install
9595
} catch (err) {
9696
tarball.rm() //FIXME resumable downloads!
9797
throw err
9898
} finally {
99-
logger?.unlocking(pkg)
99+
logger?.unlocking?.(pkg)
100100
await flock(fd, 'un')
101101
Deno.close(fd) // docs aren't clear if we need to do this or not
102102
}
@@ -111,16 +111,16 @@ async function remote_SHA(url: URL) {
111111

112112

113113
export interface Logger {
114-
locking(pkg: Package): void
114+
locking?(pkg: Package): void
115115
/// raw http info
116-
downloading(info: {pkg: Package, src?: URL, dst?: Path, rcvd?: number, total?: number}): void
116+
downloading?(info: {pkg: Package, src?: URL, dst?: Path, rcvd?: number, total?: number}): void
117117
/// we are simultaneously downloading and untarring the bottle
118118
/// the install progress here is proper and tied to download progress
119119
/// progress is a either a fraction between 0 and 1 or the number of bytes that have been untarred
120120
/// we try to give you the fraction as soon as possible, but you will need to deal with both formats
121-
installing(info: {pkg: Package, progress: number | undefined}): void
122-
unlocking(pkg: Package): void
123-
installed(installation: Installation): void
121+
installing?(info: {pkg: Package, progress: number | undefined}): void
122+
unlocking?(pkg: Package): void
123+
installed?(installation: Installation): void
124124
}
125125

126126
// deno-lint-ignore no-explicit-any

src/plumbing/resolve.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TeaError from "../utils/error.ts"
66
/// NOTE resolves to bottles
77
/// NOTE contract there are no duplicate projects in input
88

9-
interface RT {
9+
export interface Resolution {
1010
/// fully resolved list (includes both installed and pending)
1111
pkgs: Package[]
1212

@@ -22,10 +22,10 @@ interface RT {
2222
/// that resolve so if we are resolving `node>=12`, node 13 is installed, but
2323
/// node 19 is the latest we return node 13. if `update` is true we return node
2424
/// 19 and *you will need to install it*.
25-
export default async function resolve(reqs: (Package | PackageRequirement)[], {update}: {update: boolean} = {update: false}): Promise<RT> {
25+
export default async function resolve(reqs: (Package | PackageRequirement)[], {update}: {update: boolean} = {update: false}): Promise<Resolution> {
2626
const inventory = _internals.useInventory()
2727
const cellar = _internals.useCellar()
28-
const rv: RT = { pkgs: [], installed: [], pending: [] }
28+
const rv: Resolution = { pkgs: [], installed: [], pending: [] }
2929
let installation: Installation | undefined
3030
for (const req of reqs) {
3131
if (!update && (installation = await cellar.has(req))) {

src/porcelain/install.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useTestConfig } from "../hooks/useTestConfig.ts"
2-
import { assert } from "deno/testing/asserts.ts"
2+
import { assert, assertArrayIncludes } from "deno/testing/asserts.ts"
3+
import { ConsoleLogger } from "../plumbing/install.ts"
4+
import type { Package } from "../types.ts";
5+
import type { Resolution } from "../plumbing/resolve.ts";
36
import * as semver from "../utils/semver.ts"
47
import install from "./install.ts"
58

@@ -27,3 +30,18 @@ Deno.test("porcelain.install.4", async () => {
2730
useTestConfig()
2831
await install([{ project: 'tea.xyz/brewkit', constraint: new semver.Range("^0.31") }])
2932
})
33+
34+
Deno.test("porcelain.install.resolved", async () => {
35+
useTestConfig()
36+
37+
let resolution: Resolution = { pkgs: [] as Package[] } as Resolution
38+
const logger = {
39+
...ConsoleLogger(),
40+
resolved: (r: Resolution) => resolution = r
41+
}
42+
43+
await install("tea.xyz/brewkit^0.32", logger)
44+
45+
const resolvedProjects = resolution.pkgs.map((p: Package) => p.project)
46+
assertArrayIncludes(resolvedProjects, [ "deno.land", "gnu.org/bash", "tea.xyz", "tea.xyz/brewkit"])
47+
})

src/porcelain/install.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Installation, PackageSpecification } from "../types.ts"
2-
import install, { Logger } from "../plumbing/install.ts"
2+
import install, { Logger as InstallLogger } from "../plumbing/install.ts"
33
import usePantry from "../hooks/usePantry.ts"
44
import hydrate from "../plumbing/hydrate.ts"
5-
import resolve from "../plumbing/resolve.ts"
5+
import resolve, { Resolution } from "../plumbing/resolve.ts"
66
import { isArray, isString } from "is-what"
77
import { parse } from "../utils/pkg.ts"
88
import link from "../plumbing/link.ts"
99
import useSync from "../hooks/useSync.ts";
1010

11+
export interface Logger extends InstallLogger {
12+
resolved?(resolution: Resolution): void
13+
}
14+
1115
/// eg. install("python.org~3.10")
1216
export default async function(pkgs: PackageSpecification[] | string[] | string, logger?: Logger): Promise<Installation[]> {
1317

@@ -23,7 +27,10 @@ export default async function(pkgs: PackageSpecification[] | string[] | string,
2327
//TODO parallelize!
2428

2529
pkgs = (await hydrate(pkgs)).pkgs
26-
const { pending, installed } = await resolve(pkgs)
30+
const resolution = await resolve(pkgs)
31+
logger?.resolved?.(resolution)
32+
33+
const { pending, installed} = resolution
2734
for (const pkg of pending) {
2835
const installation = await install(pkg, logger)
2936
await link(installation)

0 commit comments

Comments
 (0)