generated from jackfirth/racket-package-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
cli.rkt
346 lines (286 loc) · 12.7 KB
/
cli.rkt
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#lang racket/base
(require fancy-app
json
racket/cmdline
racket/format
racket/logging
racket/match
racket/path
rebellion/base/comparator
rebellion/base/range
rebellion/collection/entry
rebellion/collection/hash
rebellion/collection/list
rebellion/collection/multiset
rebellion/collection/range-set
rebellion/collection/vector/builder
rebellion/streaming/reducer
rebellion/streaming/transducer
rebellion/type/enum
rebellion/type/record
resyntax
resyntax/default-recommendations
resyntax/private/file-group
resyntax/private/github
resyntax/private/refactoring-result
resyntax/private/source
resyntax/private/string-indent
resyntax/private/syntax-replacement)
;@----------------------------------------------------------------------------------------------------
(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message))
(define-enum-type resyntax-fix-method (modify-files create-multiple-git-commits))
(define-record-type resyntax-analyze-options (targets suite output-format output-destination))
(define-record-type resyntax-fix-options
(targets
suite
fix-method
output-format
max-fixes
max-modified-files
max-modified-lines
max-pass-count))
(define all-lines (range-set (unbounded-range #:comparator natural<=>)))
(define (resyntax-analyze-parse-command-line)
(define targets (make-vector-builder))
(define suite default-recommendations)
(define output-format plain-text)
(define output-destination 'console)
(command-line
#:program "resyntax analyze"
#:multi
("--file"
filepath
"A file to analyze."
(vector-builder-add targets (single-file-group filepath all-lines)))
("--directory"
dirpath
"A directory to anaylze, including subdirectories."
(vector-builder-add targets (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to analyze."
(vector-builder-add targets (package-file-group pkgname)))
("--local-git-repository"
repopath baseref
"A Git repository to search for modified files to analyze. The repopath argument is a directory
path to the root of a Git repository, and the baseref argument is a Git reference (in the form \
\"remotename/branchname\") to use as the base state of the repository. Any files that have been \
changed relative to baseref are analyzed."
(vector-builder-add targets (git-repository-file-group repopath baseref)))
#:once-each
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set! suite (dynamic-require parsed-modpath parsed-suite-name)))
("--output-to-file"
outputpath
"Store results in a file instead of printing them to the console."
(set! output-destination (simple-form-path outputpath)))
("--output-as-github-review"
"Report results by leaving a GitHub review on the pull request currently being analyzed, as \
determined by the GITHUB_REPOSITORY and GITHUB_REF environment variables."
(set! output-format github-pull-request-review)))
(resyntax-analyze-options
#:targets (build-vector targets)
#:suite suite
#:output-format output-format
#:output-destination output-destination))
(define (resyntax-fix-parse-command-line)
(define targets (make-vector-builder))
(define suite default-recommendations)
(define (add-target! target)
(vector-builder-add targets target))
(define fix-method modify-files)
(define output-format plain-text)
(define max-fixes +inf.0)
(define max-pass-count 10)
(define max-modified-files +inf.0)
(define max-modified-lines +inf.0)
(command-line
#:program "resyntax fix"
#:multi
("--file" filepath "A file to fix." (add-target! (single-file-group filepath all-lines)))
("--directory"
dirpath
"A directory to fix, including subdirectories."
(add-target! (directory-file-group dirpath)))
("--package"
pkgname
"An installed package to fix."
(add-target! (package-file-group pkgname)))
("--local-git-repository"
repopath baseref
"A Git repository to search for modified files to fix. The repopath argument is a directory
path to the root of a Git repository, and the baseref argument is a Git reference (in the form \
\"remotename/branchname\") to use as the base state of the repository. Any files that have been \
changed relative to baseref are analyzed and fixed."
(add-target! (git-repository-file-group repopath baseref)))
#:once-each
("--create-multiple-commits"
"Modify files by creating a series of individual Git commits."
(set! fix-method create-multiple-git-commits))
("--output-as-commit-message"
"Report results in the form of a Git commit message printed to stdout."
(set! output-format git-commit-message))
("--refactoring-suite"
modpath
suite-name
"The refactoring suite to analyze code with."
(define parsed-modpath (read (open-input-string modpath)))
(define parsed-suite-name (read (open-input-string suite-name)))
(set! suite (dynamic-require parsed-modpath parsed-suite-name)))
("--max-pass-count"
passcount
"The maximum number of times Resyntax will fix each file. By default, Resyntax runs at most 10 \
passes over each file (or fewer, if no fixes would be made by additional passes). Multiple passes \
are needed when applying a fix unlocks further fixes."
(set! max-pass-count (string->number passcount)))
("--max-fixes"
fixlimit
"The maximum number of fixes to apply. If not specified, all fixes found will be applied."
(set! max-fixes (string->number fixlimit)))
("--max-modified-files"
modifiedlimit
"The maximum number of files to modify. If not specified, fixes will be applied to all files."
(set! max-modified-files (string->number modifiedlimit)))
("--max-modified-lines"
modifiedlines
"The maximum number of lines to modify. If not specified, no line limit is applied."
(set! max-modified-lines (string->number modifiedlines))))
(resyntax-fix-options #:targets (build-vector targets)
#:suite suite
#:fix-method fix-method
#:output-format output-format
#:max-fixes max-fixes
#:max-modified-files max-modified-files
#:max-modified-lines max-modified-lines
#:max-pass-count max-pass-count))
(define (resyntax-run)
(command-line
#:program "resyntax"
#:usage-help
"\n<command> is one of
\tanalyze
\tfix
For help on these, use 'analyze --help' or 'fix --help'."
#:ps "\nSee https://docs.racket-lang.org/resyntax/index.html for details."
#:args (command . leftover-args)
(define leftover-arg-vector (vector->immutable-vector (list->vector leftover-args)))
(define (call-command command-thunk)
(parameterize ([current-command-line-arguments leftover-arg-vector])
(with-logging-to-port (current-error-port)
command-thunk
#:logger (current-logger)
'info 'resyntax
'error)))
(match command
["analyze" (call-command resyntax-analyze-run)]
["fix" (call-command resyntax-fix-run)])))
(define (resyntax-analyze-run)
(define options (resyntax-analyze-parse-command-line))
(define sources (file-groups-resolve (resyntax-analyze-options-targets options)))
(define analysis
(resyntax-analyze-all sources
#:suite (resyntax-analyze-options-suite options)
#:max-passes 1))
(define results
(transduce (resyntax-analysis-all-results analysis)
(append-mapping in-hash-values)
(append-mapping refactoring-result-set-results)
#:into into-list))
(define (display-results)
(match (resyntax-analyze-options-output-format options)
[(== plain-text)
(for ([result (in-list results)])
(define path
(file-source-path
(syntax-replacement-source (refactoring-result-syntax-replacement result))))
(printf "resyntax: ~a [~a]\n" path (refactoring-result-rule-name result))
(printf "\n\n~a\n" (string-indent (refactoring-result-message result) #:amount 2))
(define old-code (refactoring-result-original-code result))
(define new-code (refactoring-result-new-code result))
(printf "\n\n~a\n\n\n~a\n\n\n"
(string-indent (~a old-code) #:amount 2)
(string-indent (~a new-code) #:amount 2)))]
[(== github-pull-request-review)
(define req (refactoring-results->github-review results #:file-count (hash-count sources)))
(write-json (github-review-request-jsexpr req))]))
(match (resyntax-analyze-options-output-destination options)
['console
(printf "resyntax: --- displaying results ---\n")
(display-results)]
[(? path? output-path)
(printf "resyntax: --- writing results to file ---\n")
(with-output-to-file output-path display-results #:mode 'text)]))
(define (resyntax-fix-run)
(define options (resyntax-fix-parse-command-line))
(define fix-method (resyntax-fix-options-fix-method options))
(define output-format (resyntax-fix-options-output-format options))
(define sources (file-groups-resolve (resyntax-fix-options-targets options)))
(define max-modified-files (resyntax-fix-options-max-modified-files options))
(define max-modified-lines (resyntax-fix-options-max-modified-lines options))
(define analysis
(resyntax-analyze-all sources
#:suite (resyntax-fix-options-suite options)
#:max-fixes (resyntax-fix-options-max-fixes options)
#:max-passes (resyntax-fix-options-max-pass-count options)
#:max-modified-sources max-modified-files
#:max-modified-lines max-modified-lines))
(match fix-method
[(== modify-files)
(resyntax-analysis-write-file-changes! analysis)]
[(== create-multiple-git-commits)
(resyntax-analysis-commit-fixes! analysis)])
(match output-format
[(== git-commit-message)
(resyntax-fix-print-git-commit-message analysis)]
[(== plain-text)
(resyntax-fix-print-plain-text-summary analysis)]))
(define (resyntax-fix-print-git-commit-message analysis)
(define total-fixes (resyntax-analysis-total-fixes analysis))
(define total-files (resyntax-analysis-total-sources-modified analysis))
(define fix-counts-by-rule
(transduce (in-hash-entries (multiset-frequencies (resyntax-analysis-rules-applied analysis)))
(sorting #:key entry-value #:descending? #true)
#:into into-list))
(define issue-string (if (> total-fixes 1) "issues" "issue"))
(define file-string (if (> total-files 1) "files" "file"))
(if (zero? total-fixes)
(printf "Resyntax found no issues.\n")
(printf "Resyntax fixed ~a ~a in ~a ~a.\n\n" total-fixes issue-string total-files file-string))
(for ([rule+count (in-list fix-counts-by-rule)])
(match-define (entry rule count) rule+count)
(define occurrence-string (if (> count 1) "occurrences" "occurrence"))
(printf " * Fixed ~a ~a of `~a`\n" count occurrence-string rule))
(unless (zero? total-fixes)
(newline)))
(define (resyntax-fix-print-plain-text-summary analysis)
(printf "resyntax: --- summary ---\n\n")
(define total-fixes (resyntax-analysis-total-fixes analysis))
(define total-files (resyntax-analysis-total-sources-modified analysis))
(define message
(cond
[(zero? total-fixes) "No issues found."]
[(equal? total-fixes 1) "Fixed 1 issue in 1 file."]
[(equal? total-files 1) (format "Fixed ~a issues in 1 file." total-fixes)]
[else (format "Fixed ~a issues in ~a files." total-fixes total-files)]))
(printf " ~a\n\n" message)
(define rules-applied (resyntax-analysis-rules-applied analysis))
(transduce (in-hash-entries (multiset-frequencies rules-applied))
(sorting #:key entry-value #:descending? #true)
(mapping
(λ (e)
(match-define (entry rule-name rule-fixes) e)
(define message
(if (equal? rule-fixes 1)
(format "Fixed 1 occurrence of ~a" rule-name)
(format "Fixed ~a occurrences of ~a" rule-fixes rule-name)))
(format " * ~a\n" message)))
#:into (into-for-each display))
(when (positive? total-fixes)
(newline)))
(module+ main
(resyntax-run))