Skip to content

Commit 380dcd9

Browse files
authored
Merge pull request #509 from Leonidas-from-XIV/opam-lib-lint
Use opam libs instead of `opam` CLI to lint
2 parents 88b2512 + 86d86ee commit 380dcd9

File tree

1 file changed

+27
-60
lines changed

1 file changed

+27
-60
lines changed

lib/lint.ml

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,6 @@ let lint_std_files ~dry_run pkg =
4646
in
4747
List.fold_left go 0 std_files
4848

49-
let lint_file_with_cmd ~dry_run ~file_kind ~cmd ~handle_exit file errs =
50-
let run_linter cmd file ~exists =
51-
if not (exists || dry_run) then
52-
Ok (`Fail (strf "%a: No such file" Fpath.pp file))
53-
else
54-
Sos.run_out ~dry_run ~err:OS.Cmd.err_run_out
55-
Cmd.(cmd % p file)
56-
~default:(Sos.out "") OS.Cmd.out_string
57-
>>| fun (out, status) -> handle_exit (snd status) out
58-
in
59-
Logs.on_error_msg
60-
~use:(fun () -> errs + 1)
61-
( OS.File.exists file >>= fun exists ->
62-
run_linter cmd file ~exists >>| function
63-
| `Ok ->
64-
Logs.app (fun m ->
65-
m "%a @[lint@ %s %a.@]" Text.Pp.status `Ok file_kind Text.Pp.path
66-
file);
67-
errs
68-
| `Fail msgs ->
69-
Logs.app (fun m ->
70-
m "%a @[<v>@[lint@ %s %a:@]@,@[%a messages:@]@,%a@]"
71-
Text.Pp.status `Fail file_kind Text.Pp.path file Cmd.pp cmd
72-
Fmt.lines msgs);
73-
errs + 1 )
74-
7549
let lint_res ~msgf = function
7650
| Ok _ ->
7751
App_log.report_status `Ok msgf;
@@ -91,32 +65,6 @@ let lint_opam_home_and_dev pkg =
9165

9266
let lint_opam_github_fields pkg = lint_opam_home_and_dev pkg
9367

94-
let opam_lint_cmd ~opam_file_version =
95-
let lint_older_format =
96-
match opam_file_version with
97-
| Some "1.2" ->
98-
let _ = Deprecate.Opam_1_x.remove_me in
99-
true
100-
| _ -> false
101-
in
102-
Cmd.(Opam.cmd % "lint" %% on lint_older_format (v "--warn=-21-32-48"))
103-
104-
(* We first run opam lint with -s and if there's something beyond 5
105-
we rerun it without it for the error messages. It's ugly since 5
106-
will still but opam lint's cli is broken. *)
107-
let handle_opam_lint_exit ~dry_run ~verbose_lint_cmd ~opam_file status output =
108-
match (status, output) with
109-
| `Exited 0, ("" | "5") -> `Ok
110-
| _ -> (
111-
let default = Sos.out "" in
112-
let err = OS.Cmd.err_run_out in
113-
let cmd = Cmd.(verbose_lint_cmd % p opam_file) in
114-
let verbose_lint_output =
115-
Sos.run_out ~dry_run ~err ~default cmd OS.Cmd.out_string
116-
in
117-
match verbose_lint_output with
118-
| Ok (out, _) | Error (`Msg out) -> `Fail out)
119-
12068
let check_has_synopsis ~opam_file pkg =
12169
Pkg.opam_field_hd pkg "synopsis" >>= function
12270
| None ->
@@ -128,13 +76,32 @@ let lint_descr ~opam_file pkg =
12876
~msgf:(fun l -> l "opam field %a is present" pp_field "synopsis")
12977
(check_has_synopsis ~opam_file pkg)
13078

131-
let opam_lint ~dry_run ~opam_file_version opam_file =
132-
let base_lint_cmd = opam_lint_cmd ~opam_file_version in
133-
let short_lint_cmd = Cmd.(base_lint_cmd % "-s") in
134-
let verbose_lint_cmd = base_lint_cmd in
135-
lint_file_with_cmd ~dry_run ~file_kind:"opam file" ~cmd:short_lint_cmd
136-
~handle_exit:(handle_opam_lint_exit ~dry_run ~verbose_lint_cmd ~opam_file)
137-
opam_file 0
79+
let opam_lint ~dry_run opam_file_name =
80+
let file_kind = "opam file" in
81+
match dry_run with
82+
| true -> 0
83+
| false -> (
84+
let opam_file =
85+
opam_file_name |> Fpath.to_string |> OpamFilename.of_string
86+
|> OpamFile.make |> OpamFile.OPAM.read
87+
in
88+
match OpamFileTools.lint opam_file with
89+
| [] ->
90+
Logs.app (fun m ->
91+
m "%a @[lint@ %s %a.@]" Text.Pp.status `Ok file_kind Text.Pp.path
92+
opam_file_name);
93+
0
94+
| failures ->
95+
let msg =
96+
failures
97+
|> List.map (fun (_err_code, _severity, msg) -> msg)
98+
|> String.concat ~sep:"\n"
99+
in
100+
Logs.app (fun m ->
101+
m "%a @[<v>@[lint@ %s %a:@]@,@[%s messages:@]@,%a@]"
102+
Text.Pp.status `Fail file_kind Text.Pp.path opam_file_name
103+
"lint" Fmt.lines msg);
104+
1)
138105

139106
let extra_opam_lint ~opam_file_version ~opam_file pkg =
140107
let is_2_0_format =
@@ -160,7 +127,7 @@ let lint_opam ~dry_run pkg =
160127
App_log.unhappy (fun l -> l "%s" Deprecate.Opam_1_x.file_format_warning)
161128
| _ -> ());
162129
Pkg.opam pkg >>= fun opam_file ->
163-
let opam_lint_errors = opam_lint ~dry_run ~opam_file_version opam_file in
130+
let opam_lint_errors = opam_lint ~dry_run opam_file in
164131
let extra_errors = extra_opam_lint ~opam_file_version ~opam_file pkg in
165132
Ok (opam_lint_errors + extra_errors)
166133

0 commit comments

Comments
 (0)