Add features #362 #363 #367 #357 to run command (#368)

This commit is contained in:
Dima 2021-11-25 11:01:08 +03:00 committed by GitHub
parent 324bea5aec
commit 7ec3575a84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 125 additions and 35 deletions

View File

@ -1,6 +1,6 @@
package aqua.backend.air
import aqua.backend.{Backend, Generated}
import aqua.backend.{Backend, Generated, Version}
import aqua.model.transform.res.AquaRes
import cats.syntax.show.*
@ -9,7 +9,18 @@ object AirBackend extends Backend {
val ext = ".air"
override def generate(aqua: AquaRes): Seq[Generated] = {
val docs = s"""; This file is auto-generated. Do not edit manually: changes may be erased.
|; Generated by Aqua compiler: https://github.com/fluencelabs/aqua/.
|; If you find any bugs, please write an issue on GitHub: https://github.com/fluencelabs/aqua/issues
|; Aqua version: ${Version.version}
|
|""".stripMargin
aqua.funcs.toList
.map(fr => Generated("." + fr.funcName + ext, FuncAirGen(fr).generate.show))
.map{ fr =>
val airStr = docs + FuncAirGen(fr).generate.show
Generated("." + fr.funcName + ext, airStr)
}
}
}

View File

@ -12,7 +12,7 @@ case class OutputService(srv: ServiceRes, types: Types) {
import TypeScriptCommon.*
import types.*
val serviceTypes = types.serviceType(srv)
private val serviceTypes = types.serviceType(srv)
import serviceTypes.*
def generate: String =

View File

@ -1,7 +1,7 @@
package aqua.backend.js
import aqua.backend.ts.TypeScriptTypes
import aqua.backend.{Backend, EmptyTypes, Generated, Header, OutputFile, OutputFunc, OutputService}
import aqua.backend.*
import aqua.model.transform.res.AquaRes
case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
@ -14,9 +14,9 @@ case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
.map(s => TypeScriptTypes.serviceType(s))
.map(_.generate)
.toList
.mkString("\n\n")
.mkString("\n")
val functions =
res.funcs.map(f => TypeScriptTypes.funcType(f)).map(_.generate).toList.mkString("\n\n")
res.funcs.map(f => TypeScriptTypes.funcType(f)).map(_.generate).toList.mkString("\n")
val body = s"""${Header.header(false, false)}
|

View File

@ -1,12 +1,12 @@
package aqua.backend.ts
import aqua.backend.FuncTypes
import aqua.backend.ts.TypeScriptCommon.{fixupArgName, typeToTs, genTypeName}
import aqua.backend.ts.TypeScriptCommon.{fixupArgName, genTypeName, typeToTs}
import aqua.model.transform.res.FuncRes
import aqua.types.*
case class TSFuncTypes(func: FuncRes) extends FuncTypes {
import TypeScriptTypes._
import TypeScriptTypes.*
override val retTypeTs = func.returnType
.fold((None, "void")) { t => genTypeName(t, func.funcName.capitalize + "Result") }
@ -20,18 +20,24 @@ case class TSFuncTypes(func: FuncRes) extends FuncTypes {
(typeDesc, s"${typed(fixupArgName(arg.name), t)}")
} :+ (None, s"config$configType")
val args = argsTypescript.map(_._2)
val argsDesc = argsTypescript.map(_._1).flatten
val args = argsTypescript.map(a => " " + a._2)
val argsDesc = argsTypescript.flatMap(_._1)
// defines different types for overloaded service registration function.
var funcTypeOverload1 = args.mkString(", ")
var funcTypeOverload2 = (typed("peer", "FluencePeer") :: args).mkString(", ")
val funcTypeOverload1 = args.mkString(",\n")
val funcTypeOverload2 = ((" " + typed("peer", "FluencePeer")) :: args).mkString(",\n")
val (resTypeDesc, resType) = retTypeTs
s"""${argsDesc.mkString("\n")}
|${resTypeDesc.getOrElse("")}
|export function ${func.funcName}(${funcTypeOverload1}): ${generic("Promise", resType)};
|export function ${func.funcName}(${funcTypeOverload2}): ${generic("Promise", resType)};""".stripMargin
|export function ${func.funcName}(
|${funcTypeOverload1}
|): ${generic("Promise", resType)};
|
|export function ${func.funcName}(
|${funcTypeOverload2}
|): ${generic("Promise", resType)};
|""".stripMargin
}
}

View File

@ -9,7 +9,7 @@ case class TSServiceTypes(srv: ServiceRes) extends ServiceTypes {
private val serviceTypeName = s"${srv.name}Def";
def registerServiceArgs = {
private def registerServiceArgs = {
// defined arguments used in overloads below
val peerDecl = s"${typed("peer", "FluencePeer")}";
@ -45,8 +45,7 @@ case class TSServiceTypes(srv: ServiceRes) extends ServiceTypes {
registerServiceArgsSource.map { x =>
val args = x.mkString(", ")
s"export function register${srv.name}(${args}): void;"
}
.mkString("\n")
}.mkString("\n")
}
private def exportInterface = {

View File

@ -107,10 +107,13 @@ object NamesConfigJs {
}
}
type AvmLogLevel = "trace" | "debug" | "info" | "warn" | "error" | "off"
@JSExportAll
case class PeerConfig(
connectTo: String,
defaultTtlMs: Int
defaultTtlMs: Int,
avmLogLevel: AvmLogLevel
)
/**
@ -136,6 +139,16 @@ object V2 {
): js.Promise[js.Any] = js.native
}
type FluenceJSLogLevel = "trace" | "debug" | "info" | "warn" | "error" | "silent"
object FluenceUtils {
@js.native
@JSImport("@fluencelabs/fluence", "setLogLevel")
def setLogLevel(logLevel: FluenceJSLogLevel): Unit = js.native
}
/**
* Public interface to Fluence JS SDK
*/

View File

@ -44,12 +44,13 @@ object RunCommand extends Logging {
* @param air code to call
* @return
*/
def funcCall(multiaddr: String, air: String, functionDef: FunctionDef, timeout: Int, config: RunConfig)(implicit
def funcCall(multiaddr: String, air: String, functionDef: FunctionDef, config: RunConfig)(implicit
ec: ExecutionContext
): Future[Unit] = {
FluenceUtils.setLogLevel(Utils.logLevelToFluenceJS(config.logLevel))
(for {
_ <- Fluence
.start(PeerConfig(multiaddr, timeout))
.start(PeerConfig(multiaddr, config.timeout, Utils.logLevelToAvm(config.logLevel)))
.toFuture
peer = Fluence.getPeer()
_ = println("Your peerId: " + peer.getStatus().peerId)
@ -138,9 +139,8 @@ object RunCommand extends Logging {
args: List[LiteralModel],
input: Path,
imports: List[Path],
timeout: Int,
transformConfig: TransformConfig = TransformConfig(),
runConfig: RunConfig = RunConfig()
runConfig: RunConfig,
transformConfig: TransformConfig = TransformConfig()
)(implicit ec: ExecutionContext): F[Unit] = {
implicit val aio: AquaIO[IO] = new AquaFilesIO[IO]
@ -169,8 +169,12 @@ object RunCommand extends Logging {
val air = FuncAirGen(funcRes).generate.show
if (runConfig.printAir) {
println(air)
}
Async[F].fromFuture {
funcCall(multiaddr, air, definitions, timeout, runConfig).pure[F]
funcCall(multiaddr, air, definitions, runConfig).pure[F]
}.map { _ =>
Validated.validNec(())
}

View File

@ -0,0 +1,29 @@
package aqua
import scribe.Level
object Utils {
def logLevelToAvm(logLevel: Level): AvmLogLevel = {
logLevel match {
case Level.Trace => "trace"
case Level.Debug => "debug"
case Level.Info => "info"
case Level.Warn => "warn"
case Level.Error => "error"
case Level.Fatal => "off"
case _ => "info"
}
}
def logLevelToFluenceJS(logLevel: Level): FluenceJSLogLevel = {
logLevel match {
case Level.Trace => "trace"
case Level.Debug => "debug"
case Level.Info => "info"
case Level.Warn => "warn"
case Level.Error => "error"
case Level.Fatal => "silent"
case _ => "info"
}
}
}

View File

@ -18,8 +18,7 @@ object RunCommand {
args: List[LiteralModel],
input: Path,
imports: List[Path],
timeout: Int,
transformConfig: TransformConfig = TransformConfig(),
runConfig: RunConfig = RunConfig()
runConfig: RunConfig,
transformConfig: TransformConfig = TransformConfig()
)(implicit ec: ExecutionContext): F[Unit] = ???
}

View File

@ -1,7 +1,12 @@
package aqua.run
import scribe.Level
// `run` command configuration
case class RunConfig(
timeout: Int,
logLevel: Level,
printAir: Boolean,
consoleServiceId: String = "--after-callback-srv-service--",
printFunctionName: String = "print-and-stop",
resultName: String = "-some-unique-res-name-",

View File

@ -5,7 +5,7 @@ import aqua.parser.expr.func.CallArrowExpr
import aqua.parser.lexer.{Literal, VarLambda}
import aqua.parser.lift.LiftParser.Implicits.idLiftParser
import aqua.parser.lift.Span
import aqua.{AppOpts, AquaIO, RunCommand}
import aqua.{AppOpts, AquaIO, LogFormatter, RunCommand}
import cats.data.{NonEmptyList, Validated}
import cats.effect.kernel.Async
import cats.effect.{ExitCode, IO}
@ -16,10 +16,11 @@ import cats.syntax.functor.*
import cats.{Id, Monad, ~>}
import com.monovore.decline.{Command, Opts}
import fs2.io.file.Files
import scribe.Logging
import scala.concurrent.ExecutionContext
object RunOpts {
object RunOpts extends Logging {
val timeoutOpt: Opts[Int] =
Opts.option[Int]("timeout", "Request timeout in milliseconds", "t")
@ -39,6 +40,12 @@ object RunOpts {
}
}
val printAir: Opts[Boolean] =
Opts
.flag("print-air", "Prints generated AIR code before function execution")
.map(_ => true)
.withDefault(false)
val funcOpt: Opts[(String, List[LiteralModel])] =
Opts
.option[String]("func", "Function to call with args", "f")
@ -66,19 +73,36 @@ object RunOpts {
def runOptions[F[_]: Files: AquaIO: Async](implicit
ec: ExecutionContext
): Opts[F[cats.effect.ExitCode]] =
(AppOpts.inputOpts[F], AppOpts.importOpts[F], multiaddrOpt, funcOpt, timeoutOpt).mapN {
case (inputF, importF, multiaddr, (func, args), timeout) =>
(AppOpts.inputOpts[F], AppOpts.importOpts[F], multiaddrOpt, funcOpt, timeoutOpt, AppOpts.logLevelOpt, printAir).mapN {
case (inputF, importF, multiaddr, (func, args), timeout, logLevel, printAir) =>
scribe.Logger.root
.clearHandlers()
.clearModifiers()
.withHandler(formatter = LogFormatter.formatter, minimumLevel = Some(logLevel))
.replace()
for {
inputV <- inputF
impsV <- importF
result <- inputV.fold(
_ => cats.effect.ExitCode.Error.pure[F],
errs => {
Async[F].pure {
errs.map(logger.error)
cats.effect.ExitCode.Error
}
},
{ input =>
impsV.fold(
_ => cats.effect.ExitCode.Error.pure[F],
errs => {
Async[F].pure {
errs.map(logger.error)
cats.effect.ExitCode.Error
}
},
{ imps =>
RunCommand
.run(multiaddr, func, args, input, imps, timeout)
.run(multiaddr, func, args, input, imps, RunConfig(timeout, logLevel, printAir))
.map(_ => cats.effect.ExitCode.Success)
}
)