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

OpamFilter file substitutions with source & destination #5629

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,7 @@ users)

## opam-format

* Add `OpamFilter.expand_interpolations_in_file_full` which allows setting the
output file along with the input file [#5629 @rgrinberg]

## opam-core
14 changes: 9 additions & 5 deletions src/format/opamFilter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,10 @@ let ident_string ?default env id = value_string ?default (resolve_ident env id)

let ident_bool ?default env id = value_bool ?default (resolve_ident env id)

(* Substitute the file contents *)
let expand_interpolations_in_file env file =
let f = OpamFilename.of_basename file in
let src = OpamFilename.add_extension f "in" in
(* Substitute the file contents and specify the source and destination *)
let expand_interpolations_in_file_full env ~src ~dst =
let ic = OpamFilename.open_in_bin src in
let oc = OpamFilename.open_out_bin f in
let oc = OpamFilename.open_out_bin dst in
(* Determine if the input file parses in opam-file-format *)
let is_opam_format =
try
Expand Down Expand Up @@ -455,6 +453,12 @@ let expand_interpolations_in_file env file =
close_in ic;
close_out oc

(* Substitute the file contents *)
let expand_interpolations_in_file env file =
let file = OpamFilename.of_basename file in
let src = OpamFilename.add_extension file "in" in
expand_interpolations_in_file_full env ~src ~dst:file

(* Apply filters and interpolations to package commands *)

let arguments env (a,f) =
Expand Down
4 changes: 4 additions & 0 deletions src/format/opamFilter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ val ident_string: ?default:string -> env -> fident -> string
(** Like [ident_value], but casts the result to a bool *)
val ident_bool: ?default:bool -> env -> fident -> bool

val expand_interpolations_in_file_full: env -> src:filename -> dst:filename -> unit
Leonidas-from-XIV marked this conversation as resolved.
Show resolved Hide resolved
(** Same as [expand_interpolations_in_file] but allows to set the source [src] and
destination [dst] files independently instead of implying [src] = [dst].in *)

(** Rewrites [basename].in to [basename], expanding interpolations.
If the first line begins ["opam-version:"], assumes that expansion of
variables within strings should be properly escaped. In particular, this
Expand Down