Skip to content

Commit

Permalink
Integrate daml2ts into the assistant (digital-asset#4252)
Browse files Browse the repository at this point in the history
changelog_begin
- Generate TypeScript bindings to DAML with `daml codegen ts DAR -o OUTDIR`
changelog_end
  • Loading branch information
shayne-fletcher authored and mergify[bot] committed Jan 28, 2020
1 parent a8343e6 commit 0e53ec0
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 26 deletions.
75 changes: 53 additions & 22 deletions daml-assistant/daml-helper/src/DA/Daml/Helper/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ data Command
| LedgerAllocateParties { flags :: LedgerFlags, parties :: [String] }
| LedgerUploadDar { flags :: LedgerFlags, darPathM :: Maybe FilePath }
| LedgerNavigator { flags :: LedgerFlags, remainingArguments :: [String] }
| Codegen { lang :: Lang, remainingArguments :: [String] }

data Lang = Java | Scala | TypeScript

commandParser :: Parser Command
commandParser = subparser $ fold
Expand All @@ -64,6 +67,7 @@ commandParser = subparser $ fold
, command "deploy" (info (deployCmd <**> helper) deployCmdInfo)
, command "ledger" (info (ledgerCmd <**> helper) ledgerCmdInfo)
, command "run-jar" (info runJarCmd forwardOptions)
, command "codegen" (info (codegenCmd <**> helper) forwardOptions)
]
where

Expand Down Expand Up @@ -147,6 +151,18 @@ commandParser = subparser $ fold
(Right . Just . JsonApiPort)
(readMaybe s)

codegenCmd = asum
[ subparser $ fold
[ command "java" $ info codegenJavaCmd forwardOptions
, command "scala" $ info codegenScalaCmd forwardOptions
, command "ts" $ info codegenTypeScriptCmd forwardOptions
]
]

codegenJavaCmd = Codegen Java <$> many (argument str (metavar "ARG"))
codegenScalaCmd = Codegen Scala <$> many (argument str (metavar "ARG"))
codegenTypeScriptCmd = Codegen TypeScript <$> many (argument str (metavar "ARG"))

ledgerCmdInfo = mconcat
[ forwardOptions
, progDesc $ concat
Expand Down Expand Up @@ -228,25 +244,40 @@ commandParser = subparser $ fold
<> help "Path to the token-file for ledger authorization"

runCommand :: Command -> IO ()
runCommand DamlStudio {..} = runDamlStudio replaceExtension remainingArguments
runCommand RunJar {..} = runJar jarPath mbLogbackConfig remainingArguments
runCommand New {..} = runNew targetFolder templateNameM [] []
runCommand Migrate {..} = runMigrate targetFolder pkgPathFrom pkgPathTo
runCommand Init {..} = runInit targetFolderM
runCommand ListTemplates = runListTemplates
runCommand Start {..} =
runStart
sandboxPortM
startNavigator
jsonApiCfg
openBrowser
onStartM
waitForSignal
sandboxOptions
navigatorOptions
jsonApiOptions
runCommand Deploy {..} = runDeploy flags
runCommand LedgerListParties {..} = runLedgerListParties flags json
runCommand LedgerAllocateParties {..} = runLedgerAllocateParties flags parties
runCommand LedgerUploadDar {..} = runLedgerUploadDar flags darPathM
runCommand LedgerNavigator {..} = runLedgerNavigator flags remainingArguments
runCommand = \case
DamlStudio {..} -> runDamlStudio replaceExtension remainingArguments
RunJar {..} -> runJar jarPath mbLogbackConfig remainingArguments
New {..} -> runNew targetFolder templateNameM [] []
Migrate {..} -> runMigrate targetFolder pkgPathFrom pkgPathTo
Init {..} -> runInit targetFolderM
ListTemplates -> runListTemplates
Start {..} ->
runStart
sandboxPortM
startNavigator
jsonApiCfg
openBrowser
onStartM
waitForSignal
sandboxOptions
navigatorOptions
jsonApiOptions
Deploy {..} -> runDeploy flags
LedgerListParties {..} -> runLedgerListParties flags json
LedgerAllocateParties {..} -> runLedgerAllocateParties flags parties
LedgerUploadDar {..} -> runLedgerUploadDar flags darPathM
LedgerNavigator {..} -> runLedgerNavigator flags remainingArguments
Codegen {..} ->
case lang of
TypeScript ->
runDaml2ts remainingArguments
Java ->
runJar
"daml-sdk/daml-sdk.jar"
(Just "daml-sdk/codegen-logback.xml")
(["codegen", "java"] ++ remainingArguments)
Scala ->
runJar
"daml-sdk/daml-sdk.jar"
(Just "daml-sdk/codegen-logback.xml")
(["codegen", "scala"] ++ remainingArguments)
7 changes: 7 additions & 0 deletions daml-assistant/daml-helper/src/DA/Daml/Helper/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module DA.Daml.Helper.Run
, runNew
, runMigrate
, runJar
, runDaml2ts
, runListTemplates
, runStart

Expand Down Expand Up @@ -310,6 +311,12 @@ runJar jarPath mbLogbackPath remainingArgs = do
mbLogbackArg <- traverse getLogbackArg mbLogbackPath
withJar jarPath (toList mbLogbackArg) remainingArgs (const $ pure ())

runDaml2ts :: [String] -> IO ()
runDaml2ts remainingArgs = do
daml2ts <- fmap (</> "daml2ts" </> "daml2ts") getSdkPath
withProcessWait_ (proc daml2ts remainingArgs) (const $ pure ()) `catchIO`
(\e -> hPutStrLn stderr "Failed to invoke daml2ts." *> throwIO e)

getLogbackArg :: FilePath -> IO String
getLogbackArg relPath = do
sdkPath <- getSdkPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ tests damlDir tmpDir = testGroup "Integration tests"
, quickstartTests quickstartDir mvnDir
, cleanTests cleanDir
, deployTest deployDir
, codegenTests codegenDir
]
where quickstartDir = tmpDir </> "q-u-i-c-k-s-t-a-r-t"
cleanDir = tmpDir </> "clean"
mvnDir = tmpDir </> "m2"
tarballDir = tmpDir </> "tarball"
deployDir = tmpDir </> "deploy"
codegenDir = tmpDir </> "codegen"

throwError :: MonadFail m => T.Text -> T.Text -> m ()
throwError msg e = fail (T.unpack $ msg <> " " <> e)
Expand Down Expand Up @@ -613,6 +615,32 @@ cleanTests baseDir = testGroup "daml clean"
, unlines (map (" "++) filesAtEnd)
]

-- | Check we can generate language bindings.
codegenTests :: FilePath -> TestTree
codegenTests codegenDir = testGroup "daml codegen"
[ codegenTestFor "ts" Nothing
, codegenTestFor "java" Nothing
, codegenTestFor "scala" (Just "com.cookiemonster.nomnomnom")
]
where
codegenTestFor :: String -> Maybe String -> TestTree
codegenTestFor lang namespace =
testCase lang $ do
createDirectoryIfMissing True codegenDir
withCurrentDirectory codegenDir $ do
let projectDir = codegenDir </> ("proj-" ++ lang)
callCommandQuiet $ unwords ["daml new", projectDir, "skeleton"]
withCurrentDirectory projectDir $ do
callCommandQuiet "daml build"
let darFile = projectDir</> ".daml/dist/proj-" ++ lang ++ "-0.0.1.dar"
outDir = projectDir</> "generated" </> lang
callCommandQuiet $
unwords [ "daml", "codegen", lang
, darFile ++ maybe "" ("=" ++) namespace
, "-o", outDir]
contents <- listDirectory outDir
assertBool "bindings were written" (not $ null contents)

deployTest :: FilePath -> TestTree
deployTest deployDir = testCase "daml deploy" $ do
createDirectoryIfMissing True deployDir
Expand Down
8 changes: 4 additions & 4 deletions release/sdk-config.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ commands:
path: daml-helper/daml-helper
desc: "Interact with a DAML ledger (experimental)"
args: ["ledger"]
- name: codegen
path: daml-helper/daml-helper
desc: "Run a language bindings code generation tool"
args: ["codegen"]
- name: deploy
path: daml-helper/daml-helper
desc: "Deploy DAML project to a ledger (experimental)"
Expand All @@ -61,10 +65,6 @@ commands:
path: daml-helper/daml-helper
desc: "Launch the HTTP JSON API (experimental)"
args: ["run-jar", "--logback-config=daml-sdk/json-api-logback.xml", "daml-sdk/daml-sdk.jar", "json-api"]
- name: codegen
path: daml-helper/daml-helper
desc: "Run the DAML codegen"
args: ["run-jar", "--logback-config=daml-sdk/codegen-logback.xml", "daml-sdk/daml-sdk.jar", "codegen"]
- name: trigger
path: daml-helper/daml-helper
args: ["run-jar", "--logback-config=daml-sdk/trigger-logback.xml", "daml-sdk/daml-sdk.jar", "trigger"]
Expand Down

0 comments on commit 0e53ec0

Please sign in to comment.