diff --git a/doc/dev.md b/doc/dev.md index 6a227e4c..5bc22ec9 100644 --- a/doc/dev.md +++ b/doc/dev.md @@ -61,6 +61,9 @@ $ BABASHKA_XMX="-J-Xmx4g" script/compile Keep notes here about how adding libraries and classes to Babashka affects the binary size. +2020/03/28 Added java.nio.file.FileSystem(s) to support extracting zip files +42562284 - 42021244 = 541kb added. + 2020/03/22 Added java.io.FileReader 42025276 - 42008876 = 16kb added. diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 8d2cbb66..b4ebf827 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -56,6 +56,8 @@ java.net.URLDecoder java.nio.file.CopyOption java.nio.file.FileAlreadyExistsException + java.nio.file.FileSystem + java.nio.file.FileSystems java.nio.file.Files java.nio.file.LinkOption java.nio.file.NoSuchFileException @@ -201,7 +203,9 @@ (instance? java.security.MessageDigest v) java.security.MessageDigest (instance? java.io.InputStream v) - java.io.InputStream))))) + java.io.InputStream + (instance? java.nio.file.FileSystem v) + java.nio.file.FileSystem))))) (def class-map (gen-class-map)) diff --git a/test/babashka/file_var_test.clj b/test/babashka/file_var_test.clj index 0ab76cb9..6aeb7577 100644 --- a/test/babashka/file_var_test.clj +++ b/test/babashka/file_var_test.clj @@ -1,8 +1,8 @@ (ns babashka.file-var-test (:require [babashka.test-utils :as tu] - [clojure.test :as t :refer [deftest is]] - [clojure.string :as str])) + [clojure.string :as str] + [clojure.test :as t :refer [deftest is]])) (defn bb [input & args] (apply tu/bb (when (some? input) (str input)) (map str args))) diff --git a/test/babashka/main_test.clj b/test/babashka/main_test.clj index 28df8581..d7172641 100644 --- a/test/babashka/main_test.clj +++ b/test/babashka/main_test.clj @@ -375,6 +375,9 @@ (prn "output:" v) (is v)))) +(deftest download-and-extract-test + (is (= 6 (bb nil (io/file "test" "babashka" "scripts" "download_and_extract_zip.bb"))))) + ;;;; Scratch (comment diff --git a/test/babashka/scripts/download_and_extract_zip.bb b/test/babashka/scripts/download_and_extract_zip.bb new file mode 100644 index 00000000..fd2bc150 --- /dev/null +++ b/test/babashka/scripts/download_and_extract_zip.bb @@ -0,0 +1,31 @@ +(require '[clojure.java.io :as io] '[clojure.java.shell :refer [sh]] '[clojure.string :as str]) +(import '[java.net URL HttpURLConnection]) + +(set! *warn-on-reflection* true) + +(let [os-name (System/getProperty "os.name") + os-name (str/lower-case os-name) + os (cond (str/includes? os-name "linux") "linux" + (str/includes? os-name "mac") "macos" + (str/includes? os-name "win") "windows") + tmp-dir (System/getProperty "java.io.tmpdir") + zip-file (io/file tmp-dir "bb-0.0.78.zip") + source (URL. (format "https://github.com/borkdude/babashka/releases/download/v0.0.78/babashka-0.0.78-%s-amd64.zip" os)) + conn ^HttpURLConnection (.openConnection ^URL source)] + (.connect conn) + (with-open [is (.getInputStream conn)] + (io/copy is zip-file)) + (let [bb-file (io/file tmp-dir "bb-extracted") + fs (java.nio.file.FileSystems/newFileSystem (.toPath zip-file) nil) + to-extract (.getPath fs "bb" (into-array String []))] + (java.nio.file.Files/copy to-extract (.toPath bb-file) + ^"[Ljava.nio.file.CopyOption;" + (into-array java.nio.file.CopyOption [])) + (.setExecutable bb-file true) + (let [out (:out (sh (.getPath bb-file) "(+ 1 2 3)"))] + (.delete bb-file) + (.delete zip-file) + (println out)))) + + +