Skip to content

Commit

Permalink
Improve CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
greghendershott committed Aug 17, 2024
1 parent 9b0db74 commit 7ea1365
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 deletions.
6 changes: 3 additions & 3 deletions analyze.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -852,9 +852,9 @@
[(andmap sub-range-valid-srcloc? adjusted-ranges)
(hash-set! (file-pdb-exports f) export-ibk adjusted-ranges)]
[else
(log-pdb-warning "ignoring export due to bogus source locations: ~v ~v"
export-ibk
adjusted-ranges)])
(log-pdb-debug "ignoring export due to bogus source locations: ~v ~v"
export-ibk
adjusted-ranges)])

;; When a `rename` clause, and the new name exists in source (not
;; synthesized by e.g. prefix-out) then we'll want to add an
Expand Down
116 changes: 71 additions & 45 deletions cli.rkt
Original file line number Diff line number Diff line change
@@ -1,68 +1,94 @@
;; Copyright (c) 2021-2024 by Greg Hendershott.
;; SPDX-License-Identifier: GPL-3.0-or-later

#lang racket/base
#lang at-exp racket/base

(require racket/match
(require racket/format
racket/logging
racket/match
racket/path
(only-in raco/command-name
short-program+command-name)
"main.rkt")
"analyze.rkt"
"common.rkt"
(only-in (submod "store.rkt" stats)
db-stats
file-stats))

(define (err format-string . args)
(apply eprintf
(string-append (short-program+command-name) ": " format-string "\n")
args))
args)
(exit 1))

(define (parse vec)
(match (vector->list vec)
[(list* "analyze"
(? path-string? (app simple-form-path path)) _)
(define depth (or (and (= (vector-length vec) 3)
(match (vector-ref vec 2)
["0" 0]
["1" 1]
["all" 9999]))
0))
(cond
[(directory-exists? path)
(list 'analyze 'dir path depth)]
[(file-exists? path)
(list 'analyze 'file path depth)]
[else
(err "~v is not an existing file or directory" path)])]
[(list "forget" (? path-string? (app simple-form-path path)))
(cond
[(equal? path (path-only path)) ;directory?
(list 'forget 'dir path)]
[else
(list 'forget 'file path )])]
[(list "stats")
(define (analyze-file-or-dir path [depth 0])
(cond
[(directory-exists? path)
(void (add-directory path #:import-depth depth))]
[(file-exists? path)
(unless (fresh-analysis? (analyze-path path #:import-depth depth))
(displayln "Already in cache"))]
[else
(err "~v is not an existing file or directory" path)]))

(define (forget-file-or-dir path)
(cond
[(equal? path (path-only path)) ;directory?
(forget-directory path)]
[else
(forget-path path)]))

(define (parse* vec)
(match vec
[(vector (or "analyze" "add")
(app simple-form-path path)
(app string->number depth))
(analyze-file-or-dir path (or depth 0))]
[(vector (or "analyze" "add")
(app simple-form-path path))
(analyze-file-or-dir path)]
[(vector (or "analyze" "add"))
(analyze-file-or-dir (simple-form-path (current-directory)))]
[(vector "forget"
(app simple-form-path path))
(forget-file-or-dir path)]
[(vector "forget")
(forget-file-or-dir (simple-form-path (current-directory)))]
[(vector "stats")
(displayln (db-stats))]
[(list "stats" (app simple-form-path path))
[(vector "stats"
(app simple-form-path path))
(displayln (file-stats path))]
[_
(err "~v is not a valid command" vec)
(usage)]))
(usage)
(exit 1)]))

(define (usage)
(eprintf "todo\n"))
(define (parse vec)
(with-logging-to-port
#:logger pdb-logger
(current-error-port)
(λ () (parse* vec))
'info 'pdb))

(module+ example
(parse (vector "analyze" "cli.rkt"))
(parse (vector "analyze" "cli.rkt" "1"))
(parse (vector "analyze" "cli.rkt" "all"))
(parse (vector "analyze" (current-directory)))
(parse (vector "analyze" (current-directory) "1"))
(parse (vector "analyze" (current-directory) "all"))
(define (usage)
(displayln
@~a{Usage:

(parse (vector "forget" "cli.rkt"))
(parse (vector "forget" (current-directory)))
raco pdb add [<file-or-directory> [<import-depth>]]

(parse (vector "stats"))
(parse (vector "stats" "cli.rkt")))
<file-or-directory> defaults to the current directory.
<import-depth> defaults to 0, and says how far to analyze
transitively imported files.

raco pdb forget [<file-or-directory>]

#;(parse (current-command-line-arguments ))
<file-or-directory> defaults to the current directory.

raco pdb stats [<file>]

When <file> is supplied, show stats about the file.
Otherwise show stats about the entire database.
}
(current-error-port)))

(parse (current-command-line-arguments))
2 changes: 1 addition & 1 deletion info.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
(define compile-omit-paths '("example/"))
(define test-omit-paths '("example/"))
(define clean '("compiled"))
(define raco '(("pdb" pdb/cli "pdb commands")))
(define raco-commands '(("pdb" pdb/cli "program database" #f)))

0 comments on commit 7ea1365

Please sign in to comment.