Skip to content

Commit

Permalink
use Fpath.t where possible, drop ocaml < 4.14
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Jan 20, 2025
1 parent 2244f26 commit 0bd3951
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- "5.3"
include:
- os: ubuntu-latest
ocaml-compiler: "4.07"
ocaml-compiler: "4.14"
runs-on: ${{ matrix.os }}
steps:
- name: checkout
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## unreleased

- use `Fpath.t` instead of `string` where possible

## 0.5 - 2022-01-09

- add Base_dirs.state_dir, on linux it uses $XDG_STATE_HOME and default to $HOME/.local/.state on macOS and Windows it's equivalent to Base_dirs.cache_dir ; add Projects_dirs.state_dir
Expand Down
3 changes: 2 additions & 1 deletion directories.opam
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ homepage: "https://github.com/ocamlpro/directories"
bug-reports: "https://github.com/ocamlpro/directories/issues"
depends: [
"dune" {>= "2.1"}
"ocaml" {>= "4.07.0"}
"ocaml" {>= "4.14.0"}
"ctypes" {>= "0.17.1" & (os = "win32" | os = "cygwin")}
"fpath"
]
build: [
["dune" "subst"] {pinned}
Expand Down
2 changes: 1 addition & 1 deletion example/print_dir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let () =
(* functions to print a dir path *)
let print_dir = function
| None, s -> Format.printf " %s None@." s
| Some dir, s -> Format.printf " %s Some `%s`@." s dir
| Some dir, s -> Format.printf " %s Some `%a`@." s Fpath.pp dir
in
let print_dirs = List.iter print_dir in

Expand Down
2 changes: 1 addition & 1 deletion example/quick_start.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let () =
let application = "yourapp"
end in
let module M = Directories.Project_dirs (App_id) in
let option_value = function None -> "None" | Some v -> v in
let option_value = function None -> "None" | Some v -> Fpath.to_string v in
Format.printf "cache dir = `%s`@." (option_value M.cache_dir);
Format.printf "config dir = `%s`@." (option_value M.config_dir);
Format.printf "data dir = `%s`@." (option_value M.data_dir)
52 changes: 26 additions & 26 deletions src/directories.mli
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
module Base_dirs () : sig
val home_dir : string option
val home_dir : Fpath.t option

val cache_dir : string option
val cache_dir : Fpath.t option

val config_dir : string option
val config_dir : Fpath.t option

val data_dir : string option
val data_dir : Fpath.t option

val data_local_dir : string option
val data_local_dir : Fpath.t option

val preference_dir : string option
val preference_dir : Fpath.t option

val runtime_dir : string option
val runtime_dir : Fpath.t option

val state_dir : string option
val state_dir : Fpath.t option

val executable_dir : string option
val executable_dir : Fpath.t option
end

module User_dirs () : sig
val home_dir : string option
val home_dir : Fpath.t option

val audio_dir : string option
val audio_dir : Fpath.t option

val desktop_dir : string option
val desktop_dir : Fpath.t option

val document_dir : string option
val document_dir : Fpath.t option

val download_dir : string option
val download_dir : Fpath.t option

val font_dir : string option
val font_dir : Fpath.t option

val picture_dir : string option
val picture_dir : Fpath.t option

val public_dir : string option
val public_dir : Fpath.t option

val template_dir : string option
val template_dir : Fpath.t option

val video_dir : string option
val video_dir : Fpath.t option
end

module Project_dirs (App_id : sig
Expand All @@ -47,17 +47,17 @@ module Project_dirs (App_id : sig

val application : string
end) : sig
val cache_dir : string option
val cache_dir : Fpath.t option

val config_dir : string option
val config_dir : Fpath.t option

val data_dir : string option
val data_dir : Fpath.t option

val data_local_dir : string option
val data_local_dir : Fpath.t option

val preference_dir : string option
val preference_dir : Fpath.t option

val runtime_dir : string option
val runtime_dir : Fpath.t option

val state_dir : string option
val state_dir : Fpath.t option
end
16 changes: 6 additions & 10 deletions src/directories_common.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let ( / ) = Filename.concat

module type App_id = sig
val qualifier : string

Expand All @@ -8,21 +6,19 @@ module type App_id = sig
val application : string
end

(* TODO: remove once we drop 4.07 *)
let option_map f = function None -> None | Some v -> Some (f v)

(* TODO: remove once we drop 4.07 *)
let option_bind o f = match o with None -> None | Some v -> f v

let relative_opt dir = if Filename.is_relative dir then None else Some dir
let relative_opt dir = if Fpath.is_rel dir then None else Some dir

let getenv env =
match Sys.getenv env with
| exception Not_found -> None
| "" -> None
| v -> Some v

let getenvdir env = option_bind (getenv env) relative_opt
let getenvdir env =
match getenv env with
| None -> None
| Some v -> (
match Fpath.of_string v with Error _ -> None | Ok v -> relative_opt v )

let lower_and_replace_ws s replace =
let s = String.trim s in
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let () =
(wrapped false)
(modules directories directories_common)
(private_modules directories_common)
(libraries %s))
(libraries fpath %s))

(copy_files# %s/*)
|}
Expand Down
46 changes: 29 additions & 17 deletions src/linux/directories.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,27 @@ module Base_dirs () = struct
match (Unix.getpwuid (Unix.getuid ())).Unix.pw_dir with
| exception Unix.Unix_error _ -> None
| exception Not_found -> None
| dir -> relative_opt dir )
| dir ->
let dir = Fpath.of_string dir |> Result.to_option in
Option.bind dir relative_opt )
| Some _dir as dir -> dir

(** $XDG_CACHE_HOME or $HOME/.cache *)
let cache_dir =
match getenvdir "XDG_CACHE_HOME" with
| None -> option_map (fun dir -> dir / ".cache") home_dir
| None -> Option.map (fun dir -> Fpath.(dir / ".cache")) home_dir
| Some _dir as dir -> dir

(** $XDG_CONFIG_HOME or $HOME/.config *)
let config_dir =
match getenvdir "XDG_CONFIG_DIR" with
| None -> option_map (fun dir -> dir / ".config") home_dir
| None -> Option.map (fun dir -> Fpath.(dir / ".config")) home_dir
| Some _dir as dir -> dir

(** $XDG_DATA_HOME or $HOME/.local/share *)
let data_dir =
match getenvdir "XDG_DATA_DIR" with
| None -> option_map (fun dir -> dir / ".local" / "share") home_dir
| None -> Option.map (fun dir -> Fpath.(dir / ".local" / "share")) home_dir
| Some _dir as dir -> dir

(** $XDG_DATA_HOME or $HOME/.local/share *)
Expand All @@ -39,7 +41,7 @@ module Base_dirs () = struct
(** $XDG_STATE_HOME or $HOME/.local/state *)
let state_dir =
match getenvdir "XDG_STATE_HOME" with
| None -> option_map (fun dir -> dir / ".local" / "state") home_dir
| None -> Option.map (fun dir -> Fpath.(dir / ".local" / "state")) home_dir
| Some _dir as dir -> dir

(** $XDG_RUNTIME_DIR *)
Expand All @@ -50,8 +52,8 @@ module Base_dirs () = struct
match getenvdir "XDG_BIN_HOME" with
| None -> (
match getenvdir "XDG_DATA_HOME" with
| None -> option_map (fun dir -> dir / ".local" / "bin") home_dir
| Some dir -> Some (dir / ".." / "bin") )
| None -> Option.map (fun dir -> Fpath.(dir / ".local" / "bin")) home_dir
| Some dir -> Some Fpath.(dir / ".." / "bin") )
| Some _dir as dir -> dir
end

Expand All @@ -63,14 +65,17 @@ module User_dirs () = struct
let home_dir = Base_dirs.home_dir

let user_dirs =
option_map (fun dir -> dir / "user-dirs.dirs") Base_dirs.config_dir
Option.map (fun dir -> Fpath.(dir / "user-dirs.dirs")) Base_dirs.config_dir

let user_dirs =
option_bind user_dirs (fun f -> if Sys.file_exists f then Some f else None)
Option.bind user_dirs (fun f ->
(* TODO: use Bos here instead of Sys? *)
if Sys.file_exists (Fpath.to_string f) then Some f else None )

let user_dirs =
option_bind user_dirs (fun f ->
if Sys.is_directory f then None else Some f )
Option.bind user_dirs (fun f ->
(* TODO: use Bos here instead of Sys? *)
if Sys.is_directory (Fpath.to_string f) then None else Some f )

let user_shell = getenv "SHELL"

Expand All @@ -80,18 +85,23 @@ module User_dirs () = struct
try
let chan =
Unix.open_process_in
(Format.sprintf "%s -c '. %s && echo \"$XDG_%s_DIR\"'" sh f dir)
(Format.asprintf "%s -c '. %a && echo \"$XDG_%s_DIR\"'" sh Fpath.pp
f dir )
in
let xdg = input_line chan in
let result = Unix.close_process_in chan in
match result with WEXITED 0 -> Some xdg | _ -> None
match result with
| WEXITED 0 -> begin
match Fpath.of_string xdg with Error _ -> None | Ok xdg -> Some xdg
end
| _ -> None
with _ -> None )
| _ -> None

let get_user_dir (env, default) =
match get_user_dir env with
| Some v -> Some v
| None -> option_map (fun dir -> dir / default) home_dir
| None -> Option.map (fun dir -> Fpath.(dir / default)) home_dir

(** Defaults can be found here
https://cgit.freedesktop.org/xdg/xdg-user-dirs/tree/user-dirs.defaults *)
Expand All @@ -112,8 +122,10 @@ module User_dirs () = struct
let font_dir =
match getenvdir "XDG_DATA_HOME" with
| None ->
option_map (fun dir -> dir / ".local" / "share" / "fonts") home_dir
| Some dir -> Some (dir / "fonts")
Option.map
(fun dir -> Fpath.(dir / ".local" / "share" / "fonts"))
home_dir
| Some dir -> Some Fpath.(dir / "fonts")

(** $XDG_PICTURES_DIR *)
let picture_dir = get_user_dir ("PICTURES", "Pictures")
Expand All @@ -134,7 +146,7 @@ module Project_dirs (App_id : App_id) = struct
let project_path =
Directories_common.lower_and_replace_ws App_id.application ""

let concat_project_path = option_map (fun dir -> dir / project_path)
let concat_project_path = Option.map (fun dir -> Fpath.(dir / project_path))

(** $XDG_CACHE_HOME/<project_path> or $HOME/.cache/<project_path> *)
let cache_dir = concat_project_path Base_dirs.cache_dir
Expand Down
28 changes: 19 additions & 9 deletions src/macos/directories.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,33 @@ module Base_dirs () = struct
match (Unix.getpwuid (Unix.getuid ())).Unix.pw_dir with
| exception Unix.Unix_error _ -> None
| exception Not_found -> None
| dir -> relative_opt dir )
| Some _dir as dir -> dir
| dir ->
let dir = Fpath.of_string dir |> Result.to_option in
Option.bind dir relative_opt )
| Some _ as dir -> dir

(** $HOME/Library/Caches *)
let cache_dir = option_map (fun dir -> dir / "Library" / "Caches") home_dir
let cache_dir =
Option.map (fun dir -> Fpath.(dir / "Library" / "Caches")) home_dir

(** $HOME/Library/Application Support *)
let config_dir =
option_map (fun dir -> dir / "Library" / "Application Support") home_dir
Option.map
(fun dir -> Fpath.(dir / "Library" / "Application Support"))
home_dir

(** $HOME/Library/Application Support *)
let data_dir =
option_map (fun dir -> dir / "Library" / "Application Support") home_dir
Option.map
(fun dir -> Fpath.(dir / "Library" / "Application Support"))
home_dir

(** $HOME/Library/Application Support *)
let data_local_dir = data_dir

(** $HOME/Library/Preferences *)
let preference_dir =
option_map (fun dir -> dir / "Library" / "Preferences") home_dir
Option.map (fun dir -> Fpath.(dir / "Library" / "Preferences")) home_dir

(** None *)
let runtime_dir = None
Expand All @@ -46,7 +53,8 @@ module User_dirs () = struct
user database) *)
let home_dir = Base_dirs.home_dir

let concat_home_dir suffix = option_map (fun dir -> dir / suffix) home_dir
let concat_home_dir suffix =
Option.map (fun dir -> Fpath.(dir / suffix)) home_dir

(** $HOME/Music *)
let audio_dir = concat_home_dir "Music"
Expand All @@ -61,7 +69,9 @@ module User_dirs () = struct
let download_dir = concat_home_dir "Downloads"

(** $HOME/Library/Fonts *)
let font_dir = concat_home_dir ("Library" / "Fonts")
let font_dir =
let library_dir = concat_home_dir "Library" in
Option.map (fun dir -> Fpath.(dir / "Fonts")) library_dir

(** $HOME/Pictures *)
let picture_dir = concat_home_dir "Pictures"
Expand Down Expand Up @@ -90,7 +100,7 @@ module Project_dirs (App_id : App_id) = struct
let project_path =
Format.sprintf "%s.%s.%s" qualifier organization application

let concat_project_path = option_map (fun dir -> dir / project_path)
let concat_project_path = Option.map (fun dir -> Fpath.(dir / project_path))

(** $HOME/Libary/Caches/<project_path> *)
let cache_dir = concat_project_path Base_dirs.cache_dir
Expand Down
Loading

0 comments on commit 0bd3951

Please sign in to comment.