-
Notifications
You must be signed in to change notification settings - Fork 145
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
Classpath resource APIs #9
Comments
Is anyone working on this? I can take it up if not. |
@acrisci : Feel free to pick this up. Before you begin, can you outline your plan? Like what APIs would you like to add? |
I am kind of new to the jvm and scala and my knowledge of what makes for a good scala library interface is lacking so I might need some help with this :) My use case I think is a rather common one. I have an application which I package as a jar and I want to get the contents of some file in the resources directory such as a config file or something. When I simply run val f = File(getClass.getResource("/some-file.txt").toURI)
println(f.contentAsString) But when the application is packaged as a jar (such as with
The URI in the first case is the path to the actual file on the disk. In the second case, it is this:
This obviously doesn't exist in the file system, so I don't believe it is possible to turn it into a proper better files So what I will propose first is a new class called val resource = resource"/some-file.txt"
val resource = Resource("/some-file.txt") That will load the file from the resource path similar to what you would expect could be gotten with Loading resources from other classes would also be useful, and maybe that could be accomplished with a method such as The Then we can do cool stuff like this: val myConfig = resource"/path-to-config.txt".contentAsString
// parse config somehow instead of having to look up how to do that another way on google over and over again like i do now. |
@acrisci: I think for all practical purposes, resources are just vanilla files that happen to live in a special place and loaded in a special way (i.e. inside the app's jar). Thus, if we classify all operations on a file into 2 camps - reads (e.g. Thus we can do this then: trait ReadOperations {
def contentAsString: String
def bytes: Iterator[Byte]
....
}
trait WriteOperations {
def delete(): Unit
def moveTo(dest: File): File
....
}
class File extends ReadOperations with WriteOperations
class Resource extends ReadOperations The above looks elegant but I ran into problems with certain read operations (e.g. listing a directory) which is not easy for Resources and thus I shelved progress on this issue. Maybe Would be happy to hear your thoughts on this. |
This blog post gives a fairly reasonable explanation on how to do that (untested). I think it would be less evil to throw a kind of I'm starting to think that the best solution for this would be for java NIO to implement the jar file system but I'm not sure how likely that is to happen. Another solution might be to divide the operations based on what can be generalized to any stream, and what is specifically a file system operation. trait StreamOperations {
def contentAsString: String
def bytes: Iterator[Byte]
....
}
trait FileSystemOperations {
def delete(): Unit
def moveTo(dest: File): File
....
}
class File extends StreamOperations with FileSystemOperations
class Resource extends StreamOperations Then we don't have to worry about nasty implementations of file system operations which aren't even that useful for resources to begin with. This also can be reused for other streams in the future (STDIN, STDOUT). |
SGTM. As a first attempt, just add a new Resource.scala class. I can work on the refactor of common APIs into separate traits. |
Any progress on this? I would love to browse jar files this way... |
Is there a timeline for when this will be released? |
@lu4nm3 : This month |
No description provided.
The text was updated successfully, but these errors were encountered: