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.
pollen/file-tools.rkt

192 lines
6.3 KiB
Racket

#lang racket/base
11 years ago
(require racket/contract racket/path)
(require (only-in racket/path filename-extension))
11 years ago
(require "world.rkt" sugar)
;; for files like svg that are not source in pollen terms,
;; but have a textual representation separate from their display.
11 years ago
(define+provide/contract (sourceish? x)
(any/c . -> . coerce/boolean?)
(define sourceish-extensions (list "svg"))
(try ((get-ext x) . in? . sourceish-extensions)
(except [exn:fail? (λ(e) #f)])))
;; if something can be successfully coerced to a url,
;; it's urlish.
11 years ago
(define+provide/contract (urlish? x)
(any/c . -> . coerce/boolean?)
(try (->url x) (except [exn:fail? (λ(e) #f)])))
11 years ago
;; if something can be successfully coerced to a path,
;; it's pathish.
11 years ago
(define+provide/contract (pathish? x)
(any/c . -> . coerce/boolean?)
(try (->path x) (except [exn:fail? (λ(e) #f)])))
;; like pathish, but for directories
;; todo: is this contract too restrictive?
;; pathish doesn't require the path to exist,
;; but this one does.
11 years ago
(define+provide/contract (directory-pathish? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) (directory-exists? (->path x))))
;; compare directories by their exploded path elements,
;; not by equal?, which will give wrong result if no slash on the end
11 years ago
(define+provide/contract (directories-equal? dirx diry)
(coerce/path? coerce/path? . -> . coerce/boolean?)
(equal? (explode-path dirx) (explode-path diry)))
11 years ago
(define+provide/contract (get-enclosing-dir p)
(coerce/path? . -> . path?)
(simplify-path (build-path p 'up)))
11 years ago
;; helper function for ptree
;; make paths absolute to test whether files exist,
;; then convert back to relative
11 years ago
(define+provide/contract (visible? path)
(coerce/path? . -> . coerce/boolean?)
(not ((->string path) . starts-with? . ".")))
11 years ago
11 years ago
(define+provide/contract (visible-files dir)
11 years ago
(directory-pathish? . -> . (listof path?))
(filter visible?
(map (λ(p) (find-relative-path dir p))
(filter file-exists?
(directory-list dir #:build? #t)))))
;; does path have a certain extension
11 years ago
(define+provide/contract (has-ext? x ext)
(coerce/path? coerce/symbol? . -> . coerce/boolean?)
(define ext-of-path (filename-extension x))
(and ext-of-path (equal? (string-downcase (bytes->string/utf-8 ext-of-path)) (string-downcase (symbol->string ext)))))
11 years ago
;; todo: add extensions
(define binary-extensions
11 years ago
'(gif jpg jpeg mp3 png zip pdf ico tar ai eps))
11 years ago
11 years ago
(define+provide/contract (has-binary-ext? x)
(coerce/path? . -> . coerce/boolean?)
(ormap (λ(ext) (has-ext? x ext)) binary-extensions))
;; get file extension as a string, or return #f
;; (consistent with filename-extension behavior)
11 years ago
(define+provide/contract (get-ext x)
(coerce/path? . -> . (or/c string? #f))
(let ([fe-result (filename-extension x)])
(and fe-result (bytes->string/utf-8 fe-result))))
;; put extension on path
;; use local contract here because this function is used within module
11 years ago
(define/contract+provide (add-ext x ext)
(coerce/string? coerce/string? . -> . coerce/path?)
(string-append x "." ext))
;; take one extension off path
11 years ago
(define+provide/contract (remove-ext x)
(coerce/path? . -> . path?)
11 years ago
;; pass through hidden files (those starting with a dot)
(if (x . starts-with? . ".")
x
(path-replace-suffix x "")))
;; take all extensions off path
11 years ago
(define+provide/contract (remove-all-ext x)
(coerce/path? . -> . path?)
11 years ago
;; pass through hidden files (those starting with a dot)
(if (x . starts-with? . ".")
x
(let ([path-with-removed-ext (remove-ext x)])
(if (equal? x path-with-removed-ext)
x
11 years ago
(remove-all-ext path-with-removed-ext)))))
11 years ago
;; todo: tests for these predicates
11 years ago
(define+provide/contract (preproc-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) (has-ext? (->path x) PREPROC_SOURCE_EXT)))
11 years ago
(define+provide/contract (has-preproc-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) (file-exists? (->preproc-source-path (->path x)))))
11 years ago
(define+provide/contract (has-decoder-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) (file-exists? (->decoder-source-path (->path x)))))
11 years ago
(define+provide/contract (needs-preproc? x)
(any/c . -> . coerce/boolean?)
; it's a preproc source file, or a file that's the result of a preproc source
(and (pathish? x) (ormap (λ(proc) (proc (->path x))) (list preproc-source? has-preproc-source?))))
11 years ago
(define+provide/contract (needs-template? x)
(any/c . -> . coerce/boolean?)
; it's a pollen source file
; or a file (e.g., html) that has a pollen source file
(and (pathish? x) (ormap (λ(proc) (proc (->path x))) (list decoder-source? has-decoder-source?))))
11 years ago
(define+provide/contract (ptree-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) ((->path x) . has-ext? . PTREE_SOURCE_EXT)))
11 years ago
11 years ago
(define+provide/contract (decoder-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x) (has-ext? x DECODER_SOURCE_EXT)))
11 years ago
(define+provide/contract (template-source? x)
(any/c . -> . coerce/boolean?)
(and (pathish? x)
(let-values ([(dir name ignore) (split-path x)])
(equal? (get (->string name) 0) TEMPLATE_SOURCE_PREFIX))))
11 years ago
;; todo: tighten these input contracts
;; so that, say, a source-path cannot be input for make-preproc-source-path
11 years ago
(define+provide/contract (->preproc-source-path x)
(coerce/path? . -> . coerce/path?)
(if (preproc-source? x)
x
(add-ext x PREPROC_SOURCE_EXT)))
11 years ago
(define+provide/contract (->output-path x)
(coerce/path? . -> . coerce/path?)
(if (or (decoder-source? x) (preproc-source? x))
(remove-ext x)
x))
;; turns input into corresponding pollen source path
;; does not, however, validate that new path exists
;; todo: should it? I don't think so, sometimes handy to make the name for later use
;; OK to use pollen source as input (comes out the same way)
11 years ago
(define+provide/contract (->decoder-source-path x)
(coerce/path? . -> . coerce/path?)
(if (decoder-source? x)
x
(add-ext x DECODER_SOURCE_EXT)))
11 years ago
(define+provide/contract (project-files-with-ext ext)
(coerce/symbol? . -> . (listof complete-path?))
(map ->complete-path (filter (λ(i) (has-ext? i ext)) (directory-list (CURRENT_PROJECT_ROOT)))))
11 years ago
;; to identify unsaved sources in DrRacket
(define (unsaved-source? path-string)
((substring (->string path-string) 0 7) . equal? . "unsaved"))
11 years ago
;; todo: write tests for project-files-with-ext