Lein repl resets namespace after loading Clojure script - clojure

Strange problem, I know. So lein can be instructed to load a specific namespace when running the REPL with lein repl. That's great, let's assume I have a file called ns1.clj, so my project.clj file contains the line:
:repl-options {:init-ns ns1}
And, as expected, that file is loaded. However, I want to switch to another namespace (ns2) after ns1.clj does it's job, so I append the following to ns1.clj:
(ns ns2)
The problem is that Leiningen resets the REPL namespace to ns1 after the ns1.clj has finished. Is there any way to start the REPL by loading ns1.clj but not resetting the namespace post-load? By the way, I would assume that Leiningen should just execute the script and not set the namespace explicitly.
Background: I want to load a clj script and then switch to a namespace that has been loaded from an external source by that very script. So the logic in ns1.clj figures out what namespace should the REPL start in.

Based on your goal of wanting to extend repl functionality, I think you might do better to look at creating a lien plugin. You can then use this plugin without needing to modify your code and through your lein profiles, only have it installed when needed.
You might get some good ideas/pointers by looking at a number of existing lein plugins. In particular, there was quite an interesting blog post on planet clojure talking about a plugin called 'ultra' which extends the repl by adding ansi colours, improved test output formatting, enhanced stacktrace display and a few other repl tweaks. The project can be found on githug at https://www.github.com/venantius/ultra

Related

Alternative to using the REPL in Clojure for quickly trying out stuff

In Python world, whenever I need to try something I'd just make a new file a.py and insert the code that I want to try, and run it. This works because of the shebang line
#!/usr/bin/env python3
Which tells the os which interpreter to call for the file.
Is there an equivalent to this in clojure? I don't want to jump through all the hoops of running lein new app and specifying the main ns, everytime I just want to check out what a few lines of code does in a file.
Note that I already know about lein repl. And I tried to use that as the shebang
#!/usr/bin/lein repl
But this just brings up an error
No :main namespace specified in
project.clj.
If you have Clojure CLI tools installed, then you can use shebang scripts:
test.clj:
#!/usr/bin/env clj
(def x 10)
(println "x =" x)
In terminal:
chmod +x test.clj
./test.clj
Output:
x = 10
You can do this with boot. See https://github.com/boot-clj/boot/wiki/Scripts
Also, the ClojureVerse has a thread on this at https://clojureverse.org/t/scripting-with-clj/1618/5 where some options are being discussed.
Still, you should be aware that such an approach is probably not very useful. On the one hand you quite often want to manage dependencies on the other there's the startup time of the JVM, in particular when you need to compile some clojure sources first. If you need to run your script a few times because you're trying out thing, the startup time quickly becomes are real obstacle.
As an idea, I usually keep a leiningen project around for quick experiments. Here, I can adjust the dependencies as needed and quickly fire up a REPL to tinker with ideas and try out things. For me, it is not uncommon to find this REPL up and running and an Emacs already connected to it.

Is there a good ClojureScript workflow?

I'm trying to use ClojureScript for a project. To be a happy developer, I need a quick feedback-loop: write some code and see it as soon as it's possible. I'd also love to use REPL in the context of the current page.
I'm using lein-cljsbuild and from the issues I'm having I think I failed to set it up properly.
First of all, compilation time for a small file is too big, even after "JWM warm-up" with :optimizations :simple:
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 8.233018 seconds.
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 5.522989 seconds.
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Successfully compiled "resources/public/js/cljs.js" in 8.144354 seconds.
Second, from time to time I get stuff like this:
Compiling "resources/public/js/cljs.js" from ["src-cljs"]...
Compiling "resources/public/js/cljs.js" failed.
Exception in thread "main" java.lang.NullPointerException
at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
at java.util.regex.Matcher.reset(Matcher.java:291)
at java.util.regex.Matcher.<init>(Matcher.java:211)
at java.util.regex.Pattern.matcher(Pattern.java:888)
at clj_stacktrace.utils$re_gsub.invoke(utils.clj:6)
# ... many similar useless lines
at clojure.lang.Var.applyTo(Var.java:532)
at clojure.main.main(main.java:37)
These stacktraces (most of the time related to syntax errors, as I understood from experimenting), are worse than not helpful: not only they provide zero clues about the error, they also stop automatic recompilation. After every such stacktrace I should do these things:
Figure out what's the problem, using a technique I call "pretend you're a parser". Thank God it's Lisp and the only thing I should do is to carefully count matching parentheses.
Press Cmd-C in the shell where the lein cljsbuild auto runs
Run lein cljsbuild auto again
Make some useless change, like inserting a newline, in the file, to trigger a new build
Wait for ~30 seconds until the file is compiled again, since the JVM has to "warm-up" again
In many cases at this point I get another stacktrace, after getting which I have to lather, rinse and repeat all the damned procedure
Third thing which I failed to understand is REPL. From the interwebs I've learned how to connect rlwrap lein trampoline cljsbuild repl-listen to the current page. However, every reload, navigation, and syntax error (god forbid you forget to use rlwrap and press an arrow key) in the console makes the REPL completely stuck. I have to restart it (~10 seconds) and reload the page after that, then start again trying stuff out in a friendly environment.
I think, probably someone has figured it all out and has a workflow that works. Please help me set it all up from the start, because I feel like a failed software engineer.
For me Catnip provides the best dev experience for ClojureScript at the moment.
Check out figwheel! It's like livereload on steroids.
Working with clojurescript and front-end development was never so much fun!
See for yourself (the demo is a bit outdated – it's even better now!)
It not just reloads the page when files are changed – it provides you interactive programming Bret Victor was talking about (I recommend you to watch that talk if you haven't)
I've tried inside Emacs this tool https://github.com/cemerick/austin and works very well as you can see in his demo and screencast.
Have a good development
Juan
When in development mode turn :optimizations to :none, it makes a huge difference it won't change the initial compile time but any additional changes will only take milliseconds typically.
I have the following in my project.clj
:cljsbuild {:builds [{;; Directories of interest:
:source-paths ["src"]
;; Compiler flags:
:compiler {;; Where to save the file:
:output-to "resources/public/js/app.js"
;; Where to put the output directory
:output-dir "resources/public/js/out"
;; Optimizations:
:optimizations :none
}}]}
compilation time for a small file is too big
Try :optimizations :whitespace. And use lein cljsbuild auto
Second, from time to time I get stuff like this: ......
I haven't met this problem before. It looks like a bug in cljsbuild. Try to change another version maybe. I'm using [lein-cljsbuild "1.0.2"] with [org.clojure/clojurescript "0.0-2156"]. The 1.0.3 doesn't work in my setup.
Third thing which I failed to understand is REPL
In your case the key point is to find a way reloading namespaces without restart REPL or reloading page.
I find load-file is good for this.
My workflow is:
First start REPL: rlwrap lein trampoline cljsbuild repl-listen
Make sure there is (repl/connect "http://localhost:9000/repl") in my code.
Open my page in browser.
Do some test (my-namespace/my-func a b c)
Edit my codes.
In my REPL, run (load-file "path-to-my-file.cljs")
Repeat 4
This is still a pretty annoying workflow. load-file is slow and fragile so sometimes I browser to test. I hope someone can provide a better answer.

Forcing Cake to reload functions from my .clj files

I'm new to Clojure, and after too much of my life have been already wasted on waiting for Leiningen to run my code, I'm trying to move to Cake. While Cake's persistent JVM loads up blazing fast - it presents a bigger problem - my functions are also persistent!
To demonstrate the problem, I've started a cake project(using cake new mess-up-with-cake), and wrote this in core.clj:
(ns mess-up-with-cake.core)
(defn main-function[]
(println "I'm in the main function")
)
(println "I'm in core.clj, not inside in any function")
And this is project.clj:
(defproject mess-up-with-cake "0.0.1-SNAPSHOT"
:description "TODO: add summary of your project"
:dependencies [[clojure "1.2.0"]])
(use 'mess-up-with-cake.core)
(deftask my-task
(println "I'm in my task")
(main-function)
)
When running it with cake my-task, I get:
I'm in core.clj, not inside in any function
I'm in my task
I'm in the main function
No surprise here.
Now, I've changed core.clj to this:
(ns mess-up-with-cake.core)
(defn main-function[]
(println "I'm in the main function")
(println "I've made a change in the main function")
)
(println "I'm in core.clj, not inside in any function")
(println "I've made a change outside the main function")
And when I run it, I get
I'm in core.clj, not inside in any function
I've made a change outside the main function
I'm in my task
I'm in the main function
core.clj was clearly reloaded, but the change I've made inside the main function was not printed! Only when I stop the JVM with cake kill and rerun it I get the desired result - but if I have to restart the JVM every time I change a function, I might as well go back to lein...
Any idea how to force cake to reload my functions(and only my functions - reloading the entire Clojure runtime + any libraries I'm using probably won't be much faster than restarting the JVM..)?
This may not directly answer your question, though I hope it helps:
It sounds like your workflow if you where using leiningen would be to run:
lein run
wait for the JVM to start up .... get bored ...
observe result
edit code
repeat
This is a very common pattern in most languages and it's occasionally used for Clojure development (and Cake is very helpful here). It is much more common for Clojure development to use a single instance of the project and connect the editor to that instance using nrepl (or Slime and Swank). Because most everyone leaves the project running while they do the development not many people feel this pain and so the solutions are not as good in my opinion. Cake has largely been merged into Leiningen and the future direction of the Cake project is not clear to me (I could very well be wrong on this point). Of the Clojureians I know, all of them have moved to Leiningen and connect to their project from an editor like Emacs or vim.
a common workflow is:
start Emacs
M-x nrepl-jack-in
Ctrl-c Crtl-l to reload all the namespace and all it's dependent namespaces (this is close to a solution to your problem)
hack, load, repeat ;-)
This workflow is not Emacs or VI specific, the same method is used from Eclipse and Intelij
re: " reloading the entire Clojure runtime + any libraries I'm using probably won't be much faster than restarting the JVM".
I find it to be no more than two seconds even with my larger projects
I used to struggle with the slow JVM startup speed as well, and had mixed success with Cake. You might want to take a look at the excellent autoexpect plugin for Leiningen, explained in some length in the author's blog post. Basically autoexpect reloads your code every time the working directory tree is updated (and evaluates any expect clause, reporting any test failures). Makes continuous testing a dream -- I sometimes have two shells going in Emacs -- one for the output of lein autoexpect, one for a connected REPL to send snippets of code to as the other poster is suggesting.
I like the continuous testing style so much I wrote a similar utility in Python for my non-Clojure development (described in this blog post) -- for Clojure, I use autoexpect.

Is it possible to call a lein-plugin function from a lein-repl?

I want to control a number of lein-plugins (lein-cljs build, lein-aws, lein-beanstalk) from my lein repl. Is there a way to do this?
For example, I want to be able to call
plugin/src/leiningen/cljsbuild.clj: once
from the repl -- however, I apparently can't (require 'leiningen.cljsbuild) into my lein repl.
Thanks!
I'm not sure if this addresses what you are actually trying to do, but do you know about lein interactive mode? EDIT: Since lein interactive doesn't exist in the new version of lein, the solution may be in some use of jark. Jark has an interactive mode and a plugin to use lein, so it may be possible to issue both lein commands and call clojure functions from a single interactive prompt.
The Vinyasa library claims to provide this functionality. At the time of this writing, vinyasa.lein is broken, but it may be fixed in the near future.

expected usage of pallet from lein

I was reading about pallet here: http://twoguysarguing.wordpress.com/2010/11/01/starting-a-cluster-on-ec2-with-pallet/, as well as on the pallet site: http://palletops.com/. I'm still a little confused. The examples are arranged as if I'm expected to enter the code at a REPL.
But I think I'm missing something. Because I feel like (or I'm hoping) there should be some sort of lein support/integration, so that I can define some code to spin up a cluster, start the cluster, stop the cluster, deploy a war file, etc. via a series of lein commands.
I did find the following project on github, but it appears to be written to work with maven instead of lein: https://github.com/cemerick/clojure-web-deploy-conj.
And, this is very close to what I want, except that I want this tied into lein targets somehow: http://cemerick.com/2010/05/12/provisioning-administration-and-deployment-of-couchdb-java-tomcat/.
The Leiningen wiki refers to pallet/pallet-lein.
I don't have any experience with pallet, but it looks like the Leiningen plugin passes the first plugin argument to a function in the pallet.main namespace which appears to call into pallet.
So, the argument foo in lein pallet foo would be passed along to pallet.
So I found the following http://nakkaya.com/2010/02/25/writing-leiningen-plugins-101/, which describes the process for creating your own plugin. As it turns out it's fairly straightforward. Create a top level leiningen/ directory, give it a namespace name, and a function of the same name. And the function becomes a task in leiningen. So for example to provision a machine all I have to do is:
; In file leiningen/aws_provision.clj
(ns leiningen.aws-provision)
(defn aws-provision [project & args]
(println "pallet code to provision the box here..."))
Then from the lein prompt I can do:
lein aws-provision
I guess pallet-lein isn't going to really be able to do what I want, because the details of which cloud provider, which machine size, which packages to install on the machine, etc. will be different for each person.