package mcrunch
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
sha256=8d16b87ca7218c28903d8a0a318740a8245fe072a7b015bd125effa403fdd4fb
sha512=662fd99a9a518e233bd0f1ae4415d77d776ad84819e71dabf5df5de8ac7b5e28cf97fa9a1199e3bf337b123a50ffb4a96339a56e6d8ee11eae9a9070a6fe75e0
Description
Added to opam-repository:
README
mcrunch
mcrunch is a command-line tool that embeds files into OCaml source code. It reads one or more files and produces an OCaml module where each file's contents are encoded as a hexadecimal string array (or list or a single string). The generated module can be statically linked into an OCaml program, giving it access to the file contents at runtime without any I/O operations.
This is useful for embedding static assets (configuration files, templates, certificates, binary data) directly into an OCaml binary.
Installation
$ opam install mcrunchUsage
The simplest invocation takes a file and writes an OCaml module to stdout:
$ mcrunch -f foo.txt
let foo_txt = [| "\x66\x6f\x6f\x0a" |]The OCaml binding name is derived from the filename, with characters like . and % replaced by underscores. You can also specify the binding name explicitly using the name:filename syntax:
$ mcrunch -f contents:foo.txt
let contents = [| "\x66\x6f\x6f\x0a" |]Multiple files can be crunched into a single module:
$ mcrunch -f foo.txt -f bar.txt -o assets.mlIf a filename contains the character :, use the prefix -: to let mcrunch infer the name automatically:
$ mcrunch -f -:path:to:fileOptions
-f,--file [NAME|-:]FILENAMEspecifies a file to crunch. This option can be repeated to include multiple files. An optional name can be given before the filename, separated by:.-d,--directory DIRECTORYspecifies a directory to crunch. It is walked recursively and every regular file found is crunched as if it had been given with--file, the path walked to it —DIRECTORYincluded — being used to infer the OCaml name. Entries are visited in a stable order, so the output only depends on the contents of the directory. This option can be repeated.-e,--ext EXTENSIONrestricts--directoryto the files carrying this extension. The leading.is optional. This option can be repeated. Without it, every file is crunched.--lookup=NAMEalso emits a function mapping each crunched filename to its contents, so that they can be reached by name at run-time rather than through the bindingsmcrunchinfers. The function NAME returns an option. In this mode, it is possible to associate a file with its name even if the latter cannot be ocamlify (as an OCaml identifier).-o,--output FILENAMEwrites the output to the given file instead of stdout. The file must not already exist. Use-for stdout (the default).-a,--arrayserializes each file's contents as an array of strings. This is the default.-l,--listserializes each file's contents as a list of strings instead of an array.-s,--stringserializes each file's contents as a single string instead of an array of strings.-c,--cols COLSsets the number of octets per line in the hex output. Default is 16, maximum is 256.-uuses uppercase hex letters instead of the default lowercase.--with-commentsappends a human-readable ASCII representation of each line as an OCaml comment:
$ mcrunch -f foo.txt --with-comments
let foo_txt = [| "\x66\x6f\x6f\x0a" |] (* foo. *)Note that comments are not supported for --string output. A warning is printed on stderr:
$ mcrunch --string --with-comments -f foo.txt
Comments are not supported for string output. Not outputting comments.
let foo_txt = "\x66\x6f\x6f\x0a"Example
Given two files index.html and style.css, you can generate an OCaml module that embeds both:
$ mcrunch -f index.html -f style.css -o static.mlThe resulting static.ml contains two bindings, index_html and style_css, each holding the full file contents as a string array. You can then reference these values from your OCaml program and reconstruct the original content with String.concat "" (for arrays, Array.to_list first).
A whole tree of assets can be embedded at once, optionally restricted to some extensions:
$ mcrunch -d static -e html -e css -o static.ml
let static_index_html = [| … |]
let static_style_css = [| … |]When the file to serve is only known at run-time, --lookup gives the module a function to reach it by path, and lets mcrunch name the bindings so that any filename can be crunched:
$ mcrunch -d static -s --lookup -o static.ml
let d_0 = "…"
let d_1 = "…"
let read = function
| "static/index.html" -> Some d_0
| "static/style.css" -> Some d_1
| _ -> NoneWith --string, read hands back the string literal itself: nothing is concatenated or copied, and the contents stay where the linker put them.
Dependencies (8)
Dev Dependencies
None
Optional Dependencies
None
Used by
None
Conflicts
None
Build status
Results of building mcrunch with each compiler on check.ci.ocaml.org, run of against opam-repository 78c202b. All results for this package.
Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.
| Version | OCaml 4.11 | OCaml 4.14 | OCaml 5.2 | OCaml 5.4 | OCaml 5.5 |
|---|---|---|---|---|---|
| 0.0.5 (this version) | n/a | n/a | good | good | good |
- good Package successfully built
- partial One of the dependencies failed to build
- bad Package failed to build
- n/a Package is not available in this environment
- internal Internal failure