[racket] requiring a file in current-directory, if it exists
This worked — for some reason, moving the '(file ...)' expression into the
`with-syntax` expression made a difference.
(define-syntax (overriding-require+provide-with-prefix stx)
(syntax-case stx () [(_ main override out-prefix)
(let ([path-to-override (path->string (build-path
(current-directory) (syntax->datum #'override)))])
(if (file-exists? path-to-override)
(with-syntax ([override (datum->syntax stx
`(file ,(datum->syntax stx path-to-override)))])
#'(begin
(require (combine-in override
(subtract-in main override)))
(provide (prefix-out out-prefix
(combine-out (all-from-out main) (all-from-out override))))))
#'(begin
(require main)
(provide (prefix-out out-prefix
(all-from-out main))))))]))
(overriding-require+provide-with-prefix "main.rkt" "override.rkt"
the-prefix:)
On Fri, Oct 24, 2014 at 1:20 PM, Matthew Butterick <mb at mbtype.com> wrote:
> I know that a hard-coded absolute path can be used in `require`:
>
> (require (file "/path/to/directory/module.rkt"))))
>
> But how can a generated path be used? Like so:
>
> (require (file (path->string (build-path (current-directory)
> "module.rkt"))))
>
> In particular, I want to `require` the file if it exists, or otherwise
> skip it.
>
> I thought it should be a macro like this, but it doesn't work. The correct
> `require` syntax pops out, but it doesn't bind any identifiers.
>
> (define-syntax (try-current-directory-require stx)
> (syntax-case stx ()
> [(_ filename)
> (with-syntax ([path-string (datum->syntax stx (path->string
> (build-path (current-directory) (syntax->datum #'filename))))])
> (if (file-exists? (syntax->datum #'path-string))
> #'(require (prefix-in local: (file path-string)))
> #'(void)))]))
>
> (try-current-directory-require "module.rkt")
>
>
> Any suggestions? Is this a job for `define-require-syntax`?
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20141024/f6452382/attachment.html>