Fork me on GitHub
#leiningen
<
2022-07-25
>
apt14:07:38

Hi folks. I’d like to get the value of some alias at runtime.

(apply hash-map (drop 3 (let [project-path "project.clj"]
                                  (when (.exists (io/as-file project-path))
                                    (read-string (slurp project-path))))))
I’m using this. The whole file is list: it’s first element is defproject, the second is the name of the project, and so on. I’m wondering if this drop 3 is reliable or if there’s a better way of doing that.

vemv15:07:39

I'd suggest instead sth like

(defproject p "0"
  :middleware [~(do
                  (defn foo [project]
                    (-> project
                        (update :jvm-opts conj (str "-Dlein-project=" (pr-str project)))
                        (update :jvm-opts vec)))
                  `foo)])
Then you can (read-string (System/getProperty "lein-project")) in your project repl

apt17:07:14

Interesting! The thing is that the tool will be injected on many projects as they are, with no modifications. So unfortunately I can’t use this custom logic. In any case, I didn’t know that was possible, thanks.

vemv06:07:07

You can extract the same middleware to a Lein plugin

apt13:07:18

And inject the plugin with lein update-in, right? Yeah, sounds feasible.

vemv13:07:02

You can simply add the plugin to ~/.lein/profiles.clj

apt13:07:27

I think I forgot to explain the full context here. The code I proposed would reside in a library that is injected in many Clojure services in a pipeline. It extracts information from tests, so whenever one commits something to a service, that lib runs in the service in a pipeline (with lein update-in, which appends the lib as a dependency). That’s why I have little control over the service code.

vemv13:07:51

Yeah update-in can inject plugins :) Writing to the :user profile in ~/.lein/profiles.clj should work in CI as well

apt13:07:47

Ah, okay, makes sense :thumbsup:

apt13:07:07

Thanks for the suggestion gratitude

🍻 1