Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opinionated default implementations of common tasks in scripts. #15

Open
KristianAN opened this issue Mar 10, 2023 · 9 comments
Open

Opinionated default implementations of common tasks in scripts. #15

KristianAN opened this issue Mar 10, 2023 · 9 comments

Comments

@KristianAN
Copy link
Contributor

Languages like python provide really fast and simple ways to perform most operations, which lends itself very well to scripting.
I think that Scala fails at this, while it's better than Java it's still not great. I don't think this is a problem for most situations.

I do however think it would be very nice if toolkit could provide default implementations of some common tasks in a referential transparent and resource-safe way. An example would be reading and writing files.

I'm pretty new to using CE so I ended up writing this, which I'm not even sure is the correct way to do this.

def fileWriter(path: String): Resource[IO, FileWriter] = 
    Resource.make(IO.blocking(new FileWriter(new File(name))))(file => IO.blocking(file.close()))
def openFile(path: String) : Resource[IO, Source] = 
    Resource.make(IO.blocking(Source.fromFile(path)))(file => IO.blocking(file.close()))

def writeToFile(path: String, content: String) =
    fileWriter(path).use(writer => IO.blocking(writer.write(content)))

if instead similar operations were already implemented for me I think that would be really great. It would at least make it a lot easier to convince other people to use this :)

All of this might obviously be outside of the scope of toolkit as it's a collection of libraries, but it would certainly make it easier to pick up for people who just want something to write small tools with.

@armanbilge
Copy link
Member

For referentially-transparent, resource-safe APIs for working with files you can look at fs2.io.file.Files.

I think the quickest way to read a file to a string is:

Files[IO].readUtf8(Path(...)).compile.string

Unfortunately, I think writing currently needs more boilerplate.

Stream.emit(str)
  .through(fs2.text.utf8.encode)
  .through(Files[IO].writeAll(Path(...)))
  .compile.drain

Maybe we need a helper for writing in FS2, and some examples in this area.

@armanbilge
Copy link
Member

Btw: another possibility is that we need a new lib, similar to os-lib, that provides a bunch of helpers on top of fs2.io.file and fs2.io.process that make it easy to do stuff in a non-streaming way. Streaming is awesome, but it's probably unnecessary boilerplate for many scripts.

@KristianAN
Copy link
Contributor Author

I think creating another lib is probably the best idea. tos-lib? I'm sure it would be useful in a lot of projects.

@ChristopherDavenport
Copy link
Member

Feel free to pick and choose from what I put together a while ago https://github.com/davenverse/shellfish

@TonioGela
Copy link
Member

TonioGela commented Mar 11, 2023

Maybe we need a helper for writing in FS2, and some examples in this area.

A helper? Btw, examples are a great idea, I'll write some of them for sure

[EDIT] ah, that helper! :D

@TonioGela
Copy link
Member

TonioGela commented May 12, 2023

Maybe we need a helper for writing in FS2, and some examples in this area.

A helper? Btw, examples are a great idea, I'll write some of them for sure

[EDIT] ah, that helper! :D

I wrote that helpers and they got published here

Also, to revive this issue, are we considering writing an os-lib?

@zetashift
Copy link
Contributor

Also, to revive this issue, are we considering writing an os-lib?

I have no experience in writing OS abstraction stuff, but I'll gladly contribute whereever possible!
But wouldn't it be better to improve shellfish?

@armanbilge
Copy link
Member

But wouldn't it be better to improve shellfish?

Yes, I believe that's what we should do. If anyone has time to work on this, I can review / merge PRs 🙂

The first step can be rewriting what's already there with FS2 I/O so that it's cross-platform (it's currently JVM only).

@zetashift
Copy link
Contributor

The first step can be rewriting what's already there with FS2 I/O so that it's cross-platform (it's currently JVM only).

It looks like it uses fs2.io in some places no? #15 (comment)

Though I see it uses some JVM only APIs as well: https://github.com/davenverse/shellfish/blob/main/core/src/main/scala/io/chrisdavenport/shellfish/Shell.scala#L158

I'll try to see what currently can be made cross-platform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants