summaryrefslogtreecommitdiff
path: root/lib/working_directory.ml
diff options
context:
space:
mode:
author·𐑑𐑴𐑕𐑑𐑩𐑀2025-12-10 13:00:26 +0000
committer·𐑑𐑴𐑕𐑑𐑩𐑀2025-12-10 13:00:26 +0000
commit3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0 (patch)
tree5ce28db0cd6a4f15a7626fb1b9982e13a7b6f086 /lib/working_directory.ml
parentd3f85acf813d78c6d9972c8f10ff9c3a76bd0f08 (diff)
downloadnixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar.gz
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar.bz2
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar.lz
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar.xz
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.tar.zst
nixtaml-3df27ffb2bd40f3eaeed6dfb08ef3041cc60bfe0.zip
ocaml onset
Diffstat (limited to 'lib/working_directory.ml')
-rw-r--r--lib/working_directory.ml76
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/working_directory.ml b/lib/working_directory.ml
new file mode 100644
index 0000000..5946890
--- /dev/null
+++ b/lib/working_directory.ml
@@ -0,0 +1,76 @@
+(*─────────────────────────────────────────────────────────────────────────────┐
+β”‚ SPDX-FileCopyrightText: 2025 toastal <https://toast.al/contact/> β”‚
+β”‚ SPDX-License-Identifier: LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception β”‚
+└─────────────────────────────────────────────────────────────────────────────*)
+let working_directory : [`Dir] Eio.Path.t option ref = ref None
+
+let get () =
+ match !working_directory with
+ | Some dir -> dir
+ | None -> failwith "Working directory not set up"
+
+let set ~directory =
+ working_directory := Some directory
+
+let set_default ~env () =
+ let new_dir =
+ let cwd = Eio.Stdenv.cwd env in
+ Eio.Path.(cwd / "nix" / "tamal")
+ in
+ working_directory := Some new_dir
+
+let pp_native_path =
+ Fmt.using
+ Eio.Path.native
+ (Fmt.option (fun ppf -> Fmt.pf ppf " @@ %a" Fmt.string))
+
+(* Without the need for magic strings, we can use tabs in Nix! *)
+let root_editor_config_content =
+ {|root = true
+
+[*]
+charset = utf-8
+end_of_line = lf
+indent_style = tab
+insert_final_newline = true
+trim_trailing_whitespace = true
+|}
+
+let set_up_editor_config ~dir ~content =
+ let editor_config_file = Eio.Path.(dir / ".editorconfig") in
+ Logs.app (fun m -> m "Writing new Nixtamal EditorConfig%a …" pp_native_path editor_config_file);
+ Eio.Path.with_open_out ~create: (`Or_truncate 0o644) editor_config_file @@ fun flow ->
+ Eio.Buf_write.with_flow flow @@ fun writer ->
+ Eio.Buf_write.string writer content
+
+let set_up_root () =
+ let dir = get () in
+ match Eio.Path.kind ~follow: true dir with
+ | `Not_found ->
+ Logs.app (fun m -> m "Creating Nixtamal directory%a" pp_native_path dir);
+ Eio.Path.mkdirs ~perm: 0o755 dir;
+ set_up_editor_config ~dir ~content: root_editor_config_content
+ | `Directory ->
+ Logs.warn (fun m -> m "Nixtamal directory already exists%a" pp_native_path dir)
+ | _ ->
+ failwith @@ Fmt.str "There is a Nixtamal path, but is not a directory%a" pp_native_path dir
+
+let darcs_context_dir = "darcs_context"
+
+let darcs_context_editor_config_content =
+ {|root = true
+|}
+
+let set_up_darcs_context_if_needed () : (unit, string) result =
+ let dir = Eio.Path.(get () / darcs_context_dir) in
+ match Eio.Path.kind ~follow: true dir with
+ | `Directory ->
+ Ok ()
+ | `Not_found ->
+ Logs.app (fun m -> m "Creating Nixtamal’s darcs_context directory%a" pp_native_path dir);
+ Eio.Path.mkdirs ~perm: 0o755 dir;
+ (* as these files are just copied over, unset rules *)
+ set_up_editor_config ~dir ~content: darcs_context_editor_config_content;
+ Ok ()
+ | _ ->
+ Error (Fmt.str "There is a Nixtamal path, but is not a directory%a" pp_native_path dir)