Skip to content

Commit

Permalink
feat!: DXJ-283 support new JS client (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst authored Feb 16, 2023
1 parent ed46d62 commit 94cbb85
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 63 deletions.
48 changes: 31 additions & 17 deletions backend/ts/src/main/scala/aqua/backend/Header.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@ package aqua.backend

object Header {

def header(isJs: Boolean, isCommonJS: Boolean): String = {
private def oldFluenceJsImports(isJs: Boolean) = {
val callParams =
if (isJs) ""
else
"import type { CallParams$$ } from '@fluencelabs/fluence/dist/internal/compilerSupport/v4'"
val imports = if (isCommonJS) {
"""const { FluencePeer } = require('@fluencelabs/fluence');
|const {
| callFunction$$,
| registerService$$,
|} = require('@fluencelabs/fluence/dist/internal/compilerSupport/v4${if (isJs) ".js" else ""}');""".stripMargin
} else {
s"""import { FluencePeer } from '@fluencelabs/fluence';
|$callParams
|import {
| callFunction$$$$,
| registerService$$$$,
|} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4${if (isJs) ".js"
else ""}';""".stripMargin
}

s"""import { FluencePeer } from '@fluencelabs/fluence';
|$callParams
|import {
| callFunction$$$$,
| registerService$$$$,
|} from '@fluencelabs/fluence/dist/internal/compilerSupport/v4${if (isJs) ".js"
else ""}';""".stripMargin
}

private def imports(isJs: Boolean) = {
val callParams =
if (isJs) ""
else
"import type { IFluenceClient as IFluenceClient$$, CallParams as CallParams$$ } from '@fluencelabs/js-client.api';"

s"""$callParams
|import {
| v5_callFunction as callFunction$$$$,
| v5_registerService as registerService$$$$,
|} from '@fluencelabs/js-client.api';
""".stripMargin
}

def header(isJs: Boolean, isOldFluenceJs: Boolean): String = {

val importsStr = if (isOldFluenceJs) oldFluenceJsImports(isJs)
else imports(isJs)

s"""/**
| *
| * This file is auto-generated. Do not edit manually: changes may be erased.
Expand All @@ -30,7 +44,7 @@ object Header {
| * Aqua version: ${Version.version}
| *
| */
|$imports
|$importsStr
|""".stripMargin
}
}
4 changes: 2 additions & 2 deletions backend/ts/src/main/scala/aqua/backend/OutputFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import aqua.res.AquaRes

case class OutputFile(res: AquaRes) {

def generate(types: Types, isJs: Boolean, isCommonJS: Boolean): (List[AirFunction], String) = {
def generate(types: Types, isJs: Boolean, isOldFluenceJs: Boolean): (List[AirFunction], String) = {
import types.*
val services = res.services
.map(s => OutputService(s, types))
Expand All @@ -22,7 +22,7 @@ case class OutputFile(res: AquaRes) {
airs,
s"""/* eslint-disable */
|// @ts-nocheck
|${Header.header(isJs, isCommonJS)}
|${Header.header(isJs, isOldFluenceJs)}
|
|// Services
|$services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import aqua.backend.ts.TypeScriptTypes
import aqua.backend.*
import aqua.res.AquaRes

case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
case class JavaScriptBackend(isOldFluenceJs: Boolean, client: String) extends Backend {

val ext = ".js"
val tsExt = ".d.ts"
val types = TypeScriptTypes(client)

def typesFile(res: AquaRes): Generated = {
val services = res.services
.map(s => TypeScriptTypes.serviceType(s))
.map(s => types.serviceType(s))
.map(_.generate)
.toList
.mkString("\n")
val functions =
res.funcs.map(f => TypeScriptTypes.funcType(f)).map(_.generate).toList.mkString("\n")
res.funcs.map(f => types.funcType(f)).map(_.generate).toList.mkString("\n")

val body = s"""/* eslint-disable */
|// @ts-nocheck
|${Header.header(true, false)}
|${Header.header(true, isOldFluenceJs)}
|
|// Services
|$services
Expand All @@ -36,7 +37,7 @@ case class JavaScriptBackend(isCommonJS: Boolean) extends Backend {
override def generate(res: AquaRes): Seq[Generated] =
if (res.isEmpty) Nil
else {
val (airs, script) = OutputFile(res).generate(EmptyTypes, true, isCommonJS)
val (airs, script) = OutputFile(res).generate(EmptyTypes, true, isOldFluenceJs)
Generated(ext, script, airs) :: typesFile(res) :: Nil
}
}
7 changes: 4 additions & 3 deletions backend/ts/src/main/scala/aqua/backend/ts/TSFuncTypes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import aqua.backend.ts.TypeScriptCommon.{fixupArgName, genTypeName, typeToTs}
import aqua.res.FuncRes
import aqua.types.*

case class TSFuncTypes(func: FuncRes) extends FuncTypes {
import TypeScriptTypes.*
case class TSFuncTypes(func: FuncRes, client: String) extends FuncTypes {
val types = TypeScriptTypes(client)
import types.*

override val retTypeTs =
genTypeName(func.returnType, func.funcName.capitalize + "Result")
Expand All @@ -25,7 +26,7 @@ case class TSFuncTypes(func: FuncRes) extends FuncTypes {

// defines different types for overloaded service registration function.
val funcTypeOverload1 = args.mkString(",\n")
val funcTypeOverload2 = ((" " + typed("peer", "FluencePeer")) :: args).mkString(",\n")
val funcTypeOverload2 = ((" " + typed("peer", client)) :: args).mkString(",\n")

val (resTypeDesc, resType) = retTypeTs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import aqua.backend.ServiceTypes
import aqua.backend.ts.TypeScriptCommon.fnDef
import aqua.res.ServiceRes

case class TSServiceTypes(srv: ServiceRes) extends ServiceTypes {
case class TSServiceTypes(srv: ServiceRes, client: String) extends ServiceTypes {

import TypeScriptTypes.*
val types = TypeScriptTypes(client)
import types.*

private val serviceTypeName = s"${srv.name}Def";

private def registerServiceArgs = {

// defined arguments used in overloads below
val peerDecl = s"${typed("peer", "FluencePeer")}";
val peerDecl = s"${typed("peer", client)}";
val serviceIdDecl = s"${typed("serviceId", "string")}";
val serviceDecl = s"${typed("service", serviceTypeName)}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import aqua.backend.{Backend, Generated, OutputFile}
import aqua.res.AquaRes
import cats.data.NonEmptyChain

object TypeScriptBackend extends Backend {
case class TypeScriptBackend(isOldFluenceJs: Boolean, client: String) extends Backend {

val ext = ".ts"

override def generate(res: AquaRes): Seq[Generated] =
if (res.isEmpty) Nil
else {
val (airs, script) = OutputFile(res).generate(TypeScriptTypes, false, false)
val (airs, script) = OutputFile(res).generate(TypeScriptTypes(client), false, isOldFluenceJs)
Generated(ext, script, airs) :: Nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package aqua.backend.ts
import aqua.backend.{FuncTypes, ServiceTypes, Types}
import aqua.res.{FuncRes, ServiceRes}

object TypeScriptTypes extends Types {
case class TypeScriptTypes(client: String) extends Types {
override def typed(field: String, t: String): String = s"$field: $t"
override def generic(field: String, t: String): String = s"$field<$t>"
override def bang(field: String): String = s"$field!"
def funcType(f: FuncRes): FuncTypes = TSFuncTypes(f)
def serviceType(s: ServiceRes): ServiceTypes = TSServiceTypes(s)
def funcType(f: FuncRes): FuncTypes = TSFuncTypes(f, client)
def serviceType(s: ServiceRes): ServiceTypes = TSServiceTypes(s, client)
}

This file was deleted.

2 changes: 1 addition & 1 deletion cli/cli-npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"scripts": {
"run": "node index.js",
"from:scalajs": "cp ../cli/.js/target/scala-3.1.3/cli-opt/main.js ./aqua.js && npm run build && npm run run -- $@",
"from:scalajs": "cp ../cli/.js/target/scala-3.2.2/cli-opt/main.js ./aqua.js && npm run build && npm run run -- $@",
"build": "tsc"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion cli/cli/.jvm/src/main/scala/aqua/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Test extends IOApp.Simple {
Path("./aqua-src/antithesis.aqua"),
List(Path("./aqua")),
Option(Path("./target")),
TypeScriptBackend,
TypeScriptBackend(false),
TransformConfig(wrapWithXor = false),
false
)
Expand Down
2 changes: 1 addition & 1 deletion cli/cli/.jvm/src/test/scala/WriteFileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WriteFileSpec extends AnyFlatSpec with Matchers {

val bc = TransformConfig()
AquaPathCompiler
.compileFilesTo[IO](src, List.empty, Option(targetTs), TypeScriptBackend, bc, false)
.compileFilesTo[IO](src, List.empty, Option(targetTs), TypeScriptBackend(false), bc, false)
.unsafeRunSync()
.leftMap { err =>
println(err)
Expand Down
28 changes: 26 additions & 2 deletions cli/cli/src/main/scala/aqua/AppOpts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import aqua.raw.value.LiteralRaw
import aqua.constants.Constants
import cats.data.Validated.{Invalid, Valid}
import cats.data.{NonEmptyList, Validated, ValidatedNec, ValidatedNel}
import cats.data.Validated.{invalidNel, validNel}
import cats.effect.kernel.Async
import cats.effect.std.Console
import cats.effect.{ExitCode, IO}
Expand All @@ -19,7 +20,7 @@ import cats.syntax.functor.*
import cats.syntax.traverse.*
import cats.{~>, Comonad, Functor, Monad}
import com.monovore.decline.Opts.help
import com.monovore.decline.{Opts, Visibility}
import com.monovore.decline.{Argument, Opts, Visibility}
import fs2.io.file.{Files, Path}
import scribe.Level

Expand Down Expand Up @@ -63,7 +64,12 @@ object AppOpts {

def outputOpts[F[_]: Monad: Files]: Opts[F[ValidatedNec[String, Option[Path]]]] =
Opts
.option[String]("output", "Path to the output directory. Will be created if it doesn't exists", "o", "path")
.option[String](
"output",
"Path to the output directory. Will be created if it doesn't exists",
"o",
"path"
)
.map(s => Option(s))
.withDefault(None)
.map(_.map(checkOutput[F]).getOrElse(Validated.validNec[String, Option[Path]](None).pure[F]))
Expand Down Expand Up @@ -138,6 +144,24 @@ object AppOpts {
.map(_ => true)
.withDefault(false)

val isOldFluenceJs: Opts[Boolean] =
Opts
.flagOption[String](
"old-fluence-js",
"Generate TypeScript or JavaScript files for new JS Client"
)
.mapValidated {
case Some(str) =>
str.toLowerCase match {
case "true" => validNel(true)
case "false" => validNel(false)
case s => invalidNel(s"'$s' must be 'true' or 'false'")
}
case None => validNel(true)

}
.withDefault(false)

val dryOpt: Opts[Boolean] =
Opts
.flag("dry", "Checks if compilation is succeeded, without output")
Expand Down
11 changes: 7 additions & 4 deletions cli/cli/src/main/scala/aqua/AquaCli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ object AquaCli extends IOApp with Logging {
case object JavaScriptTarget extends CompileTarget
case object AirTarget extends CompileTarget

def targetToBackend(target: CompileTarget): Backend = {
def targetToBackend(target: CompileTarget, isOldFluenceJs: Boolean): Backend = {
val client = if (isOldFluenceJs) "FluencePeer" else "IFluenceClient$$"
target match {
case TypescriptTarget =>
TypeScriptBackend
TypeScriptBackend(isOldFluenceJs, client)
case JavaScriptTarget =>
JavaScriptBackend(false)
JavaScriptBackend(isOldFluenceJs, client)
case AirTarget =>
AirBackend
}
Expand Down Expand Up @@ -94,6 +95,7 @@ object AquaCli extends IOApp with Logging {
compileToJs,
noRelay,
noXorWrapper,
isOldFluenceJs,
wrapWithOption(helpOpt),
wrapWithOption(versionOpt),
FluenceOpts.logLevelOpt,
Expand All @@ -110,6 +112,7 @@ object AquaCli extends IOApp with Logging {
toJs,
noRelayOp,
noXorOp,
isOldFluenceJsOp,
h,
v,
logLevel,
Expand Down Expand Up @@ -158,7 +161,7 @@ object AquaCli extends IOApp with Logging {
input,
imports,
resultOutput,
targetToBackend(target),
targetToBackend(target, isOldFluenceJsOp),
bc,
disableAirValidation
)
Expand Down

0 comments on commit 94cbb85

Please sign in to comment.