From 6b2c051624327e8c0c8fc630f7d4f5e1cf4f0661 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Mon, 24 Oct 2022 15:26:29 +0000 Subject: [PATCH] ci: type package generation script --- --- /dev/null +++ b/tests/scripts/functions.sh @@ -0,0 +1,4 @@ + +function do_rpc { + curl -s --header "Content-Type: application/json" -XPOST --data "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"$1\",\"params\":[$2]}" $RPC_URL +} --- /dev/null +++ b/tests/scripts/generate_types_package.sh @@ -0,0 +1,206 @@ +#!/usr/bin/env bash + +set -eu + +DIR=$(realpath $(dirname "$0")) +TEMPLATE=$DIR/types_template +GIT_REPO=git@github.com:UniqueNetwork/unique-types-js.git + +. $DIR/functions.sh + +usage() { + echo "Usage: [RPC_URL=http://localhost:9933] $0 [--rc|--release|--sapphire] [--force] [--rpc-url=http://localhost:9933]" 1>&2 + exit 1 +} + +rc= +sapphire= +release= +force= + +for i in "$@"; do +case $i in + --rc) + rc=1 + if test "$release" -o "$sapphire"; then usage; fi + ;; + --sapphire) + sapphire=1 + if test "$rc" -o "$release"; then usage; fi + ;; + --release) + release=1 + if test "$rc" -o "$sapphire"; then usage; fi + ;; + --force) + force=1 + ;; + --rpc-url=*) + RPC_URL=${i#*=} + ;; + *) + usage + ;; +esac +done + +if test \( ! \( "$rc" -o "$release" -o "$sapphire" \) \) -o \( "${RPC_URL=}" = "" \); then + usage +elif test "$rc"; then + echo "Rc build" +else + echo "Release build" +fi + +cd $DIR/.. +yarn polkadot-types + +version=$(do_rpc state_getRuntimeVersion "") +spec_version=$(echo $version | jq -r .result.specVersion) +spec_name=$(echo $version | jq -r .result.specName) +echo "Spec version: $spec_version, name: $spec_name" + +case $spec_name in + opal) + package_name=@unique-nft/opal-testnet-types + repo_branch=opal-testnet + repo_tag=$repo_branch + ;; + quartz) + package_name=@unique-nft/quartz-mainnet-types + repo_branch=quartz-mainnet + repo_tag=$repo_branch + ;; + unique) + package_name=@unique-nft/unique-mainnet-types + repo_branch=master + repo_tag=unique-mainnet + ;; + *) + echo "unknown spec name: $spec_name" + exit 1 + ;; +esac + +if test "$rc" = 1; then + if "$spec_name" != opal; then + echo "rc types can only be based on opal spec" + exit 1 + fi + package_name=@unique-nft/rc-types + repo_branch=rc + repo_tag=$repo_branch +fi +if test "$sapphire" = 1; then + if "$spec_name" != opal; then + echo "sapphire types can only be based on opal spec" + exit 1 + fi + package_name=@unique-nft/sapphire-mainnet-types + repo_branch=sapphire-mainnet + repo_tag=$repo_branch +fi + +package_version=${spec_version:0:3}.$(echo ${spec_version:3:3} | sed 's/^0*//'). +last_patch=NEVER +for tag in $(git ls-remote -t --refs $GIT_REPO | cut -f 2 | sort -r); do + tag_prefix=refs/tags/$repo_tag-v$package_version + if [[ $tag == $tag_prefix* ]]; then + last_patch=${tag#$tag_prefix} + break; + fi +done +echo "Package version: ${package_version}X, name: $package_name" +echo "Last published: $package_version$last_patch" + +if test "$last_patch" = "NEVER"; then + new_package_version=${package_version}0 +else + new_package_version=${package_version}$((last_patch+1)) +fi +package_version=${package_version}$last_patch +echo "New package version: $new_package_version" + +pjsapi_ver=^$(cat $DIR/../package.json | jq -r '.dependencies."@polkadot/api"' | sed -e "s/^\^//") +tsnode_ver=^$(cat $DIR/../package.json | jq -r '.devDependencies."ts-node"' | sed -e "s/^\^//") +ts_ver=^$(cat $DIR/../package.json | jq -r '.devDependencies."typescript"' | sed -e "s/^\^//") + +gen=$(mktemp -d) +pushd $gen +git clone $GIT_REPO -b $repo_branch --depth 1 . +if test "$last_patch" != "NEVER"; then + git reset --hard $repo_tag-v$package_version +fi +git rm -r "*" +popd + +# Using old package_version here, becaue we first check if +# there is any difference between generated and already uplaoded types +cat $TEMPLATE/package.json \ +| jq '.private = false' - \ +| jq '.name = "'$package_name'"' - \ +| jq '.version = "'$package_version'"' - \ +| jq '.peerDependencies."@polkadot/api" = "'$pjsapi_ver'"' - \ +| jq '.peerDependencies."@polkadot/types" = "'$pjsapi_ver'"' - \ +| jq '.devDependencies."@polkadot/api" = "'$pjsapi_ver'"' - \ +| jq '.devDependencies."@polkadot/types" = "'$pjsapi_ver'"' - \ +| jq '.devDependencies."ts-node" = "'$tsnode_ver'"' - \ +| jq '.devDependencies."typescript" = "'$ts_ver'"' - \ +> $gen/package.json +for file in .gitignore .npmignore README.md tsconfig.json; do + cp $TEMPLATE/$file $gen/ +done +package_name_replacement=$(printf '%s\n' "$package_name" | sed -e 's/[\/&]/\\&/g') +sed -i 's/PKGNAME/'$package_name_replacement'/' $gen/README.md + +rsync -ar --exclude .gitignore src/interfaces/ $gen +for file in $gen/augment-* $gen/**/types.ts $gen/registry.ts; do + sed -i '1s;^;//@ts-nocheck\n;' $file +done + +pushd $gen +git add . +popd + +pushd $gen +if git diff --quiet HEAD && test ! "$force"; then + echo "no changes detected" + exit 0 +fi +popd + +mv $gen/package.json $gen/package.old.json +cat $gen/package.old.json \ +| jq '.version = "'$new_package_version'"' - \ +> $gen/package.json +rm $gen/package.old.json +pushd $gen +git add package.json +popd + +echo "package.json contents:" +cat $gen/package.json +echo "overall diff:" +pushd $gen +git status +git diff HEAD || true +popd + +# This check is only active if running in interactive terminal +if [ -t 0 ]; then + read -p "Is everything ok at $gen? " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + echo "Aborting!" + exit 1 + fi +fi + +pushd $gen +yarn +yarn prepublish +git commit -m "chore: upgrade types to v$new_package_version" +git tag --force $repo_tag-v$new_package_version +git push --tags --force -u origin HEAD +#yarn publish +popd --- /dev/null +++ b/tests/scripts/types_template/.gitignore @@ -0,0 +1,5 @@ +*.js +*.map +*.d.ts +/node_modules +metadata.json --- /dev/null +++ b/tests/scripts/types_template/.npmignore @@ -0,0 +1 @@ +/src --- /dev/null +++ b/tests/scripts/types_template/README.md @@ -0,0 +1,31 @@ +# PKGNAME + +Unique network api types + +Do not edit by hand, those types are generated automatically, and definitions are located in chain repo + +## Using types + +Install library: + +```bash +yarn add --dev PKGNAME +``` + +Replace polkadot.js types with our chain types adding corresponding path override to the tsconfig `compilerOptions.paths` section: + +```json +// in tsconfig.json +{ + "compilerOptions": { + "paths": { + "@polkadot/types/lookup": ["node_modules/PKGNAME/types-lookup"] + } + } +} +``` + +Since polkadot v7 api augmentations not loaded by default, in every file, where you need to access `api.tx`, `api.query`, `api.rpc`, etc; you should explicitly import corresponding augmentation before any other `polkadot.js` related import: +``` +import 'PKGNAME/augment-api'; +``` --- /dev/null +++ b/tests/scripts/types_template/package.json @@ -0,0 +1,22 @@ +{ + "name": "TODO", + "private": true, + "version": "TODO", + "main": "index.js", + "repository": "git@github.com:UniqueNetwork/unique-types-js.git", + "homepage": "https://unique.network/", + "license": "MIT", + "scripts": { + "prepublish": "tsc -d" + }, + "peerDependencies": { + "@polkadot/api": "TODO", + "@polkadot/types": "TODO" + }, + "devDependencies": { + "@polkadot/api": "TODO", + "@polkadot/types": "TODO", + "ts-node": "TODO", + "typescript": "TODO" + } +} --- /dev/null +++ b/tests/scripts/types_template/tsconfig.json @@ -0,0 +1,28 @@ +{ + "exclude": [ + "node_modules", + "node_modules/**/*", + "../node_modules/**/*", + "**/node_modules/**/*" + ], + "compilerOptions": { + "target": "ES2020", + "moduleResolution": "node", + "esModuleInterop": true, + "resolveJsonModule": true, + "module": "commonjs", + "sourceMap": true, + "outDir": ".", + "rootDir": ".", + "strict": true, + "paths": { + }, + "skipLibCheck": true, + }, + "include": [ + "**/*", + ], + "lib": [ + "es2017" + ], +} --- a/tests/scripts/wait_for_first_block.sh +++ b/tests/scripts/wait_for_first_block.sh @@ -1,15 +1,11 @@ #!/usr/bin/env bash -function do_rpc { - curl -s --header "Content-Type: application/json" -XPOST --data "{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"$1\",\"params\":[$2]}" $RPC_URL -} +DIR=$(dirname "$0") + +. $DIR/functions.sh function is_started { - block_hash_rpc=$(do_rpc chain_getFinalizedHead) - echo Rpc response = $block_hash_rpc - block_hash=$(echo $block_hash_rpc | jq -r .result) - echo Head = $block_hash - block_id_hex=$(do_rpc chain_getHeader "\"$block_hash\"" | jq -r .result.number) + block_id_hex=$(do_rpc chain_getHeader | jq -r .result.number) block_id=$((${block_id_hex})) echo Id = $block_id if (( $block_id > 1 )); then -- gitstuff