The unpac monorepo manager self-hosting as a monorepo using unpac

Merge pull request #745 from talex5/fs-example

examples/fs: show how to read files while scanning

authored by

Thomas Leonard and committed by
GitHub
33d4e01a 37840760

+22 -11
+22 -11
examples/fs/main.ml
··· 1 - (* Walks the directory tree rooted at the current directory, 2 - displaying all directory names (skipping hidden directories and `_build`). *) 1 + (* Walk the directory tree rooted at the current directory, 2 + showing a summary for any .mli files. *) 3 + 4 + let ( / ) = Eio.Path.( / ) 3 5 4 - open Eio.Std 6 + let is_doc_comment = String.starts_with ~prefix:"(** " 5 7 6 - let ( / ) = Eio.Path.( / ) 8 + (* Print the first line of [t]'s doc-comment, if any *) 9 + let scan_mli t f = 10 + Eio.Path.with_lines t (fun lines -> 11 + Seq.find is_doc_comment lines 12 + |> Option.iter (fun line -> 13 + let stop = String.index_from_opt line 4 '*' |> Option.value ~default:(String.length line) in 14 + Format.fprintf f "%a: %s@." Eio.Path.pp t (String.sub line 4 (stop - 4)) 15 + ) 16 + ) 7 17 8 - let rec scan t = 18 + (* Walk the tree rooted at [t] and scan any .mli files found. *) 19 + let rec scan t f = 9 20 match Eio.Path.kind ~follow:false t with 10 21 | `Directory -> 11 - traceln "Visiting %a" Eio.Path.pp t; 12 22 Eio.Path.read_dir t |> List.iter (function 13 - | "_build" -> () 14 - | item when String.starts_with ~prefix:"." item -> () 15 - | item -> scan (t / item) 16 - ) 23 + | "_build" | "_opam" -> () (* Don't examine these directories *) 24 + | item when String.starts_with ~prefix:"." item -> () (* Skip hidden items *) 25 + | item -> scan (t / item) f 26 + ) 27 + | `Regular_file when Filename.check_suffix (snd t) ".mli" -> scan_mli t f 17 28 | _ -> () 18 29 19 30 let () = 20 31 Eio_main.run @@ fun env -> 21 - scan (Eio.Stdenv.cwd env) 32 + scan (Eio.Stdenv.cwd env) Format.std_formatter