Compare commits

...

2 Commits

Author SHA1 Message Date
017291f784 Full modularized project 2025-10-13 01:22:03 +02:00
17a7a09ab0 Fully modularized project 2025-10-13 01:21:36 +02:00
8 changed files with 157 additions and 80 deletions

2
.gitignore vendored
View File

@@ -1,5 +1,5 @@
/.clj-kondo/
/.cpcache/
/**/.cpcache/
/.lsp/
/target/
.nrepl-port

View File

@@ -152,6 +152,7 @@ clj-totp.sh import <alias> "<url>"
- [x] Native compilation script corrections
### v2
- [x] Restructurate as a multiproject
- [ ] REST API
- [ ] User management
- [ ] Robust BD backend (H2, datomic, or similar)
@@ -176,14 +177,14 @@ The first step is to install Java JDK, version 11 or newer (version 21 recommend
To execute manually the main function, simple use the `:run` alias:
```clojure
clojure -M:run <commands and parameters>
```bash
clojure -M:run/cli <commands and parameters>
```
To build the uberjar:
```clojure
clojure -T:build uber
```bash
clojure -T:build :uber/cli
```
There is a utility script to build a native executable using Graal VM. Please, edit the script and

121
build.clj
View File

@@ -1,62 +1,91 @@
(ns build
(:refer-clojure :exclude [test])
(:require [clojure.tools.build.api :as b]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[clojure.pprint :as pp]
[clojure.java.basis :as basis]))
(def lib-group "es.rcorral")
(def artifact-prefix "clj-totp")
(def subprojs-base "projects")
(def curr-version (format "2.0.%s" (b/git-count-revs nil)))
;; Builds artifact's full descriptor for each subproject
(defn lib [subproj]
(symbol (str lib-group "/" artifact-prefix "-" subproj )))
;; Basis for each subproject, using their own deps.edn
;; Injects :extra-deps from :build as additional dependencies
(defn basis [subproj]
;(b/create-basis {:project (str subproj "/deps.edn")}))
(delay (b/create-basis {:project (str "projects/" subproj "/deps.edn")})))
(delay (b/create-basis {:project (str subprojs-base "/" subproj "/deps.edn")
;; Inject extra deps as deps
:extra {:deps (get-in (basis/initial-basis) [:aliases :build :extra-deps])}
})))
;; Show basis generated for a subproject
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
(defn show-basis [subproj]
(println (with-out-str
(pp/pprint
@(basis subproj)
;(basis/initial-basis)
))))
(comment
(pp/pprint (keys (basis/initial-basis)))
(pp/pprint (:deps (basis/initial-basis)))
(pp/pprint (:libs (basis/initial-basis)))
(pp/pprint (sort (keys (:aliases (basis/initial-basis)))))
(get-in (basis/initial-basis) [:aliases :build :extra-deps])
)
;; Target dir for each subproject
(defn target-dir [subproj]
(str "target/" subproj))
;; Path for compiled classes
(defn class-dir [subproj]
(str (target-dir subproj) "/" "classes"))
;; Jar file for each subproject. :uber type adds -standalone suffix
(defn jar-file [subproj version type]
(format "target/%s-%s-%s%s.jar" artifact-prefix subproj version
(if (= type :uber) "-standalone" "")))
;; Clean target dir for subproject
(defn clean [{:keys [subproj]}]
(b/delete {:path (target-dir subproj)})
(println "Project" subproj "cleaned"))
;; Compile java classes, only if java subdir exists
(defn compile-java [subproj]
(let [java-dir (str "projects/" subproj "/java")]
(if (.exists (io/file java-dir))
(do
(println "Compiling java code for" subproj)
(b/javac {:src-dirs [java-dir]
:class-dir (class-dir subproj)
:basis @(basis subproj)
:javac-opts ["-source" "11" "--target" "11" "-proc:none"]}))
(println "No java code for" subproj ", skipping compilation"))))
(let [java-dir (str subprojs-base "/" subproj "/java")]
(when (.exists (io/file java-dir))
(println "Compiling java code for" subproj)
(b/javac {:src-dirs [java-dir]
:class-dir (class-dir subproj)
:basis @(basis subproj)
:javac-opts ["-source" "11" "--target" "11" "-proc:none"]}))))
;; Create a jar file
(defn jar
"Build a simple jar file, with no dependencies included."
[{:keys [subproj version]}]
(let [real-version (if version version curr-version)
target-dir (target-dir subproj)
[{:keys [subproj version]
:or {version curr-version}}]
(let [target-dir (target-dir subproj)
class-dir (class-dir subproj)
src-dir (str "projects/" subproj "/src")
resources-dir (str "projects/" subproj "/resources")
src-dir (str subprojs-base "/" subproj "/src")
resources-dir (str subprojs-base "/" subproj "/resources")
basis (basis subproj)
jar-file (jar-file subproj real-version :plain)]
jar-file (jar-file subproj version :plain)]
;; Clean only class dir
(b/delete {:path class-dir})
;; Copy code
@@ -72,19 +101,23 @@
:basis @basis
:jar-file jar-file
:lib (lib subproj)
:version real-version})
:version version})
(println "Generated jar file:" jar-file)))
;; Create an uber jar, with all dependencies inside
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
(defn uber
"Build a uberjar with all dependencies included"
[{:keys [subproj version main-ns]}]
(let [real-version (if version version curr-version)target-dir (target-dir subproj)
[{:keys [subproj version main-ns]
:or {version curr-version}}]
(let [target-dir (target-dir subproj)
basis (basis subproj)
class-dir (class-dir subproj)
src-dir (str "projects/" subproj "/src")
resources-dir (str "projects/" subproj "/resources")
uber-file (jar-file subproj real-version :uber)]
src-dir (str subprojs-base "/" subproj "/src")
resources-dir (str subprojs-base "/" subproj "/resources")
uber-file (jar-file subproj version :uber)]
;(println "Using basis: ")(show-basis subproj)
(b/delete {:path class-dir})
(b/copy-dir {:src-dirs [src-dir]
:target-dir class-dir})
@@ -99,3 +132,43 @@
:main main-ns})
(println "Generated uberjar executable:" uber-file)))
;; Multimethod to get the name of all subdirs in a dir.
;; Accepts strings or files
(defmulti get-subdirs type)
(defmethod get-subdirs
java.lang.String [dir]
(get-subdirs (io/file dir)))
(defmethod get-subdirs
java.io.File [dir]
(if (.isDirectory dir)
(filter #(.isDirectory %) (.listFiles dir))
(println "Directory" subprojs-base "doesn't exists!")))
;; Get the name of all subdir in a given directory
(defn get-subdir-names
"Get a list projects in the 'projects' directory"
[dir-name]
(map #(.getName %) (get-subdirs dir-name)))
(comment
(get-subdirs "projects")
(get-subdirs (io/file "projects"))
(get-subdir-names "projects")
)
;; Generate jar files for all projects
(defn jar-all
"Build jar files for all projects"
[& {:keys [version]
:or {version curr-version}}]
(dorun (map #(jar {:subproj % :version version}) (get-subdir-names subprojs-base))))
(comment
(jar-all )
)

View File

@@ -1,58 +1,60 @@
{:paths ["src" "resources" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
;; Native image (GraalVM)
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}}
;; Native image (GraalVM). Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
com.github.clj-easy/graal-build-time {:mvn/version "1.0.5"}
;; Local subprojects
clj-totp/core {:local/root "projects/core"}
clj-totp/cli {:local/root "projects/cli"}
clj-totp/web {:local/root "projects/web"}
}
:aliases {;; Execute the app. Tutorial: https://shagunagrawal.me/posts/setup-clojure-with-graalvm-for-native-image/
:run {:main-opts ["-m" "totp.app"]}
:aliases {;; Execute the app.
:run {:main-opts ["-m" "totp.app"]}
;:run {:exec-fn totp.app/-main}
;; Execute the app (prepared for more subprojects)
:run/cli {:main-opts ["-m" "totp.app"]}
;:run {:exec-fn totp.app/-main}
;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
;; Check test.edn for kaocha runner's config
:test {:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}
lambdaisland/kaocha-cloverage {:mvn/version "1.1.89"}}
:main-opts ["-m" "kaocha.runner"]}
;; Run with clj -T:build function-in-build
:build {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
;; Used by all compilations
:extra-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build}
;; COMMON ALIASES FOR ALL PROJECTS
:root/run-x {:exec-fn -main}
:root/extra-paths [:totp.core/extra-paths
:totp.cli/extra-paths
:totp.web/extra-paths]
:root/all {:extra-paths ["src" "resources"
:root/extra-paths]}
:root/test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.91.1392"}}
:main-opts ["-m" "kaocha.runner"]}
:boot/build {:extra-paths ["build"]
:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
;; Aliases for easy building
:build/core {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
;:exec-fn ci
;:exec-args {:app-alias :com.example.core}
}
:exec-fn jar
:exec-args {:subproj "core"}}
:totp.core/extra-paths ["projects/core/src"
"projects/core/test"
"projects/core/java"
"projects/core/resources"]
:totp.core {:ns-default totp.core
:main-opts ["-m" "totp.core"]
:extra-deps {projects/core {:local/root "projects/core"}}
:exec-args {:dirs ["projects/core"]}}
:build/cli {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}
clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "cli"}}
:build/web {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:replace-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar
:exec-args {:subproj "web"}}
:totp.cli/extra-paths ["projects/app/src"]
:totp.cli {:ns-default totp.cli
:main-opts ["-m" "totp.cli"]
:extra-deps {;; does not use parts/grugstack {:local/root "parts"}
projects/cli {:local/root "projects/cli"}}
:exec-args {:dirs ["projects/cli"]}}
:totp.web/extra-paths ["projects/web/src"]
}}
;; Build the three libraries
:build/all {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:replace-deps {clj-totp/core {:local/root "projects/core"}}
:ns-default build
:exec-fn jar-all}
;; Build uber jar for CLI app
:uber/cli {:deps {io.github.clojure/tools.build {:mvn/version "0.10.10"}}
:ns-default build
:exec-fn uber
:exec-args {:subproj "cli" :main-ns "totp.app"}}}}

View File

@@ -1,13 +1,12 @@
{:paths ["src" "resources" "target/classes"]
:deps {clj-totp/core {:local/root "projects/core"}
:deps {;clj-totp/core {:local/root "../core"}
org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; Progress bar
com.github.pmonks/spinner {:mvn/version "2.0.284"}}
:aliases {;; Execute the app
:run {:main-opts ["-m" "totp.app"]}
;:run {:main-opts ["-m" "totp.app"]}
;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
:test {:extra-paths ["test"]

View File

@@ -1,8 +1,6 @@
{:paths ["src" "resources" "target/classes"]
:deps {org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
mvxcvi/alphabase {:mvn/version "3.0.185"} ;; https://github.com/greglook/alphabase
cli-matic/cli-matic {:mvn/version "0.5.4"} ;; https://github.com/l3nz/cli-matic
;; Protobuf for java
com.google.protobuf/protobuf-java {:mvn/version "3.25.8"}
}

View File

@@ -1,7 +1,6 @@
{:paths ["src" "resources" "target/classes"]
:deps {clj-totp/core {:local/root "projects/core"}
:deps {;clj-totp/core {:local/root "../core"}
org.clojure/clojure {:mvn/version "1.12.1"}
io.github.clojure/tools.build {:mvn/version "0.10.10"}
;; For SQLite
com.github.seancorfield/next.jdbc {:mvn/version "1.3.1048"}
org.xerial/sqlite-jdbc {:mvn/version "3.50.3.0"}
@@ -10,7 +9,7 @@
}
:aliases {;; Execute the app
:run {:main-opts ["-m" "totp.app"]}
;:run {:main-opts ["-m" "totp.app"]}
;; Kaocha runner. You can use the 'kaocha' wrapper located in ~/bin/kaocha
:test {:extra-paths ["test"]

View File

@@ -1 +1,6 @@
#kaocha/v1 {}
#kaocha/v1
{:tests [{:test-paths ["projects/core/src" "projects/core/test"]}]
:plugins [:kaocha.plugin/cloverage]
:cloverage/opts {:src-ns-path ["projects/core/src" "projects/core/test"]
:ns-regex ["totp\\..*(?<!test)$"] ;; All starting with "totp" but not ending by "test"
}}