From 69c8e8bb4a4088f62cebaaf39aac99a175cfb682 Mon Sep 17 00:00:00 2001 From: Mohd Bilal Date: Tue, 14 Mar 2023 19:55:47 +0530 Subject: [PATCH] Make command to generate types from typed metadata in languages other than Typescript PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8238 GitOrigin-RevId: 9a4730e0d311916c0d837586485382c58033c914 --- metadata-api-types/.gitignore | 4 ++ metadata-api-types/Makefile | 60 +++++++++++++++++-- .../scripts/generate-types-for-lang.sh | 25 ++++++++ ...-types.sh => generate-typescript-types.sh} | 0 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 metadata-api-types/.gitignore create mode 100755 metadata-api-types/scripts/generate-types-for-lang.sh rename metadata-api-types/scripts/{generate-types.sh => generate-typescript-types.sh} (100%) diff --git a/metadata-api-types/.gitignore b/metadata-api-types/.gitignore new file mode 100644 index 00000000000..59a7c520860 --- /dev/null +++ b/metadata-api-types/.gitignore @@ -0,0 +1,4 @@ +go/ +haskell/ +rust/ +kotlin/ diff --git a/metadata-api-types/Makefile b/metadata-api-types/Makefile index 77bf0b7b0cb..b393c3b6249 100644 --- a/metadata-api-types/Makefile +++ b/metadata-api-types/Makefile @@ -5,6 +5,24 @@ TYPESCRIPT_ROOT := typescript TYPESCRIPT_SRC := ${TYPESCRIPT_ROOT}/src PATCHES := $(wildcard ${TYPESCRIPT_ROOT}/patches/*.patch) +FILE_NAME := metadata.openapi + +GO_ROOT := go +GO_FILE := ${FILE_NAME}.go +GO_TARGET := ${GO_ROOT}/${GO_FILE} + +RUST_ROOT := rust +RUST_FILE := ${FILE_NAME}.rs +RUST_TARGET := ${RUST_ROOT}/${RUST_FILE} + +HASKELL_ROOT := haskell +HASKELL_FILE := ${FILE_NAME}.hs +HASKELL_TARGET := ${HASKELL_ROOT}/${HASKELL_FILE} + +KOTLIN_ROOT := kotlin +KOTLIN_FILE := ${FILE_NAME}.kt +KOTLIN_TARGET := ${KOTLIN_ROOT}/${KOTLIN_FILE} + # default target .PHONY: help ## help: prints help message @@ -13,18 +31,50 @@ help: @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' .PHONY: generate-types -## generate-types: Generate the TypeScript types for working with the Metadata API -generate-types: ${TYPESCRIPT_SRC} +## generate-types: Generate types for working with the Metadata API +generate-types: generate-typescript-types generate-go-types generate-rust-types generate-haskell-types generate-kotlin-types + +.PHONY: generate-typescript-types +## generate-typescript-types: Generate the TypeScript types for working with the Metadata API +generate-typescript-types: ${TYPESCRIPT_SRC} + +.PHONY: generate-go-types +## generate-go-types: Generate the Go types for working with the Metadata API +generate-go-types: ${GO_TARGET} + +.PHONY: generate-rust-types +## generate-rust-types: Generate the Rust types for working with the Metadata API +generate-rust-types: ${RUST_TARGET} + +.PHONY: generate-haskell-types +## generate-haskell-types: Generate the Haskell types for working with the Metadata API +generate-haskell-types: ${HASKELL_TARGET} + +.PHONY: generate-kotlin-types +## generate-kotlin-types: Generate the Kotlin types for working with the Metadata API +generate-kotlin-types: ${KOTLIN_TARGET} .PHONY: typecheck ## typecheck: Typechecks generated type definitions typecheck: typecheck-metadata-api-types -${TYPESCRIPT_SRC}: ${SCHEMA_FILE} ${TYPESCRIPT_ROOT}/package.json ${TYPESCRIPT_ROOT}/package-lock.json ${PATCHES} - ./scripts/generate-types.sh "${SCHEMA_FILE}" - .PHONY: typecheck-metadata-api-types ## typecheck-metadata-api-types: Typechecks the metadata-api-types typecheck-metadata-api-types: cd ${TYPESCRIPT_ROOT} && \ npm run typecheck + +${TYPESCRIPT_SRC}: ${SCHEMA_FILE} ${TYPESCRIPT_ROOT}/package.json ${TYPESCRIPT_ROOT}/package-lock.json ${PATCHES} + ./scripts/generate-typescript-types.sh "${SCHEMA_FILE}" + +${GO_TARGET}: ${TYPESCRIPT_SRC} + ./scripts/generate-types-for-lang.sh ${GO_ROOT} ${GO_FILE} + +${RUST_TARGET}: ${TYPESCRIPT_SRC} + ./scripts/generate-types-for-lang.sh ${RUST_ROOT} ${RUST_FILE} + +${HASKELL_TARGET}: ${TYPESCRIPT_SRC} + ./scripts/generate-types-for-lang.sh ${HASKELL_ROOT} ${HASKELL_FILE} + +${KOTLIN_TARGET}: ${TYPESCRIPT_SRC} + ./scripts/generate-types-for-lang.sh ${KOTLIN_ROOT} ${KOTLIN_FILE} diff --git a/metadata-api-types/scripts/generate-types-for-lang.sh b/metadata-api-types/scripts/generate-types-for-lang.sh new file mode 100755 index 00000000000..f8debe5d3fd --- /dev/null +++ b/metadata-api-types/scripts/generate-types-for-lang.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -euo pipefail + +PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )" # ... https://stackoverflow.com/a/246128/176841 + +LANG="$1" +FILE="$2" +# We are generating types from already generated Typescript types. +# `index.js`` is the file that contains the Typescript types +INDEX_JS="${PROJECT_ROOT}/typescript/src/index.ts" + +# Dir where generated types will be published. +DIR="${PROJECT_ROOT}/${LANG}" + +if [ ! -d "${DIR}" ]; then + mkdir "${DIR}" +fi + +# npm package used to generate types. +# Use `quicktype --help` to see a list of supported languages. +# Use `npm i -g quicktype` to install. +quicktype --lang "${LANG}" \ + --out "${DIR}/${FILE}" \ + --src-lang typescript \ + --src "${INDEX_JS}" # add --quiet to silence output diff --git a/metadata-api-types/scripts/generate-types.sh b/metadata-api-types/scripts/generate-typescript-types.sh similarity index 100% rename from metadata-api-types/scripts/generate-types.sh rename to metadata-api-types/scripts/generate-typescript-types.sh