package drom_lib

  1. Overview
  2. Docs

Source file commandDep.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2020 OCamlPro & Origin Labs                               *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open Ezcmd.V2
open EZCMD.TYPES
open Update
open Types

let cmd_name = "dep"

let print_dep (name, d) =
  Printf.printf "Dependency: %S\n%!" name;
  Printf.printf "  version (--ver) : %s\n%!"
    ( Project.string_of_versions d.depversions );
  Printf.printf "  libname (--lib) : %s\n%!"
    (match d.depname with
     | None -> name
     | Some name -> name);
  Printf.printf "  for-test (--test) : %b\n%!" d.deptest;
  Printf.printf "  for-doc (--test) : %b\n%!" d.depdoc;
  ()

let action ~dep
    ~package ~tool
    ~add ~remove
    ~version ~depname ~deptest ~depdoc ~depopt ~args =
  let p, _inferred_dir = Project.get () in
  let upgrade = ref args.arg_upgrade in
  let update package_kind dep_kind deps setdeps =

    match dep with
    | None ->
        Printf.printf "Printing %s %s dependencies:\n%!" package_kind dep_kind;
        List.iter print_dep deps

    | Some dep ->
        if add then begin
          if List.mem_assoc dep deps then
            Error.raise "%S is already a dependency" dep;

          let d = {
            depversions = ( match version with
                | None -> []
                | Some version -> Project.versions_of_string version );
            depname ;
            deptest = ( match deptest with
                | None -> false
                | Some b -> b );
            depdoc = ( match depdoc with
                | None -> false
                | Some b -> b );
            depopt = ( match depopt with
                | None -> false
                | Some b -> b );
          } in
          upgrade := true;
          Printf.eprintf "Adding %s %s dependency %S\n%!"
            package_kind dep_kind dep;
          setdeps ( (dep, d) :: deps )

        end else begin
          if not ( List.mem_assoc dep deps ) then
            Error.raise "%S is not a current dependency" dep;

          if remove then begin
            let deps = List.filter (fun (name, _) -> name <> dep) deps in
            setdeps deps;
            upgrade := true;
            Printf.eprintf "Removed %s %s dependency %S\n%!"
              package_kind dep_kind dep
          end else
            let deps = List.map (fun (name, d) ->
                if name = dep then
                  let d = match version with
                    | None -> d
                    | Some version ->
                        upgrade := true;
                        { d with
                          depversions = Project.versions_of_string version }
                  in
                  let d = match depname with
                    | None -> d
                    | Some depname ->
                        upgrade := true;
                        { d with
                          depname =
                            if depname = dep then None else Some depname }
                  in
                  let d = match deptest with
                    | None -> d
                    | Some deptest ->
                        upgrade := true; { d with deptest }
                  in
                  let d = match depdoc with
                    | None -> d
                    | Some depdoc ->
                        upgrade := true; { d with depdoc }
                  in
                  let d = match depopt with
                    | None -> d
                    | Some depopt ->
                        upgrade := true; { d with depopt }
                  in

                  if not !upgrade then begin
                    print_dep (dep, d);
                    exit 0;
                  end;
                  Printf.eprintf "Updating %s %s dependency %S\n%!"
                    package_kind dep_kind dep;
                  (name, d)
                else
                  (name, d)
              ) deps in
            setdeps deps

        end
  in

  begin
    match package with
    | None ->
        let kind, deps, setdeps =
          if tool then
            "tools", p.tools, (fun deps -> p.tools <- deps)
          else
            "libs", p.dependencies, (fun deps -> p.dependencies <- deps)
        in
        update "project" kind deps setdeps
    | Some name ->
        List.iter (fun package ->
            if package.name = name then
              let kind, deps, setdeps =
                if tool then
                  "tools", package.p_tools,
                  (fun deps -> package.p_tools <- deps)
                else
                  "libs", package.p_dependencies,
                  (fun deps -> package.p_dependencies <- deps)
              in
              update "package" kind deps setdeps
          ) p.packages
  end;

  if !upgrade then
    let args = { args with arg_upgrade = !upgrade } in
    Update.update_files ~twice:false ~create:false ~git:true p ~args;
    ()

let cmd =
  let package = ref None in
  let dep = ref None in
  let tool = ref false in
  let add = ref false in
  let remove = ref false in
  let version = ref None in
  let depname = ref None in
  let deptest = ref None in
  let depdoc = ref None in
  let depopt = ref None in
  let args, specs = Update.update_args () in
  EZCMD.sub cmd_name
    (fun () ->
       action ~dep:(!dep)
         ~package:!package ~tool:!tool
         ~add:!add ~remove:!remove
         ~version:!version ~depname:!depname
         ~deptest:!deptest ~depdoc:!depdoc
         ~depopt:!depopt
         ~args
    )
    ~args: (
      specs @
      [ [ "package" ],
        Arg.String (fun s -> package := Some s),
        EZCMD.info ~docv:"PACKAGE" "Attach dependency to this package name" ;
        [ "tool" ], Arg.Unit (fun () -> tool := true),
        EZCMD.info "Dependency is a tool, not a library" ;
        [ "add" ], Arg.Unit (fun () -> add := true),
        EZCMD.info "Add as new dependency" ;
        [ "remove" ], Arg.Unit (fun () -> tool := true),
        EZCMD.info "Remove this dependency" ;
        [ "ver" ], Arg.String (fun s -> version := Some s),
        EZCMD.info ~docv:"VERSION" "Dependency should have this version" ;
        [ "lib" ], Arg.String (fun s -> depname := Some s),
        EZCMD.info ~docv:"LIBNAME"
          "Dependency should have this libname in dune" ;
        [ "test" ], Arg.Bool (fun b -> deptest := Some b),
        EZCMD.info "Whether dependency is only for tests" ;
        [ "doc" ], Arg.Bool (fun b -> depdoc := Some b),
        EZCMD.info "Whether dependency is only for doc" ;
        [ "opt" ], Arg.Bool (fun b -> depopt := Some b),
        EZCMD.info "Whether dependency is optional or not" ;
        ( [],
          Arg.Anon (0, fun name -> dep := Some name),
          EZCMD.info ~docv:"DEPENDENCY" "Name of dependency" )
      ]
    )
    ~doc: "Manage dependency of a package"
    ~version:"0.2.1"
    ~man: [
      `S "DESCRIPTION";
      `Blocks [
        `P "Add, remove and modify dependencies from $(b,drom.toml) and  $(b,package.toml) files.";
        `P "If the argument $(b,--package) is not specified, the dependency is added project-wide (i.e. for all packages), updating the $(i,drom.toml) file.";
        `P "If the argument $(b,--package) is provided, the dependency is added to the $(i,package.toml) file for that particular package.";
        `P "Dependencies can be added $(b,--add), removed $(b,--remove) or just modified. The $(b,--tool) argument should be used for tool dependencies, i.e. dependencies that are not linked to the library/program.";
        `P "If no modification argument is provided, the dependency is printed in the terminal. Modification arguments are $(b,--ver VERSION) for the version, $(b,--lib LIBNAME) for the $(i,dune) library name, $(b,--doc BOOL) for documentation deps and $(b,--test BOOL) for test deps.";
      ];
      `S "EXAMPLE";
      `Pre {|
drom dep --package drom_lib --add ez_cmdliner --ver ">0.1"
drom dep --package drom_lib --remove ez_cmdliner
drom dep --add --tool odoc --ver ">1.0 <3.0" --doc true
|};
      `S "VERSION SPECIFICATION";
      `P "The version specified in the $(b,--ver VERSION) argument should follow the following format:";
      `I ("1.", "Spaces are used to separate a conjunction of version constraints.");
      `I ("2.", "An empty string is equivalent to no version constraint.");
      `I ("3.", "Constraints are specified using a comparison operation directly followed by the version, like $(b,>1.2) or $(b,<=1.0).");
      `I ("4.",
          {|A semantic version like $(b,1.2.3) is equivalent to the constraints  $(b,>=1.2.3) and $(b,<2.0.0).|});
    ]