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

Slim/genclass #39

Merged
merged 9 commits into from
Jun 4, 2023
Merged
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 deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
babashka/fs {:mvn/version "0.1.6"}
org.clj-commons/byte-streams {:mvn/version "0.3.0"}
version-clj/version-clj {:mvn/version "2.0.2"}
borkdude/rewrite-edn {:mvn/version "0.2.0"}
borkdude/rewrite-edn {:mvn/version "0.4.6"}
io.github.clojure/tools.build {:git/tag "v0.8.2"
:git/sha "ba1a2bf421838802e7bdefc541b41f57582e53b6"}}

Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
pkgs.bats
pkgs.envsubst
pkgs.mustache-go
pkgs.diffutils
];
commands = [
{
Expand Down
8 changes: 4 additions & 4 deletions pkgs/builder-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"hash": "sha256-10dINOFvu1Kk2bexqmBSyk0BucZx0f9BWYIxwaF1Vr4="
},
{
"url": "https://repo.clojars.org/borkdude/rewrite-edn/0.2.0/rewrite-edn-0.2.0.jar",
"hash": "sha256-xEsgLvDRpJdCTfWh5jRpLAw0KWcpsl9IMNChK29uR7g="
"url": "https://repo.clojars.org/borkdude/rewrite-edn/0.4.6/rewrite-edn-0.4.6.jar",
"hash": "sha256-R2wBPIrfn5G83zPbcejQw3EKF8WKKQR5rLsp7YyPyqM="
},
{
"url": "https://repo.clojars.org/manifold/manifold/0.2.3/manifold-0.2.3.jar",
Expand All @@ -29,8 +29,8 @@
"hash": "sha256-jf43Wpf+4h86ri7BjxNoQVQNzyOUAIzUv6hTA39pXK4="
},
{
"url": "https://repo.clojars.org/rewrite-clj/rewrite-clj/1.0.572-alpha/rewrite-clj-1.0.572-alpha.jar",
"hash": "sha256-/ktU4zGJG8IsU37VINOM6PJgLwRq/m8CkJPX91EI9pY="
"url": "https://repo.clojars.org/rewrite-clj/rewrite-clj/1.1.45/rewrite-clj-1.1.45.jar",
"hash": "sha256-RdiztNjxqnlGyHx2VjCu6GWLNjwAjZ+ZzB5YVp68yOA="
},
{
"url": "https://repo.clojars.org/riddley/riddley/0.1.15/riddley-0.1.15.jar",
Expand Down
1 change: 1 addition & 0 deletions pkgs/mkCljBin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ stdenv.mkDerivation ({
''
else
''
${clj-builder} --check-main "${fullId}" "${version}" "${main-ns}"
${buildCommand}
''
)
Expand Down
42 changes: 42 additions & 0 deletions src/cljnix/check.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(ns cljnix.check
(:require
[cljnix.build :as build]
[clojure.java.io :as io]
[clojure.string :as string]
[rewrite-clj.zip :as z]))

(defn- clj-file [main-ns]
(str
(->>
(string/split main-ns #"\.")
(map #(string/replace % "-" "_"))
(string/join "/")) ".clj"))

(defn- gen-class-node? [node]
(and
(= :list (:tag node))
(some #(= :gen-class (:k %)) (:children node))))

(defn check-src-dirs
"search for a main-ns clojure file with a (:gen-class) list in the ns form"
[src-dirs main-ns]
(let [main-clj-file (clj-file main-ns)]
(when-let [f (->> src-dirs
(map #(io/file % main-clj-file))
(some #(when (.exists %) %)))]
(let [zloc (z/down (z/of-string (slurp f)))]
;; zloc will be pointing at the first child of the first non-whitespace non-comment expression in the file
(and
(= 'ns (-> zloc z/node :value))
(->> (iterate z/right zloc)
(take-while #(not (nil? %)))
(some (comp gen-class-node? z/node))))))))

(defn main-gen-class
"assumes the namespace is the first sexpr in the main clj file"
[{:keys [main-ns] :as opts}]
(println "check" main-ns "for :gen-class")
(let [{:keys [src-dirs]}
(build/common-compile-options opts)]
(check-src-dirs src-dirs main-ns)))

30 changes: 25 additions & 5 deletions src/cljnix/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[cljnix.utils :refer [throw+] :as utils]
[cljnix.nix :refer [nix-hash]]
[cljnix.build :as build]
[cljnix.check :as check]
[clojure.tools.deps.alpha.util.dir :as tools-deps.dir]
[clojure.tools.deps.alpha.util.io :refer [printerrln]]))

Expand All @@ -35,8 +36,13 @@

(defn- artifact->pom
[path]
(str (first (fs/glob (fs/parent path) "*.pom"))))

(str (->> (fs/glob (fs/parent path) "*.pom")
(sort (fn [x y]
(cond
(string/includes? (fs/file-name x) "SNAPSHOT") -1
(string/includes? (fs/file-name y) "SNAPSHOT") 1
:else (compare (fs/file-name x) (fs/file-name y)))))
first)))

(defn maven-deps
[basis]
Expand Down Expand Up @@ -376,6 +382,15 @@
:git extra-git}
(fs/glob project-dir "**deps.edn")))))

(defn- check-main-class
[& [_ value & more :as args]]
(or
(check/main-gen-class
(interleave
[:lib-name :version :main-ns]
(apply vector value more)))
(throw (ex-info "main-ns class does not specify :gen-class" {:args args}))))

(defn -main
[& [flag value & more :as args]]
(cond
Expand All @@ -389,10 +404,15 @@
(apply vector value more)))

(= flag "--uber")
(build/uber
(interleave
(do
(apply check-main-class args)
(build/uber
(interleave
[:lib-name :version :main-ns :java-opts]
(apply vector value more)))
(apply vector value more))))

(= flag "--check-main")
(apply check-main-class args)

:else
(let [deps-ignore (remove #(= "--lein" %) args)]
Expand Down
10 changes: 10 additions & 0 deletions test/cljnix/check_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns cljnix.check-test
(:require [clojure.test :as t]
[cljnix.check :refer [check-src-dirs]]))

(t/deftest gen-class-check-tests
(t/is (not (check-src-dirs ["test/resources"] "example.no-gen-class")))
(t/is (check-src-dirs ["test/resources"] "example.has-gen-class"))
(t/is (check-src-dirs ["test/resources"] "example.has-gen-class-with-comment"))
(t/is (check-src-dirs ["test/resources"] "example.has-gen-class-with-odd-spacing"))
)
2 changes: 2 additions & 0 deletions test/resources/example/has_gen_class.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns resources.example.has-gen-class
(:gen-class))
3 changes: 3 additions & 0 deletions test/resources/example/has_gen_class_with_comment.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
;; first content is a comment
(ns resources.example.has-gen-class-with-comment
(:gen-class))
2 changes: 2 additions & 0 deletions test/resources/example/has_gen_class_with_odd_spacing.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(ns resources.example.has-gen-class-with-odd-spacing
( :gen-class))
1 change: 1 addition & 0 deletions test/resources/example/no_gen_class.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(ns resources.example.no-gen-class)