On this page:
pathy/  c
build-complete-path/  passive
vantage
path-extension-in?
find-relative-path/  by-file
path-replace-base
7.7

6 idiocket/path

 (require idiocket/path) package: idiocket

A contract that captures "path-y" values.

procedure

(build-complete-path/passive base    
  path-elements ...)  path?
  base : pathy/c
  path-elements : pathy/c
Like build-path, except if path-elements form a complete path independently of base, that complete path is returned.

The returned path is always simplified, and base is made complete before using it to compute paths.

If you apply curry to this procedure and a complete path, you will create a flexible path building procedure.

(define-runtime-path project "/prj")
(define make-project-path (curry build-complete-path/passive project))
(define make-asset-path (curry build-complete-path/passive (make-project-path "files" "assets")))
(define make-dist-path (curry build-complete-path/passive (make-project-path "dist")))
 
(make-project-path "README.md") ; /prj/README.md
(make-asset-path "script.js")   ; /prj/files/assets/script.js
(make-dist-path "index.html")   ; /prj/dist/index.html
(make-dist-path "/override")    ; /override

procedure

(vantage p)

  (->* () #:rest pathy/c (or/c procedure? complete-path?))
  p : pathy/c
Return a procedure V that encapsulates use of build-complete-path/passive.

Example:

(define-runtime-path project-dir "/prj")
 
(define project-v (vantage project-dir))
(define assets/   (project-v "assets"))
(define images/   (assets/ "images"))
(define audio/    (assets/ "audio"))
(define vectors/  (images/ "vectors"))
(define rasters/  (images/ "rasters"))
 
(rasters/ "marvel.png") ; /prj/assets/images/rasters/marvel.png
(audio/ "scream.wav")   ; /prj/assets/audio/scream.wav
 
(define audio-v (audio/)) ; ...and so on

procedure

(path-extension-in? p exts)  boolean?

  p : pathy/c
  exts : (non-empty-listof (or/c string? bytes?))
Returns the first extension in exts that appears as the string suffix of p, or #f if no extension matches.

The matching extension will be returned unaltered.

> (path-extension-in? "/foo/bar.tar.gz" '(#".bz" ".tar.gz"))

".tar.gz"

procedure

(find-relative-path/by-file requestor-path    
  requestee-path)  path?
  requestor-path : pathy/c
  requestee-path : pathy/c
Like find-relative-path, except the first argument must be a path to a file.

Returns a relative path that a file located at requestor-path can use to refer to the file located at requestee-path. Both path arguments will be made complete before computing the result.

procedure

(path-replace-base p    
  input-dir    
  output-dir    
  [ext])  complete-path?
  p : complete-path?
  input-dir : complete-path?
  output-dir : complete-path?
  ext : bytes? = (path-get-extension p)
Replaces input-dir with output-dir in p. You can optionally replace the extension as well. Useful for deriving an output file path from an input file path.

Equivalent to:

(build-path output-dir (path-replace-extension (find-relative-path input-dir p) ext))