91 lines
2.4 KiB
Clojure
91 lines
2.4 KiB
Clojure
(ns Path
|
|
"Elixir Path module — file path manipulation.
|
|
|
|
In CljElixir: (Path/join \"a\" \"b\"), (Path/expand \"~/file\"), etc.
|
|
All functions work with forward slashes on all platforms.")
|
|
|
|
(defn join
|
|
"Joins path segments.
|
|
(Path/join \"a\" \"b\") ;=> \"a/b\"
|
|
(Path/join [\"a\" \"b\" \"c\"]) ;=> \"a/b/c\""
|
|
([paths])
|
|
([left right]))
|
|
|
|
(defn expand
|
|
"Expands a path to an absolute path, resolving ~ and relative components.
|
|
(Path/expand \"~/file.txt\") ;=> \"/Users/ajet/file.txt\"
|
|
(Path/expand \"../other\" \"/base\") ;=> \"/other\""
|
|
([path])
|
|
([path relative-to]))
|
|
|
|
(defn absname
|
|
"Converts to absolute path.
|
|
(Path/absname \"file.txt\") ;=> \"/Users/ajet/repos/clje/file.txt\""
|
|
([path])
|
|
([path relative-to]))
|
|
|
|
(defn relative-to
|
|
"Returns the relative path from `path` to `from`.
|
|
(Path/relative-to \"/a/b/c\" \"/a\") ;=> \"b/c\""
|
|
([path from])
|
|
([path from opts]))
|
|
|
|
(defn relative
|
|
"Forces path to be relative.
|
|
(Path/relative \"/a/b\") ;=> \"a/b\""
|
|
[path])
|
|
|
|
(defn basename
|
|
"Returns the last component of the path.
|
|
(Path/basename \"/a/b/c.txt\") ;=> \"c.txt\"
|
|
(Path/basename \"/a/b/c.txt\" \".txt\") ;=> \"c\""
|
|
([path])
|
|
([path extension]))
|
|
|
|
(defn dirname
|
|
"Returns the directory component.
|
|
(Path/dirname \"/a/b/c.txt\") ;=> \"/a/b\""
|
|
[path])
|
|
|
|
(defn extname
|
|
"Returns the file extension.
|
|
(Path/extname \"file.ex\") ;=> \".ex\"
|
|
(Path/extname \"file\") ;=> \"\""
|
|
[path])
|
|
|
|
(defn rootname
|
|
"Returns the path without extension.
|
|
(Path/rootname \"file.ex\") ;=> \"file\"
|
|
(Path/rootname \"file.tar.gz\" \".tar.gz\") ;=> \"file\""
|
|
([path])
|
|
([path extension]))
|
|
|
|
(defn split
|
|
"Splits a path into its components.
|
|
(Path/split \"/a/b/c\") ;=> [\"/\" \"a\" \"b\" \"c\"]"
|
|
[path])
|
|
|
|
(defn type
|
|
"Returns the path type: :absolute, :relative, or :volumerelative.
|
|
(Path/type \"/a/b\") ;=> :absolute
|
|
(Path/type \"a/b\") ;=> :relative"
|
|
[path])
|
|
|
|
(defn wildcard
|
|
"Expands a glob pattern. Returns matching paths.
|
|
(Path/wildcard \"lib/**/*.ex\") ;=> [\"lib/my_app.ex\" ...]
|
|
(Path/wildcard \"*.{ex,exs}\") ;=> matches .ex and .exs files"
|
|
([glob])
|
|
([glob opts]))
|
|
|
|
(defn safe-relative
|
|
"Returns a safe relative path (no ..). Returns {:ok path} or :error.
|
|
(Path/safe-relative \"a/b/c\") ;=> {:ok \"a/b/c\"}
|
|
(Path/safe-relative \"../etc/passwd\") ;=> :error"
|
|
([path])
|
|
([path cwd]))
|
|
|
|
(defn safe-relative-to
|
|
"Returns a safe relative path from `path` to `cwd`. Returns {:ok path} or :error."
|
|
[path cwd])
|