简体   繁体   English

如何使用来自 Ammonite 脚本的资源?

[英]How to use resources from an Ammonite script?

I have a few ammonite scripts, they are stored in a folder.我有一些菊石脚本,它们存储在一个文件夹中。 That folder is on my PATH, so I can invoke those scripts easily wherever I am.该文件夹位于我的 PATH 上,因此无论身在何处,我都可以轻松调用这些脚本。

I can import other ammonite scripts in the same folder:我可以在同一个文件夹中导入其他 ammonite 脚本:

$import $file.<name_of_the_other_script>

However I want to 'import' a simple text file, save it's content as a string and use it later.但是我想“导入”一个简单的文本文件,将其内容保存为字符串并稍后使用。

I can do:我可以:

val myString = os.read(os.root/"absolute"/"path"/"to"/"the"/"file")

But I would prefer not to use absolute paths, for obvious reasons.但出于显而易见的原因,我宁愿不使用绝对路径。 Relative path does not work, beacuse it is relative to the folder where I am invoking the script from, and not to where the script is located.相对路径不起作用,因为它是相对于我从中调用脚本的文件夹,而不是相对于脚本所在的文件夹。

Is there any way to achieve this?有什么办法可以做到这一点?

EDIT:编辑:

#/bin/bash

echo $BASH_SOURCE

The problem could be solved easily if the functionality in the above bash script could be replicated in Ammonite.如果上述 bash 脚本中的功能可以在 Ammonite 中复制,则问题可以轻松解决。

I think you can read resources using ammonite.ops like so:我认为您可以像这样使用ammonite.ops读取资源:

val resourcePath = resource/'test/'ammonite/'ops/'folder/"file.txt"
read(resourcePath).length        ==> 18
read.bytes(resourcePath).length  ==> 18
read.lines(resourcePath).length  ==> 1

See Reading Resources in the Ammonite documentation.请参阅 Ammonite 文档中的阅读资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM