Skip to content

Commit

Permalink
Add --mode option to built in target task
Browse files Browse the repository at this point in the history
For situations where the default 'rw-------' file modes are not enough,
a (posix) mode string can be given to target. All files will get the 
mode written.

When an invalid mode is given, a warning is printed and standard 
permissions will be used.
  • Loading branch information
ekroon committed Dec 3, 2016
1 parent 2ffab17 commit bb89a54
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions boot/core/src/boot/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1002,10 +1002,10 @@
(let [prev (atom nil)
clean! (if clean empty-dir! identity)
dirs (delay (mapv #(doto (io/file %) .mkdirs clean!) dirs))]
(fn [fs & {:keys [link]}]
(fn [fs & {:keys [link mode]}]
(let [link (when link :tmp)
[a b] [@prev (reset! prev (output-fileset fs))]]
(mapv deref (for [d @dirs :let [p! (partial fs/patch! (fs/->path d) a b :link)]]
(mapv deref (for [d @dirs :let [p! (partial fs/patch! (fs/->path d) a b :mode mode :link)]]
(future (try (p! link)
(catch Throwable t
(if-not link (throw t) (p! nil)))))))))))
Expand Down
11 changes: 10 additions & 1 deletion boot/core/src/boot/task/built_in.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
[boot.pedantic :as pedantic])
(:import
[java.io File]
[java.nio.file.attribute PosixFilePermissions]
[java.util Arrays]
[java.util.concurrent LinkedBlockingQueue TimeUnit]
[javax.tools ToolProvider DiagnosticCollector Diagnostic$Kind]))
Expand Down Expand Up @@ -361,16 +362,24 @@
(core/with-post-wrap [_] @(promise))
(core/with-pass-thru [fs] (Thread/sleep time))))

(defn- mk-posix-file-permissions [posix-string]
(try
(when posix-string
(PosixFilePermissions/fromString posix-string))
(catch IllegalArgumentException _
(util/warn "Could not parse mode string, ignoring...\n"))))

(core/deftask target
"Writes output files to the given directory on the filesystem."
[d dir PATH #{str} "The set of directories to write to (target)."
m mode VAL str "The mode of written files in 'rwxrwxrwx' format"
L no-link bool "Don't create hard links."
C no-clean bool "Don't clean target before writing project files."]
(let [dir (or (seq dir) ["target"])
sync! (#'core/fileset-syncer dir :clean (not no-clean))]
(core/with-pass-thru [fs]
(util/info "Writing target dir(s)...\n")
(sync! fs :link (not no-link)))))
(sync! fs :link (not no-link) :mode (mk-posix-file-permissions mode)))))

(core/deftask watch
"Call the next handler when source files change."
Expand Down
12 changes: 7 additions & 5 deletions boot/pod/src/boot/filesystem.clj
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,22 @@
(Files/setLastModifiedTime dst (FileTime/fromMillis time))))

(defn copy!
[^Path dest path ^Path src time]
[^Path dest path ^Path src time mode]
(let [dst (doto (rel dest path) mkparents!)]
(util/dbug* "Filesystem: copying %s...\n" (string/join "/" path))
(try (Files/copy ^Path src ^Path dst copy-opts)
(Files/setLastModifiedTime dst (FileTime/fromMillis time))
(when mode (Files/setPosixFilePermissions dst mode))
(catch java.nio.file.NoSuchFileException ex
(util/dbug* "Filesystem: %s\n", (str ex))))))

(defn link!
[dest path src]
[dest path src mode]
(let [dst (rel dest path)]
(util/dbug* "Filesystem: linking %s...\n" (string/join "/" path))
(try (Files/deleteIfExists dst)
(Files/createLink (doto dst mkparents!) src)
(when mode (Files/setPosixFilePermissions dst mode))
(catch java.nio.file.NoSuchFileException ex
(util/dbug* "Filesystem: %s\n" (str ex))))))

Expand All @@ -188,11 +190,11 @@
(writer-fn os))))

(defn patch!
[dest before after & {:keys [link]}]
[dest before after & {:keys [link mode]}]
(with-let [_ (fsp/patch-result before after)]
(doseq [[op path & [arg1 arg2]] (fsp/patch before after link)]
(case op
:delete (delete! dest path)
:write (copy! dest path arg1 arg2)
:link (link! dest path arg1)
:write (copy! dest path arg1 arg2 mode)
:link (link! dest path arg1 mode)
:touch (touch! dest path arg1)))))

0 comments on commit bb89a54

Please sign in to comment.