Search code examples
schemelispracketrequire

Racket - require an entire directory of files


Currently I have to do this at the top of every file:

(require "dynamore/dynamore.rkt")
(require "dynamore/db.rkt")
(require "dynamore/types.rkt")

I would like to be able to do something like this:

(require dynamore)

Preferably without having to develop my own collection.


Solution

  • If you prefer not to create a package (which means you need to require relatively):

    1. If dynamore contains only those three files, you can use reprovide-lang's glob-in as follows: (require (glob-in "dynamore/*.rkt")).
    2. You can also create main.rkt in dynamore that uses the main functionality of reprovide-lang to specifically reprovide only those three files:

      #lang reprovide
      "dynamore.rkt"
      "db.rkt"
      "types.rkt"
      

      To use it, simply (require "dynamore/main.rkt").

      • A native solution that doesn't use reprovide-lang would be to use all-from-out manually:

        #lang racket/base
        (require "dynamore.rkt"
                 "db.rkt"
                 "types.rkt")
        (provide (all-from-out "dynamore.rkt"
                               "db.rkt"
                               "types.rkt"))
        

    If you prefer to create a package, then follow Solution 2 above (create main.rkt, etc.), create info.rkt in the dynamore directory as follows:

    #lang info
    (define collection "dynamore")
    

    Then run raco pkg install. From now on, you will be able to (require dynamore) from anywhere.

    Note: to install reprovide-lang, run raco pkg install reprovide-lang.