From 3b22ec390fd2d4efed8ce17b9091bf61b3e16d45 Mon Sep 17 00:00:00 2001 From: Sergey Nazarov Date: Fri, 1 Dec 2023 11:26:15 +0400 Subject: [PATCH] Support imports in file compiler (#3921) --- .../com/wavesplatform/lang/FileCompiler.scala | 33 +++++++++++++++++++ .../com/wavesplatform/lang/FileCompiler.scala | 20 ----------- 2 files changed, 33 insertions(+), 20 deletions(-) create mode 100644 lang/jvm/src/main/scala/com/wavesplatform/lang/FileCompiler.scala delete mode 100644 lang/shared/src/main/scala/com/wavesplatform/lang/FileCompiler.scala diff --git a/lang/jvm/src/main/scala/com/wavesplatform/lang/FileCompiler.scala b/lang/jvm/src/main/scala/com/wavesplatform/lang/FileCompiler.scala new file mode 100644 index 0000000000..70528554f8 --- /dev/null +++ b/lang/jvm/src/main/scala/com/wavesplatform/lang/FileCompiler.scala @@ -0,0 +1,33 @@ +package com.wavesplatform.lang + +import com.google.common.base.Charsets +import com.google.common.io +import com.wavesplatform.lang.v1.estimator.v3.ScriptEstimatorV3 + +import java.io.File + +object FileCompiler extends App { + private val estimator = ScriptEstimatorV3.latest + + args + .foreach { path => + val scriptFile = new File(path).getAbsoluteFile + require(scriptFile.isFile, s"$path is not a file") + val baseDirectory = scriptFile.getParentFile + val imports = baseDirectory + .listFiles({ (pathname: File) => + pathname.isFile && pathname.getAbsoluteFile != scriptFile + }) + .map { f => + f.getName -> io.Files.asCharSource(f, Charsets.UTF_8).read() + } + .toMap + + API + .compile(io.Files.asCharSource(scriptFile, Charsets.UTF_8).read(), estimator, libraries = imports) + .fold( + error => throw new RuntimeException(s"$error while compiling $path"), + _ => println(s"successfully compiled $path") + ) + } +} diff --git a/lang/shared/src/main/scala/com/wavesplatform/lang/FileCompiler.scala b/lang/shared/src/main/scala/com/wavesplatform/lang/FileCompiler.scala deleted file mode 100644 index 7382776be5..0000000000 --- a/lang/shared/src/main/scala/com/wavesplatform/lang/FileCompiler.scala +++ /dev/null @@ -1,20 +0,0 @@ -package com.wavesplatform.lang - -import com.wavesplatform.lang.v1.estimator.v3.ScriptEstimatorV3 - -import java.nio.file.{Files, Paths} -import scala.jdk.CollectionConverters.* - -object FileCompiler extends App { - private val estimator = ScriptEstimatorV3.latest - args - .foreach { path => - val script = Files.readAllLines(Paths.get(path)).asScala.reduce(_ + "\n" + _) - API - .compile(script, estimator) - .fold( - error => throw new RuntimeException(s"$error while compiling $path"), - _ => println(s"successfully compiled $path") - ) - } -}