Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

How to get the main golo file's location? #496

Open
utybo opened this issue Aug 28, 2017 · 10 comments
Open

How to get the main golo file's location? #496

utybo opened this issue Aug 28, 2017 · 10 comments
Labels

Comments

@utybo
Copy link

utybo commented Aug 28, 2017

How can I get where the golo file containing the "main" function is? In Java, you can find where the JAR file is using one of the solutions posted here : https://stackoverflow.com/questions/15359702/get-location-of-jar-file but none of them work in Golo using golo golo ...

@yloiseau
Copy link
Contributor

Do you have a use case?

@utybo
Copy link
Author

utybo commented Aug 28, 2017

The use case here would be if one wants to distribute a free-form golo project that includes some resources like text files or images that need to be loaded at runtime. An example of a layout would be this :

myproject/
  main.golo
  resources/
    image.png
    data.json

There is no simple way to retrieve the full path of myproject. currentDir() isn't useful as the location of the main.golo file is not always the working directory (especially when using golosh when main.golo could be called from anywhere with an alias or as being in env)

@utybo
Copy link
Author

utybo commented Aug 28, 2017

The only workaround I can think of is using a script to launch the golo file that retrieves the path and passes it as an argument to the golo program, though this is far from straightforward and requires reliance on third-party tools as well as an additional argument to the program, which requires modifications to the main method/function.

@utybo
Copy link
Author

utybo commented Aug 28, 2017

One way to fix this would be to use, in the GoloClassLoader, the version of defineClass that supports the a custom ProtectionDomain that can thus encapsulate a correct CodeSource object that includes location information to the source of the golo file. Doing this would require creating a new method in GoloClassLoader that supports a URL instead of an InputStream or that has a URL as an additional argument.

@utybo
Copy link
Author

utybo commented Aug 28, 2017

I have implemented the mentioned fix in this commit : https://github.com/utybo/golo-lang/commit/49a9d89335a3b13007e3649d457b8146e4e91984

Should I open a pull request for this? @yloiseau

@utybo
Copy link
Author

utybo commented Aug 28, 2017

I also added support for golosh in https://github.com/utybo/golo-lang/commit/cc7f15b7a73a651748b80937ced577e53c8d24e8

So now both golo golo and golosh have support for CodeSource, which fully fixes this issue.

@yloiseau
Copy link
Contributor

For your resources use case, the classical approach is to create a jar and use the usual java methods to get the resources.
Btw, what third party dependency are you refering to? The golo command is already a shell script. Gradle for instance can generate the required script that pass the script directory as argument (which in my opinion is way more flexible)

@utybo
Copy link
Author

utybo commented Aug 28, 2017

Well I guess I didn't express myself clearly for the third party stuff, which meant "something outside of Golo" in that case.

I still do not think having to use a script is handy for this, but yes gradle could do that. I still think having the option to grab the location from inside the code is much easier and doesn't require setting up an entire gradle build or jar just for this reason. However, feel free to reject the PR and this suggestion if you do not feel like it is a worthy addition.

EDIT : Woops, looks as if I failed some of the requirements for contributing to an Eclipse project on the PR, I'll fix that ASAP if you are ok with the PR.

@yloiseau
Copy link
Contributor

I still don't see the point of your use case. Except for single-file scripts, you should distribute a jar bundling your resources, and thus use Class::getResource to retrieve them. Otherwise, when the resources are "external" to the application (e.g. a web application assets), their path should be a parameter of the application imo, not hard-coded nor deployment dependent.

However, since compiled class can give access to this information, why not...

@utybo
Copy link
Author

utybo commented Aug 29, 2017

Well, seeing that compiled classes and JARs give access to this data, I think it should be added to Golo for the sake of completeness.

(and yeah, my use case kind of sucks, I'll give you that, but I think giving the information inside Golo is as flexible as it gets... Oh well)

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

Successfully merging a pull request may close this issue.

2 participants