Fork me on GitHub
#babashka
<
2021-11-15
>
mmz11:11:12

I'm using (clojure.java.shell/sh "open" filename) to open a newly created file, but that's probably not OS independent. I can't find anything similar in babashka.fs or ... what is the best way to open a file via the filesystem from a babashka script?

borkdude11:11:24

There probably isn't a cross platform way. What does open do, spawn a UI?

borkdude11:11:50

You can use (System/getProperty "os.name") to dispatch on os

1
mmz11:11:08

It's the end of a script. It outputs a html file (that I don't want to serve) . And the output is too much to print user friendly in the terminal.

mmz11:11:39

I've had this before with a csv ... but I think dispatching on os will do. Thanks!

borkdude11:11:39

bb script && open foo.html
?

1
mmz11:11:58

maybe better indeed

mmz11:11:09

let it do one thing well

mmz11:11:59

too bad it will 'duplicate' filename ... it's configured in the script and now I have to type it in the terminal ... but I guess the history completion will help here.

borkdude11:11:29

well it's up to you, you can also still do the dispatch on OS, or only call open when open is available

mmz11:11:12

Going for the bb script && open foo.html option. Thanks 🙂

hugod11:11:46

Maybe use $EDITOR foo.html

1
mynomoto12:11:51

If the script return the filename you could pipe it to open.

💡 1
jkrasnay12:11:00

A cross-platform option is to use explorer instead of open on Windows and xdg-open on Linux. Each of these opens a file in the app that is bound to that file type, e.g. the default browser for HTML files.

1
hairfire22:11:21

I have the following in a bb.edn file:

{:tasks {aaaa {:doc  "testing"
               :task (shell "cd build")}}}
when I execute
bb aaaa
I get the following error
----- Error --------------------------------------------------------------------
Type:     java.io.IOException
Message:  Cannot run program "cd": error=2, No such file or directory
Location: 21:11
Does "shell" support changing directory?

borkdude22:11:19

@hairfire cd is often a bash / zsh / etc built-in, not a real program. also shell doesn't really invoke a default shell, it just calls an executable program either on your PATH or via an absolute/relative path

borkdude22:11:30

The JVM environment doesn't allow changing working directory. A workaround for this is often to define a dynamic var or atom which contains the current working directory and then use that in functions that should take that into consideration.

borkdude22:11:18

If you want to invoke a program in another directory, you can do that with shell like this:

(shell {:dir "some-other-directory"} "my-program")

borkdude22:11:47

similar for clojure:

(clojure {:dir "other-dir"} "-Spath")

hairfire22:11:05

Thank you for the quick response 😀

hairfire22:11:04

This "limitation" makes it difficult to use bb as a bash replacement

quoll18:11:50

I know this is a few days late, but I’m confused by this statement. e.g. If I’m using bash, then:

~ pgearon$ cat op
#!/bin/bash
cd tmp
~ pgearon$ ./op
~ pgearon$ pwd
/Users/pgearon
~ pgearon$
The cd command only affects the process that it’s in, which is the equivalent of bb using the {:dir "other-dir"} option. I’m curious as to the bash code that you were hoping to replace with bb?

borkdude22:11:12

I have heard that comment from other people as well. I haven't personally missed it a lot. There are other ways to accomplish the same task.

borkdude22:11:09

But babashka is bound to what GraalVM / JVM allows you to do. For example #nbb which is based on Node.js, does allow it.

hairfire22:11:09

At the moment, I wanted to change into a directory, execute 'lein uberjar', then use the generated jar in my bb script.

borkdude22:11:52

@hairfire how I would do that is:

(def project-dir "project")
(shell {:dir project-dir} "lein uberjar")
(fs/file project-dir "target" "foo-standalone.jar")
see e.g.: https://github.com/clj-kondo/clj-kondo.lsp/blob/b1097c318c0d51b3c7e45192005c0f7d8765cd68/bb.edn#L37

hairfire22:11:55

It looks like I can use

(shell "bash -c 'cd build && lein uberjar'")

borkdude22:11:17

just use (shell {:dir "build"} "lein uberjar")

hairfire22:11:28

Ah! So {:dir "build"} instructs shell to execute the code in the "build" directory. Perfect 👍

borkdude22:11:10

The bb.edn link I sent is a project which multiple directories, one with a JVM leiningen project and one with a Node project. It builds everything from the root.

✔️ 1