Back
canonicalize (clj)
(source)function
(canonicalize f)
(canonicalize f {:keys [:nofollow-links]})
Returns the canonical path via
java.io.File#getCanonicalPath. If `:nofollow-links` is set, then it
will fall back on `absolutize` + `normalize.` This function can be used
as an alternative to `real-path` which requires files to exist.
Examples
nextjournal/clerk
(ns nextjournal.clerk.builder-test
(:require [babashka.fs :as fs]
[clojure.java.browse :as browse]
[clojure.string :as str]
[clojure.test :refer [deftest is testing]]
[matcher-combinators.test]
[nextjournal.clerk.builder :as builder]
[nextjournal.clerk.viewer :as viewer])
(:import (clojure.lang ExceptionInfo)
(java.io File)))
(deftest url-canonicalize
(testing "canonicalization of file components into url components"
(let [dice (str/join (File/separator) ["notebooks" "dice.clj"])]
(is (= (#'builder/path-to-url-canonicalize dice) (str/replace dice (File/separator) "/"))))))
babashka/bbin
(ns babashka.bbin.scripts.http-file
(:require [babashka.bbin.protocols :as p]
[babashka.bbin.dirs :as dirs]
[babashka.bbin.scripts.common :as common]
[babashka.fs :as fs]))
(defrecord HttpFile [cli-opts coords]
p/Script
(install [_]
(let [http-url (:script/lib cli-opts)
script-deps {:bbin/url http-url}
header {:coords script-deps}
script-name (or (:as cli-opts) (common/http-url->script-name http-url))
script-contents (-> (slurp (:bbin/url script-deps))
(common/insert-script-header header))
script-file (fs/canonicalize (fs/file (dirs/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(common/install-script script-name header script-file script-contents cli-opts)))
babashka/bbin
(ns babashka.bbin.scripts.local-jar
(:require [babashka.bbin.protocols :as p]
[babashka.bbin.scripts.common :as common]
[babashka.bbin.dirs :as dirs]
[babashka.bbin.util :as util]
[babashka.fs :as fs]
[clojure.string :as str]
[selmer.parser :as selmer]
[selmer.util :as selmer-util]))
(defrecord LocalJar [cli-opts coords]
p/Script
(install [_]
(fs/create-dirs (dirs/jars-dir cli-opts))
(let [file-path (str (fs/canonicalize (:script/lib cli-opts) {:nofollow-links true}))
main-ns (common/jar->main-ns file-path)
script-deps {:bbin/url (str "file://" file-path)}
header {:coords script-deps}
script-name (or (:as cli-opts) (common/file-path->script-name file-path))
cached-jar-path (fs/file (dirs/jars-dir cli-opts) (str script-name ".jar"))
script-edn-out (with-out-str
(binding [*print-namespace-maps* false]
(util/pprint header)))
template-opts {:script/meta (->> script-edn-out
str/split-lines
(map #(str common/comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
script-file (fs/canonicalize (fs/file (dirs/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(fs/copy file-path cached-jar-path {:replace-existing true})
(common/install-script script-name header script-file script-contents cli-opts)))
babashka/bbin
(ns babashka.bbin.scripts.http-jar
(:require [babashka.bbin.protocols :as p]
[babashka.bbin.scripts.common :as common]
[babashka.bbin.dirs :as dirs]
[babashka.bbin.util :as util]
[babashka.fs :as fs]
[babashka.http-client :as http]
[clojure.java.io :as io]
[clojure.string :as str]
[selmer.parser :as selmer]
[selmer.util :as selmer-util]))
(defrecord HttpJar [cli-opts coords]
p/Script
(install [_]
(fs/create-dirs (dirs/jars-dir cli-opts))
(let [http-url (:script/lib cli-opts)
script-deps {:bbin/url http-url}
header {:coords script-deps}
script-name (or (:as cli-opts) (common/http-url->script-name http-url))
tmp-jar-path (doto (fs/file (fs/temp-dir) (str script-name ".jar"))
(fs/delete-on-exit))
_ (io/copy (:body (http/get http-url {:as :bytes})) tmp-jar-path)
main-ns (common/jar->main-ns tmp-jar-path)
cached-jar-path (fs/file (dirs/jars-dir cli-opts) (str script-name ".jar"))
_ (fs/move tmp-jar-path cached-jar-path {:replace-existing true})
script-edn-out (with-out-str
(binding [*print-namespace-maps* false]
(util/pprint header)))
template-opts {:script/meta (->> script-edn-out
str/split-lines
(map #(str common/comment-char " " %))
(str/join "\n"))
:script/main-ns main-ns
:script/jar cached-jar-path}
script-contents (selmer-util/without-escaping
(selmer/render local-jar-template-str template-opts))
script-file (fs/canonicalize (fs/file (dirs/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(common/install-script script-name header script-file script-contents cli-opts)))
babashka/bbin
(ns babashka.bbin.scripts.local-file
(:require [babashka.bbin.protocols :as p]
[babashka.bbin.scripts.common :as common]
[babashka.bbin.dirs :as dirs]
[babashka.fs :as fs]))
(defrecord LocalFile [cli-opts coords]
p/Script
(install [_]
(let [file-path (str (fs/canonicalize (:script/lib cli-opts) {:nofollow-links true}))
script-deps {:bbin/url (str "file://" file-path)}
header {:coords script-deps}
script-name (or (:as cli-opts) (common/file-path->script-name file-path))
script-contents (-> (slurp file-path)
(common/insert-script-header header))
script-file (fs/canonicalize (fs/file (dirs/bin-dir cli-opts) script-name)
{:nofollow-links true})]
(common/install-script script-name header script-file script-contents cli-opts)))