├── .clj-kondo └── babashka │ └── fs │ └── config.edn ├── deps.edn ├── .gitignore ├── src └── spootnik │ └── deps_proto.clj └── README.md /.clj-kondo/babashka/fs/config.edn: -------------------------------------------------------------------------------- 1 | {:lint-as {babashka.fs/with-temp-dir clojure.core/let}} 2 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src"] 2 | :deps {org.clojure/clojure {:mvn/version "1.11.1"} 3 | io.github.clojure/tools.build {:git/tag "v0.8.2" :git/sha "ba1a2bf"} 4 | com.google.protobuf/protobuf-java {:mvn/version "3.20.1"} 5 | babashka/fs {:mvn/version "0.1.6"}} 6 | :tools/usage {:ns-default spootnik.deps-proto}} 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .calva/output-window/ 2 | .classpath 3 | .clj-kondo/.cache 4 | .cpcache 5 | .eastwood 6 | .factorypath 7 | .hg/ 8 | .hgignore 9 | .java-version 10 | .lein-* 11 | .lsp/.cache 12 | .lsp/sqlite.db 13 | .nrepl-history 14 | .nrepl-port 15 | .project 16 | .rebel_readline_history 17 | .settings 18 | .socket-repl-port 19 | .sw* 20 | .vscode 21 | *.class 22 | *.jar 23 | *.swp 24 | *~ 25 | /checkouts 26 | /classes 27 | /target 28 | **/resources/git-version 29 | .clj-kondo/.cache 30 | -------------------------------------------------------------------------------- /src/spootnik/deps_proto.clj: -------------------------------------------------------------------------------- 1 | (ns spootnik.deps-proto 2 | (:require [clojure.tools.build.api :as b] 3 | [babashka.fs :as fs])) 4 | 5 | (def default-source-path 6 | "proto") 7 | 8 | (def default-classes-path 9 | (str (fs/path "target" "classes"))) 10 | 11 | (def default-generate-path 12 | (str (fs/path "target" "java"))) 13 | 14 | (defn compile-java 15 | [{:keys [generate-path classes-path] 16 | :or {generate-path default-generate-path 17 | classes-path default-classes-path} 18 | :as opts}] 19 | (b/javac {:src-dirs [generate-path] 20 | :class-dir classes-path 21 | :basis (b/create-basis {:project "deps.edn"})}) 22 | opts) 23 | 24 | (defn generate 25 | [{:keys [source-path generate-path proto-files protoc-bin] 26 | :or {source-path default-source-path 27 | generate-path default-generate-path 28 | protoc-bin "protoc"} 29 | :as opts}] 30 | (fs/create-dirs source-path) 31 | (fs/create-dirs generate-path) 32 | (let [proto-files (or proto-files (for [f (fs/glob source-path "**.proto")] 33 | (str (fs/relativize source-path f)))) 34 | cmdline (concat [protoc-bin 35 | (str "-I" (or source-path default-source-path)) 36 | (str "--java_out=" (or generate-path "target/java"))] 37 | proto-files) 38 | res (b/process {:command-args cmdline})] 39 | (when-not (zero? (:exit res)) 40 | (binding [*out* *err*] 41 | (println "failed to generate protobuf code") 42 | (throw (ex-info "failed to generate protobuf code" res)))))) 43 | 44 | (def generate-and-compile 45 | (comp compile-java generate)) 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # deps-proto: compiling protobuf for the deps toolchain 2 | 3 | This deps tool expose three functions to generate and compile protobuf 4 | source files. 5 | 6 | ## Prerequisites 7 | 8 | This tool assumes you have a `protoc` binary somewhere on the system. 9 | 10 | ## Installation 11 | 12 | The tool can be used as a global clojure tool: 13 | 14 | ``` shell 15 | clj -Ttools install io.github.pyr/deps-proto '{:git/sha "…"}' :as proto 16 | ``` 17 | 18 | Or can be defined in your `deps.edn` as an alias: 19 | 20 | ``` clojure 21 | {:aliases 22 | {:proto 23 | {:deps {io.github.pyr/deps-proto {:git/sha "…"}} 24 | :ns-default spootnik.deps-proto}}} 25 | ``` 26 | 27 | ## Functions 28 | 29 | ### `generate` 30 | 31 | Generates java code from protobuf files. 32 | 33 | ``` shell 34 | clj -Tproto generate 35 | ``` 36 | 37 | Supported options: 38 | 39 | - `:source-path`: where protobuf files are stored, defaults to `"proto"` 40 | - `:generate-path`: where to store generated java files, defaults to `"target/java"` 41 | - `:protoc-bin`: path to the protoc binary, defaults to `"protoc"` 42 | - `:proto-files`: A collection of protobuf files to generate code from. Defaults to 43 | all protobuf files found in the source path. 44 | 45 | ### `compile-java` 46 | 47 | Compiles the generated source 48 | 49 | ``` shell 50 | clj -Tproto compile-java 51 | ``` 52 | Supported options: 53 | 54 | - `:classes-path`: where to store compiled java classes, defaults to `"target/classes"` 55 | - `:generate-path`: where to store generated java files, defaults to `"target/java"` 56 | 57 | ### `generate-and-compile` 58 | 59 | Generates java code from protobuf files and compiles the generated source. 60 | 61 | ``` shell 62 | clj -Tproto generate-and-compile 63 | ``` 64 | Supported options: 65 | 66 | - `:classes-path`: where to store compiled java classes, defaults to `"target/classes"` 67 | - `:source-path`: where protobuf files are stored, defaults to `"proto"` 68 | - `:generate-path`: where to store generated java files, defaults to `"target/java"` 69 | - `:protoc-bin`: path to the protoc binary, defaults to `"protoc"` 70 | - `:proto-files`: A collection of protobuf files to generate code from. Defaults to 71 | all protobuf files found in the source path. 72 | --------------------------------------------------------------------------------