diff --git a/.gitignore b/.gitignore index d0469292e..64ecd2a89 100644 --- a/.gitignore +++ b/.gitignore @@ -66,3 +66,7 @@ tests/shredding/config # Merlin *.merlin + +# Autogenerated files +config +install.inc diff --git a/dune b/dune index f5bcb970c..ebfd20965 100644 --- a/dune +++ b/dune @@ -18,3 +18,10 @@ (ocamlopt_flags (:standard -O3 ;; Applies (aggressive) optimisations to the resulting executable. )))) + +(install + (package links) + (section doc) + (files (INSTALL as README))) + +(include install.inc) diff --git a/install.inc b/install.inc new file mode 100644 index 000000000..8d1e46e06 --- /dev/null +++ b/install.inc @@ -0,0 +1 @@ +; this file is empty on purpose diff --git a/links.opam b/links.opam index 5abf2c823..2e7100e30 100644 --- a/links.opam +++ b/links.opam @@ -10,47 +10,10 @@ license: "GPL-2" build: [ [ "dune" "subst" ] {pinned} + [ "dune" "exec" "preinstall/preinstall.exe" "--" "-libdir" links:lib ] [ "dune" "build" "-p" name "-j" jobs ] ] -install: [ -# Install documentation - [ "mkdir" "-p" "%{doc}%" ] {with-doc} - [ "cp" "INSTALL" "%{doc}%/README" ] {with-doc} -# Copy examples - [ "mkdir" "-p" "%{share}%/examples" ] - [ "sh" "-c" "cp -r examples/dbsetup %{share}%/examples/dbsetup/" ] - [ "sh" "-c" "cp examples/*.links %{share}%/examples" ] - [ "sh" "-c" "cp examples/*.jpg %{share}%/examples" ] - [ "sh" "-c" "cp examples/*.sql %{share}%/examples" ] - [ "sh" "-c" "cp examples/*.html %{share}%/examples" ] - [ "sh" "-c" "cp examples/*.css %{share}%/examples" ] - [ "sh" "-c" "cp examples/*.js %{share}%/examples" ] - [ "sh" "-c" "cp -r examples/dictionary %{share}%/examples/" ] - [ "sh" "-c" "cp -r examples/games %{share}%/examples/" ] - [ "sh" "-c" "cp -r examples/sessions %{share}%/examples/" ] - [ "sh" "-c" "cp -r examples/webserver %{share}%/examples/" ] - [ "sh" "-c" "cp -r examples/css %{share}%/examples/" ] -# Generate and install a config file - [ "touch" "config" ] - [ "sh" "-c" "echo jsliburl=/lib/js > config" ] - [ "sh" "-c" "echo jslibdir=%{lib}%/js >> config" ] - [ "sh" "-c" "echo #database_driver=postgresql >> config" ] - [ "sh" "-c" "echo #database_args=localhost:5432:user:pass >> config" ] - [ "mkdir" "-p" "%{etc}%" ] - [ "cp" "config" "%{etc}%" ] -] - -remove: [ - [ "rm" "-f" "%{bin}%/linx" ] - [ "rm" "-f" "%{lib}%/prelude.links" ] - [ "rm" "-rf" "%{lib}%/stdlib" ] - [ "rm" "-rf" "%{lib}%/js" ] - [ "rm" "-rf" "%{share}%/examples" ] - [ "rm" "-f" "%{etc}%/config" ] - [ "rm" "-f" "%{doc}%/README" ] {with-doc} -] - depends: [ "ocaml" {>= "4.06.0"} "dune" {build} diff --git a/preinstall/dune b/preinstall/dune new file mode 100644 index 000000000..8db21781b --- /dev/null +++ b/preinstall/dune @@ -0,0 +1,3 @@ +(executable + (name preinstall) + (libraries dune.configurator)) diff --git a/preinstall/preinstall.ml b/preinstall/preinstall.ml new file mode 100644 index 000000000..8700a9cbd --- /dev/null +++ b/preinstall/preinstall.ml @@ -0,0 +1,70 @@ +module C = Configurator.V1 + +let rec get_content path = + let join prefix file = if prefix="" then file else Printf.sprintf "%s/%s" prefix file in + if Sys.is_directory path then + Array.map (join path) (Sys.readdir path) + |> Array.fold_left (fun acc path -> + let content = get_content path in + Array.append acc content) [||] + else + [|path|] + +let make_stanza section files = + Printf.sprintf + "(install\n\ + \ (package links)\n\ + \ (section %s)\n\ + \ (files %s))\n" + section files + +let install_examples contents = + contents + |> Array.to_list + |> List.map (fun file -> Printf.sprintf {|(%s as %s)|} file file) + |> String.concat "\n " + |> make_stanza "share" + +let install_config = + make_stanza "etc" "config" + +let write_content filename = + let examples = + get_content "examples" + |> install_examples + |> String.split_on_char '\n' + in + let config = + install_config + |> String.split_on_char '\n' + in + C.Flags.write_lines filename (List.concat [examples; config]) + + +let config libdir = [ + "jsliburl=/lib/js"; + Printf.sprintf "jslibdir=%s/js" libdir; + "#database_driver=postgresql"; + "#database_args=localhost:5432:user:pass" ] + +let write_config libdir = + config libdir + |> C.Flags.write_lines "config" + +let _ = + let libdir = ref None in + let incfile = ref "install.inc" in + C.main ~name:"preinstall" + ~args:[ + "-libdir", Arg.String (fun s -> libdir := Some s), + "Path to the opam package libdir" + ; "-output", Arg.String (fun s -> incfile := s), + "Name for the install sexp output (default install.inc)"] + (fun _ -> + begin match !libdir with + | Some libdir -> write_config libdir + | _ -> failwith "-libdir is needed" + end; + write_content !incfile; + ) +