~srpablo/fleaswallow

ref: e47d9cf97cab1ca711a8cecb1c9173f920041015 fleaswallow/src/fleaswallow_lib/files.ml -rw-r--r-- 1.4 KiB View raw
e47d9cf9 — Pablo Meier wow dependencies lol 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
open Core
(** "Imperative shell" functions for dealing with the filesystem. *)

open Filename
open Re2

type file_with_contents = { name : string; lines : string list }

type t = file_with_contents

let to_record x = { name = x; lines = In_channel.read_lines x }

let name { name; _ } = name

let lines { lines; _ } = lines

let contents { lines; _ } = Utils.add_newlines lines

let file_contents path = try Some (to_record path) with Sys_error _ -> None

let check_exists path =
  match Sys.file_exists ~follow_symlinks:true path with
  | `No -> false
  | `Yes | `Unknown -> true

let whitelist = [ ".md$"; ".html$" ]

let whitelist_regexes =
  List.map ~f:(Re2.create_exn ~options:Options.default) whitelist

let in_whitelist x =
  List.exists whitelist_regexes ~f:(fun y -> Re2.matches y x)

(** Retrieves files from a directory and their contents as lines *)
let file_contents_in_dir dirname =
  Sys.readdir dirname |> Array.to_list
  |> List.map ~f:(Filename.concat dirname)
  |> List.filter ~f:in_whitelist
  |> List.map ~f:to_record

let write_out_to_file build_dir (path, content) =
  let filename = Filename.concat build_dir path in
  let () = Unix.mkdir_p (dirname filename) in
  Out_channel.write_all filename ~data:content

let copy_static_dir toplevel build_dir =
  let static_dir = Filename.concat toplevel "static/" in
  FileUtil.cp ~force:Force ~recurse:true ~preserve:true [ static_dir ]
    build_dir