Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions example/dir.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List.iter (fun (k,v) -> Printf.printf "%s: %S\n" k v) [%blob "dir"]
1 change: 1 addition & 0 deletions example/dir/a.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAA?
1 change: 1 addition & 0 deletions example/dir/b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BBB!
9 changes: 8 additions & 1 deletion example/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
(executable
(name quine)
(modules quine)
(preprocess (pps ppx_blob)))

(executable
(name dir)
(modules dir)
(preprocess (pps ppx_blob))
(preprocessor_deps (source_tree dir)))

(alias
(name example)
(deps quine.exe))
(deps quine.exe dir.exe))
21 changes: 18 additions & 3 deletions src/ppx_blob.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,30 @@ let read_file path =
with _ ->
None

let read_path path =
match Sys.is_directory path with
| true ->
let l = Sys.readdir path
|> Array.to_list
|> List.filter_map (fun p -> match read_file @@ Filename.concat path p with None -> None | Some s -> Some (p,s))
in Some (`Dir l)
| false ->
match read_file path with
| None -> None
| Some s -> Some (`File s)

let get_blob ~loc path =
match find_map read_file (get_candidate_paths ~loc path) with
match find_map read_path (get_candidate_paths ~loc path) with
| Some blob -> blob
| None -> location_errorf ~loc "[%%blob] could not find or load file %s" path
| None -> location_errorf ~loc "[%%blob] could not find or load path %s" path

let expand ~ctxt path =
let open Ppxlib in
let loc = Expansion_context.Extension.extension_point_loc ctxt in
Ast_builder.Default.estring ~loc (get_blob ~loc path)
let open Ast_builder.Default in
match get_blob ~loc path with
| `File s -> estring ~loc s
| `Dir l -> elist ~loc (l |> List.map (fun (p,s) -> pexp_tuple ~loc [estring ~loc p; estring ~loc s]))

let extension =
let open Ppxlib in
Expand Down
Loading