mirror of
https://github.com/fluencelabs/aqua.git
synced 2025-04-24 22:42:13 +00:00
parent
324bea5aec
commit
7ec3575a84
@ -1,6 +1,6 @@
|
|||||||
package aqua.backend.air
|
package aqua.backend.air
|
||||||
|
|
||||||
import aqua.backend.{Backend, Generated}
|
import aqua.backend.{Backend, Generated, Version}
|
||||||
import aqua.model.transform.res.AquaRes
|
import aqua.model.transform.res.AquaRes
|
||||||
import cats.syntax.show.*
|
import cats.syntax.show.*
|
||||||
|
|
||||||
@ -9,7 +9,18 @@ object AirBackend extends Backend {
|
|||||||
val ext = ".air"
|
val ext = ".air"
|
||||||
|
|
||||||
override def generate(aqua: AquaRes): Seq[Generated] = {
|
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
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ case class OutputService(srv: ServiceRes, types: Types) {
|
|||||||
|
|
||||||
import TypeScriptCommon.*
|
import TypeScriptCommon.*
|
||||||
import types.*
|
import types.*
|
||||||
val serviceTypes = types.serviceType(srv)
|
private val serviceTypes = types.serviceType(srv)
|
||||||
import serviceTypes.*
|
import serviceTypes.*
|
||||||
|
|
||||||
def generate: String =
|
def generate: String =
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package aqua.backend.js
|
package aqua.backend.js
|
||||||
|
|
||||||
import aqua.backend.ts.TypeScriptTypes
|
import aqua.backend.ts.TypeScriptTypes
|
||||||
import aqua.backend.{Backend, EmptyTypes, Generated, Header, OutputFile, OutputFunc, OutputService}
|
import aqua.backend.*
|
||||||
import aqua.model.transform.res.AquaRes
|
import aqua.model.transform.res.AquaRes
|
||||||
|
|
||||||
case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
|
case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
|
||||||
@ -14,9 +14,9 @@ case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
|
|||||||
.map(s => TypeScriptTypes.serviceType(s))
|
.map(s => TypeScriptTypes.serviceType(s))
|
||||||
.map(_.generate)
|
.map(_.generate)
|
||||||
.toList
|
.toList
|
||||||
.mkString("\n\n")
|
.mkString("\n")
|
||||||
val functions =
|
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)}
|
val body = s"""${Header.header(false, false)}
|
||||||
|
|
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
package aqua.backend.ts
|
package aqua.backend.ts
|
||||||
|
|
||||||
import aqua.backend.FuncTypes
|
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.model.transform.res.FuncRes
|
||||||
import aqua.types.*
|
import aqua.types.*
|
||||||
|
|
||||||
case class TSFuncTypes(func: FuncRes) extends FuncTypes {
|
case class TSFuncTypes(func: FuncRes) extends FuncTypes {
|
||||||
import TypeScriptTypes._
|
import TypeScriptTypes.*
|
||||||
|
|
||||||
override val retTypeTs = func.returnType
|
override val retTypeTs = func.returnType
|
||||||
.fold((None, "void")) { t => genTypeName(t, func.funcName.capitalize + "Result") }
|
.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)}")
|
(typeDesc, s"${typed(fixupArgName(arg.name), t)}")
|
||||||
} :+ (None, s"config$configType")
|
} :+ (None, s"config$configType")
|
||||||
|
|
||||||
val args = argsTypescript.map(_._2)
|
val args = argsTypescript.map(a => " " + a._2)
|
||||||
val argsDesc = argsTypescript.map(_._1).flatten
|
val argsDesc = argsTypescript.flatMap(_._1)
|
||||||
|
|
||||||
// defines different types for overloaded service registration function.
|
// defines different types for overloaded service registration function.
|
||||||
var funcTypeOverload1 = args.mkString(", ")
|
val funcTypeOverload1 = args.mkString(",\n")
|
||||||
var funcTypeOverload2 = (typed("peer", "FluencePeer") :: args).mkString(", ")
|
val funcTypeOverload2 = ((" " + typed("peer", "FluencePeer")) :: args).mkString(",\n")
|
||||||
|
|
||||||
val (resTypeDesc, resType) = retTypeTs
|
val (resTypeDesc, resType) = retTypeTs
|
||||||
|
|
||||||
s"""${argsDesc.mkString("\n")}
|
s"""${argsDesc.mkString("\n")}
|
||||||
|${resTypeDesc.getOrElse("")}
|
|${resTypeDesc.getOrElse("")}
|
||||||
|export function ${func.funcName}(${funcTypeOverload1}): ${generic("Promise", resType)};
|
|export function ${func.funcName}(
|
||||||
|export function ${func.funcName}(${funcTypeOverload2}): ${generic("Promise", resType)};""".stripMargin
|
|${funcTypeOverload1}
|
||||||
|
|): ${generic("Promise", resType)};
|
||||||
|
|
|
||||||
|
|export function ${func.funcName}(
|
||||||
|
|${funcTypeOverload2}
|
||||||
|
|): ${generic("Promise", resType)};
|
||||||
|
|""".stripMargin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ case class TSServiceTypes(srv: ServiceRes) extends ServiceTypes {
|
|||||||
|
|
||||||
private val serviceTypeName = s"${srv.name}Def";
|
private val serviceTypeName = s"${srv.name}Def";
|
||||||
|
|
||||||
def registerServiceArgs = {
|
private def registerServiceArgs = {
|
||||||
|
|
||||||
// defined arguments used in overloads below
|
// defined arguments used in overloads below
|
||||||
val peerDecl = s"${typed("peer", "FluencePeer")}";
|
val peerDecl = s"${typed("peer", "FluencePeer")}";
|
||||||
@ -45,8 +45,7 @@ case class TSServiceTypes(srv: ServiceRes) extends ServiceTypes {
|
|||||||
registerServiceArgsSource.map { x =>
|
registerServiceArgsSource.map { x =>
|
||||||
val args = x.mkString(", ")
|
val args = x.mkString(", ")
|
||||||
s"export function register${srv.name}(${args}): void;"
|
s"export function register${srv.name}(${args}): void;"
|
||||||
}
|
}.mkString("\n")
|
||||||
.mkString("\n")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private def exportInterface = {
|
private def exportInterface = {
|
||||||
|
@ -107,10 +107,13 @@ object NamesConfigJs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AvmLogLevel = "trace" | "debug" | "info" | "warn" | "error" | "off"
|
||||||
|
|
||||||
@JSExportAll
|
@JSExportAll
|
||||||
case class PeerConfig(
|
case class PeerConfig(
|
||||||
connectTo: String,
|
connectTo: String,
|
||||||
defaultTtlMs: Int
|
defaultTtlMs: Int,
|
||||||
|
avmLogLevel: AvmLogLevel
|
||||||
)
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -136,6 +139,16 @@ object V2 {
|
|||||||
): js.Promise[js.Any] = js.native
|
): 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
|
* Public interface to Fluence JS SDK
|
||||||
*/
|
*/
|
||||||
|
@ -44,12 +44,13 @@ object RunCommand extends Logging {
|
|||||||
* @param air code to call
|
* @param air code to call
|
||||||
* @return
|
* @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
|
ec: ExecutionContext
|
||||||
): Future[Unit] = {
|
): Future[Unit] = {
|
||||||
|
FluenceUtils.setLogLevel(Utils.logLevelToFluenceJS(config.logLevel))
|
||||||
(for {
|
(for {
|
||||||
_ <- Fluence
|
_ <- Fluence
|
||||||
.start(PeerConfig(multiaddr, timeout))
|
.start(PeerConfig(multiaddr, config.timeout, Utils.logLevelToAvm(config.logLevel)))
|
||||||
.toFuture
|
.toFuture
|
||||||
peer = Fluence.getPeer()
|
peer = Fluence.getPeer()
|
||||||
_ = println("Your peerId: " + peer.getStatus().peerId)
|
_ = println("Your peerId: " + peer.getStatus().peerId)
|
||||||
@ -138,9 +139,8 @@ object RunCommand extends Logging {
|
|||||||
args: List[LiteralModel],
|
args: List[LiteralModel],
|
||||||
input: Path,
|
input: Path,
|
||||||
imports: List[Path],
|
imports: List[Path],
|
||||||
timeout: Int,
|
runConfig: RunConfig,
|
||||||
transformConfig: TransformConfig = TransformConfig(),
|
transformConfig: TransformConfig = TransformConfig()
|
||||||
runConfig: RunConfig = RunConfig()
|
|
||||||
)(implicit ec: ExecutionContext): F[Unit] = {
|
)(implicit ec: ExecutionContext): F[Unit] = {
|
||||||
implicit val aio: AquaIO[IO] = new AquaFilesIO[IO]
|
implicit val aio: AquaIO[IO] = new AquaFilesIO[IO]
|
||||||
|
|
||||||
@ -169,8 +169,12 @@ object RunCommand extends Logging {
|
|||||||
|
|
||||||
val air = FuncAirGen(funcRes).generate.show
|
val air = FuncAirGen(funcRes).generate.show
|
||||||
|
|
||||||
|
if (runConfig.printAir) {
|
||||||
|
println(air)
|
||||||
|
}
|
||||||
|
|
||||||
Async[F].fromFuture {
|
Async[F].fromFuture {
|
||||||
funcCall(multiaddr, air, definitions, timeout, runConfig).pure[F]
|
funcCall(multiaddr, air, definitions, runConfig).pure[F]
|
||||||
}.map { _ =>
|
}.map { _ =>
|
||||||
Validated.validNec(())
|
Validated.validNec(())
|
||||||
}
|
}
|
||||||
|
29
cli/.js/src/main/scala/aqua/Utils.scala
Normal file
29
cli/.js/src/main/scala/aqua/Utils.scala
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,8 +18,7 @@ object RunCommand {
|
|||||||
args: List[LiteralModel],
|
args: List[LiteralModel],
|
||||||
input: Path,
|
input: Path,
|
||||||
imports: List[Path],
|
imports: List[Path],
|
||||||
timeout: Int,
|
runConfig: RunConfig,
|
||||||
transformConfig: TransformConfig = TransformConfig(),
|
transformConfig: TransformConfig = TransformConfig()
|
||||||
runConfig: RunConfig = RunConfig()
|
|
||||||
)(implicit ec: ExecutionContext): F[Unit] = ???
|
)(implicit ec: ExecutionContext): F[Unit] = ???
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package aqua.run
|
package aqua.run
|
||||||
|
|
||||||
|
import scribe.Level
|
||||||
|
|
||||||
// `run` command configuration
|
// `run` command configuration
|
||||||
case class RunConfig(
|
case class RunConfig(
|
||||||
|
timeout: Int,
|
||||||
|
logLevel: Level,
|
||||||
|
printAir: Boolean,
|
||||||
consoleServiceId: String = "--after-callback-srv-service--",
|
consoleServiceId: String = "--after-callback-srv-service--",
|
||||||
printFunctionName: String = "print-and-stop",
|
printFunctionName: String = "print-and-stop",
|
||||||
resultName: String = "-some-unique-res-name-",
|
resultName: String = "-some-unique-res-name-",
|
||||||
|
@ -5,7 +5,7 @@ import aqua.parser.expr.func.CallArrowExpr
|
|||||||
import aqua.parser.lexer.{Literal, VarLambda}
|
import aqua.parser.lexer.{Literal, VarLambda}
|
||||||
import aqua.parser.lift.LiftParser.Implicits.idLiftParser
|
import aqua.parser.lift.LiftParser.Implicits.idLiftParser
|
||||||
import aqua.parser.lift.Span
|
import aqua.parser.lift.Span
|
||||||
import aqua.{AppOpts, AquaIO, RunCommand}
|
import aqua.{AppOpts, AquaIO, LogFormatter, RunCommand}
|
||||||
import cats.data.{NonEmptyList, Validated}
|
import cats.data.{NonEmptyList, Validated}
|
||||||
import cats.effect.kernel.Async
|
import cats.effect.kernel.Async
|
||||||
import cats.effect.{ExitCode, IO}
|
import cats.effect.{ExitCode, IO}
|
||||||
@ -16,10 +16,11 @@ import cats.syntax.functor.*
|
|||||||
import cats.{Id, Monad, ~>}
|
import cats.{Id, Monad, ~>}
|
||||||
import com.monovore.decline.{Command, Opts}
|
import com.monovore.decline.{Command, Opts}
|
||||||
import fs2.io.file.Files
|
import fs2.io.file.Files
|
||||||
|
import scribe.Logging
|
||||||
|
|
||||||
import scala.concurrent.ExecutionContext
|
import scala.concurrent.ExecutionContext
|
||||||
|
|
||||||
object RunOpts {
|
object RunOpts extends Logging {
|
||||||
|
|
||||||
val timeoutOpt: Opts[Int] =
|
val timeoutOpt: Opts[Int] =
|
||||||
Opts.option[Int]("timeout", "Request timeout in milliseconds", "t")
|
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])] =
|
val funcOpt: Opts[(String, List[LiteralModel])] =
|
||||||
Opts
|
Opts
|
||||||
.option[String]("func", "Function to call with args", "f")
|
.option[String]("func", "Function to call with args", "f")
|
||||||
@ -66,19 +73,36 @@ object RunOpts {
|
|||||||
def runOptions[F[_]: Files: AquaIO: Async](implicit
|
def runOptions[F[_]: Files: AquaIO: Async](implicit
|
||||||
ec: ExecutionContext
|
ec: ExecutionContext
|
||||||
): Opts[F[cats.effect.ExitCode]] =
|
): Opts[F[cats.effect.ExitCode]] =
|
||||||
(AppOpts.inputOpts[F], AppOpts.importOpts[F], multiaddrOpt, funcOpt, timeoutOpt).mapN {
|
(AppOpts.inputOpts[F], AppOpts.importOpts[F], multiaddrOpt, funcOpt, timeoutOpt, AppOpts.logLevelOpt, printAir).mapN {
|
||||||
case (inputF, importF, multiaddr, (func, args), timeout) =>
|
case (inputF, importF, multiaddr, (func, args), timeout, logLevel, printAir) =>
|
||||||
|
|
||||||
|
scribe.Logger.root
|
||||||
|
.clearHandlers()
|
||||||
|
.clearModifiers()
|
||||||
|
.withHandler(formatter = LogFormatter.formatter, minimumLevel = Some(logLevel))
|
||||||
|
.replace()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
inputV <- inputF
|
inputV <- inputF
|
||||||
impsV <- importF
|
impsV <- importF
|
||||||
result <- inputV.fold(
|
result <- inputV.fold(
|
||||||
_ => cats.effect.ExitCode.Error.pure[F],
|
errs => {
|
||||||
|
Async[F].pure {
|
||||||
|
errs.map(logger.error)
|
||||||
|
cats.effect.ExitCode.Error
|
||||||
|
}
|
||||||
|
},
|
||||||
{ input =>
|
{ input =>
|
||||||
impsV.fold(
|
impsV.fold(
|
||||||
_ => cats.effect.ExitCode.Error.pure[F],
|
errs => {
|
||||||
|
Async[F].pure {
|
||||||
|
errs.map(logger.error)
|
||||||
|
cats.effect.ExitCode.Error
|
||||||
|
}
|
||||||
|
},
|
||||||
{ imps =>
|
{ imps =>
|
||||||
RunCommand
|
RunCommand
|
||||||
.run(multiaddr, func, args, input, imps, timeout)
|
.run(multiaddr, func, args, input, imps, RunConfig(timeout, logLevel, printAir))
|
||||||
.map(_ => cats.effect.ExitCode.Success)
|
.map(_ => cats.effect.ExitCode.Success)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user