|
8 | 8 | racket/logging |
9 | 9 | racket/match |
10 | 10 | racket/path |
| 11 | + racket/port |
11 | 12 | rebellion/base/comparator |
12 | 13 | rebellion/base/range |
13 | 14 | rebellion/collection/entry |
|
33 | 34 | ;@---------------------------------------------------------------------------------------------------- |
34 | 35 |
|
35 | 36 |
|
36 | | -(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message)) |
| 37 | +(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message json)) |
37 | 38 | (define-enum-type resyntax-fix-method (modify-files create-multiple-git-commits)) |
38 | 39 | (define-record-type resyntax-analyze-options (targets suite output-format output-destination)) |
39 | 40 |
|
@@ -160,6 +161,10 @@ changed relative to baseref are analyzed and fixed." |
160 | 161 | "Report results in the form of a Git commit message printed to stdout." |
161 | 162 | (set! output-format git-commit-message)) |
162 | 163 |
|
| 164 | + ("--output-as-json" |
| 165 | + "Report results in the form of a JSON object printed to stdout." |
| 166 | + (set! output-format json)) |
| 167 | + |
163 | 168 | ("--refactoring-suite" |
164 | 169 | modpath |
165 | 170 | suite-name |
@@ -293,6 +298,8 @@ For help on these, use 'analyze --help' or 'fix --help'." |
293 | 298 | (match output-format |
294 | 299 | [(== git-commit-message) |
295 | 300 | (resyntax-fix-print-git-commit-message analysis)] |
| 301 | + [(== json) |
| 302 | + (resyntax-fix-print-json analysis)] |
296 | 303 | [(== plain-text) |
297 | 304 | (resyntax-fix-print-plain-text-summary analysis)])) |
298 | 305 |
|
@@ -344,5 +351,37 @@ For help on these, use 'analyze --help' or 'fix --help'." |
344 | 351 | (newline))) |
345 | 352 |
|
346 | 353 |
|
| 354 | +(define (resyntax-fix-print-json analysis) |
| 355 | + (define total-fixes (resyntax-analysis-total-fixes analysis)) |
| 356 | + (define total-files (resyntax-analysis-total-sources-modified analysis)) |
| 357 | + (define fix-counts-by-rule |
| 358 | + (transduce (in-hash-entries (multiset-frequencies (resyntax-analysis-rules-applied analysis))) |
| 359 | + (sorting #:key entry-value #:descending? #true) |
| 360 | + #:into into-list)) |
| 361 | + |
| 362 | + ;; Build commit message |
| 363 | + (define commit-message |
| 364 | + (with-output-to-string |
| 365 | + (λ () |
| 366 | + (define issue-string (if (> total-fixes 1) "issues" "issue")) |
| 367 | + (define file-string (if (> total-files 1) "files" "file")) |
| 368 | + (if (zero? total-fixes) |
| 369 | + (printf "Resyntax found no issues.") |
| 370 | + (printf "Automated Resyntax fixes\n\nResyntax fixed ~a ~a in ~a ~a." |
| 371 | + total-fixes issue-string total-files file-string)) |
| 372 | + (unless (zero? total-fixes) |
| 373 | + (printf "\n") |
| 374 | + (for ([rule+count (in-list fix-counts-by-rule)]) |
| 375 | + (match-define (entry rule count) rule+count) |
| 376 | + (define occurrence-string (if (> count 1) "occurrences" "occurrence")) |
| 377 | + (printf "\n * Fixed ~a ~a of `~a`" count occurrence-string rule)))))) |
| 378 | + |
| 379 | + ;; Output JSON |
| 380 | + (write-json |
| 381 | + (hasheq 'commit_message commit-message |
| 382 | + 'fix_count total-fixes)) |
| 383 | + (newline)) |
| 384 | + |
| 385 | + |
347 | 386 | (module+ main |
348 | 387 | (resyntax-run)) |
0 commit comments