Skip to content

Commit ec3848e

Browse files
Copilotjackfirth
andcommitted
Add --output-summary-stats-only option to resyntax analyze command
Co-authored-by: jackfirth <[email protected]>
1 parent 7e0a5c4 commit ec3848e

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

cli.rkt

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message json))
3939
(define-enum-type resyntax-fix-method (modify-files create-multiple-git-commits))
40-
(define-record-type resyntax-analyze-options (targets suite output-format output-destination analyzer-timeout-ms))
40+
(define-record-type resyntax-analyze-options
41+
(targets suite output-format output-destination analyzer-timeout-ms output-stats-only? has-rule-filter?))
4142

4243

4344
(define-record-type resyntax-fix-options
@@ -62,6 +63,7 @@
6263
(define output-format plain-text)
6364
(define output-destination 'console)
6465
(define analyzer-timeout-ms 10000)
66+
(define output-stats-only? #false)
6567

6668
(command-line
6769
#:program "resyntax analyze"
@@ -120,7 +122,13 @@ changed relative to baseref are analyzed."
120122
("--output-as-github-review"
121123
"Report results by leaving a GitHub review on the pull request currently being analyzed, as \
122124
determined by the GITHUB_REPOSITORY and GITHUB_REF environment variables."
123-
(set! output-format github-pull-request-review)))
125+
(set! output-format github-pull-request-review))
126+
127+
("--output-summary-stats-only"
128+
"Print summary statistics instead of individual refactoring suggestions. When no \
129+
--refactoring-rule filter is given, prints total matches per rule. When a --refactoring-rule \
130+
filter is given, prints matches per file."
131+
(set! output-stats-only? #true)))
124132

125133
(when selected-rule
126134
(define filtered-suite
@@ -136,7 +144,9 @@ determined by the GITHUB_REPOSITORY and GITHUB_REF environment variables."
136144
#:suite suite
137145
#:output-format output-format
138146
#:output-destination output-destination
139-
#:analyzer-timeout-ms analyzer-timeout-ms))
147+
#:analyzer-timeout-ms analyzer-timeout-ms
148+
#:output-stats-only? output-stats-only?
149+
#:has-rule-filter? (and selected-rule #true)))
140150

141151

142152
(define (resyntax-fix-parse-command-line)
@@ -296,6 +306,42 @@ For help on these, use 'analyze --help' or 'fix --help'."
296306
(append-mapping refactoring-result-set-results)
297307
#:into into-list))
298308

309+
(define (display-summary-stats)
310+
(cond
311+
[(resyntax-analyze-options-has-rule-filter? options)
312+
;; When a rule filter is given, print matches per file
313+
(displayln "resyntax: --- summary statistics (matches per file) ---\n")
314+
(define matches-by-file
315+
(transduce results
316+
(indexing
317+
(λ (result)
318+
(file-source-path
319+
(syntax-replacement-source (refactoring-result-syntax-replacement result)))))
320+
(grouping into-count)
321+
(sorting #:key entry-value #:descending? #true)
322+
#:into into-list))
323+
(for ([file+count (in-list matches-by-file)])
324+
(match-define (entry file-path count) file+count)
325+
(printf " ~a: ~a match~a\n" file-path count (if (equal? count 1) "" "es")))
326+
(printf "\n Total: ~a match~a\n" (length results) (if (equal? (length results) 1) "" "es"))]
327+
[else
328+
;; When no rule filter is given, print matches per rule
329+
(displayln "resyntax: --- summary statistics (matches per rule) ---\n")
330+
(define matches-by-rule
331+
(transduce results
332+
(indexing refactoring-result-rule-name)
333+
(grouping into-count)
334+
(sorting #:key entry-value #:descending? #true)
335+
#:into into-list))
336+
(for ([rule+count (in-list matches-by-rule)])
337+
(match-define (entry rule-name count) rule+count)
338+
(printf " ~a: ~a match~a\n" rule-name count (if (equal? count 1) "" "es")))
339+
(printf "\n Total: ~a match~a across ~a rule~a\n"
340+
(length results)
341+
(if (equal? (length results) 1) "" "es")
342+
(length matches-by-rule)
343+
(if (equal? (length matches-by-rule) 1) "" "s"))]))
344+
299345
(define (display-results)
300346
(match (resyntax-analyze-options-output-format options)
301347
[(== plain-text)
@@ -316,13 +362,17 @@ For help on these, use 'analyze --help' or 'fix --help'."
316362
(define req (refactoring-results->github-review results #:file-count (hash-count sources)))
317363
(write-json (github-review-request-jsexpr req))]))
318364

319-
(match (resyntax-analyze-options-output-destination options)
320-
['console
321-
(displayln "resyntax: --- displaying results ---")
322-
(display-results)]
323-
[(? path? output-path)
324-
(displayln "resyntax: --- writing results to file ---")
325-
(with-output-to-file output-path display-results #:mode 'text)]))
365+
(cond
366+
[(resyntax-analyze-options-output-stats-only? options)
367+
(display-summary-stats)]
368+
[else
369+
(match (resyntax-analyze-options-output-destination options)
370+
['console
371+
(displayln "resyntax: --- displaying results ---")
372+
(display-results)]
373+
[(? path? output-path)
374+
(displayln "resyntax: --- writing results to file ---")
375+
(with-output-to-file output-path display-results #:mode 'text)])]))
326376

327377

328378
(define (resyntax-fix-run)

0 commit comments

Comments
 (0)