Skip to content

Commit

Permalink
Merge pull request #537 from ekroon/optional-target-mode
Browse files Browse the repository at this point in the history
Add --mode option to built in target task
  • Loading branch information
alandipert authored Dec 15, 2016
2 parents 8e87ecd + c9f2111 commit b89738e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changes

## Master

##### Task Options
- Added `-m, --mode` option to the `target` task — specifies the file
mode for written files — should only be used when default `rw-------`
is not enough.

## 2.7.0

#### Improved
Expand Down
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
13 changes: 12 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,26 @@
(core/with-post-wrap [_] @(promise))
(core/with-pass-thru [fs] (Thread/sleep time))))

(defn- mk-posix-file-permissions [posix-string]
(try
(when posix-string
(if (boot.App/isWindows)
(util/warn "Filemode not supported on Windows\n")
(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
11 changes: 7 additions & 4 deletions boot/pod/src/boot/file.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
(def ^:dynamic *sync-delete* true)
(def ^:dynamic *hard-link* true)

(def windows? (boot.App/isWindows))

(def tmpfile-permissions
(into-array FileAttribute
[(PosixFilePermissions/asFileAttribute
(PosixFilePermissions/fromString "rw-------"))]))
(if windows?
[]
[(PosixFilePermissions/asFileAttribute
(PosixFilePermissions/fromString "rw-------"))])))

(defn file? [f] (when (try (.isFile (io/file f)) (catch Throwable _)) f))
(defn dir? [f] (when (try (.isDirectory (io/file f)) (catch Throwable _)) f))
Expand Down Expand Up @@ -247,8 +251,7 @@

(defn match-filter?
[filters f]
(let [windows? (boot.App/isWindows)
normalize #(if-not windows? % (str/replace % #"\\" "/"))]
(let [normalize #(if-not windows? % (str/replace % #"\\" "/"))]
((apply some-fn (map (partial partial re-find) filters)) (normalize (.getPath ^File f)))))

(defn keep-filters?
Expand Down
14 changes: 9 additions & 5 deletions boot/pod/src/boot/filesystem.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
(def read-only
(PosixFilePermissions/fromString "r--r--r--"))

(def windows? (boot.App/isWindows))

(defprotocol IToPath
(->path [x] "Returns a java.nio.file.Path for x."))

Expand Down Expand Up @@ -155,20 +157,22 @@
(Files/setLastModifiedTime dst (FileTime/fromMillis time))))

(defn copy!
[^Path dest path ^Path src time]
[^Path dest path ^Path src time & {:keys [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 (and mode (not windows?)) (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 & {:keys [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 (and mode (not windows?)) (Files/setPosixFilePermissions dst mode))
(catch java.nio.file.NoSuchFileException ex
(util/dbug* "Filesystem: %s\n" (str ex))))))

Expand All @@ -188,11 +192,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 mode)
:link (link! dest path arg1 :mode mode)
:touch (touch! dest path arg1)))))

0 comments on commit b89738e

Please sign in to comment.