简体   繁体   English

在 Ammonite (scala) 中重新导入脚本

[英]Re-import a script in Ammonite (scala)

I can import a script's methods/variables in Ammonite using import $file.MyScript, MyScript._ .我可以使用import $file.MyScript, MyScript._在 Ammonite 中导入脚本的方法/变量。 It works well the first time but what if I wish to import an updated version of MyScript.sc ?第一次运行良好,但如果我想导入更新版本的MyScript.sc怎么办?

Documentation suggests to do文档建议做

@ repl.sess.load()

Which initialises the REPL, allowing me to import the script again.它初始化 REPL,允许我再次导入脚本。 Sounds good, however, I then get a java.lang.NoClassDefFoundError when I import the script again -- see an example session below.听起来不错,但是,当我再次导入脚本时,我得到一个java.lang.NoClassDefFoundError - 请参阅下面的示例 session。

repl.sess.load() may not be the right functionality here, but then how can I re-import a script after updating it? repl.sess.load()在这里可能不是正确的功能,但是更新后如何重新导入脚本?

I know that there is a functionality to re-run a script after making changes using Ammonite script with a --watch argument, but that is not using the REPL, so it is not a good workaround.我知道有一个功能可以在使用带有--watch参数的 Ammonite 脚本进行更改后重新运行脚本,但这不是使用 REPL,所以这不是一个好的解决方法。

Example session示例 session

// As per docs: "If you want to re-load a script, you should use Ammonite's Save/Load Session functionality to 
// sess.save() the session before importing the script, and sess.load()ing to reset the script 
// before re-importing it."
@ repl.sess.save()


@ import $file.MyScript, MyScript._
Compiling /File/Path/MyScript.sc
import $file.$       , MyScript._

@ myWord 
res2: String = "mountain"

@ surroundWord(myWord) 
res3: String = "barwordfoo"

@ repl.sess.load() 
res4: ammonite.repl.api.SessionChanged = SessionChanged(
  HashSet(
    Symbol(notify),
    Symbol(prefixWord),
    Symbol(wait),
    Symbol(equals),
    Symbol(asInstanceOf),
    Symbol(synchronized),
    Symbol(notifyAll),
    Symbol(isInstanceOf),
    Symbol(!=),
    Symbol(res2),
    Symbol(ne),
    Symbol(hashCode),
    Symbol(surroundWord),
    Symbol(myWord),
    Symbol(res3),
    Symbol(==),
...

@ myWord 
cmd5.sc:1: not found: value myWord
val res5 = myWord
           ^
Compilation Failed

@ import $file.MyScript, MyScript._ 
import $file.$       , MyScript._

@ myWord 
java.lang.NoClassDefFoundError: ammonite/$sess/MyScript$
  ammonite.$sess.cmd6$.<clinit>(cmd6.sc:1)
java.lang.ClassNotFoundException: ammonite.$sess.MyScript$
  java.net.URLClassLoader.findClass(URLClassLoader.java:435)
  ammonite.runtime.SpecialClassLoader.findClass(ClassLoaders.scala:241)
  java.lang.ClassLoader.loadClass(ClassLoader.java:589)
  java.lang.ClassLoader.loadClass(ClassLoader.java:522)
  ammonite.$sess.cmd6$.<clinit>(cmd6.sc:1)

Think it's an issue, which I raised here .认为这是我在这里提出的问题。

I also understand that there's alternative functionality that almost achieves the same outcome and works well.我也知道有替代功能几乎可以达到相同的结果并且效果很好。 Use利用

amm --watch --predef MyScript.sc

This launches the Ammonite REPL, with the predef argument loading the script, and the watch argument reloading the script with any saved changes whenever I exit the REPL.这会启动 Ammonite REPL, predef参数会加载脚本,而watch参数会在我退出 REPL 时使用已保存的更改重新加载脚本。

So my typical workflow is所以我典型的工作流程是

  1. Start scripting a few methods in the file开始在文件中编写一些方法的脚本
  2. Launch the REPL and poke the code around to experiment with new stuff启动 REPL 并打开代码来试验新的东西
  3. Add that stuff to the file将这些东西添加到文件中
  4. Re-load the script to check to check that everything runs as expected重新加载脚本以检查一切是否按预期运行
  5. Go back to 2. Go 回到 2。

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

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