Fork me on GitHub
#clj-on-windows
<
2021-12-25
>
Chad kennedy21:12:51

I've got my project working great, under Windows. Only problem is that I have to modify the scripts in the project's package.json to call cmd-clojure instead of clojure. (I'be installed clojure via scoop) Is there a way to get clojure to be available from the cmd shell? This is a shared project with Linux users, so changing package.json isn't really an option.

borkdude21:12:23

@chad.kennedy I guess you could make a clojure.bat file which calls cmd-clojure and have that on that PATH

Chad kennedy21:12:19

It would have to pass through all the command line arguments as well. I'll give that a try.

borkdude21:12:04

shelling out to clojure has been a problem for me too in cmd.exe - nowadays I use bb clojure in CI which works the same in all environments

borkdude21:12:54

I think scoop's cmd-clojure seems to work under both cmd.exe and powershell - perhaps renaming or copying that to clojure.exe would solve the problem too

Chad kennedy21:12:22

Copying cmd-clojure.exe to clojure.exe didn't work. But creating a clojure.bat with the contents cmd-clojure %* worked a treat. Thanks for the suggestion! I have to say though the fact that I have to do things like this makes me think I must be going "against the grain" in terms of how I'm going about setting up my development environment. Does almost everyone doing Clojure just use Linux? Or am I missing something else?

littleli17:12:18

Hi @chad.kennedy Author of scoop-clojure here. I read your suggestion https://github.com/littleli/scoop-clojure/issues/162#issuecomment-1001076507. But I'm sorry I won't add it. The reason is simple. It's already confusing enough in it's current form. But I have a suggestion 🙂 in a directory where you have package.json create both files clojure.bat and clj.bat with appropriate content. Then this basically works the way you want:

shelling $ ls

    Directory: C:\Users\alesn\Projects\shelling

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          27.12.2021    18:50             15 clojure.bat
-a---          27.12.2021    18:29            248 package.json

shelling $ cat .\clojure.bat
@cmd-clojure %*
shelling $ cat .\package.json
{
  "name": "shelling",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "clojure": "clojure"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
shelling $ npm run clojure

> [email protected] clojure C:\Users\alesn\Projects\shelling
> clojure

Clojure 1.10.3
user=>

littleli17:12:13

On Linux and Mac it will just run clojure from the platform specific place. On Windows it will run local .bat file.

borkdude21:12:11

I think so yes, and if you're on Windows, using WSL2 is also a popular option. But still there's "pure" Windows users too. Having a task runner call out to Clojure is maybe not so common, it's usually about invoking a REPL, which works fine under pure Windows.

borkdude21:12:04

There are some tools which fabricate some powershell incantation to call out to Clojure under Windows, I think shadow-cljs does this. But package.json scripts doesn't support OS-conditional commands.

borkdude21:12:20

A tool like babashka does support this in its task runner.

Chad kennedy21:12:42

Interesting. Thanks so much for your feedback and help!

borkdude21:12:00

@chad.kennedy

{:tasks {compile (if (babashka.core/windows?) (shell "powershell.exe -command 'clojure ...') (shell "clojure ..."))}}
bb compile
or to use the built-in clojure CLI:
{:tasks {compile (clojure "...")}}

Chad kennedy21:12:31

Nice! Not sure that's an option for the shared project I'm working on, but I'll def keep it in mind for personal projects. Thanks for being so quick to help! Greatly appreciated!

borkdude21:12:10

@chad.kennedy Oh, it seems I was wrong about package.json, it does have some OS-specific thing: https://stackoverflow.com/a/53197655/6264

littleli11:12:44

It turned out there is a good solution to this:

npm config set script-shell "C:\\Program Files\\PowerShell\\7\\pwsh.exe"

# or yarn version
yarn config set script-shell "C:\\Program Files\\PowerShell\\7\\pwsh.exe"
Path above is my specific, can be on different place. Maybe the whole path is not even needed. now one can just run tasks on Windows the same way as on Unices.

littleli11:12:56

It's good that @chad.kennedy came up. We are smarter every day.

Chad kennedy14:12:27

Glad I could bring some "beginner's mind" into the mix. 🙂

borkdude21:12:28

So you could use this and write powershell -command '...' perhaps

👍 1