improve path handling in 'raco pollen render' (closes #53)

pull/58/head
Matthew Butterick 10 years ago
parent 5d6903de2e
commit 6660b0a947

@ -1,7 +1,10 @@
#lang racket/base
(require pollen/world)
(require pollen/world sugar/coerce)
(provide (all-defined-out))
(define (very-nice-path x)
(path->complete-path (simplify-path (cleanse-path (->path x)))))
(define (handle-test)
`(displayln "raco pollen is installed correctly"))
@ -15,25 +18,35 @@ render [dir] [dest] render project in dir (default is current dir)
render filename render filename only (can be source or output name)
clone copy project to desktop without source files" ,(world:current-server-port))))
(define (handle-render dir-or-path rest-args)
`(begin
(require pollen/render pollen/world pollen/file sugar pollen/pagetree racket/list)
(define (handle-render path-args)
`(begin
(require pollen/render pollen/world pollen/file sugar pollen/pagetree racket/list pollen/command racket/string)
(parameterize ([current-directory (world:current-project-root)])
(define dir-or-path ,dir-or-path)
(apply render-batch (map ->complete-path (if (not (directory-exists? dir-or-path))
(begin
(displayln (format "Rendering ~a" dir-or-path))
(cons dir-or-path ',rest-args))
(let ([dir dir-or-path]) ; now we know it's a dir
(displayln (format "Rendering preproc & pagetree files in directory ~a" dir))
(define preprocs (filter preproc-source? (directory-list dir)))
(define static-pagetrees (filter pagetree-source? (directory-list dir)))
;; if there are no static pagetrees, use make-project-pagetree
;; (which will synthesize a pagetree if needed, which includes all sources)
(define pagetrees (if (empty? static-pagetrees)
(list (make-project-pagetree dir))
static-pagetrees))
(append* preprocs pagetrees))))))))
(define path-args ',path-args)
(define first-arg (car path-args))
(if (directory-exists? first-arg)
(let ([dir first-arg]) ; now we know it's a dir
(parameterize ([current-directory dir]
[world:current-project-root dir])
(define preprocs (filter preproc-source? (directory-list dir)))
(define static-pagetrees (filter pagetree-source? (directory-list dir)))
;; if there are no static pagetrees, use make-project-pagetree
;; (which will synthesize a pagetree if needed, which includes all sources)
(define preprocs-and-static-pagetrees (append preprocs static-pagetrees))
(define batch-to-render
(map very-nice-path
(cond
[(empty? preprocs-and-static-pagetrees)
(displayln (format "Rendering generated pagetree for directory ~a" dir))
(cdr (make-project-pagetree dir))]
[else
(displayln (format "Rendering preproc & pagetree files in directory ~a" dir))
preprocs-and-static-pagetrees])))
(apply render-batch batch-to-render)))
(begin ; first arg is a file
(displayln (format "Rendering ~a" (string-join (map ->string path-args) " ")))
(apply render-batch path-args))))))
(define (handle-start directory [port #f])

@ -6,12 +6,11 @@
(define-for-syntax args (current-command-line-arguments))
(define-for-syntax arg-command-name (with-handlers ([exn:fail? (λ(exn) #f)]) (vector-ref args 0)))
(define-for-syntax first-arg-or-current-dir
(with-handlers ([exn:fail? (λ(exn) (current-directory))])
;; incoming path argument is handled as described in
;; docs for current-directory
(path->complete-path (simplify-path (cleanse-path (string->path (vector-ref args 1)))))))
(very-nice-path (vector-ref args 1))))
(define-for-syntax rest-args
(with-handlers ([exn:fail? (λ(exn) #f)])
@ -35,7 +34,7 @@
[("test" "xyzzy") (handle-test)]
[(#f "help") (handle-help)]
[("start") (handle-start (path->directory-path first-arg-or-current-dir) port-arg)]
[("render") (handle-render first-arg-or-current-dir rest-args)]
[("render") (handle-render (cons first-arg-or-current-dir (map very-nice-path (cdr (vector->list (current-command-line-arguments))))))]
[("clone") (handle-clone first-arg-or-current-dir rest-args)]
[else (handle-else arg-command-name)]))))

@ -1,6 +1,6 @@
#lang scribble/manual
@(require "mb-tools.rkt" scribble/eval pollen/world (for-label racket pollen/world))
@(require "mb-tools.rkt" scribble/eval pollen/world (for-label (except-in racket ...) pollen/world))
@(define my-eval (make-base-eval))
@(my-eval `(require pollen pollen/file))
@ -47,7 +47,7 @@ Displays a list of available commands.
@section{@racket[raco pollen start]}
Starts the project server from the current directory using the default port, which is the value of the parameter @racket[world:current-server-port] (by default, port @(format "~a" world:default-port)).
Start the project server from the current directory using the default port, which is the value of the parameter @racket[world:current-server-port] (by default, port @(format "~a" world:default-port)).
This command can be invoked with two optional arguments.
@ -69,13 +69,13 @@ If you want to start in the current directory but with a different port, use @li
@section{@racket[raco pollen render]}
Renders all preprocessor source files and then all pagetree files found in the current directory. If no pagetree files are found, all source files will be rendered.
Render all preprocessor source files and then all pagetree files found in the current directory. If none of these files are found, a pagetree will be generated for the directory (which will include all source files) and then rendered.
This command can be invoked with extra arguments.
@racket[raco pollen render _directory] will render the preprocessor source files and pagetree files in the specified directory.
@racket[raco pollen render _directory] will perform the render described above, but in the specified directory.
Alternatively, the command can take a variable number of path arguments. @racket[raco pollen render _path...] will render only the paths specified in @racket[_path...]. Consistent with the usual command-line idiom, this can be a single path, a list of paths, or a pattern:
Alternatively, the command can take a variable number of path arguments. @racket[raco pollen render _path ...] will render only the paths specified in @racket[_path ...]. Consistent with the usual command-line idiom, this can be a single path, a list of paths, or a pattern:
@terminal{
> raco pollen render foo.html.pm
@ -85,7 +85,7 @@ Alternatively, the command can take a variable number of path arguments. @racket
@section{@racket[raco pollen clone]}
Makes a copy of the project directory on the desktop, and removes any source files or other Pollen-related files.
Make a copy of the project directory on the desktop, and remove any source files or other Pollen-related files. (This function is pretty lame, and I invite suggestions for improvement.)
@racket[raco pollen clone _source-dir] will clone source from @racket[_source-dir] onto the desktop.

Loading…
Cancel
Save