Fork me on GitHub
#tools-deps
<
2021-02-12
>
vemv10:02:21

how to run a random .clj file's -main that has no 3rd party dependencies or a deps.edn file? I tried clojure -M some_folder/the_file.clj, it loads the file but it doesn't run its -main

practicalli-john10:02:23

Edit: sorry I read the question incorrectly. If you do add a deps.edn, then you can do this.. The -M flag specifies that the clojure command should call Clojure main in the project (as opposed to the -X flag that calls a qualified function) -m is required to state which namespace to run -main, the namespace that includes (defn -main ,,,) So from your example it would be clojure -M -m some-folder.the-file

vemv10:02:07

looks like in that case I'd have to add some-folder to the classpath. How to do it in an inline fashion?

practicalli-john10:02:47

I was assuming that some_folder is under a src folder and you have a {:path ["src"]} in the deps.edn file Edit: can the -i flag be used for this, it loads a resource?

practicalli-john10:02:27

I believe you can use . Instead of src if there is no directory structure, but t that still requires a deps.edn file.

borkdude10:02:34

@U45T93RA6 this is a similar problem in babashka world. we invented this pattern for it: https://book.babashka.org/#main_file inspired by the Python way of doing things

👍 1
🙂 1
borkdude10:02:24

So you can check using that condition if you are directly invoking this file or if you are referring to that file using the classpath

borkdude10:02:04

But for clj this doesn't work. Some people also solve this using an env var in babashka: REPL=true -> don't invoke main at the bottom

vemv12:02:15

clojure -M -i ./some-folder/the_file.clj -m the-file worked. Bit of an intrincate construction though :) Thank you both!

practicalli-john16:02:09

Thanks for sharing the solution that worked for you.

borkdude17:02:10

What's the point of having a -main btw if you are invoking this as a standalone file?

vemv17:02:26

we are all a curious bunch :) This was a experimental CI-oriented script belonging to a big repo. Didn't seem to deserve its own deps.edn file. Indeed I could have unwrapped the main but there's some remote possibility of the top-level side-effect being executed by tooling (e.g. Eastwood, refactor-nrepl or t.n (refresh))

mbjarland14:02:54

is there any way of using tools.deps code to retrieve the available list of versions for an artifact? This normally comes from the maven-metadata.xml file

agilecreativity00:02:21

I build something like this once back in 2017. It allow you to fetch the version from maven https://github.com/agilecreativity/pom-versions @U4VDXB2TU

alexmiller14:02:15

nothing in the tools.deps api, no

alexmiller14:02:43

you could use the lower parts of the tools.deps api as an access point into maven apis to answer that question though

alexmiller15:02:37

you basically want to use the Maven RepositorySystem API to make VersionRequests or VersionRangeRequests etc

mbjarland15:02:19

@alexmiller ok thanks, that's the route I was heading, just wanted to make sure I wasn't reinventing the wheel