From fc5ca9f65928a581b2545edf71a3201dab807883 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 12 Dec 2019 18:27:12 -0800 Subject: [PATCH] add --null switch to `raco pollen render` (closes #24) --- pollen/private/command.rkt | 8 +++++--- pollen/private/ts.rktd | 2 +- pollen/render.rkt | 28 ++++++++++++++++------------ pollen/scribblings/raco.scrbl | 5 ++++- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/pollen/private/command.rkt b/pollen/private/command.rkt index 634e73a..84a499d 100644 --- a/pollen/private/command.rkt +++ b/pollen/private/command.rkt @@ -98,7 +98,7 @@ version print the version" (current-server-port) (make-publish-di (define render-target-wanted (make-parameter (current-poly-target))) (define render-with-subdirs? (make-parameter #f)) (define render-parallel? (make-parameter #f)) - (define dry-run? (make-parameter #f)) + (define special-output? (make-parameter #f)) (define parsed-args (command-line #:program "raco pollen render" #:argv (vector-drop (current-command-line-arguments) 1) ; snip the 'render' from the front @@ -108,7 +108,9 @@ version print the version" (current-server-port) (make-publish-di [("-r" "--recursive") "Render subdirectories recursively" (render-with-subdirs? 'recursive)] [("-s" "--subdir") "Render subdirectories nonrecursively" (render-with-subdirs? 'include)] - [("-d" "--dry-run") "Print paths that would be rendered" (dry-run? #true)] + #:once-any + [("-d" "--dry-run") "Print paths that would be rendered" (special-output? 'dry-run)] + [("-n" "--null") "Suppress file output" (special-output? 'null)] #:once-any [("-p" "--parallel") "Render in parallel using all cores" (render-parallel? #true)] [("-j" "--jobs") job-count "Render in parallel using jobs" (render-parallel? (or (string->number job-count) (raise-argument-error 'handle-render "exact positive integer" job-count)))] @@ -116,7 +118,7 @@ version print the version" (current-server-port) (make-publish-di other-args)) (define (handle-batch-render paths) - (apply render-batch (map very-nice-path paths) #:parallel (render-parallel?) #:dry-run (dry-run?))) + (apply render-batch (map very-nice-path paths) #:parallel (render-parallel?) #:special (special-output?))) (parameterize ([current-poly-target (render-target-wanted)]) ;; applies to both cases (let loop ([args parsed-args]) diff --git a/pollen/private/ts.rktd b/pollen/private/ts.rktd index 4c013c4..cb27ade 100644 --- a/pollen/private/ts.rktd +++ b/pollen/private/ts.rktd @@ -1 +1 @@ -1575833252 +1576204032 diff --git a/pollen/render.rkt b/pollen/render.rkt index 33ea3fa..f7fcb0c 100644 --- a/pollen/render.rkt +++ b/pollen/render.rkt @@ -155,9 +155,11 @@ [(list wpidx wp 'wants-lock path) (loop source-paths locks (append blocks (list (cons wp path))) completed-jobs completed-job-count)])]))) +(define current-null-output? (make-parameter #f)) + (define+provide/contract (render-batch #:parallel [wants-parallel-render? #false] - #:dry-run [wants-dry-run? #false] . paths-in) - ((#:parallel any/c) (#:dry-run boolean?) #:rest (listof pathish?) . ->* . void?) + #:special [special-output #false] . paths-in) + ((#:parallel any/c) (#:special (or/c boolean? symbol?)) #:rest (listof pathish?) . ->* . void?) ;; Why not just (for-each render ...)? ;; Because certain files will pass through multiple times (e.g., templates) ;; And with render, they would be rendered repeatedly. @@ -176,12 +178,13 @@ [_ (loop rest acc)])]))) (cond [(null? expanded-source-paths) (message "[no paths to render]")] - [wants-dry-run? (for-each message expanded-source-paths)] - [else (for-each render-to-file-if-needed - (match wants-parallel-render? - ;; returns crashed jobs for serial rendering - [#false expanded-source-paths] - [jobs-arg (parallel-render expanded-source-paths jobs-arg)]))])) + [(eq? special-output 'dry-run) (for-each message expanded-source-paths)] + [else (parameterize ([current-null-output? (eq? special-output 'null)]) + (for-each render-to-file-if-needed + (match wants-parallel-render? + ;; returns crashed jobs for serial rendering + [#false expanded-source-paths] + [jobs-arg (parallel-render expanded-source-paths jobs-arg)])))])) (define (pagetree->paths pagetree-or-path) (parameterize ([current-directory (current-project-root)]) @@ -246,10 +249,11 @@ (message (format "from cache /~a" (find-relative-path (current-project-root) output-path))))))] [else (render-thunk)])) - (display-to-file render-result - output-path - #:exists 'replace - #:mode (if (string? render-result) 'text 'binary)))) + (unless (current-null-output?) + (display-to-file render-result + output-path + #:exists 'replace + #:mode (if (string? render-result) 'text 'binary))))) (define+provide/contract (render-to-file-if-needed source-path [maybe-template-path #f] [maybe-output-path #f] [maybe-render-thunk #f]) ((complete-path?) ((or/c #f complete-path?) (or/c #f complete-path?) (or/c #f procedure?)) . ->* . void?) diff --git a/pollen/scribblings/raco.scrbl b/pollen/scribblings/raco.scrbl index 3b9816c..434e917 100644 --- a/pollen/scribblings/raco.scrbl +++ b/pollen/scribblings/raco.scrbl @@ -76,7 +76,10 @@ Adding the optional @exec{--local} switch will restrict the project server to re This command can be invoked two ways: in source mode or directory mode. -In both modes, the optional @exec{--dry-run} or @exec{-d} switch prints the paths that would be rendered by this command without actually doing so. +In both modes, the optional @exec{--dry-run} or @exec{-d} switch prints the paths that would be rendered by this command without actually doing so. + +In both modes, the optional @exec{--null} or @exec{-n} switch renders as usual, but doesn't write any files. (Convenient if you're arranging special render behavior, for instance writing to a database or network server.) + @bold{Source mode}: @racket[raco pollen render _sourceĀ ...] will render only the source paths specified in @racket[_source ...]. Consistent with the usual command-line idiom, this can be a single path, a list of paths, or a pattern: