<commit_before>open Printf
module Findlib = Solvuu_build_findlib
open Tools
open Util
open Util.Filename
let (/) = Util.Filename.concat

type pkg = string

type app = {
  name : string;
  internal_deps : item list;
  findlib_deps : pkg list;
  file : string;

  annot : unit option;
  bin_annot : unit option;
  g : unit option;
  safe_string : unit option;
  short_paths : unit option;
  thread : unit option;
  w : string option;
}

and lib = {
  name : string;
  internal_deps : item list;
  findlib_deps : pkg list;
  style : [ `Basic | `Pack of string];
  dir : string;
  ml_files : string list;
  mli_files : string list;
  c_files : string list;
  pkg : Solvuu_build_findlib.pkg;

  annot : unit option;
  bin_annot : unit option;
  g : unit option;
  safe_string : unit option;
  short_paths : unit option;
  thread : unit option;
  w : string option;
  linkall : unit option;
  shared : unit option;
}

and item = Lib of lib | App of app

let lib
    ?annot ?bin_annot ?g ?safe_string ?short_paths ?thread ?w ?linkall
    ?shared
    ?(internal_deps=[]) ?(findlib_deps=[])
    ?ml_files ?mli_files ?c_files ~pkg ~style ~dir name
  =
  let files =
    try Sys.readdir dir |> Array.to_list
    with _ -> []
  in
  let ml_files =
    List.filter files ~f:(fun x -> check_suffix x ".ml") |> fun l ->
    (match ml_files with
     |None -> l | Some (`Add x) -> x@l | Some (`Replace x) -> x
    )
    |> List.sort_uniq ~cmp:String.compare
  in
  let mli_files =
    List.filter files ~f:(fun x -> check_suffix x ".mli") |> fun l ->
    (match mli_files with
     | None -> l | Some (`Add x) -> x@l | Some (`Replace x) -> x
    )
    |> List.sort_uniq ~cmp:String.compare
  in
  let c_files =
    List.filter files ~f:(fun x -> check_suffix x ".c") |> fun l ->
    (match c_files with
     | None -> l | Some (`Add x) -> x@l | Some (`Replace x) -> x
    )
    |> List.sort_uniq ~cmp:String.compare
  in
  Lib {
    name; internal_deps; findlib_deps; style;
    dir; ml_files; mli_files; c_files; pkg;
    annot; bin_annot; g; safe_string; short_paths; thread; w; linkall;
    shared;
  }

let app
    ?annot ?bin_annot ?g ?safe_string ?short_paths ?thread ?w
    ?(internal_deps=[]) ?(findlib_deps=[])
    ~file name
  =
  App {
    name; internal_deps; findlib_deps; file;
    annot; bin_annot; g; safe_string; short_paths; thread; w;
  }

let name = function Lib x -> x.name | App x -> x.name

let internal_deps = function<commit_msg>Project.lib: factor some local code<commit_after>open Printf
module Findlib = Solvuu_build_findlib
open Tools
open Util
open Util.Filename
let (/) = Util.Filename.concat

type pkg = string

type app = {
  name : string;
  internal_deps : item list;
  findlib_deps : pkg list;
  file : string;

  annot : unit option;
  bin_annot : unit option;
  g : unit option;
  safe_string : unit option;
  short_paths : unit option;
  thread : unit option;
  w : string option;
}

and lib = {
  name : string;
  internal_deps : item list;
  findlib_deps : pkg list;
  style : [ `Basic | `Pack of string];
  dir : string;
  ml_files : string list;
  mli_files : string list;
  c_files : string list;
  pkg : Solvuu_build_findlib.pkg;

  annot : unit option;
  bin_annot : unit option;
  g : unit option;
  safe_string : unit option;
  short_paths : unit option;
  thread : unit option;
  w : string option;
  linkall : unit option;
  shared : unit option;
}

and item = Lib of lib | App of app

let lib
    ?annot ?bin_annot ?g ?safe_string ?short_paths ?thread ?w ?linkall
    ?shared
    ?(internal_deps=[]) ?(findlib_deps=[])
    ?ml_files ?mli_files ?c_files ~pkg ~style ~dir name
  =
  let all_files =
    try Sys.readdir dir |> Array.to_list
    with _ -> []
  in
  let select_files ?add_replace suffix =
    List.filter all_files ~f:(fun x -> check_suffix x suffix) |> fun l ->
    (match add_replace with
     | None -> l
     | Some (`Add x) -> x@l
     | Some (`Replace x) -> x
    ) |>
    List.sort_uniq ~cmp:String.compare
  in
  let ml_files = select_files ".ml" ?add_replace:ml_files in
  let mli_files = select_files ".mli" ?add_replace:mli_files in
  let c_files = select_files ".c" ?add_replace:c_files in
  Lib {
    name; internal_deps; findlib_deps; style;
    dir; ml_files; mli_files; c_files; pkg;
    annot; bin_annot; g; safe_string; short_paths; thread; w; linkall;
    shared;
  }

let app
    ?annot ?bin_annot ?g ?safe_string ?short_paths ?thread ?w
    ?(internal_deps=[]) ?(findlib_deps=[])
    ~file name
  =
  App {
    name; internal_deps; findlib_deps; file;
    annot; bin_annot; g; safe_string; short_paths; thread; w;
  }

let name = function Lib x -> x.name | App x -> x.name

let internal_deps = function
{'fraction_non_alphanumeric': 0.11605995717344754, 'fraction_numerical': 0.0, 'mean_word_length': 3.033678756476684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4461373', 'n_tokens_mistral': 1734, 'n_tokens_neox': 1690, 'n_words': 523}