Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example export #9756

Merged
merged 12 commits into from
May 21, 2021
20 changes: 20 additions & 0 deletions daml-script/export/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ da_scala_binary(
"@maven//:org_scala_lang_modules_scala_collection_compat",
"@maven//:org_scalaz_scalaz_core",
"@maven//:org_typelevel_paiges_core",
"@maven//:io_circe_circe_core",
"@maven//:io_circe_circe_yaml",
],
visibility = ["//visibility:public"],
deps = [
Expand Down Expand Up @@ -143,12 +145,17 @@ client_server_build(
outs = [
"example-export/Export.daml",
"example-export/args.json",
"example-export/daml.yaml",
],
client = ":example-export-client",
client_files = ["//daml-script/test:script-test.dar"],
data = ["//daml-script/test:script-test.dar"],
output_env = "EXPORT_OUT",
server = "//ledger/sandbox:sandbox-binary",
server_args = [
"--port",
"0",
],
Comment on lines +163 to +166
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oversight in #9732. I forgot to configure dynamic port selection there.

server_files = ["//daml-script/test:script-test.dar"],
)

Expand All @@ -159,13 +166,18 @@ client_server_build(
# Normalizes the expected output by removing the copyright header and any
# documentation import markers and normalizes the actual output by adding a
# newline to the last line if missing.
#
# Normalizes the data-dependencies by replacing package-id hashes with a
# placeholder.
sh_inline_test(
name = "example-export-compare",
cmd = """\
EXPECTED_EXPORT=$$(canonicalize_rlocation $(rootpath //docs:source/tools/export/output-root/Export.daml))
EXPECTED_ARGS=$$(canonicalize_rlocation $(rootpath //docs:source/tools/export/output-root/args.json))
EXPECTED_YAML=$$(canonicalize_rlocation $(rootpath //docs:source/tools/export/output-root/daml.yaml))
ACTUAL_EXPORT=$$(canonicalize_rlocation $(rootpath :example-export/Export.daml))
ACTUAL_ARGS=$$(canonicalize_rlocation $(rootpath :example-export/args.json))
ACTUAL_YAML=$$(canonicalize_rlocation $(rootpath :example-export/daml.yaml))
# Normalize the expected file by removing the copyright header and any documentation import markers.
# Normalize the actual output by adding a newline to the last line if missing.
$(POSIX_DIFF) -Naur --strip-trailing-cr <($(POSIX_SED) '1,3d;/^-- EXPORT/d' $$EXPECTED_EXPORT) <($(POSIX_SED) '$$a\\' $$ACTUAL_EXPORT) || {
Expand All @@ -176,12 +188,20 @@ $(POSIX_DIFF) -Naur --strip-trailing-cr $$EXPECTED_ARGS <($(POSIX_SED) '$$a\\' $
echo "$$EXPECTED_ARGS did not match $$ACTUAL_ARGS"
exit 1
}
# Normalize the expected file by removing the copyright header and any documentation import markers.
# Normalize the data-dependencies by replacing package-id hashes with a placeholder.
$(POSIX_DIFF) -Naur --strip-trailing-cr <($(POSIX_SED) '1,3d;s/[0-9a-f]\\{64\\}/HASH/' $$EXPECTED_YAML) <($(POSIX_SED) 's/[0-9a-f]\\{64\\}/HASH/;$$a\\' $$ACTUAL_YAML) || {
echo "$$EXPECTED_YAML did not match $$ACTUAL_YAML"
exit 1
}
""",
data = [
":example-export/Export.daml",
":example-export/args.json",
":example-export/daml.yaml",
"//docs:source/tools/export/output-root/Export.daml",
"//docs:source/tools/export/output-root/args.json",
"//docs:source/tools/export/output-root/daml.yaml",
],
toolchains = ["@rules_sh//sh/posix:make_variables"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

package com.daml.script.export

import java.io.File
import java.io.{File, PrintWriter}
import java.nio.file.{Path, Paths}
import java.time.Duration

import com.daml.ledger.api.tls.TlsConfiguration
import com.daml.lf.engine.script.{RunnerConfig, RunnerMain}

import scala.io.Source

case class ExampleClientConfig(
darPath: File,
targetPort: Int,
Expand All @@ -32,20 +34,22 @@ object ExampleClientConfig {
): Either[String, ExampleClientConfig => ExampleClientConfig] = {
if (envVar.isEmpty) Left("Environment variable EXPORT_OUT must not be empty")
else
envVar.split(" ") match {
case Array(export_daml, args_json) =>
val export_daml_path = Paths.get(export_daml)
val args_json_path = Paths.get(args_json)
if (export_daml_path.getParent == null) {
envVar.split(" ").map(s => Paths.get(s)) match {
case Array(export_daml, args_json, daml_yaml) =>
if (export_daml.getParent == null) {
Left("First component in environment variable EXPORT_OUT has no parent")
} else if (export_daml_path.getParent != args_json_path.getParent) {
} else if (export_daml.getParent != args_json.getParent) {
Left(
"First and second component in environment variable EXPORT_OUT have different parent"
)
} else if (export_daml.getParent != daml_yaml.getParent) {
Left(
"First and third component in environment variable EXPORT_OUT have different parent"
)
} else {
Right(c => c.copy(outputPath = export_daml_path.getParent))
Right(c => c.copy(outputPath = export_daml.getParent))
}
case _ => Left("Environment variable EXPORT_OUT must contain one path")
case _ => Left("Environment variable EXPORT_OUT must contain three paths")
}
}

Expand Down Expand Up @@ -113,5 +117,20 @@ object ExampleClient {
),
)
)
normalizeDataDependencies(clientConfig.outputPath, clientConfig.outputPath.resolve("daml.yaml"))
}

private def normalizeDataDependencies(outputPath: Path, damlYaml: Path): Unit = {
val tmpFile = damlYaml.resolveSibling("daml.yaml.new").toFile
val w = new PrintWriter(tmpFile)
Source
.fromFile(damlYaml.toFile)
.getLines()
.map { x => x.replaceFirst(outputPath.toString, "EXPORT_OUT") }
.foreach(x => w.println(x))
w.close()
if (!tmpFile.renameTo(damlYaml.toFile)) {
throw new RuntimeException(s"Failed to normalize daml.yaml file: $damlYaml")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import scalaz.std.iterable._
import scalaz.std.set._
import scalaz.syntax.foldable._

import scala.collection.mutable

case class Export(
partyMap: Map[Party, String],
cidMap: Map[ContractId, String],
Expand Down Expand Up @@ -149,30 +151,59 @@ object Export {
val exposedPackages: Seq[String] =
pkgRefs.view.collect(Function.unlift(Dependencies.toPackages(_, pkgs))).toSeq
val deps = Files.createDirectory(dir.resolve("deps"))
val dalfFiles = pkgs.map { case (pkgId, (bs, pkg)) =>
val prefix = pkg.metadata.map(md => s"${md.name}-${md.version}-").getOrElse("")
val file = deps.resolve(s"$prefix$pkgId.dalf")
Dependencies.writeDalf(file, pkgId, bs)
file
}.toSeq
val dalfFiles = pkgs.toSeq
.sortBy { case (pkgId, (_, pkg)) =>
(pkg.metadata.map(md => (md.name, md.version)), pkgId)
}
.map { case (pkgId, (bs, pkg)) =>
val prefix = pkg.metadata.map(md => s"${md.name}-${md.version}-").getOrElse("")
val file = deps.resolve(s"$prefix$pkgId.dalf")
Dependencies.writeDalf(file, pkgId, bs)
file
}
val lfTarget = Dependencies.targetLfVersion(pkgs.values.map(_._2.languageVersion))
val targetFlag = lfTarget.fold("")(Dependencies.targetFlag(_))

val buildOptions = targetFlag +: exposedPackages.map(pkgId => s"--package=$pkgId")
val damlYaml = {
import io.circe.Json
import io.circe.yaml.Printer
val json = Json.fromFields(
mutable.LinkedHashMap(
"sdk-version" -> Json.fromString(sdkVersion),
"name" -> Json.fromString("export"),
"version" -> Json.fromString("1.0.0"),
"source" -> Json.fromString("."),
"init-script" -> Json.fromString("Export:export"),
"script-options" -> Json.fromValues(
List(
Json.fromString("--input-file"),
Json.fromString("args.json"),
)
),
"parties" -> Json.fromValues(
export.partyMap.keys.map(p => Json.fromString(Party.unwrap(p)))
),
"dependencies" -> Json.fromValues(
List(
Json.fromString("daml-stdlib"),
Json.fromString("daml-prim"),
Json.fromString(damlScriptLib),
)
),
"data-dependencies" -> Json.fromValues(dalfFiles.map(p => Json.fromString(p.toString))),
"build-options" -> Json.fromValues(buildOptions.map(Json.fromString)),
)
)
Printer(
indent = 4,
preserveOrder = true,
).pretty(json)
}

Files.write(
dir.resolve("daml.yaml"),
s"""sdk-version: $sdkVersion
|name: export
|version: 1.0.0
|source: .
|init-script: Export:export
|script-options: [--input-file, args.json]
|parties: [${export.partyMap.keys.mkString(",")}]
|dependencies: [daml-stdlib, daml-prim, $damlScriptLib]
|data-dependencies: [${dalfFiles.mkString(",")}]
|build-options: [${buildOptions.mkString(",")}]
|""".stripMargin.getBytes(StandardCharsets.UTF_8),
damlYaml.getBytes(StandardCharsets.UTF_8),
)
}
}
1 change: 1 addition & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports_files(
"scripts/check-closing-quotes.sh.allow",
"source/tools/export/output-root/Export.daml",
"source/tools/export/output-root/args.json",
"source/tools/export/output-root/daml.yaml",
],
)

Expand Down
42 changes: 36 additions & 6 deletions docs/source/tools/export/output-root/daml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,39 @@ name: export
version: 1.0.0
source: .
init-script: Export:export
script-options: [--input-file, args.json]
parties: [Bank,Alice,Bob]
dependencies: [daml-stdlib, daml-prim, daml-script]
data-dependencies: [EXPORT_OUT/deps/daml-stdlib-0.0.0-68a8ef91f10c283e1a5fc914bbf0addd41a089ad3625641966df2415e6010e2e.dalf,EXPORT_OUT/deps/script-test-0.0.1-87967f7d2b53db9652bd343a24db7cbe371c4b105005d57eed853bd309bac477.dalf,EXPORT_OUT/deps/c1f1f00558799eec139fb4f4c76f95fb52fa1837a5dd29600baa1c8ed1bdccfd.dalf,EXPORT_OUT/deps/733e38d36a2759688a4b2c4cec69d48e7b55ecc8dedc8067b815926c917a182a.dalf,EXPORT_OUT/deps/bfcd37bd6b84768e86e432f5f6c33e25d9e7724a9d42e33875ff74f6348e733f.dalf,EXPORT_OUT/deps/518032f41fd0175461b35ae0c9691e08b4aea55e62915f8360af2cc7a1f2ba6c.dalf,EXPORT_OUT/deps/cc348d369011362a5190fe96dd1f0dfbc697fdfd10e382b9e9666f0da05961b7.dalf,EXPORT_OUT/deps/6839a6d3d430c569b2425e9391717b44ca324b88ba621d597778811b2d05031d.dalf,EXPORT_OUT/deps/99a2705ed38c1c26cbb8fe7acf36bbf626668e167a33335de932599219e0a235.dalf,EXPORT_OUT/deps/76bf0fd12bd945762a01f8fc5bbcdfa4d0ff20f8762af490f8f41d6237c6524f.dalf,EXPORT_OUT/deps/e22bce619ae24ca3b8e6519281cb5a33b64b3190cc763248b4c3f9ad5087a92c.dalf,EXPORT_OUT/deps/d58cf9939847921b2aab78eaa7b427dc4c649d25e6bee3c749ace4c3f52f5c97.dalf,EXPORT_OUT/deps/6c2c0667393c5f92f1885163068cd31800d2264eb088eb6fc740e11241b2bf06.dalf,EXPORT_OUT/deps/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662.dalf,EXPORT_OUT/deps/057eed1fd48c238491b8ea06b9b5bf85a5d4c9275dd3f6183e0e6b01730cc2ba.dalf,EXPORT_OUT/deps/e491352788e56ca4603acc411ffe1a49fefd76ed8b163af86cf5ee5f4c38645b.dalf,EXPORT_OUT/deps/daml-prim-0.0.0-b76d13799ea5488f281440b63072ad3e3d48d72936144e7d19209c9cf115aa9d.dalf,EXPORT_OUT/deps/40f452260bef3f29dede136108fc08a88d5a5250310281067087da6f0baddff7.dalf,EXPORT_OUT/deps/daml-stdlib-DA-Set-Types-1.0.0-97b883cd8a2b7f49f90d5d39c981cf6e110cf1f1c64427a28a6d58ec88c43657.dalf,EXPORT_OUT/deps/daml-script-0.0.0-024f7a831e04595b36c668121dce9209d63e5a6a46f01a4533fca5e4c521e7e4.dalf,EXPORT_OUT/deps/8a7806365bbd98d88b4c13832ebfa305f6abaeaf32cfa2b7dd25c4fa489b79fb.dalf]
build-options: [--target=1.12,--package=script-test-0.0.1]

script-options:
- --input-file
- args.json
parties:
- Bank
- Alice
- Bob
dependencies:
- daml-stdlib
- daml-prim
- daml-script
data-dependencies:
- EXPORT_OUT/deps/057eed1fd48c238491b8ea06b9b5bf85a5d4c9275dd3f6183e0e6b01730cc2ba.dalf
- EXPORT_OUT/deps/40f452260bef3f29dede136108fc08a88d5a5250310281067087da6f0baddff7.dalf
- EXPORT_OUT/deps/518032f41fd0175461b35ae0c9691e08b4aea55e62915f8360af2cc7a1f2ba6c.dalf
- EXPORT_OUT/deps/6839a6d3d430c569b2425e9391717b44ca324b88ba621d597778811b2d05031d.dalf
- EXPORT_OUT/deps/6c2c0667393c5f92f1885163068cd31800d2264eb088eb6fc740e11241b2bf06.dalf
- EXPORT_OUT/deps/733e38d36a2759688a4b2c4cec69d48e7b55ecc8dedc8067b815926c917a182a.dalf
- EXPORT_OUT/deps/76bf0fd12bd945762a01f8fc5bbcdfa4d0ff20f8762af490f8f41d6237c6524f.dalf
- EXPORT_OUT/deps/8a7806365bbd98d88b4c13832ebfa305f6abaeaf32cfa2b7dd25c4fa489b79fb.dalf
- EXPORT_OUT/deps/99a2705ed38c1c26cbb8fe7acf36bbf626668e167a33335de932599219e0a235.dalf
- EXPORT_OUT/deps/bfcd37bd6b84768e86e432f5f6c33e25d9e7724a9d42e33875ff74f6348e733f.dalf
- EXPORT_OUT/deps/c1f1f00558799eec139fb4f4c76f95fb52fa1837a5dd29600baa1c8ed1bdccfd.dalf
- EXPORT_OUT/deps/cc348d369011362a5190fe96dd1f0dfbc697fdfd10e382b9e9666f0da05961b7.dalf
- EXPORT_OUT/deps/d14e08374fc7197d6a0de468c968ae8ba3aadbf9315476fd39071831f5923662.dalf
- EXPORT_OUT/deps/d58cf9939847921b2aab78eaa7b427dc4c649d25e6bee3c749ace4c3f52f5c97.dalf
- EXPORT_OUT/deps/e22bce619ae24ca3b8e6519281cb5a33b64b3190cc763248b4c3f9ad5087a92c.dalf
- EXPORT_OUT/deps/e491352788e56ca4603acc411ffe1a49fefd76ed8b163af86cf5ee5f4c38645b.dalf
- EXPORT_OUT/deps/daml-prim-0.0.0-b76d13799ea5488f281440b63072ad3e3d48d72936144e7d19209c9cf115aa9d.dalf
- EXPORT_OUT/deps/daml-script-0.0.0-024f7a831e04595b36c668121dce9209d63e5a6a46f01a4533fca5e4c521e7e4.dalf
- EXPORT_OUT/deps/daml-stdlib-0.0.0-68a8ef91f10c283e1a5fc914bbf0addd41a089ad3625641966df2415e6010e2e.dalf
- EXPORT_OUT/deps/daml-stdlib-DA-Set-Types-1.0.0-97b883cd8a2b7f49f90d5d39c981cf6e110cf1f1c64427a28a6d58ec88c43657.dalf
- EXPORT_OUT/deps/script-test-0.0.1-87967f7d2b53db9652bd343a24db7cbe371c4b105005d57eed853bd309bac477.dalf
build-options:
- --target=1.12
- --package=script-test-0.0.1