package owebview

  1. Overview
  2. Docs
OCaml bindings for the webview library

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.0.tar.gz
md5=96958382fcfef04cc489a87b392abd65
sha512=0494c253643890138099fb14023476d4fef95299449c118301461efa0d2e316bf37a326a76bcf8d29512cdb621c144d06deb76ac5e22adeab76f9fae8a443d24

doc/src/owebview/utils.ml.html

Source file utils.ml

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
(* Filesystem helpers for locating on-disk assets relative to the running
   executable, independently of the current working directory. *)

(* Absolute path to the directory containing the running executable. *)
let exe_dir () =
  let dir = Filename.dirname Sys.executable_name in
  if Filename.is_relative dir then Filename.concat (Sys.getcwd ()) dir else dir

(* Directory to resolve on-disk assets against, independently of the cwd.

   When launched via [dune exec], the executable lives under
   [<root>/_build/<context>/...] but the source assets are not copied there.
   We map such a path back to the matching source directory so the assets are
   found. When run from an installed location (no [_build] segment) the
   executable directory is used as-is. *)
let asset_dir () =
  let rec strip = function
    | "_build" :: _context :: rest -> rest (* drop "_build/<context>/" *)
    | x :: rest -> x :: strip rest
    | [] -> []
  in
  String.concat Filename.dir_sep
    (strip (String.split_on_char '/' (exe_dir ())))

(* Locate the directory holding the web assets (index.html + style.css +
   app.js), independently of the current working directory.

   We read from the {b target} directory next to the binary ([exe_dir/web]),
   because it holds both the staged source assets and any {e generated} ones
   (e.g. an app.js produced by js_of_ocaml) — the source tree has only the
   former. [dune build] stages/generates them there (see the [all] alias in the
   examples' dune files); a [_build] copy relocated elsewhere keeps working
   since the assets sit beside the binary. Falling back to the source tree
   ([asset_dir/web]) only covers the case where nothing was staged yet. *)
let web_dir () =
  let has_index dir = Sys.file_exists (Filename.concat dir "index.html") in
  let beside_binary = Filename.concat (exe_dir ()) "web" in
  if has_index beside_binary then beside_binary
  else Filename.concat (asset_dir ()) "web"