use dune_site module to find inputs

this packages the inputs as part of the build and exposes the directory
to find them via a generated module. this way, inputs are a resource of
the library rather than an input.

this means that `dune exec aoc` and `dune utop` will always find the
file regardless of where they are invoked, which makes developing in
emacs simpler :)
This commit is contained in:
ryan 2023-09-21 21:40:21 -07:00
parent e1402bc17d
commit 89eb0c7a27
6 changed files with 24 additions and 11 deletions

View File

@ -23,9 +23,11 @@ build: [
name
"-j"
jobs
"--promote-install-files=false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
dev-repo: "git+https://github.com/username/reponame.git"

View File

@ -15,11 +15,14 @@
(documentation https://url/to/documentation)
(using dune_site 0.1)
(package
(name aoc)
(synopsis "A short synopsis")
(description "A longer description")
(depends ocaml dune)
(sites (lib inputs))
(tags
(topics "to describe" your project)))

View File

@ -24,7 +24,7 @@ let part2 lines =
;;
let print () =
let lines = Utils.lines_of_file "inputs/day1.tt" in
let lines = Utils.lines_of_input 1 in
Printf.(
printf "Day 1.1: %d\n" @@ part1 lines;
printf "Day 1.2: %d\n" @@ part2 lines)

View File

@ -87,7 +87,7 @@ let part2 lines =
;;
let print () =
let lines = Utils.lines_of_file "./inputs/day2.tt" in
let lines = Utils.lines_of_input 2 in
Printf.(
printf "Day 2.1: %d\n" @@ part1 lines;
printf "Day 2.2: %d\n" @@ part2 lines)

View File

@ -1,4 +1,12 @@
(library
(name aoc)
(libraries stdio re)
(libraries stdio re dune-site)
(preprocess (pps ppx_regexp)))
(install
(section (site (aoc inputs)))
(files (glob_files ../inputs/*)))
(generate_sites_module
(module inputs)
(sites aoc))

View File

@ -1,9 +1,9 @@
let lines_of_file filename = Stdio.In_channel.(with_file filename ~f:input_lines)
let rec print_int_list = function
| [] -> ()
| e :: l ->
print_int e;
print_string " ";
print_int_list l
let lines_of_input day =
let base_path =
match Inputs.Sites.inputs with
| path :: _ -> path
| _ -> failwith "cant find sites path"
in
let path = Printf.sprintf "%s/day%d.tt" base_path day in
Stdio.In_channel.(with_file path ~f:input_lines)
;;