Generate clients (#206)

This commit is contained in:
Janos Dobronszki
2021-09-16 12:52:36 +01:00
committed by GitHub
parent 552c321dd7
commit d4d9c1c176
334 changed files with 9334 additions and 45 deletions

View File

@@ -1,9 +1,9 @@
package main
const goIndexTemplate = `package micro
const goIndexTemplate = `package m3o
import(
{{ range $service := .services }}"github.com/micro/micro-go/{{ $service.Name}}"
{{ range $service := .services }}"github.com/micro/services/clients/go/{{ $service.Name}}"
{{ end }}
)
@@ -59,7 +59,7 @@ const goExampleTemplate = `{{ $service := .service }}package example
import(
"fmt"
"os"
"github.com/micro/micro-go/{{ $service.Name }}"
"github.com/micro/services/clients/go/{{ $service.Name}}"
)
{{ if endpointComment .endpoint $service.Spec.Components.Schemas }}{{ endpointComment .endpoint $service.Spec.Components.Schemas }}{{ end }}func {{ .funcName }}() {

View File

@@ -52,6 +52,13 @@ func main() {
fmt.Println(err)
os.Exit(1)
}
examplesPath := filepath.Join(workDir, "examples")
err = os.MkdirAll(goPath, 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
funcs := map[string]interface{}{
"recursiveTypeDefinition": func(language, serviceName, typeName string, schemas map[string]*openapi3.SchemaRef) string {
return schemaToType(language, serviceName, typeName, schemas)
@@ -103,6 +110,9 @@ func main() {
services := []service{}
tsExportsMap := map[string]string{}
for _, f := range files {
if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") {
continue
}
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
serviceName := f.Name()
// see https://stackoverflow.com/questions/44345257/import-from-subfolder-of-npm-package
@@ -277,12 +287,12 @@ func main() {
}
// create go examples directory
err = os.MkdirAll(filepath.Join(goPath, serviceName, "examples", endpoint), 0777)
err = os.MkdirAll(filepath.Join(examplesPath, serviceName, endpoint, "go"), 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
goExampleFile := filepath.Join(goPath, serviceName, "examples", endpoint, title+".go")
goExampleFile := filepath.Join(examplesPath, serviceName, endpoint, "go", title+".go")
f, err = os.OpenFile(goExampleFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
if err != nil {
fmt.Println("Failed to open schema file", err)
@@ -297,7 +307,7 @@ func main() {
}
cmd := exec.Command("gofmt", "-w", title+".go")
cmd.Dir = filepath.Join(goPath, serviceName, "examples", endpoint)
cmd.Dir = filepath.Join(examplesPath, serviceName, endpoint, "go")
outp, err = cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprintf("Problem with '%v' example '%v': %v", serviceName, endpoint, string(outp)))
@@ -319,12 +329,12 @@ func main() {
"funcName": strcase.UpperCamelCase(title),
})
err = os.MkdirAll(filepath.Join(tsPath, serviceName, "examples", endpoint), 0777)
err = os.MkdirAll(filepath.Join(examplesPath, serviceName, endpoint, "node"), 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
tsExampleFile := filepath.Join(tsPath, serviceName, "examples", endpoint, title+".js")
tsExampleFile := filepath.Join(examplesPath, serviceName, endpoint, "node", title+".js")
f, err = os.OpenFile(tsExampleFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
if err != nil {
fmt.Println("Failed to open schema file", err)
@@ -339,7 +349,7 @@ func main() {
}
cmd = exec.Command("prettier", "-w", title+".js")
cmd.Dir = filepath.Join(tsPath, serviceName, "examples", endpoint)
cmd.Dir = filepath.Join(examplesPath, serviceName, endpoint, "node")
outp, err = cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprintf("Problem with '%v' example '%v': %v", serviceName, endpoint, string(outp)))
@@ -361,7 +371,13 @@ func main() {
"funcName": strcase.UpperCamelCase(title),
})
curlExampleFile := filepath.Join(goPath, serviceName, "examples", endpoint, title+".sh")
err = os.MkdirAll(filepath.Join(examplesPath, serviceName, endpoint, "curl"), 0777)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
curlExampleFile := filepath.Join(examplesPath, serviceName, endpoint, "curl", title+".sh")
f, err = os.OpenFile(curlExampleFile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0744)
if err != nil {
fmt.Println("Failed to open schema file", err)
@@ -378,7 +394,7 @@ func main() {
// only build after each example is generated as old files from
// previous generation might not compile
cmd = exec.Command("go", "build", "-o", "/tmp/bin/outputfile")
cmd.Dir = filepath.Join(goPath, serviceName, "examples", endpoint)
cmd.Dir = filepath.Join(examplesPath, serviceName, endpoint, "go")
outp, err = cmd.CombinedOutput()
if err != nil {
fmt.Println(fmt.Sprintf("Problem with '%v' example '%v': %v", serviceName, endpoint, string(outp)))
@@ -425,7 +441,7 @@ func main() {
os.Exit(1)
}
tsFiles := filepath.Join(workDir, "cmd", "clients", "ts")
cmd = exec.Command("cp", filepath.Join(tsFiles, "package.json"), filepath.Join(tsFiles, ".npmrc"), filepath.Join(tsFiles, ".gitignore"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "tsconfig.json"), filepath.Join(workDir, "clients", "ts"))
cmd = exec.Command("cp", filepath.Join(tsFiles, "package.json"), filepath.Join(tsFiles, ".gitignore"), filepath.Join(tsFiles, "package-lock.json"), filepath.Join(tsFiles, "tsconfig.json"), filepath.Join(workDir, "clients", "ts"))
cmd.Dir = filepath.Join(tsPath)
outp, err = cmd.CombinedOutput()
if err != nil {
@@ -474,7 +490,7 @@ func main() {
}
// login to NPM
f, err = os.OpenFile(filepath.Join(tsPath, ".npmrc"), os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
f, err = os.OpenFile(filepath.Join(tsPath, ".npmrc"), os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
fmt.Println("Failed to open npmrc", err)
os.Exit(1)
@@ -485,13 +501,13 @@ func main() {
fmt.Println("No NPM_TOKEN env found")
os.Exit(1)
}
if _, err = f.WriteString("\n//npm.pkg.github.com/:_authToken=" + os.Getenv("NPM_TOKEN")); err != nil {
if _, err = f.WriteString("//registry.npmjs.org/:_authToken=" + os.Getenv("NPM_TOKEN")); err != nil {
fmt.Println("Failed to open npmrc", err)
os.Exit(1)
}
// get latest version from github
getVersions := exec.Command("npm", "show", "@micro/services", "--time", "--json")
getVersions := exec.Command("npm", "show", "m3o", "--time", "--json")
getVersions.Dir = tsPath
outp, err = getVersions.CombinedOutput()
@@ -553,7 +569,7 @@ func main() {
os.Exit(1)
}
m["exports"] = tsExportsMap
pakJS, err := json.Marshal(m)
pakJS, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println(err)
os.Exit(1)

View File

@@ -1 +0,0 @@
registry=https://npm.pkg.github.com/micro

View File

@@ -1,5 +1,5 @@
{
"name": "@micro/services",
"name": "m3o",
"version": "1.0.1",
"description": "",
"main": "dist/index.js",

View File

@@ -35,7 +35,7 @@ export interface {{ title $typeName }}{{ "{" }}
{{end}}
`
const tsExampleTemplate = `{{ $service := .service }}import * as {{ $service.Name }} from '@m3o/services/{{ $service.Name }}';
const tsExampleTemplate = `{{ $service := .service }}import * as {{ $service.Name }} from 'm3o/{{ $service.Name }}';
{{ if endpointComment .endpoint $service.Spec.Components.Schemas }}{{ endpointComment .endpoint $service.Spec.Components.Schemas }}{{ end }}async function {{ .funcName }}() {
let {{ $service.Name }}Service = new {{ $service.Name }}.{{ title $service.Name }}Service(process.env.MICRO_API_TOKEN)

View File

@@ -68,6 +68,9 @@ func main() {
workDir, _ := os.Getwd()
for _, f := range files {
if strings.Contains(f.Name(), "clients") || strings.Contains(f.Name(), "examples") {
continue
}
if f.IsDir() && !strings.HasPrefix(f.Name(), ".") {
serviceDir := filepath.Join(workDir, f.Name())
serviceFiles, err := ioutil.ReadDir(serviceDir)