// What install and info print about a package. ///| /// Label and values. Each value after the first gets its own line. type Detail = (String, Array[String]) ///| /// Align details in columns, dropping empty ones. Values may come from a /// registry, so control characters are blanked. fn detail_lines(details : Array[Detail], indent : String) -> Array[String] { let lines : Array[String] = [] for detail in details { let (label, values) = detail let mut head = label + ":" for v in values { if v != "" { lines.push(indent + pad_right(head, 13) + printable(v)) head = "" } } } lines } ///| fn print_details(details : Array[Detail], indent : String) -> Unit { for line in detail_lines(details, indent) { println(line) } } ///| /// The details of a build that install and info share. `bin_dir` is where its /// commands are linked, `store` where its files are. fn package_details( m : Manifest, channel~ : String, digest~ : String, store~ : String, bin_dir~ : String, ) -> Array[Detail] { let bins = m.bins.map(b => bin_dir + "/" + basename(b) + " -> " + b) if bins.is_empty() { bins.push("(none)") } let specs = fn(reqs : Map[String, String]) { let out : Array[String] = [] for n, c in reqs { out.push(n + "@" + c) } join(out, " ") } [ ("description", [m.description]), ("channel", [channel]), ("digest", [digest]), ("path", [store]), ("bin", bins), ("depends", [specs(m.depends)]), ("conflicts", [specs(m.conflicts)]), ] } ///| /// The details of a build in a channel that isn't installed: it has no paths /// yet, so bins are shown as they sit in the package. fn available_details( m : Manifest, channel~ : String, digest~ : String, ) -> Array[Detail] { let bins = m.bins.copy() if bins.is_empty() { bins.push("(none)") } let specs = fn(reqs : Map[String, String]) { let out : Array[String] = [] for n, c in reqs { out.push(n + "@" + c) } join(out, " ") } [ ("description", [m.description]), ("docs", [m.documentation]), ("channel", [channel]), ("digest", [digest]), ("bin", bins), ("depends", [specs(m.depends)]), ("conflicts", [specs(m.conflicts)]), ] } ///| fn installed_details(ctx : Ctx, p : Installed) -> Array[Detail] { package_details( manifest_of_installed(p), channel=p.channel, digest=p.digest, store=store_dir(ctx, p.name, p.digest), bin_dir=bin_dir(ctx), ) } ///| fn locked_details( ctx : Ctx, project_dir : String, p : LockedPkg, ) -> Array[Detail] { package_details( manifest_of_locked(p), channel=p.channel, digest=p.digest, store=store_dir(ctx, p.name, p.digest), bin_dir=project_env_dir(project_dir) + "/bin", ) } ///| test "package details show paths and every bin" { let m = parse_manifest( "{\"name\":\"t\",\"version\":\"1.0.0\",\"description\":\"A\\u001btool\",\"bin\":[\"bin/a\",\"libexec/b\"],\"depends\":{\"x\":\"^1\"},\"conflicts\":{\"y\":\"*\"}}", ) let details = package_details( m, channel="main", digest="sha256:ab", store="/o/store/t/ab", bin_dir="/o/bin", ) assert_eq(detail_lines(details, " "), [ " description: A tool", " channel: main", " digest: sha256:ab", " path: /o/store/t/ab", " bin: /o/bin/a -> bin/a", " /o/bin/b -> libexec/b", " depends: x@^1", " conflicts: y@*", ]) let bare = package_details( parse_manifest("{\"name\":\"t\",\"version\":\"1.0.0\"}"), channel="main", digest="sha256:ab", store="/o/store/t/ab", bin_dir="/o/bin", ) assert_eq(detail_lines(bare, ""), [ "channel: main", "digest: sha256:ab", "path: /o/store/t/ab", "bin: (none)", ]) } ///| test "available details show bins as packaged and the docs link" { let m = parse_manifest( "{\"name\":\"t\",\"version\":\"1.0.0\",\"description\":\"A tool\",\"documentation\":\"https://x/t\",\"bin\":[\"bin/a\"],\"depends\":{\"x\":\"^1\"}}", ) assert_eq( detail_lines(available_details(m, channel="main", digest="sha256:ab"), ""), [ "description: A tool", "docs: https://x/t", "channel: main", "digest: sha256:ab", "bin: bin/a", "depends: x@^1", ], ) }