Fork me on GitHub
#babashka
<
2023-05-20
>
leifericf08:05:43

I’m looking for some examples of how to use regular expressions in Babashka. More specifically, I need to process a few hundred JSON files, located in a few dozen subdirectories (variable depth), and search and replace a string within the contents of each JSON file with parts of its filename. So I need to use regular expressions to capture parts of the filenames and parts of the JSON contents. I'm updating some connection strings in many appsettings.json files for a bunch of .NET microservices. Does anyone know if there are any examples of how to do something like this?

borkdude08:05:05

@U01PE7630AC you can maybe use babashka.fs/glob for finding the json files:

(babashka.fs/glob "json-dir" "**.json")
will find every json file in all the subdirectories below json-dir

borkdude08:05:37

About replacing by regex in a string:

(str/replace the-string #"my-regex" "the-replacement")
?

leifericf08:05:10

Thanks for pointing me in the right direction, @U04V15CAJ! I've been watching all your talks on YouTube today. I'm excited to try it out at the office on Monday!

πŸŽ‰ 2
leifericf08:05:24

By the way, Babashka is the perfect way to sneak some Clojure into my workplace. Nobody likes Bash scripts, so they're happy to give me all those tasks, and nobody cares what I use to solve them. I plan to break the ice with Babashka and use it to demo the strengths of Clojure to my teammates. Perhaps I can convince the rest to try to build one microservice with Clojure. At present, we’re all in on C# .NET on Microsoft Azure.

πŸŽ‰ 4
❀️ 2
leifericf10:05:53

Would it be wholly unreasonable to attempt to embed Clojure in a C# .NET application, to provide end-users with scripting capabilities, using Babashka or SCI? In a similar way to how Lua is often embedded in game engines for gameplay designers to create NPC brains, create GUI modifications, etc. We have a use case where we want to provide semi-technical business users with a DSL for creating business logic/”rules” in some of our applications, and I suspect this might be one viable path to success.

leifericf10:05:31

@U043RSZ25HQ I've had a look at that, and it seems rather thinly documented and not actively maintained. I could be mistaken. It seemed like Clojure on GraalVM could be a viable alternative to embed Clojure due to it being one small binary.

elken10:05:20

Check #clr, seems pretty active and will be the simplest option. It's still quite a niche thing, although it's the same clojure so docs for clojure should apply fine

πŸ‘ 4
teodorlu23:05:56

It's possible to compile sci to a shared library. And it's possible to call shared libraries from C#. Though you might have to break some new ground! https://github.com/babashka/sci/blob/master/doc/libsci.md

πŸ‘€ 2
teodorlu23:05:41

Using something like IronPython is probably going to be easier, though (if you want easy more than you want Clojure). Dynamo's Python cells are implemented with IronPython. IronPython: https://ironpython.net/ A video about using Python from Dynamo: https://m.youtube.com/watch?v=3sDMHG2NlQM&amp;list=PLBKwx9iS-Lun4z5igR9dCzTG3-GdNayq7&amp;index=1&amp;pp=iAQB Though babashka is definitely cooler than 🐍.

babashka 2
borkdude11:05:38

"I made all the examples from htmx in Clojure-Babashka! I had a lot of fun coming up with ideas along the way." @kyogatama on Twitter: https://twitter.com/keychera/status/1659879552175464448

πŸ‘€ 8
πŸŽ‰ 4
😍 4
πŸ’― 2
teodorlu17:05:41

Using core.match for routing was an interesting idea! Don't think I've seen that before. https://github.com/keychera/panas.example/blob/main/babashka-httpkit/src/serve.clj#L21

keychera10:05:24

just got around to opening slack, thank you for sharing this @U04V15CAJ ! @U3X7174KS I actually got the idea from somewhere in this slack here but I didn't remember exactly from who, since I often got my question answered just by searching through slack past questions/discussions

❀️ 6
πŸ’― 4
teodorlu10:05:01

Nice! I have that relationship to way too many of "my" ideas. Often I've seen them coined by somebody else. Then some time has passed. Then it feels like the idea is "mine" because it feels right. πŸ˜…πŸ˜…

πŸ˜… 2
borkdude10:05:35

When core.match in bb was just included, this was one of the original examples, to use it for routing. So the example may be somewhere in examples maybe in the babashka repo.

πŸ’― 2
borkdude10:05:14

I can also recommend https://github.com/askonomm/ruuter as a routing lib in bb

πŸ‘€ 4
πŸ‘ 2
Nundrum21:05:42

I've been playing around with Babashka and HTMX myself this week. Which brings me to a question about namspaces. I've set up my local HTTP server to run bb for .clj files and that works great! Got over all the humps reading environment variables and stdin to get POST data. But then I wanted to break that down into namespaces and can't get to a happy place that works both over HTTP and with my IDE (aka Vim). Thought maybe you folk would have some clever ideas to share.

lispyclouds21:05:38

How have you arranged the code for the project? does it have a bb.edn with a src folder structure or is it a single file?

Nundrum15:05:49

I was trying to have the code flat in a directory as would make sense for the URLs. But I believe namespace requirements won't allow for that.

borkdude15:05:50

you can do this, but you have to add :paths ["."] to bb.edn and you can only use single segment namespaces, so foo_bar.clj -> foo-bar but not foo.bar

Nundrum16:05:35

Hmmm I thought I had tried exactly that. I'll give it another go. Thanks!

borkdude16:05:03

This will work, if not, there's likely a config problem

Nundrum16:05:31

It had hung the Vim integration when evaluating the ns, which is what I saw before. But then restarted both the nrepl and vim and it was fine. A quick test refactor worked. Thanks again!

πŸ‘ 2