Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Moved tilde expansion of filenames into dedicated function #5822

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/core/opamFilename.ml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,20 @@ module Dir = struct
let equal = String.equal

let of_string dirname =
OpamSystem.(real_path @@ forward_to_back dirname)

let of_string_resolved dirname =
let home = OpamStd.Sys.home () in
let prefix = "~" ^ Filename.dir_sep in
let dirname =
if dirname = "~" then OpamStd.Sys.home ()
if dirname = "~" then home
else if
OpamStd.String.starts_with ~prefix:("~"^Filename.dir_sep) dirname
OpamStd.String.starts_with ~prefix dirname
then
Filename.concat (OpamStd.Sys.home ())
(OpamStd.String.remove_prefix ~prefix:("~"^Filename.dir_sep) dirname)
Filename.concat home (OpamStd.String.remove_prefix ~prefix dirname)
else dirname
in
OpamSystem.real_path (OpamSystem.forward_to_back dirname)
of_string dirname

let to_string dirname = dirname

Expand Down Expand Up @@ -188,6 +192,14 @@ let of_string s =
basename = Base.of_string basename;
}

let of_string_resolved s =
let dirname = Filename.dirname s in
let basename = Filename.basename s in
{
dirname = Dir.of_string_resolved dirname;
basename = Base.of_string basename;
}

let dirname t = t.dirname

let basename t = t.basename
Expand Down
10 changes: 9 additions & 1 deletion src/core/opamFilename.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ module Base: sig
end

(** Directory names *)
module Dir: OpamStd.ABSTRACT
module Dir: sig
include OpamStd.ABSTRACT

(** Resolve tilde (~) into the home directory. *)
val of_string_resolved: string -> t
end

(** Return the current working directory *)
val cwd: unit -> Dir.t
Expand Down Expand Up @@ -116,6 +121,9 @@ val create: Dir.t -> Base.t -> t
as dirname *)
val of_basename: Base.t -> t

(** Resolve tilde (~) into the home directory. *)
val of_string_resolved: string -> t

(** Creation from a raw string, without resolving symlinks, etc. *)
val raw: string -> t

Expand Down