Skip to content

Commit

Permalink
Improve CLI
Browse files Browse the repository at this point in the history
1. Change the "analyze"/"add" subcommand to work properly for non-zero
import depth.

2. Add a "query" subcommand to pretty-print syncheck annotations sexpr
to stdout.
  • Loading branch information
greghendershott committed Aug 19, 2024
1 parent 9d4fd31 commit 9ed79db
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
21 changes: 21 additions & 0 deletions analyze.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
analyze-path
fresh-analysis?
fresh-analysis-expanded-syntax
add-file
add-directory
forget-path
forget-directory)
Expand Down Expand Up @@ -181,6 +182,26 @@
(break-thread maybe-old-thread-for-path))
(hash-set! ht path (thread do-analyze-thunk)))))))

;; Like `analyze-path` but instead of waiting only for the immediate
;; file to be analyzed, waits until all imports (if any, when
;; import-depth > 0) are analyzed. Intended for use by CLI.
(define/contract (add-file path
#:code [code #f]
#:import-depth [import-depth 0]
#:always? [always? #f])
(->* ((and/c path? complete-path?))
(#:code (or/c #f string?)
#:import-depth exact-nonnegative-integer?
#:always? boolean?)
any)
(define ch (make-channel))
(spawn-do-analyze-path path
#:code code
#:always? always?
#:import-depth import-depth
#:imports-chan ch)
(sync ch))

;; Analyze all files in and under `dir`. The optional #:import-depth
;; and #:always? args are the same as for `analyze-path`.
(define/contract (add-directory dir
Expand Down
58 changes: 43 additions & 15 deletions cli.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
racket/logging
racket/match
racket/path
racket/pretty
(only-in raco/command-name
short-program+command-name)
"analyze.rkt"
"common.rkt"
(only-in "query.rkt"
get-annotations
max-position)
(only-in (submod "store.rkt" stats)
db-stats
file-stats))
Expand All @@ -31,11 +35,10 @@
#:import-depth (current-analyze-depth)
#:always? (current-analyze-always?)))]
[(file-exists? path)
(unless (fresh-analysis?
(analyze-path path
#:import-depth (current-analyze-depth)
#:always? (current-analyze-always?)))
(displayln "Already in cache"))]
(void
(add-file path
#:import-depth (current-analyze-depth)
#:always? (current-analyze-always?)))]
[else
(err "~v is not an existing file or directory" path)]))

Expand All @@ -61,10 +64,11 @@
#:ps
""
"For help on a particular subcommand, use 'raco pdb <subcommand> --help'."
" raco pdb analyze Analyze file(s)"
" raco pdb analyze Analyze a file or directory"
" raco pdb add Alias for 'analyze'"
" raco pdb forget Forget analysis of file(s)"
" raco pdb stats Show stats for a file or entire db"
" raco pdb forget Forget analysis of a file or directory"
" raco pdb query Query annotations for a file"
" raco pdb stats Show stats for a file or the entire db"
#:args (subcommand . option/arg)
(with-logging-to-port
#:logger pdb-logger
Expand All @@ -86,7 +90,7 @@
("Analyze imported files transitively to this depth."
"Reasonable values are 0 (the default) or 1."
"See also --max-depth.")
(current-analyze-depth depth)]
(current-analyze-depth (string->number depth))]
[("-D" "--max-depth") ("Maximally analyze imported files transitively."
"Analyzes the full import chains up to opaque modules"
"such as #%core or #%runtime.")
Expand All @@ -99,16 +103,40 @@
#:argv more
#:args (file-or-dir)
(forget-file-or-dir (simple-form-path file-or-dir)))]
["query"
(define *from 1)
(define *upto max-position)
(command-line
#:program (~a (short-program+command-name) " query")
#:argv more
#:once-each
[("--from") from
"Include from this position, inclusive. Defaults to 1."
(set! *from (string->number from))]
[("--upto") upto
"Include up to this position, exclusive. Defaults to very large integer."
(set! *upto (string->number upto))]
#:ps
""
"Pretty print a list of check-syntax annotations."
#:args (file)
(pretty-print
(get-annotations (simple-form-path file) *from *upto)))]
["stats"
(define path #f)
(command-line
#:program (~a (short-program+command-name) " stats")
#:argv more
#:args maybe-file
#:once-each
[("-f" "--file") file
("Show stats for specific <file>."
"When this option is omitted, show summary db stats.")
(set! path file)]
#:args ()
(displayln
(if (null? maybe-file)
(db-stats)
(file-stats (simple-form-path (car maybe-file))))))]
[v (err "Not a valid subcommand: ~v.\nUse 'raco pdb --help'." v)]))
(if path
(file-stats (simple-form-path path))
(db-stats))))]
[v (err "Not a valid subcommand: ~v.\nTry 'raco pdb --help'." v)]))

(parse (current-command-line-arguments))

1 change: 1 addition & 0 deletions query.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"span-map.rkt")

(provide get-annotations
max-position
get-submodule-names
get-completion-candidates
get-errors
Expand Down

0 comments on commit 9ed79db

Please sign in to comment.