igor-petruk / scriptisto

A language-agnostic "shebang interpreter" that enables you to write scripts in compiled languages.
Apache License 2.0
909 stars 25 forks source link

Any way to get the original script's filename/location? #25

Closed carycodes closed 4 years ago

carycodes commented 4 years ago

For example, in Rust we can normally write std::file!() to get the source file path/name, but this will just return something like src/main.rs because of the way the build is done. It would be nice to have a way to get the actual name/location of the original script--this is an important part of several shell scripts that I'd like to eventually replace.

igor-petruk commented 4 years ago

Would you like to see this compile-time?

I don't think there is a way to override what std::file!() returns. If there was, I wonder if this would be dangerous and unexpected to set.

If this is not std::file!, then it has to be something else. This is either a function, and in that case we need to redistribute scriptisto-specific library. Or it is an env variable set at runtime or compile-time.

If I were to set $SCRIPTISTO_SOURCE available at compile-time and accessible via https://doc.rust-lang.org/std/macro.env.html, would that do?

igor-petruk commented 4 years ago

In addition, setting the variable like that at build time would work for all languages. I think I should be even able to pass it into the docker.

As for the runtime, I am sure every language has a way to detect a running binary

carycodes commented 4 years ago

If I were to set $SCRIPTISTO_SOURCE available at compile-time and accessible via https://doc.rust-lang.org/std/macro.env.html, would that do?

Yes, I think so. To give specific context, one example is a shell script that refers to other resources in the project. For usability reasons, it is desirable that the script can be called from the root of the project, or from directories under that. The script handles this currently by using directories relative to $0, rather than relative to pwd. Having the rust "script" use paths relative to env!("SCRIPTISTO_SOURCE") sounds perfect.

igor-petruk commented 4 years ago

The feature is available in 0.6.10.