Skip to content

Commit

Permalink
Merge pull request #309 from NathanReb/add-cli-args-to-lockfile
Browse files Browse the repository at this point in the history
Save CLI arguments to the lock file
  • Loading branch information
NathanReb authored Jun 9, 2022
2 parents a76c324 + 60f99fa commit e319cfe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
@NathanReb)
- Add command line options to complement or overwrite `x-opam-monorepo-*`
fields. (#307, @NathanReb)
- Save the `lock` CLI arguments in `x-opam-monorepo-cli-args` when generating a
lock file. (#309, @NathanReb)

### Changed

Expand Down
8 changes: 7 additions & 1 deletion cli/lock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ let extract_source_config ~adjustment ~opam_monorepo_cwd ~opam_files
Source_opam_config.make ~opam_monorepo_cwd ~adjustment
~local_opam_files_config

let raw_cli_args () =
match Array.to_list Sys.argv with
| _bin :: "lock" :: args -> args
| _ -> assert false

let run (`Root root) (`Recurse_opam recurse) (`Build_only build_only)
(`Allow_jbuilder allow_jbuilder) (`Ocaml_version ocaml_version)
(`Require_cross_compile require_cross_compile)
Expand Down Expand Up @@ -485,8 +490,9 @@ let run (`Root root) (`Recurse_opam recurse) (`Build_only build_only)
Lockfile.create ~source_config ~root_packages:target_packages
~dependency_entries ~root_depexts:target_depexts ~duniverse ()
in
let cli_args = raw_cli_args () in
let* () =
Lockfile.save ~opam_monorepo_cwd:root ~file:lockfile_path lockfile
Lockfile.save ~opam_monorepo_cwd:root ~cli_args ~file:lockfile_path lockfile
in
Common.Logs.app (fun l ->
l
Expand Down
19 changes: 17 additions & 2 deletions lib/lockfile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,21 @@ module Depexts = struct
List.concat all |> List.sort_uniq ~cmp:compare_elm
end

module Cli_args = struct
(** Field used to store the raw command line arguments passed to [lock].
It is set but not read and therefore is not part of the main lock file
type. *)

type _t = string list

let name = "cli-args"
let shape = Serial_shape.(list string)
let from_opam_value value = Serial_shape.from_opam_val shape value
let to_opam_value t = Serial_shape.to_opam_val shape t
let field = Extra_field.make ~name ~to_opam_value ~from_opam_value
let add t opam = match t with [] -> opam | _ -> Extra_field.set field t opam
end

type t = {
version : Version.t;
root_packages : Root_packages.t;
Expand Down Expand Up @@ -364,8 +379,8 @@ let from_opam ~opam_monorepo_cwd ?file opam =
source_config;
}

let save ~opam_monorepo_cwd ~file t =
let opam = to_opam ~opam_monorepo_cwd t in
let save ~opam_monorepo_cwd ~cli_args ~file t =
let opam = Cli_args.add cli_args (to_opam ~opam_monorepo_cwd t) in
Bos.OS.File.with_oc file
(fun oc () ->
OpamFile.OPAM.write_to_channel oc opam;
Expand Down
1 change: 1 addition & 0 deletions lib/lockfile.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ val ocaml_version : t -> OpamPackage.Version.t option

val save :
opam_monorepo_cwd:Fpath.t ->
cli_args:string list ->
file:Fpath.t ->
t ->
(unit, [ `Msg of string ]) result
Expand Down
5 changes: 5 additions & 0 deletions test/bin/cli-args-in-lockfile.t/cli-args-in-lockfile.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
opam-version: "2.0"
depends: [
"dune"
"ocaml"
]
7 changes: 7 additions & 0 deletions test/bin/cli-args-in-lockfile.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
When generating a lockfile, the CLI arguments passed to lock
should be save to the lockfile

$ gen-minimal-repo
$ opam-monorepo lock --recurse --opam-provided [b] --opam-repositories '[file://$OPAM_MONOREPO_CWD/minimal-repo]' --ocaml-version 4.13.1 > /dev/null
$ opam show --just-file -fx-opam-monorepo-cli-args ./cli-args-in-lockfile.opam.locked
--recurse, --opam-provided, [b], --opam-repositories, [file://$OPAM_MONOREPO_CWD/minimal-repo], --ocaml-version, 4.13.1

0 comments on commit e319cfe

Please sign in to comment.