leiningen - clojurescript equivalent of lein run? - clojure

In clojure I can run:
lein run -m my.namespace # run the -main function of a namespace
and this will run my code from the command line.
Is there an equivalent for Clojurescipt to run the generated code in node.js? (In leiningen)
(I have read the doco for starting the Clojurescript REPL, for the running on node.js and the reply integrated into my application. I was just looking for a one-line command line solution.)

Depending on what your goal is, you might find it useful to use 'cljsbuild test'. You can specify a test context on your project.clj that uses node.js/v8/phantomjs.
Example:
:cljsbuild {
:test-commands {
"v8" ["v8"
"target/generated-sources/private/js/client.js"
"tests/v8-test-runner.js"]}}
v8-test-runner.js:
path.to.your.entrypoint.start()
You can also leave 'lein cljsbuild auto' running and start the javascript application directly:
v8 target/generated-sources/private/js/client.js tests/v8-test-wrapper.js
That's probably the best option if you are building a node.js application using clojurescript. You don't need to worry about classpaths in javascript (which is the major thing that lein run provides), you can just run your application directly from the assembled javascript file.

Related

How to load this Clojure project in my cider REPL? Why I am receiving the message "The clojure executable isn’t on your ‘exec-path’" (NixOS)?

Recently, I started learning Clojure. I have been using Emacs, and cider (REPL). Also, I have NixOS as OS.
I have been able to successfully program in Clojure using this environment. Usually, I create the files, then I go to the directory where the .clj are, I execute cider-jack-in, a REPL is created, and interactive programming magic happens. Thus, I am able to load the file, load new versions of old functions, etc.
It also works if I just start Emacs and execute `cider-jack-in. The mini-buffer asks:
Are you sure you want to run `cider-jack-in' without a Clojure project? (y or n)
After answering y, everything works fine.
While reading the book Clojure for the Brave and True, I have also successfully used:
$ lein run
For learning purposes, I decided to explore some open source projects. Thus, I did a git clone of this open-source project.
Being on the folder, I decided to reproduce the steps that have worked so far. However, it does not work. After executing cider-jack-in, the mini-buffer echoes the following:
The clojure executable isn’t on your ‘exec-path
Also, if I try with lein I get:
[pedro#system:~/projects/http-cron]$ lein run
No :main namespace specified in project.clj.
Obs.: If I type clojure on the shell, this is what I get:
[pedro#system:~]$ clojure
The program 'clojure' is not in your PATH. You can make it available in an
ephemeral shell by typing:
nix-shell -p clojure
Why is this happening?
How can I solve this (considering that I am using NixOS)?
Going the imperative route, depending on whether you use NixOS with channels (default) or flakes, you can permanently install clojure into your environment with:
channels: nix-env -f '<nixpkgs>' -iA clojure
flakes: nix profile install nixpkgs#clojure
The preferred (declarative) way would however be adding clojure to environment.systemPackages in /etc/nixos/configuration.nix and running sudo nixos-rebuild switch.

Git Bash for Windows not working 'lein repl' command

I'm actually using git bash instead of default windows command shell (cmd).But I cannot run 'lein repl' in git bash. It doesn't show anything and I must press crtl + c to regain control.
Any suggestion?
Thank you.
-- EDIT
I'm running lein instead of lein.bat because I added alias lein=lein.bat in my .bashrc, so I don't think it is a problem.
lein run is working fine in Git Bash and leiningen works fine in windows cmd. The problem is explicity with lein repl.
When I run which lein I got which: no lein in ([MY-PATH-VAR])
First Check if you have installed lein.bat in windows using below command in command prompt
lein --version
Then if lein is available, then in your git bash type the below command
alias lein='lein.bat'
Thein run your lein command it will work for sure...
I had exactly the same problem as ThomasH where calling lein repl from an Emacs shell would get me the introductory messages and a prompt ...
nREPL server started on port 55801 on host 127.0.0.1 - nrepl://127.0.0.1:55801
REPL-y 0.4.4, nREPL 0.8.3
Clojure 1.10.1
Java HotSpot(TM) 64-Bit Server VM 15.0.2+7-27
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
... but seemingly freeze when I try to get it to do anything, like:
user=> (+ 1 2 3 8)
When I open up the Task Manager, I could see the underlying Java program continuously at ~30% CPU for no apparent reason, and the only way I could reliably get out of it was to end the Java process from there.
But through a lot of effort, I finally figured out that the reason this was happening was a shell environment variable TERM=emacs. Whenever you call M-x shell, the function comint-term-environment puts it in for you.
(According to the source code in comint.el, it's done so that it "lets us specify a width". And it also mentions that "Some programs that use terminfo get very confused if TERM is not a valid terminal type". So there you have it - it looks like Java is one of those.)
To resolve this, you can set the variable system-uses-terminfo to 't before calling M-x shell.
(setq system-uses-terminfo t)
After having done so, Java is now finally responsive and I can evaluate things in the repl.
user=> (+ 1 2 3 8)
14
If you have this problem and got fortunate enough to stumble here, I hope this helps! =D
The following worked for me to get this running in Git bash on Windows 10. When I originally got this working, it was freezing, but this post helped me figure out how to fix by including winpty.
If you haven't added the lein.bat file to your Path variable, then do steps 1-3. Otherwise, skip to 4. In the System Properties -> Advanced -> Environment Variables area, select the Path variable
Click Edit and add the directory containing lein.bat to this. Mine was C:\Program Files (x86)\lein - lein.bat was stored inside the lein folder
Click OK until you close out of the prompts
Confirm the update worked by opening a command prompt (windows key, then type cmd)
Enter lein. If you haven't already, it should prompt you to run the install command. Do this if prompted, then enter lein again to make sure it worked (should output help menu)
Close out of Git bash if already open, then open again
Enter alias repl='winpty lein.bat repl'. You can choose what you want the alias to be, I used repl
Type repl (or whatever alias you added). Should take a second, then begin a REPL prompt
Using GitBash as your terminal, you can install the lein script rather than the lein.bat script.
The lein script will run correctly in GitBash (as it is effectively a Unix environment).
I recommend installing the lein script in a directory called bin in your user account directory, e.g.:
c:/Users/your-account/bin
Placing the lein script in bin will allow you to run the lein command anywhere on the GitBash command line.
A Note about Emacs, lein and windows
The lein script will also be called correctly from Emacs, assuming you run Emacs from the GitBash terminal, ie. runemacs. Add Emacs install directory to your user account PATH environment variable and GitBash will find it on the command line.
Emacs installed on Windows works better when run from the GitBash terminal, as you can then use ediff in Emacs, which requires the diff command that GitBash terminal provides.
My recommended Emacs installs for windows
zklhp emacs-w64 optomised
Chocolatey Emacs 64bit
My experience was that lein repl (and I do use the lein bash script) would start up ok, also printing the startup messages of the lein command to the shell, but would then stop to output anything (although the process might be running fine), part. not providing an interactive prompt (except for the first), not echoing user input (except for the first), nor printing evaluation results. When terminating the process with Ctrl-C all my inputs would be passed to the underlying shell (with funny results like $ (+ 3 4) bash: +: command not found).
My solution is to use Git CMD for interactive commands like lein repl, lein figwheel etc., which (unfortunately, as it might seem) works fine. Running a Git sh process inside a different terminal emulation (Cmder) also gives me a working interactive environment.

Lein console (like sbt)

Is there some built-in functionality or plugin to lein to get a lein console, so for example one could test without waiting every time for JVM to start up.
$ lein console
>>> test
...
>>> test
...
>>> jar
Note: I'd like to trigger test runs myself, not e.g. by watching source files. That's why I'd like to have a lein console.
Clarification: I'm not looking for lein repl. I'd like to have a console where I could run lein task commands.
Older versions of leiningen used to include lein interactive, which behaved much like the feature you are asking for: it opened a shell into which you could type test and have it run lein test from the already-running lein jvm, and so on. This feature was removed in the transition to lein 2.0, I think, and although I don't know why I suspect there was a good reason. Maybe try asking in #leiningen on freenode?
You might want to have a look at grenchman. While it's not a Leiningen console it at least enables reusing of an existing REPL session. From what I gather, usage is as follows:
Move somewhere that is not inside a project and call:
$ lein repl :headless
Within your project directory, use:
$ grench lein <task> <options>
Tasks will be run inside the already spun up Leiningen JVM and the startup overhead should disappear.
Building grenchman seems to be tedious, though, and it is recommended to use one of the precompiled binaries (BUT they are currently not available).
And finally, that page also states:
Grenchman is still very new and may not be fully reliable.
So, good luck, I guess?
One option is to run a repl from leiningen's own jar file.
$ java -cp ~/.lein/self-installs/leiningen-2.5.0-standalone.jar clojure.main
Clojure 1.6.0
user=> (require '[leiningen.core.project :as project] '[leiningen.test :as test])
nil
user=> (def prj (project/read))
#'user/prj
user=> (test/test prj)
lein test org.noisesmith.orsos.load-test
Ran 3 tests containing 3 assertions.
0 failures, 0 errors.
nil
user=> (require '[leiningen.jar :as jar])
nil
user=> (jar/jar prj 'org.noisesmith.orsos)
Compiling org.noisesmith.orsos
Created /media/justin/806084F16084EEEA/clojure/orsos/target/orsos-0.1.0-SNAPSHOT.jar
{[:extension "jar"] "/media/justin/806084F16084EEEA/clojure/orsos/target/orsos-0.1.0-SNAPSHOT.jar"}
user=>
As a baseline, this can run lein tasks without having to restart lein every time. If you also use rlwrap or use nrepl it becomes a bit more usable. As far as I know there is no user friendly tooling around this (though there easily could be).
If you wish to use tasks from lein plugins those can be added to the -cp arg.

ClojureScript bRepl does not respond at all

I've been tinkering with clojure and clojurescript for some time and I always found the browser repl to be extremely unresponsive. It usually hangs on without giving a result, or throws a broken pipe exception. Now I'm in a situation where it always refuses to work.
Let's say I download the modern-cljs tutorial part 2 and I do all the steps to start the page, load the compiled javascript and start the bRepl. When I try to evaluate something as simple as (+ 3 4) well, it just doesn't work.
I'm on OSX 10.8.4 and my leiningen version is 2.3.2 on Java 1.6.0_51 Java HotSpot(TM) 64-Bit Server VM. Any help is extremely appreciated.
I would suggest to clone the modern-cljs repo, then checkout the tutorial-02 branch, then clean and recompile.
git clone https://github.com/magomimmo/modern-cljs.git
cd modern-cljs
git checkout tutorial-02
lein do clean, cljsbuild clean
lein cljsbuild once
Be sure to have your http-server running (I'm assuming the port is 3000) and its root set to modern-cljs/resources/public directory
Then run the repl-listen substask as follows:
lein trampoline cljsbuild repl-listen
Wait for the cljs prompt. Then visit the localhost:3000/simple.html URL and wait that the connection with the brepl is ready.
At the cljs prompt you should now be able to evaluate cljs expressions. Eventually, reload the above URL.
If it still does not work, try to delete your maven repository:
rm -rf ~/.m2/repository
and redo everything from the lein do chain of commands.
Eventually you can follow the https://github.com/magomimmo/modern-cljs/blob/master/doc/tutorial-18.md#the-need-of-a-more-comfortable-brepl-experience tutorial to set up a more comfortable bREPL which uses https://github.com/cemerick/piggieback
HIH
I've had a few problems with this topic too. And here are the tricks that help me:
Clean leain deps: rm -rf ~/.m2/*
download lein deps: lein deps in your project root
I saw this message: "broken pipe" when there was too much time from the last brepl interaction. So, refresh the browser and that's all
sometimes my brepl hung on the first time i was trying to evaluate the code, again try to refresh the browser
don't forget to run: lein cljsbuild once before start the brepl
Good luck
PS: Anyway if you want to have a comfortable brepl experience you have to take a look to https://github.com/cemerick/austin . I have tested this brepl and works fine https://github.com/juanantonioruz/client
You should try using Chromium unsafely:
OS X:
open -a Chromium --args --disable-web-security
Linux:
chromium --disable-web-security
Then visit your page in the unsafe browser and try to connect to the brepl.
Explanation:
Because while brepl is running on port 9000, the web server hosting the page is necessarily using a different port, which constitutes XSS (see [this stack overflow discussion]).
The easiest way to get around this is to use Chromium unsafely for debugging, but you can enable XSS by some other means if you want.
What ClojureScript release are you specifying in your project.clj? If you aren't specifying a specific release, this likely the source of your troubles. 0.0-1913 is the latest one.
I've also seen a complete hang in the REPL when I forget to call repl/connect from within the browser. Since there is no output, it's hard to diagnose. It's worth verifying before you try more intrusive remedies.

Slimv setup for clojure

Has anyone recently tried to setup slimv for clojure that would be willing to describe how to do so? Slimv will currently autodetect lisp on my machine but fails to load on .clj files. I've also added the following to my .vimrc
let g:slimv_swank_clojure = '! xterm -e lein swank &'
but it has no effect (lein is installed and executable).
Sorry if this is a basic question but I am very new to both vim and clojure.
First you should check in your terminal command prompt that lein swank does really run the swank server from within the directory where your .clj file resides. If it doesn't start then you need to make a command that runs the swank server, then embed it in '! xterm -e {swank_command} &' and store it in g:slimv_swank_clojure.
If the swank command looks OK, then you should check it from within Slimv via :echo SlimvSwankCommand() that prints the actual swank command built and used by Slimv.
You may also run the swank server in a separate terminal window outside of Vim, Slimv will connect it if the port number is the same on both sides (4005 by default).
There is another Swank server embedded in Slimv, but this is only autodetected if the lein command does not exits for the user (and g:slimv_swank_clojure is not explicitly defined).
If still no luck then please contact me, I'm the author of Slimv, you can find my email address in the documentation.
Unless you are certain there are something you need in Slimv, there's very good plugin called VimClojure.