Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subprocess error handling #9

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:url "http://www.eclipse.org/legal/epl-v10.html"}
:url "https://github.com/flatland/protobuf"
:dependencies [[fs "1.2.0"]
[conch "0.2.0"]
[me.raynes/conch "0.6.0"]
[leinjacker "0.4.0"]]
:eval-in-leiningen true
;; Bug in the current 1.x branch of Leiningen causes
Expand Down
23 changes: 15 additions & 8 deletions src/leiningen/protobuf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(:require [clojure.java.io :as io]
[fs.core :as fs]
[fs.compression :as fs-zip]
[conch.core :as sh]))
[me.raynes.conch :refer [let-programs]]))

(def cache (io/file (leiningen-home) "cache" "lein-protobuf"))
(def default-version "2.5.0")
Expand Down Expand Up @@ -37,6 +37,14 @@
(doto (io/file (:target-path project))
.mkdirs))

(defn execute [cmd args]
(let-programs [command cmd]
(let [err (java.io.StringWriter.)
args (conj args {:out *out* :err err :verbose true})
result ((apply command args) :exit-code)]
(when-not (= @result 0)
(abort "ERROR:" (str err))))))

(defn extract-dependencies
"Extract all files proto depends on into dest."
[project proto-path protos dest]
Expand Down Expand Up @@ -102,9 +110,9 @@
(fs/chmod "+x" (io/file srcdir "configure"))
(fs/chmod "+x" (io/file srcdir "install-sh"))
(println "Configuring protoc")
(sh/stream-to-out (sh/proc "./configure" :dir srcdir) :out)
(execute "./configure" [:dir srcdir])
(println "Running 'make'")
(sh/stream-to-out (sh/proc "make" :dir srcdir) :out))))
(execute "make" [:dir srcdir]))))

(defn compile-protobuf
"Create .java and .class files from the provided .proto files."
Expand All @@ -121,14 +129,13 @@
(.mkdirs dest)
(extract-dependencies project proto-path protos proto-dest)
(doseq [proto protos]
(let [args (into [(.getPath (protoc project)) proto
(str "--java_out=" (.getAbsoluteFile dest)) "-I."]
(let [protoc-path (.getPath (protoc project))
args (into [proto (str "--java_out=" (.getAbsoluteFile dest))
"-I."]
(map #(str "-I" (.getAbsoluteFile %))
[proto-dest proto-path]))]
(println " > " (join " " args))
(let [result (apply sh/proc (concat args [:dir proto-path]))]
(when-not (= (sh/exit-code result) 0)
(abort "ERROR:" (sh/stream-to-string result :err))))))
(execute protoc-path (into args [:dir proto-path]))))
(javac (assoc project
:java-source-paths [(.getPath dest)]
:javac-options ["-Xlint:none"])))))))
Expand Down