Follow up to earlier PR (#5201)

changelog_begin
changelog_end
This commit is contained in:
Shayne Fletcher 2020-03-26 05:16:16 -04:00 committed by GitHub
parent c19a20669f
commit 24cbeddd35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 71 additions and 71 deletions

View File

@ -73,6 +73,11 @@ da_haskell_test(
srcs = glob(["src/**/*.hs"]),
args = [
"$(location //:yarn)",
# The TS codegen test which needs this next arg is not
# run on Windows but we need to pass something on Windows
# to make the test harness happy. If it should be misued,
# hopefully "daml-types-not-available" will end up in the
# error message.
"daml-types-not-available" if is_windows else "$(location //language-support/ts/daml-types:npm_package)",
],
data = [

View File

@ -42,9 +42,9 @@ import SdkVersion
main :: IO ()
main = do
setEnv "TASTY_NUM_THREADS" "1" True
-- We manipulate global state via the working directory and
-- the environment so running tests in parallel will cause trouble.
setEnv "TASTY_NUM_THREADS" "1" True
yarn : damlTypesPath : args <- getArgs
withTempDir $ \tmpDir -> do
oldPath <- getSearchPath
@ -506,19 +506,18 @@ codegenTests codegenDir damlTypes = testGroup "daml codegen" (
let darFile = projectDir </> ".daml/dist/proj-" ++ lang ++ "-0.0.1.dar"
outDir = projectDir </> "generated" </> lang
when (lang == "ts") $ do
-- This section makes
-- 'daml-types@0.0.0-SDKVERSION' available
-- to yarn.
createDirectoryIfMissing True "generated"
withCurrentDirectory "generated" $ do
-- SDK version is 0.0.0; daml2ts needs
-- 'daml-types' to be here in the filesystem...
copyDirectory damlTypes "daml-types"
BSL.writeFile "package.json" $ encode (
-- ... and this package.json so it can find it.
BSL.writeFile "package.json" $ encode $
object
[ "private" .= True
, "workspaces" .= [T.pack lang]
, "resolutions" .= HMS.fromList ([("@daml/types", "file:daml-types")] :: [(T.Text, T.Text)])
]
)
callCommandQuiet $
unwords [ "daml", "codegen", lang
, darFile ++ maybe "" ("=" ++) namespace

View File

@ -21,7 +21,7 @@ Usage
``daml2ts`` is invoked via the DAML SDK assistant.
In outline, the command to generate TypeScript from DAML is ``daml codegen ts DAR -o OUTDIR`` where ``DAR`` is the path to a DAR file (generated via ``daml build``), ``OUTDIR`` is a directory where you want the TypeScript to be written and ``PACKAGE`` is a desired TypeScript package name.
In outline, the command to generate JavaScript and TypeScript typings from DAML is ``daml codegen ts DAR -o OUTDIR`` where ``DAR`` is the path to a DAR file (generated via ``daml build``) and ``OUTDIR`` is a directory where you want the JavaScript to be written.
Here's a complete example that generates TypeScript from a project produced from the standard "skeleton" template.
@ -37,8 +37,8 @@ Here's a complete example that generates TypeScript from a project produced from
- The directory ``my-proj/daml2ts`` contains generated TypeScript and Javascript artifacts;
- The files are arranged into directories;
- One of those directories will be named as per the ``PACKAGE`` argument and will contain the definitions corresponding to the DAML files in the project;
- For example, ``generated/ts/my-proj/Main.ts`` contains the definitions for ``daml/Main.daml``;
- One of those directories will be named as my-proj-0.0.1 and will contain the definitions corresponding to the DAML files in the project;
- For example, ``generated/ts/my-proj/src/Main.ts`` contains the definitions for ``daml/Main.daml``;
- The remaining directories correspond to modules of the DAML standard library;
- Those directories have numeric names (the names are hashes of the DAML-LF package they are derived from).

View File

@ -47,7 +47,6 @@ import qualified DA.Daml.Project.Types as DATypes
data ConfigConsts = ConfigConsts
{ pkgDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion
, pkgDevDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion
, pkgScripts :: HMS.HashMap ScriptName Script
}
configConsts :: T.Text -> ConfigConsts
configConsts sdkVersion = ConfigConsts
@ -58,10 +57,6 @@ configConsts sdkVersion = ConfigConsts
, pkgDevDependencies = HMS.fromList
[ (NpmPackageName "typescript", NpmPackageVersion "~3.7.3")
]
, pkgScripts = HMS.fromList
[ (ScriptName "build", Script "tsc")
, (ScriptName "lint", Script "eslint --ext .ts --max-warnings 0 src/")
]
}
newtype NpmPackageName = NpmPackageName {unNpmPackageName :: T.Text}
deriving stock (Eq, Show)
@ -69,12 +64,6 @@ newtype NpmPackageName = NpmPackageName {unNpmPackageName :: T.Text}
newtype NpmPackageVersion = NpmPackageVersion {unNpmPackageVersion :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON)
newtype ScriptName = ScriptName {unScriptName :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON, ToJSONKey)
newtype Script = Script {unScript :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON)
data Options = Options
{ optInputDars :: [FilePath]
@ -689,7 +678,7 @@ writePackageJson packageDir sdkVersion (Scope scope) depends =
package = T.pack $ takeFileName packageDir
packageJson :: NpmPackageName -> NpmPackageVersion -> HMS.HashMap NpmPackageName NpmPackageVersion -> Value
packageJson name version@(NpmPackageVersion sdkVersion) dependencies = object
packageJson name version dependencies = object
[ "private" .= True
, "name" .= name
, "version" .= version
@ -697,10 +686,7 @@ writePackageJson packageDir sdkVersion (Scope scope) depends =
, "types" .= ("lib/index.d.ts" :: T.Text)
, "description" .= ("Generated by daml2ts" :: T.Text)
, "dependencies" .= dependencies
, "scripts" .= pkgScripts config
]
where
config = configConsts sdkVersion
buildPackages :: SdkVersion -> Scope -> FilePath -> [(T.Text, [Dependency])] -> IO ()
buildPackages sdkVersion optScope optOutputDir dependencies = do

View File

@ -141,6 +141,7 @@ da_haskell_test(
"bytestring",
"extra",
"filepath",
"hashable",
"process",
"tasty",
"tasty-hunit",

View File

@ -59,14 +59,17 @@ cd $TMP_DIR
# Call daml2ts.
PATH=`dirname $YARN`:$PATH $DAML2TS -o daml2ts $DAR
# We only needed this package.json in order to provide resolution of
# daml-types to daml2ts and that's done with now.
rm package.json
# Since we eschew using workspaces and resolutions in this test, patch
# up the daml-ledger dependency on daml-types.
sed -i"" "s|\"@daml/types\": \"0.0.0\"|\"@daml/types\": \"file:../daml-types\"|" daml-ledger/package.json
# The previous step provides all the daml2ts Javascript needed by the
# build-and-lint-test workspace.
$YARN install --pure-lockfile > /dev/null 2>&1
$YARN workspaces run build # Build it.
$YARN workspaces run lint # No great value in this but nonetheless, lint it.
# Invoke 'yarn test' in the 'build-and-lint-test' package
# directory. Control is thereby passed to
# 'language-support/ts/codegen/tests/ts/build-and-lint-test/src/__tests__/test.ts'.
# Build, lint, test.
cd build-and-lint-test
$YARN install --pure-lockfile > /dev/null 2>&1
$YARN run build && $YARN run lint
# Invoke 'yarn test'. Control is thereby passed to
# 'language-support/ts/codegen/tests/ts/build-and-lint-test/src/__tests__/test.ts'.
JAVA=$JAVA SANDBOX=$SANDBOX JSON_API=$JSON_API DAR=$DAR $YARN test

View File

@ -1,6 +1,6 @@
-- Copyright (c) 2020 The DAML Authors. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0
{-# LANGUAGE DerivingStrategies #-}
module DA.Test.Daml2Ts (main) where
import Control.Monad.Extra
@ -19,9 +19,29 @@ import qualified Data.Text.Extended as T
import qualified Data.ByteString.Lazy as BSL
import qualified Data.HashMap.Strict as HMS
import Data.Aeson
import Data.Hashable
import Test.Tasty
import Test.Tasty.HUnit
-- Referenced from the DAVL test. Lifted here for easy
-- maintenance.
data ConfigConsts = ConfigConsts
{ pkgDevDependencies :: HMS.HashMap NpmPackageName NpmPackageVersion }
configConsts :: ConfigConsts
configConsts = ConfigConsts
{ pkgDevDependencies = HMS.fromList
[ (NpmPackageName "eslint", NpmPackageVersion "^6.7.2")
, (NpmPackageName "@typescript-eslint/eslint-plugin", NpmPackageVersion "2.11.0")
, (NpmPackageName "@typescript-eslint/parser", NpmPackageVersion "2.11.0")
]
}
newtype NpmPackageName = NpmPackageName {unNpmPackageName :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON, ToJSONKey)
newtype NpmPackageVersion = NpmPackageVersion {unNpmPackageVersion :: T.Text}
deriving stock (Eq, Show)
deriving newtype (Hashable, FromJSON, ToJSON)
main :: IO ()
main = do
setEnv "TASTY_NUM_THREADS" "1" True
@ -95,7 +115,7 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
step "daml build..."
buildProject ["-o", ".daml" </> "dist" </> "elmo-1.0.dar"]
step "daml2ts..."
copyDirectory damlTypes "daml-types"
setupYarnEnvironment
(exitCode, _, err) <- readProcessWithExitCode daml2ts ([groverDar, elmoDar] ++ ["-o", daml2tsDir]) ""
assertBool "A duplicate name for different packages error was expected." (exitCode /= ExitSuccess && isJust (stripInfix "Duplicate name 'grover-1.0' for different packages detected" err))
@ -143,8 +163,7 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
buildProject []
withCurrentDirectory here $ do
step "daml2ts..."
copyDirectory damlTypes "daml-types"
writePackageJson
setupYarnEnvironment
(exitCode, _, err) <- readProcessWithExitCode daml2ts ([groverDar, superGroverDar] ++ ["-o", daml2tsDir]) ""
assertBool "A different names for same package error was expected." (exitCode /= ExitSuccess && isJust (stripInfix "Different names ('grover-1.0' and 'super-grover-1.0') for the same package detected" err))
@ -153,8 +172,6 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
groverDaml = grover </> "daml"
daml2tsDir = here </> "daml2ts"
groverTs = daml2tsDir </> "grover-1.0"
groverTsSrc = groverTs </> "src"
groverTsLib = groverTs </> "lib"
groverDar = grover </> ".daml" </> "dist" </> "grover-1.0.dar"
createDirectoryIfMissing True groverDaml
withCurrentDirectory grover $ do
@ -174,57 +191,43 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
buildProject []
withCurrentDirectory here $ do
step "daml2ts..."
writePackageJson
copyDirectory damlTypes "daml-types"
setupYarnEnvironment
daml2tsProject [groverDar, groverDar] daml2tsDir
assertFileExists (groverTsSrc </> "Grover.ts")
assertFileExists (groverTsLib </> "Grover.js")
assertFileExists (groverTsLib </> "Grover.d.ts")
mapM_ (assertTsFileExists groverTs) [ "index", "Grover" ]
, testCaseSteps "DAVL test" $ \step -> withTempDir $ \here -> do
let daml2tsDir = here </> "daml2ts"
withCurrentDirectory here $ do
copyDirectory damlTypes "daml-types"
step "daml2ts..."
writePackageJson
setupYarnEnvironment
callProcessSilent daml2ts $
[ davl </> "davl-v4.dar"
, davl </> "davl-v5.dar"
, davl </> "davl-upgrade-v4-v5.dar" ] ++
["-o", daml2tsDir]
assertFileExists (daml2tsDir </> "davl-0.0.4" </> "src" </> "DAVL.ts")
assertFileExists (daml2tsDir </> "davl-0.0.4" </> "lib" </> "DAVL.js")
assertFileExists (daml2tsDir </> "davl-0.0.4" </> "lib" </> "DAVL.d.ts")
assertFileExists (daml2tsDir </> "davl-0.0.5" </> "src" </> "DAVL.ts")
assertFileExists (daml2tsDir </> "davl-0.0.5" </> "lib" </> "DAVL.js")
assertFileExists (daml2tsDir </> "davl-0.0.5" </> "lib" </> "DAVL.d.ts")
assertFileExists (daml2tsDir </> "davl-upgrade-v4-v5-0.0.5" </> "src" </> "Upgrade.ts")
assertFileExists (daml2tsDir </> "davl-upgrade-v4-v5-0.0.5" </> "lib" </> "Upgrade.js")
assertFileExists (daml2tsDir </> "davl-upgrade-v4-v5-0.0.5" </> "lib" </> "Upgrade.d.ts")
mapM_ (assertTsFileExists (daml2tsDir </> "davl-0.0.4")) [ "index", "DAVL" ]
mapM_ (assertTsFileExists (daml2tsDir </> "davl-0.0.5")) [ "index", "DAVL" ]
mapM_ (assertTsFileExists (daml2tsDir </> "davl-upgrade-v4-v5-0.0.5")) [ "index", "Upgrade" ]
step "eslint..."
withCurrentDirectory daml2tsDir $ do
pkgs <- (\\ ["package.json", "node_modules"]) <$> listDirectory daml2tsDir
BSL.writeFile "package.json" $ encode (
object
[ "private" .= True
, "devDependencies" .= HMS.fromList
([ ("eslint", "^6.7.2")
, ("@typescript-eslint/eslint-plugin", "2.11.0")
, ("@typescript-eslint/parser", "2.11.0")
] :: [(T.Text, T.Text)]
)
, "dependencies" .= HMS.fromList
([ ("@daml/types", "file:../daml-types")
, ("@mojotech/json-type-validation", "^3.1.0")
] :: [(T.Text, T.Text)])
, "devDependencies" .= pkgDevDependencies configConsts
, "workspaces" .= pkgs
, "name" .= ("daml2ts" :: T.Text)
, "version" .= ("0.0.0" :: T.Text)
])
callProcessSilent yarn ["install", "--pure-lockfile"]
callProcessSilent yarn ["workspaces", "run", "lint"]
callProcessSilent yarn ["workspaces", "run", "eslint", "--ext", ".ts", "--max-warnings", "0", "src/"]
]
where
setupYarnEnvironment :: IO ()
setupYarnEnvironment = do
copyDirectory damlTypes "daml-types"
writePackageJson
buildProject :: [String] -> IO ()
buildProject args = callProcessSilent damlc (["build"] ++ args)
@ -262,5 +265,11 @@ tests damlTypes yarn damlc daml2ts davl = testGroup "daml2ts tests"
, "resolutions" .= HMS.fromList ([("@daml/types", "file:daml-types")] :: [(T.Text, T.Text)])
]
assertFileExists :: FilePath -> IO ()
assertFileExists file = doesFileExist file >>= assertBool (file ++ " was not created")
assertTsFileExists :: FilePath -> String -> IO ()
assertTsFileExists proj file = do
assertFileExists (proj </> "src" </> file <.> "ts")
assertFileExists (proj </> "lib" </> file <.> "js")
assertFileExists (proj </> "lib" </> file <.> ".d.ts")
where
assertFileExists :: FilePath -> IO ()
assertFileExists file = doesFileExist file >>= assertBool (file ++ " was not created")

View File

@ -1,9 +1,6 @@
{
"private": true,
"workspaces": [
"build-and-lint-test",
"daml2ts"
],
"workspaces": ["daml2ts"],
"resolutions": {
"@daml/types": "file:daml-types"
}