Fork me on GitHub
#babashka
<
2022-01-21
>
borkdude09:01:54

Is there any bash expert in the house that can look at this PR? https://github.com/babashka/book/pull/46#issuecomment-1017932480

cap10morgan13:01:41

I don't use bash interactively (only for scripting things I should be using bb for :rolling_on_the_floor_laughing:), so I'm not very familiar with how the autocomplete stuff works. Looks cool though! Maybe I'll try implementing it for fish shell! 😎

Benjamin12:01:35

hi I wonder how I can start a proc so that it runs in the background and puts it output into the repl output. Alternatively it pops a terminal or sth; I tried (process/process ["xterm" "-e" ".."]) but its fiddely

borkdude12:01:29

That would be (process/process ["xterm" "-e" ".."] {:inherit true})

Benjamin12:01:38

yea works 🙂

Richie14:01:16

*file* isn't defined for my task. {:tasks {test (println *file*)}} running bb test prints <expr>

Richie14:01:59

If I put (println *file*) in a test.clj file and run bb -f test.clj then it doesn't print anything. Perhaps there's more to this issue.

Richie14:01:23

I'm on windows 10 with babashka v0.7.3

Richie14:01:51

Also, why can I use shell in a task but I have to require sh for a script? Initially, I approached writing tasks as if I were writing a script but now I'm wondering what's different.

borkdude14:01:12

I get:

$ bb test
<expr>

borkdude15:01:02

$ bb test.clj
/private/tmp/proj2/test.clj

borkdude15:01:32

note that when you do (println *file*) in a function, then it's not bound to the file of that function anymore. file is only bound when the file is evaluated, so you have to capture it on the top level

borkdude15:01:55

you can require babashka.tasks :refer [shell] in files too

Richie15:01:35

Oh, woa. Thanks, I hadn't noticed the (require '[babashka.tasks :as tasks]) in https://book.babashka.org/#_shell section.

borkdude15:01:08

We should probably document that then. Feel free to send a PR

borkdude15:01:39

Oh, it's documented here: > The `babashka.tasks` namespace exposes the following functions: `run`, `shell`, `clojure` and `current-task`. They are implicitly imported, thus available without a namespace prefix.

borkdude15:01:51

but it may not be apparent that you can use these in scripts as well

Richie15:01:10

Yea, I think I was treating those as magic since I didn't know where they came from. I connected cider to bb --nrepl-server so that I could eval stuff but I didn't think I could get shell so I was using clojure.java.shell/sh for testing and then changing it to shell for the task. I also tried pretty hard to nest tasks even though nothing suggests that it's supported. I found the code for tasks. I didn't know that *file* comes from clojure.core. I thought it was coming from babashka. Regarding bb -f test.clj not printing anything, I was actually just overlooking it. It was printing correctly but I thought I was looking at the command prompt which shows the current directory. I was missing the distinction between running a script and running tasks. e.g. I thought I could use *file* to get the https://book.babashka.org/#_current_file_path. Right https://book.babashka.org/#_parsing_command_line_argumentsthat section it has instructions on how to use *command-line-args* which does actually work in a task. How should I get the current file? (prn (slurp (:out (shell "pwd")))) gives me "" (prn (clojure.string/trim (:out (clojure.java.shell/sh "pwd")))) gives me "/c/Users/.../project-folder" That can be good enough. I just have to convert it to a path like "C:/Users/.../project-folder"

Richie15:01:43

(fs/directory? "C:/Users/richie/Documents") is true (fs/directory? "/c/Users/richie/Documents") is false

Richie15:01:46

I was just browsing through the recent history and I found a comment mirroring my thoughts. https://clojurians.slack.com/archives/CLX41ASCS/p1642523166058600 I'm also wondering what belongs in the bb.edn file. I initially assumed "everything" but now I'm thinking "as little as possible".

borkdude15:01:18

You get the current file with

(def current-file *file*)
`

borkdude15:01:57

yeah, you can put the code supporting your tasks in a file

borkdude15:01:09

and then keep the bb.edn as small as possible with only the task names and a few requires

borkdude15:01:33

my convention lately is to store the script files in $project/bb and then add :paths ["bb"] to bb.edn

Richie17:01:28

Sorry, I still don't have it working.

{:paths ["bb"]
 :tasks {test {:task (println (ns-map 'bb.current-file))}}}
and bb/current_file.clj
(ns bb.current-file)

(def current-file *file*)
gives
C:\Users\richie\Documents\my-project>bb test
----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  No namespace: bb.current-file found
Location: 20:11

borkdude17:01:30

The namespace should be (ns current-file) . bb isn't part of the dir structure that is on the paths

borkdude17:01:15

or if you change to :paths ["."] then it works with what you currently have

Richie17:01:46

Hmm, I removed "bb" from my bb.edn and from bb/current_file.clj but I just get the same error No namespace: current-file found I'll try on a mac as a sanity check.

Richie18:01:05

➜  test tree
.
├── bb
│   └── current_file.clj
└── bb.edn

1 directory, 2 files
➜  test cat bb.edn
{:paths ["bb"]
:tasks { test (println current-file/current-file)}}
➜  test cat bb/current_file.clj
(ns current-file)
(def current-file *file*)
➜  test bb test
----- Error --------------------------------------------------------------------
Type:     clojure.lang.ExceptionInfo
Message:  Could not resolve symbol: current-file/current-file
Location: <expr>:23:10
Phase:    analysis

----- Context ------------------------------------------------------------------
19: nil
20: (def test (binding [
21:   babashka.tasks/*task* '{:name test, :task (println current-file/current-file)}]
22:   nil
23: (println current-file/current-file))) test
             ^--- Could not resolve symbol: current-file/current-file

borkdude18:01:54

I'll be back in 30 minutes

Richie18:01:49

No worries. No rush.

borkdude19:01:33

add :requires ([current-file]) in your :tasks map

Richie19:01:19

Oh, right! Sorry, my first issue was including bb in the name and then I forgot to require it with this example. Thanks!

Michaël Salihi19:01:57

Thanks for the sharing @U04V15CAJ 👍:skin-tone-3:

borkdude19:01:12

And you for making this :)

😉 1