mirror of
https://github.com/comby-tools/comby.git
synced 2024-10-26 13:19:23 +03:00
Add -json-only-diff
This commit is contained in:
parent
336c90630e
commit
b27f6ab4b6
@ -21,7 +21,7 @@ let empty_result =
|
||||
}
|
||||
[@@deriving yojson]
|
||||
|
||||
let to_json replacements path diff result =
|
||||
let to_json diff_only replacements path diff result =
|
||||
let value = `List (List.map ~f:to_yojson replacements) in
|
||||
let uri =
|
||||
match path with
|
||||
@ -33,20 +33,29 @@ let to_json replacements path diff result =
|
||||
| Some diff -> `String diff
|
||||
| None -> `Null
|
||||
in
|
||||
`Assoc
|
||||
[ ("uri", uri)
|
||||
; ("rewritten_source", `String result)
|
||||
; ("in_place_substitutions", value)
|
||||
; ("diff", diff)
|
||||
]
|
||||
if diff_only then
|
||||
`Assoc
|
||||
[ ("uri", uri)
|
||||
; ("diff", diff)
|
||||
]
|
||||
else
|
||||
`Assoc
|
||||
[ ("uri", uri)
|
||||
; ("rewritten_source", `String result)
|
||||
; ("in_place_substitutions", value)
|
||||
; ("diff", diff)
|
||||
]
|
||||
|
||||
let yojson_to_string kind json =
|
||||
match kind with
|
||||
| `Pretty -> Yojson.Safe.pretty_to_string json
|
||||
| `Lines -> Yojson.Safe.to_string json
|
||||
|
||||
let pp_json_pretty ppf (source_path, replacements, replacement_content, diff) =
|
||||
Format.fprintf ppf "%s" @@ yojson_to_string `Pretty @@ to_json replacements source_path diff replacement_content
|
||||
let pp_json_pretty ppf (source_path, replacements, replacement_content, diff, diff_only) =
|
||||
Format.fprintf ppf "%s" @@ yojson_to_string `Pretty @@ to_json diff_only replacements source_path diff replacement_content
|
||||
|
||||
let pp_json_line ppf (source_path, replacements, replacement_content, diff) =
|
||||
Format.fprintf ppf "%s" @@ Yojson.Safe.to_string @@ to_json replacements source_path diff replacement_content
|
||||
let pp_json_pretty_diff_only ppf (source_path, replacements, replacement_content, diff, diff_only) =
|
||||
Format.fprintf ppf "%s" @@ yojson_to_string `Pretty @@ to_json diff_only replacements source_path diff replacement_content
|
||||
|
||||
let pp_json_line ppf (source_path, replacements, replacement_content, diff, diff_only) =
|
||||
Format.fprintf ppf "%s" @@ Yojson.Safe.to_string @@ to_json diff_only replacements source_path diff replacement_content
|
||||
|
@ -15,6 +15,6 @@ type result =
|
||||
|
||||
val empty_result : result
|
||||
|
||||
val pp_json_pretty : Format.formatter -> string option * t list * string * string option -> unit
|
||||
val pp_json_pretty : Format.formatter -> string option * t list * string * string option * bool -> unit
|
||||
|
||||
val pp_json_line : Format.formatter -> string option * t list * string * string option -> unit
|
||||
val pp_json_line : Format.formatter -> string option * t list * string * string option * bool -> unit
|
||||
|
@ -102,6 +102,7 @@ type output_options =
|
||||
{ color : bool
|
||||
; json_pretty : bool
|
||||
; json_lines : bool
|
||||
; json_only_diff : bool
|
||||
; in_place : bool
|
||||
; diff : bool
|
||||
; stdout : bool
|
||||
@ -211,11 +212,15 @@ module Printer = struct
|
||||
| Plain
|
||||
| Colored
|
||||
|
||||
type json_kind =
|
||||
| Everything
|
||||
| Diff
|
||||
|
||||
type replacement_output =
|
||||
| In_place
|
||||
| Stdout
|
||||
| Json_lines
|
||||
| Json_pretty
|
||||
| Json_lines of json_kind
|
||||
| Json_pretty of json_kind
|
||||
| Diff of diff_kind
|
||||
| Match_only
|
||||
|
||||
@ -229,19 +234,31 @@ module Printer = struct
|
||||
| Plain
|
||||
| Colored
|
||||
|
||||
type json_kind =
|
||||
| Everything
|
||||
| Diff
|
||||
|
||||
type replacement_output =
|
||||
| In_place
|
||||
| Stdout
|
||||
| Json_lines
|
||||
| Json_pretty
|
||||
| Json_lines of json_kind
|
||||
| Json_pretty of json_kind
|
||||
| Diff of diff_kind
|
||||
| Match_only
|
||||
|
||||
let convert output_options : replacement_output =
|
||||
match output_options with
|
||||
| { in_place = true; _ } -> In_place
|
||||
| { json_pretty = true; in_place = false; _ } -> Json_pretty
|
||||
| { json_lines = true; in_place = false; _ } -> Json_lines
|
||||
| { json_pretty = true; in_place = false; _ } ->
|
||||
if output_options.json_only_diff then
|
||||
Json_pretty Diff
|
||||
else
|
||||
Json_pretty Everything
|
||||
| { json_lines = true; in_place = false; _ } ->
|
||||
if output_options.json_only_diff then
|
||||
Json_lines Diff
|
||||
else
|
||||
Json_lines Everything
|
||||
| { stdout = true; _ } -> Stdout
|
||||
| { diff = true; color = false; _ } -> Diff Plain
|
||||
| { color = true; _ }
|
||||
@ -252,13 +269,21 @@ module Printer = struct
|
||||
match replacement_output with
|
||||
| Stdout ->
|
||||
Format.fprintf ppf "%s" replacement_content
|
||||
| Json_pretty ->
|
||||
| Json_pretty Everything ->
|
||||
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_pretty (path, replacements, replacement_content, diff) in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_pretty (path, replacements, replacement_content, diff, false) in
|
||||
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
|
||||
| Json_lines ->
|
||||
| Json_pretty Diff ->
|
||||
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_line (path, replacements, replacement_content, diff) in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_pretty (path, replacements, replacement_content, diff, true) in
|
||||
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
|
||||
| Json_lines Everything ->
|
||||
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_line (path, replacements, replacement_content, diff, false) in
|
||||
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
|
||||
| Json_lines Diff ->
|
||||
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
|
||||
let print diff = Format.fprintf ppf "%a@." Replacement.pp_json_line (path, replacements, replacement_content, diff, true) in
|
||||
Option.value_map diff ~default:() ~f:(fun diff -> print (Some diff))
|
||||
| Diff Plain ->
|
||||
let diff = Diff_configuration.get_diff Plain path source_content replacement_content in
|
||||
@ -316,6 +341,8 @@ let validate_errors { input_options; run_options = _; output_options } =
|
||||
(*Format.printf "checking %s %b@." dir (Sys.is_directory dir = `No);*)
|
||||
not (Sys.is_directory dir = `Yes))
|
||||
, "One or more directories specified with -templates is not a directory"
|
||||
; output_options.json_only_diff && (not output_options.json_lines && not output_options.json_pretty)
|
||||
, "-json-only-diff can only be supplied with -json-lines or -json-pretty."
|
||||
; let result = Rule.create input_options.rule in
|
||||
Or_error.is_error result
|
||||
, if Or_error.is_error result then
|
||||
|
@ -21,6 +21,7 @@ type output_options =
|
||||
{ color : bool
|
||||
; json_pretty : bool
|
||||
; json_lines : bool
|
||||
; json_only_diff : bool
|
||||
; in_place : bool
|
||||
; diff : bool
|
||||
; stdout : bool
|
||||
|
@ -305,6 +305,7 @@ let base_command_parameters : (unit -> 'result) Command.Param.t =
|
||||
and zip_file = flag "zip" ~aliases:["z"] (optional string) ~doc:"zipfile A zip file containing files to rewrite"
|
||||
and json_pretty = flag "json-pretty" no_arg ~doc:"Output pretty JSON format"
|
||||
and json_lines = flag "json-lines" no_arg ~doc:"Output JSON line format"
|
||||
and json_only_diff = flag "json-only-diff" no_arg ~doc:"Output only the URI and diff in JSON line format"
|
||||
and in_place = flag "in-place" no_arg ~doc:"Rewrite files on disk, in place"
|
||||
and number_of_workers = flag "jobs" (optional_with_default 4 int) ~doc:"n Number of worker processes. Default: 4"
|
||||
and dump_statistics = flag "statistics" ~aliases:["stats"] no_arg ~doc:"Dump statistics to stderr"
|
||||
@ -354,6 +355,7 @@ let base_command_parameters : (unit -> 'result) Command.Param.t =
|
||||
; count
|
||||
; json_pretty
|
||||
; json_lines
|
||||
; json_only_diff
|
||||
; in_place
|
||||
; diff
|
||||
; stdout
|
||||
|
@ -730,3 +730,20 @@ let%expect_test "infer_and_honor_extensions" =
|
||||
-// foo()
|
||||
+// bar()
|
||||
} |}]
|
||||
|
||||
let%expect_test "diff_only" =
|
||||
let source = "hello world" in
|
||||
let command_args = Format.sprintf "'hello' 'world' -stdin -sequential -json-lines -json-only-diff" in
|
||||
let command = Format.sprintf "%s %s" binary_path command_args in
|
||||
let result = read_source_from_stdin command source in
|
||||
print_string result;
|
||||
[%expect{|
|
||||
{"uri":null,"diff":"--- /dev/null\n+++ /dev/null\n@@ -1,1 +1,1 @@\n-hello world\n+world world"} |}];
|
||||
|
||||
let source = "hello world" in
|
||||
let command_args = Format.sprintf "'hello' 'world' -stdin -sequential -json-only-diff" in
|
||||
let command = Format.sprintf "%s %s" binary_path command_args in
|
||||
let result = read_source_from_stdin command source in
|
||||
print_string result;
|
||||
[%expect{|
|
||||
-json-only-diff can only be supplied with -json-lines or -json-pretty. |}];
|
||||
|
Loading…
Reference in New Issue
Block a user