Update Scala Codegen SBT Example (#558)

* Update scala codegen sbt  example to use the latest codegen binaries

Use DAR to generate Scala code, no reason to unzip DALFs.

* Add support for `DA.sdkVersion` system property.

To override the default sdkVersion:
```
> sbt -DDA.sdkVersion=100.12.6 compile
```

* Simplify the way examples run
This commit is contained in:
Leonid Shlyapnikov 2019-04-18 09:49:53 -04:00 committed by mergify[bot]
parent 91843213bc
commit f39b7a5d3f
4 changed files with 15 additions and 22 deletions

View File

@ -1,5 +1,5 @@
# Mock scala/codegen example (does not send any commands just prints them to STD OUT)
$ sbt "mock-example/runMain com.digitalasset.example.ExampleMain"
$ sbt -DDA.sdkVersion=100.12.6 mock-example/run
# Sandbox scala/codegen example (sends commands to sandbox and receives transactions)
$ sbt "sandbox-example/runMain com.digitalasset.example.ExampleMain ./scala-codegen/target/repository/daml-codegen/Main.dar"
$ sbt -DDA.sdkVersion=100.12.6 sandbox-example/run

View File

@ -82,7 +82,7 @@ lazy val commonSettings = Seq(
lazy val sandboxDependencies = Seq(
"com.daml.scala" %% "bindings-akka" % sdkVersion,
"com.digitalasset.platform" % "sandbox" % sdkVersion,
"com.digitalasset.platform" %% "sandbox" % sdkVersion,
)
lazy val codeGenDependencies = Seq(
@ -145,23 +145,15 @@ def generateScalaFrom(
// directory containing the dar is used as a work directory
val outDir = darFile.getParentFile
// Unpack all dalf files in the dar. Note that there may be many ("fat dar"), but at least daml-prim.dalf
val dalfFiles = IO.unzip(darFile, outDir, new SimpleFilter(_ endsWith ".dalf"))
// use a FileFunction.cached on the dar
val cache = FileFunction.cached(cacheDir, FileInfo.hash) { theDalfs =>
val mainDalf = theDalfs
.find(_.getName endsWith mainDalfName)
.getOrElse(sys.error(s"Main dalf file ($mainDalfName) not found in $theDalfs"))
val otherDalfs = theDalfs.filterNot(_ == mainDalf).toList
val cache = FileFunction.cached(cacheDir, FileInfo.hash) { _ =>
if (srcManagedDir.exists) // purge output if exists
IO.delete(srcManagedDir.listFiles)
CodeGen.generateCode(mainDalf, otherDalfs.map(_.toURI.toURL), packageName, srcManagedDir, Novel)
CodeGen.generateCode(List(darFile), packageName, srcManagedDir, Novel)
(srcManagedDir ** "*.scala").get.toSet
}
cache(dalfFiles)
cache(Set(darFile))
}
// ##################################### end sbt-daml ##############################

View File

@ -2,10 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
object Versions {
lazy val sdkVersion: String = "100.11.25"
val sdkVersion: String = sdkVersionFromSysProps().getOrElse("100.12.6")
println(s"DA sdkVersion: $sdkVersion")
lazy val detectedOs: String = sys.props("os.name") match {
case "Mac OS X" => "osx"
case _ => "linux"
}
private def sdkVersionFromSysProps(): Option[String] =
sys.props.get("DA.sdkVersion").map(_.toString)
}

View File

@ -40,20 +40,16 @@ import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future}
object ExampleMain extends App {
if (args.length == 0)
throw new IllegalArgumentException(
"Expected at least one DAR or DALF file passed as an argument")
private val dar = new File("./scala-codegen/target/repository/daml-codegen/Main.dar")
private val ledgerId = "codegen-sbt-example-with-sandbox"
private val port: Int = findOpenPort().fold(e => throw new IllegalStateException(e), identity)
private def archives(fileNames: List[String]): List[File] =
toFiles(args.toList).fold(e => throw new IllegalStateException(e), identity)
private val serverConfig = SandboxConfig.default.copy(
port = port,
damlPackageContainer = DamlPackageContainer(archives(args.toList)),
damlPackageContainer = DamlPackageContainer(List(dar)),
timeProviderType = TimeProviderType.WallClock,
ledgerIdMode = LedgerIdMode.HardCoded(ledgerId),
)