Have try-module-require return the name of required module (or #f)

This commit is contained in:
Leif Andersen 2015-12-30 12:14:57 -07:00
parent b885fa231d
commit 31ba226306

View File

@ -30,18 +30,18 @@
(fprintf (current-error-port) "Module ~a does not export ~a~n" mod binding) (fprintf (current-error-port) "Module ~a does not export ~a~n" mod binding)
#f])) #f]))
; try-namespace-require : string -> (U 'string 'symbol #f) ; try-namespace-require : string -> (U string symbol #f)
(define (try-namespace-require mod) (define (try-namespace-require mod)
(let/ec return (let/ec return
(with-handlers ([exn:fail? (lambda (e) (with-handlers ([exn:fail? (lambda (e)
(set! mod (string->symbol mod)))]) (set! mod (string->symbol mod)))])
(namespace-require mod) (namespace-require mod)
(return 'string)) (return mod))
(with-handlers ([exn:fail? (lambda (e) (with-handlers ([exn:fail? (lambda (e)
(fprintf (current-error-port) "Module ~a can not be loaded~n" mod) (fprintf (current-error-port) "Module ~a can not be loaded~n" mod)
(return #f))]) (return #f))])
(namespace-require mod) (namespace-require mod)
(return 'symbol)))) (return mod))))
(module+ main (module+ main
@ -70,29 +70,25 @@
(for ([a (in-list args)]) (for ([a (in-list args)])
; Determin if module exists ; Determin if module exists
(define mod-exists? (try-namespace-require a)) (define mod (try-namespace-require a))
; If the module was required a symbol, we must convert `a` to a symbol too.
(when (equal? mod-exists? 'symbol)
(set! a (string->symbol a)))
; If we succeeded in importing the module run the correct operation ; If we succeeded in importing the module run the correct operation
(when mod-exists? (when mod
(cond [(binding) (cond [(binding)
(when (do-binding! a (binding)) (when (do-binding! mod (binding))
(error-on-exit? #t))] (error-on-exit? #t))]
[(ratio) [(ratio)
(define r* (module-documentation-ratio a)) (define r* (module-documentation-ratio mod))
(printf "Module ~a document ratio: ~a~n" a r*) (printf "Module ~a document ratio: ~a~n" mod r*)
(when (r* . < . (ratio)) (when (r* . < . (ratio))
(error-on-exit? #t))] (error-on-exit? #t))]
[(ignore) [(ignore)
(when (do-ignore! a (ignore)) (when (do-ignore! mod (ignore))
(error-on-exit? #t))] (error-on-exit? #t))]
[else [else
(define undoc (module->undocumented-exported-names a)) (define undoc (module->undocumented-exported-names mod))
(cond [(set-empty? undoc) (cond [(set-empty? undoc)
(printf "Module ~a is completely documented~n" a)] (printf "Module ~a is completely documented~n" mod)]
[else [else
(printf "Module ~a is missing documentation for: ~a~n" a undoc) (printf "Module ~a is missing documentation for: ~a~n" a undoc)
(error-on-exit? #t)])]))) (error-on-exit? #t)])])))