You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
4.5 KiB
Racket
97 lines
4.5 KiB
Racket
#lang racket/base
|
|
(require racket/path racket/file compiler/cm file/cache sugar/coerce "project.rkt" "world.rkt" "rerequire.rkt" "cache-ns.rkt" "debug.rkt")
|
|
|
|
;; The cache is a hash with paths as keys.
|
|
;; The cache values are also hashes, with key/value pairs for that path.
|
|
|
|
(provide reset-cache cached-require paths->key)
|
|
|
|
|
|
(define cache-dir
|
|
(build-path (world:current-project-root) (world:current-cache-dir-name)))
|
|
|
|
|
|
(define (reset-cache)
|
|
(cache-remove #f cache-dir))
|
|
|
|
|
|
(define (paths->key source-path [template-path #f])
|
|
;; key is list of file + mod-time pairs, use #f for missing
|
|
(define path-strings (append (list source-path)
|
|
(append (list template-path) ; is either path or #f
|
|
(->list (get-directory-require-files source-path))))) ; is either list of files or (list #f)
|
|
;; can't use relative paths for cache keys because source files include `here-path` which is absolute.
|
|
;; problem is that cache could appear valid on another filesystem (based on relative pathnames & mod dates)
|
|
;; but would actually be invalid (because the `here-path` names are wrong).
|
|
(define path+mod-time-pairs
|
|
(map (λ(ps) (and ps (let ([cp (->complete-path ps)])
|
|
(cons (path->string cp) (file-or-directory-modify-seconds cp))))) path-strings))
|
|
path+mod-time-pairs)
|
|
|
|
|
|
(define (key->source-path key)
|
|
(car (car key)))
|
|
|
|
|
|
(define (path->hash path)
|
|
;; new namespace forces dynamic-require to re-instantiate 'path'
|
|
;; otherwise it gets cached in current namespace.
|
|
(define drfs (get-directory-require-files path))
|
|
(for-each managed-compile-zo (or drfs null))
|
|
|
|
(apply hash
|
|
(let ([doc-key (world:current-main-export)]
|
|
[meta-key (world:current-meta-export)])
|
|
(parameterize ([current-namespace (make-base-namespace)])
|
|
;; I monkeyed around with using the metas submodule to pull out the metas (for speed)
|
|
;; but in practice most files get their doc requested too.
|
|
;; so it's just simpler to get both at once and be done with it.
|
|
;; the savings of avoiding two cache fetches at the outset outweighs
|
|
;; the benefit of not reloading doc when you just need metas.
|
|
(list doc-key (dynamic-require path doc-key) meta-key (dynamic-require path meta-key))))))
|
|
|
|
;; include this from 6.2 for compatibility back to 6.0 (formerly `make-parent-directory*`)
|
|
(define (make-parent-directory p)
|
|
(unless (path-string? p)
|
|
(raise-argument-error 'make-parent-directory "path-string?" p))
|
|
|
|
(define (make-directory* dir)
|
|
(let-values ([(base name dir?) (split-path dir)])
|
|
(when (and (path? base) (not (directory-exists? base)))
|
|
(make-directory* base))
|
|
(unless (directory-exists? dir)
|
|
(with-handlers ([exn:fail:filesystem:exists? void])
|
|
(make-directory dir)))))
|
|
|
|
(define-values (base name dir?) (split-path p))
|
|
(when (path? base)
|
|
(make-directory* base)))
|
|
|
|
|
|
(define ram-cache (make-hash))
|
|
|
|
(define (cached-require path-string subkey)
|
|
(define path (with-handlers ([exn:fail? (λ _ (error 'cached-require (format "~a is not a valid path" path-string)))])
|
|
(->complete-path path-string)))
|
|
|
|
(when (not (file-exists? path))
|
|
(error (format "cached-require: ~a does not exist" path)))
|
|
|
|
(cond
|
|
[(world:current-compile-cache-active)
|
|
(define key (paths->key path))
|
|
;; use multiple pickup files to avoid locking issues.
|
|
;; pickup-file hierarchy just mirrors the project hierarchy.
|
|
(define dest-file (build-path cache-dir (path->string (find-relative-path (world:current-project-root) (string->path (format "~a.rktd" (key->source-path key)))))))
|
|
(make-parent-directory dest-file)
|
|
(hash-ref (hash-ref! ram-cache key (λ _
|
|
(cache-file dest-file
|
|
#:exists-ok? #t
|
|
key
|
|
cache-dir
|
|
(λ _
|
|
(write-to-file (path->hash path) dest-file #:exists 'replace))
|
|
#:max-cache-size (world:current-compile-cache-max-size))
|
|
(file->value dest-file))) subkey)]
|
|
[else (parameterize ([current-namespace (make-base-namespace)])
|
|
(dynamic-require path subkey))])) |