dev-stylish
Matthew Butterick 6 years ago
parent 02f6c1e83b
commit 8f3de0d73c

@ -5,6 +5,7 @@
racket/list racket/list
racket/vector racket/vector
racket/cmdline racket/cmdline
racket/match
sugar/coerce sugar/coerce
"file-utils.rkt" "file-utils.rkt"
"../setup.rkt" "../setup.rkt"
@ -18,18 +19,16 @@
;; todo: investigate this ;; todo: investigate this
(module+ raco (module+ raco
(define command-name (with-handlers ([exn:fail? (λ _ #f)]) (define command-name (with-handlers ([exn:fail? (λ () #f)])
(vector-ref (current-command-line-arguments) 0))) (vector-ref (current-command-line-arguments) 0)))
(dispatch command-name)) (dispatch command-name))
(define (get-first-arg-or-current-dir [args (cdr (vector->list (current-command-line-arguments)))]) ; cdr to strip command name from front (define (get-first-arg-or-current-dir [args (cdr (vector->list (current-command-line-arguments)))]) ; cdr to strip command name from front
(normalize-path (normalize-path
(with-handlers ([exn:fail? (λ (exn) (current-directory))]) (with-handlers ([exn:fail? (λ (exn) (current-directory))])
;; incoming path argument is handled as described in docs for current-directory ;; incoming path argument is handled as described in docs for current-directory
(very-nice-path (car args))))) (very-nice-path (car args)))))
(define (dispatch command-name) (define (dispatch command-name)
(case command-name (case command-name
[("test" "xyzzy") (handle-test)] [("test" "xyzzy") (handle-test)]
@ -67,108 +66,102 @@ version print the version" (current-server-port) (make-publish-di
(define (handle-version) (define (handle-version)
(displayln (dynamic-require 'pollen/private/version 'pollen:version))) (displayln (dynamic-require 'pollen/private/version 'pollen:version)))
(define (handle-reset directory-maybe) (define (handle-reset directory-maybe)
(displayln "resetting cache ...") (displayln "resetting cache ...")
((dynamic-require 'pollen/cache 'reset-cache) directory-maybe)) ((dynamic-require 'pollen/cache 'reset-cache) directory-maybe))
(define (handle-setup directory-maybe) (define (handle-setup directory-maybe)
(displayln "preheating cache ...") (displayln "preheating cache ...")
((dynamic-require 'pollen/private/preheat-cache 'preheat-cache) directory-maybe)) ((dynamic-require 'pollen/private/preheat-cache 'preheat-cache) directory-maybe))
(define (handle-render) (define (handle-render)
(define render-target-wanted (make-parameter (current-poly-target))) (define render-target-wanted (make-parameter (current-poly-target)))
(define render-with-subdirs? (make-parameter #f)) (define render-with-subdirs? (make-parameter #f))
(define parsed-args (command-line #:program "raco pollen render" (define parsed-args
#:argv (vector-drop (current-command-line-arguments) 1) ; snip the 'render' from the front (command-line #:program "raco pollen render"
#:once-each #:argv (vector-drop (current-command-line-arguments) 1) ; snip the 'render' from the front
[("-t" "--target") target-arg "Render target for poly sources" #:once-each
(render-target-wanted (->symbol target-arg))] [("-t" "--target") target-arg "Render target for poly sources"
[("-r" "--recursive") "Render subdirectories recursively" (render-target-wanted (->symbol target-arg))]
(render-with-subdirs? 'recursive)] [("-r" "--recursive") "Render subdirectories recursively"
[("-s" "--subdir") "Render subdirectories nonrecursively" (render-with-subdirs? 'include)] (render-with-subdirs? 'recursive)]
#:args other-args [("-s" "--subdir") "Render subdirectories nonrecursively" (render-with-subdirs? 'include)]
other-args)) #:args other-args
(define path-args (if (empty? parsed-args) other-args))
(list (current-directory))
parsed-args))
(parameterize ([current-poly-target (render-target-wanted)]) ;; applies to both cases (parameterize ([current-poly-target (render-target-wanted)]) ;; applies to both cases
(cond (let loop ([args parsed-args])
;; directory mode: one directory as argument (match args
[(and (= 1 (length path-args)) (directory-exists? (car path-args))) [(== empty) (loop (list (current-directory)))]
(define top-dir (very-nice-path (car path-args))) [(list dir) ;; directory mode: one directory as argument
(let render-one-dir ([dir top-dir]) #:when (directory-exists? dir)
(parameterize ([current-directory dir] (define top-dir (very-nice-path dir))
[current-project-root (if (eq? (render-with-subdirs?) 'recursive) (let render-one-dir ([dir top-dir])
dir (parameterize ([current-directory dir]
top-dir)]) [current-project-root (case (render-with-subdirs?)
(define dirlist (directory-list dir)) [(recursive) dir]
(define preprocs (filter preproc-source? dirlist)) [else top-dir])])
(define static-pagetrees (filter pagetree-source? dirlist)) (define dirlist (directory-list dir))
;; if there are no static pagetrees, use make-project-pagetree (define preprocs (filter preproc-source? dirlist))
;; (which will synthesize a pagetree if needed, which includes all sources) (define static-pagetrees (filter pagetree-source? dirlist))
(define batch-to-render ;; if there are no static pagetrees, use make-project-pagetree
(map very-nice-path ;; (which will synthesize a pagetree if needed, which includes all sources)
(cond (define batch-to-render
[(null? static-pagetrees) (map very-nice-path
(displayln (format "rendering generated pagetree for directory ~a" dir)) (cond
(cdr (make-project-pagetree dir))] [(null? static-pagetrees)
[else (displayln (format "rendering generated pagetree for directory ~a" dir))
(displayln (format "rendering preproc & pagetree files in directory ~a" dir)) (cdr (make-project-pagetree dir))]
(append preprocs static-pagetrees)]))) [else
(apply render-batch batch-to-render) (displayln (format "rendering preproc & pagetree files in directory ~a" dir))
(when (render-with-subdirs?) (append preprocs static-pagetrees)])))
(for ([path (in-list dirlist)] (apply render-batch batch-to-render)
#:when (and (directory-exists? path) (when (render-with-subdirs?)
(not (omitted-path? path)))) (for ([path (in-list dirlist)]
(render-one-dir (->complete-path path))))))] #:when (and (directory-exists? path)
[else ;; path mode (not (omitted-path? path))))
(displayln (format "rendering ~a" (string-join (map ->string path-args) " "))) (render-one-dir (->complete-path path))))))]
(apply render-batch (map very-nice-path path-args))]))) [path-args ;; path mode
(displayln (format "rendering ~a" (string-join (map ->string path-args) " ")))
(apply render-batch (map very-nice-path path-args))]))))
(define (handle-start) (define (handle-start)
(define launch-wanted #f) (define launch-wanted #f)
(define localhost-wanted #f) (define localhost-wanted #f)
(define clargs (command-line #:program "raco pollen start" (define clargs
#:argv (vector-drop (current-command-line-arguments) 1) ; snip the 'start' from the front (command-line #:program "raco pollen start"
#:once-each #:argv (vector-drop (current-command-line-arguments) 1) ; snip the 'start' from the front
[("--launch" "-l") "Launch browser after start" (set! launch-wanted #t)] #:once-each
[("--local") "Restrict access to localhost" (set! localhost-wanted #t)] [("--launch" "-l") "Launch browser after start" (set! launch-wanted #t)]
#:args other-args [("--local") "Restrict access to localhost" (set! localhost-wanted #t)]
other-args)) #:args other-args
other-args))
(define dir (path->directory-path (get-first-arg-or-current-dir clargs))) (define dir (path->directory-path (get-first-arg-or-current-dir clargs)))
(unless (directory-exists? dir) (unless (directory-exists? dir)
(error (format "~a is not a directory" dir))) (error (format "~a is not a directory" dir)))
(define port (with-handlers ([exn:fail? (λ (e) #f)]) (define http-port (with-handlers ([exn:fail? (λ (e) #f)])
(string->number (cadr clargs)))) (string->number (cadr clargs))))
(when (and port (not (exact-positive-integer? port))) (when (and http-port (not (exact-positive-integer? http-port)))
(error (format "~a is not a valid port number" port))) (error (format "~a is not a valid port number" http-port)))
(parameterize ([current-project-root dir] (parameterize ([current-project-root dir]
[current-server-port (or port (setup:project-server-port))] [current-server-port (or http-port (setup:project-server-port))]
[current-server-listen-ip (and localhost-wanted "127.0.0.1")]) [current-server-listen-ip (and localhost-wanted "127.0.0.1")])
(displayln "Starting project server ...") (displayln "Starting project server ...")
((dynamic-require 'pollen/private/project-server 'start-server) (format "/~a" (setup:main-pagetree dir)) launch-wanted))) ((dynamic-require 'pollen/private/project-server 'start-server) (format "/~a" (setup:main-pagetree dir)) launch-wanted)))
(define (make-publish-dir-name [project-root (current-directory)] [arg-command-name #f]) (define (make-publish-dir-name [project-root (current-directory)] [arg-command-name #f])
(define user-publish-path (define user-publish-path
(expand-user-path (->path (setup:publish-directory project-root)))) (expand-user-path (->path (setup:publish-directory project-root))))
(if (complete-path? user-publish-path) (if (complete-path? user-publish-path)
user-publish-path user-publish-path
(build-path (find-system-path 'desk-dir) (build-path (find-system-path 'desk-dir)
(->path (if (equal? arg-command-name "clone") ; bw compat (->path (case arg-command-name
"clone" [("clone") "clone"] ; bw compat
user-publish-path))))) [else user-publish-path])))))
(define (delete-it path) (define (delete-it path)
(cond (match path
[(directory-exists? path) (delete-directory/files path)] [(? directory-exists?) (delete-directory/files path)]
[(file-exists? path) (delete-file path)])) [(? file-exists?) (delete-file path)]))
(define (contains-directory? possible-superdir possible-subdir) (define (contains-directory? possible-superdir possible-subdir)
(define (has-prefix? xs prefix) (define (has-prefix? xs prefix)
@ -176,11 +169,10 @@ version print the version" (current-server-port) (make-publish-di
(andmap equal? prefix (take xs (length prefix))))) (andmap equal? prefix (take xs (length prefix)))))
((explode-path possible-subdir) . has-prefix? . (explode-path possible-superdir))) ((explode-path possible-subdir) . has-prefix? . (explode-path possible-superdir)))
(define (handle-publish) (define (handle-publish)
(define command-name ; either "publish" or "clone" (define command-name ; either "publish" or "clone"
(vector-ref (current-command-line-arguments) 0)) (vector-ref (current-command-line-arguments) 0))
(define force-target-overwrite? (make-parameter #t)) (define force-target-overwrite? (make-parameter #true))
(define other-args (command-line (define other-args (command-line
;; drop command name ;; drop command name
#:argv (vector-drop (current-command-line-arguments) 1) #:argv (vector-drop (current-command-line-arguments) 1)
@ -217,8 +209,8 @@ version print the version" (current-server-port) (make-publish-di
(begin (begin
(display (format "destination directory ~a exists. Overwrite? [yes/no] " dest-dir)) (display (format "destination directory ~a exists. Overwrite? [yes/no] " dest-dir))
(case (read) (case (read)
[(y yes) #t] [(y yes) #true]
[else #f])))) [else #false]))))
(cond (cond
[do-publish-operation? [do-publish-operation?
(when (directory-exists? dest-dir) (when (directory-exists? dest-dir)
@ -236,11 +228,11 @@ version print the version" (current-server-port) (make-publish-di
[else (displayln "publish aborted")])) [else (displayln "publish aborted")]))
(define (handle-unknown command) (define (handle-unknown command)
(if (regexp-match #rx"(shit|fuck)" command) (match command
(displayln (let ([responses '("Cursing at free software? Really?" "How uncouth." "Same to you, buddy.")]) [(regexp #rx"(shit|fuck)")
(list-ref responses (random (length responses))))) (define responses '("Cursing at free software? Really?" "How uncouth." "Same to you, buddy."))
(begin (displayln (list-ref responses (random (length responses))))]
(displayln (format "`~a` is an unknown command." command)) [_ (displayln (format "`~a` is an unknown command." command))
(display "These are the available ") ; ... "Pollen commands:" (display "These are the available ") ; ... "Pollen commands:"
(handle-help) (handle-help)
(exit 1)))) (exit 1)]))

@ -1 +1 @@
1540858411 1540858414

Loading…
Cancel
Save