locate_assets.ml1 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74(* 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 (* [Sys.executable_name] is absolute on every platform owebview supports, but the manual only promises "absolute or relative", so the relative case is handled. The bare "." is worth its own branch rather than being concatenated: it is what a plain "probe" or "./probe" reduces to, and "<cwd>/." names the right directory while answering "." to [Filename.basename] -- which is exactly the question [in_opam_share] below asks. *) if dir = Filename.current_dir_name then Sys.getcwd () else if Filename.is_relative dir then Filename.concat (Sys.getcwd ()) dir else dir (* Locate a directory of on-disk assets (index.html + style.css + app.js), independently of the current working directory. [path] says where that directory sits {b relative to the executable}, not relative to the cwd: ["web"] for a subdirectory beside the binary, ["../web"] for a directory next to the one holding the binary, and so on. An absolute [path] is returned unchanged, for assets installed at a fixed location. Resolving against the executable is what makes a build tree relocatable -- the assets travel with the binary -- and it is where a build puts them: both the files staged from the sources and the {e generated} ones, such as a js_of_ocaml bundle, which exist nowhere else. The corollary is that the assets must have been built. Under dune that means [dune build], which stages them through the [all] alias, and not [dune exec <exe>] alone, which builds the executable and nothing beside it. No normalisation is performed: the two halves are joined with [Filename.concat], so a ["../web"] keeps its [..] in the result. Both the filesystem and the [file://] URLs built from it resolve those on their own. *) let assets_dir path = if Filename.is_relative path then Filename.concat (exe_dir ()) path else path (* Where an `opam install` of the application put [name]. opam installs a dune project's executables into <prefix>/bin and the files of its `(install (section share))` stanza into <prefix>/share/<package>. The binary is therefore one directory away from its own data, and that is the whole of this function: take the directory holding the executable, step up, go down into share/<package>. Derived from the executable rather than from a path configured at build time, like everything else here, so that a switch that is moved or copied takes both halves along together. [None] when the executable is not in a directory named bin. That one test is what tells an installed binary from the other layouts an owebview application is distributed in, where the assets sit beside the binary and [assets_dir] is what finds them: a dune build tree (_build/default/<dir>/), a macOS bundle (Contents/MacOS), the private directory a .deb or .rpm unpacks into, the directory a Windows installer writes. Whether [name] is actually there is not tested, deliberately. A [Some path] that does not exist means a broken install rather than a different layout, and the caller wants that path in its error message rather than a silent fallback to somewhere the file was never going to be. *) let ~package name = let dir = exe_dir () in (* Lowercased: the name compared against is one opam writes, but the prefix it writes it in belongs to the user, who on a case-insensitive system may well have spelled it Bin. *) if String.lowercase_ascii (Filename.basename dir) <> "bin" then None else let = Filename.concat (Filename.dirname dir) "share" in Some (Filename.concat (Filename.concat share package) name)