Homebrew bottle builder and tap manager for OCaml monorepos
bottler lib
65 files

bottle_archive.ml
bottle_archive.mli
bottler.ml
bottler.mli
build.ml
build.mli
config.ml
config.mli
context.ml
context.mli
darwin.ml
darwin.mli
doctor.ml
doctor.mli
dune
errors.ml
errors.mli
formula.ml
formula.mli
image.ml
image.mli
init.ml
init.mli
install.ml
install.mli
keys.ml
keys.mli
linux.ml
linux.mli
list.ml
list.mli
login.ml
login.mli
macos.ml
macos.mli
output.ml
output.mli
readme.ml
readme.mli
release.ml
release.mli
remove.ml
remove.mli
retry.ml
retry.mli
returned.ml
returned.mli
runner.ml
runner.mli
screen.ml
screen.mli
switch.ml
switch.mli
tap_path.ml
tap_path.mli
tree.ml
tree.mli
upload.ml
upload.mli
url.ml
url.mli
version.ml
version.mli
workflow.ml
workflow.mli
readme.ml
(*---------------------------------------------------------------------------
  Copyright (c) 2025 Thomas Gazagnaire. All rights reserved.
  SPDX-License-Identifier: ISC
  ---------------------------------------------------------------------------*)

type entry = { name : string; description : string; owned : bool }

let is_installable (config : Config.t) e =
  not
    (Stdlib.List.exists
       (fun (r : Config.removed) -> String.equal r.name e.name)
       config.removed)

let entries ~fs (config : Config.t) =
  let tap = Fpath.v config.tap.local_path in
  let own_tbl = Hashtbl.create 16 in
  Stdlib.List.iter
    (fun (p : Config.package) -> Hashtbl.replace own_tbl p.name p.description)
    config.packages;
  let files =
    match Tap_path.dir ~tap with
    | Error _ -> []
    | Ok formula_dir -> (
        match Bos.OS.Dir.contents ~rel:true formula_dir with
        | Ok e -> e
        | Error _ -> [])
  in
  let on_disk =
    Stdlib.List.filter_map
      (fun rel ->
        let fname = Fpath.to_string rel in
        if not (Filename.check_suffix fname ".rb") then None
        else
          let name = Filename.chop_suffix fname ".rb" in
          match Tap_path.formula ~tap name with
          | Error _ -> None
          | Ok path ->
              let path = Fpath.to_string path in
              let owned = Hashtbl.mem own_tbl name in
              let description =
                if owned then Hashtbl.find own_tbl name
                else
                  Homebrew.Formula.desc_of_file Eio.Path.(fs / path)
                  |> Option.value ~default:""
              in
              Some { name; description; owned })
      files
  in
  let on_disk_tbl = Hashtbl.create 16 in
  Stdlib.List.iter (fun e -> Hashtbl.replace on_disk_tbl e.name ()) on_disk;
  let own_not_yet_on_disk =
    Stdlib.List.filter_map
      (fun (p : Config.package) ->
        if Hashtbl.mem on_disk_tbl p.name then None
        else Some { name = p.name; description = p.description; owned = true })
      config.packages
  in
  on_disk @ own_not_yet_on_disk
  |> Stdlib.List.sort (fun a b -> String.compare a.name b.name)

let tap_repo_name tap_name =
  match Astring.String.cut ~sep:"/" tap_name with
  | Some (_, r) -> r
  | None -> tap_name

let generate ~fs (config : Config.t) =
  let buf = Buffer.create 512 in
  let pr fmt = Fmt.kstr (fun s -> Buffer.add_string buf s) fmt in
  let tap_name = Homebrew.Tap.name_of_url config.tap.clone_url in
  let formulas = entries ~fs config in
  pr "# homebrew-%s\n\n" (tap_repo_name tap_name);
  pr "Homebrew tap.\n\n";
  pr "## Installation\n\n";
  pr "```bash\n";
  pr "brew tap %s\n" tap_name;
  pr "```\n\n";
  pr "## Available Formulas\n\n";
  pr "| Formula | Description |\n";
  pr "|---------|-------------|\n";
  Stdlib.List.iter
    (fun e ->
      let description =
        if is_installable config e then e.description
        else Fmt.str "%s (retired)" e.description
      in
      pr "| `%s` | %s |\n" e.name description)
    formulas;
  let installable = Stdlib.List.filter (is_installable config) formulas in
  pr "\n## Usage\n\n";
  pr "```bash\n";
  pr "# Install pre-built binaries\n";
  pr "brew install %s\n"
    (String.concat " " (Stdlib.List.map (fun e -> e.name) installable));
  pr "\n# Or build from source\n";
  pr "brew install --HEAD %s\n"
    (match installable with e :: _ -> e.name | [] -> "NAME");
  pr "```\n\n";
  pr "## License\n\n";
  pr "%s\n" config.license;
  Buffer.contents buf