Skip to content

Commit

Permalink
working core code gen
Browse files Browse the repository at this point in the history
  • Loading branch information
omerdemirok committed Nov 8, 2023
1 parent 7b2bdcc commit b19abbd
Show file tree
Hide file tree
Showing 74 changed files with 123 additions and 1,729 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ help: ## Display help

.PHONY: generate-core-code
generate-core-code:
go run xata/internal/core_code_gen.go
go run xata/internal/core_code_gen.go -scope=core
217 changes: 112 additions & 105 deletions xata/internal/core_code_gen.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"gopkg.in/yaml.v3"
"io"
"log"
"os"
Expand All @@ -12,110 +13,105 @@ import (

// OpenAPI struct for parsing the OpenAPI YAML file
type OpenAPI struct {
OpenAPI string `yaml:"openapi"`
Info map[string]any `yaml:"info"`
Servers []map[string]any `yaml:"servers"`
Paths map[string]any `yaml:"paths"`
Components map[string]any `yaml:"components"`
Tags []map[string]any `yaml:"tags"`
XTagGroups []map[string]any `yaml:"x-tagGroups"`
OpenAPI string `yaml:"openapi" json:"openapi"`
Info map[string]any `yaml:"info" json:"info"`
Servers []map[string]any `yaml:"servers" json:"servers"`
Paths map[string]any `yaml:"paths" json:"paths"`
Components map[string]any `yaml:"components" json:"components"`
Tags []map[string]any `yaml:"tags" json:"tags"`
XTagGroups []map[string]any `yaml:"x-tagGroups" json:"x-tagGroups"`
}

func main() {
var err error
//files, err := os.ReadDir(".")
//if err != nil {
// log.Fatal(err)
//}
//
//for _, file := range files {
// fmt.Println(file.Name(), file.Type(), file.IsDir())
//}
//
//// backup
//err = os.Rename("xata/internal/fern-core", "xata/internal/fern-core-old")
//if err != nil {
// log.Fatal(err)
//}
//
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println(wd)

err = os.Mkdir("xata/internal/fern-core-auto", 0755)
if err != nil {
log.Fatal(err)
}

err = os.Chdir("xata/internal/fern-core-auto")
if err != nil {
log.Fatal(err)
}

wd, err = os.Getwd()
if err != nil {
log.Fatal(err)
}
fmt.Println(wd)

//initFernCmdName := "fern"
//initFernCmdArgs := []string{"init", "--openapi", "https://xata.io/api/openapi\\?scope\\=core"}
//
//initFernCmd := exec.Command(initFernCmdName, initFernCmdArgs...)
//
//output, err := initFernCmd.CombinedOutput()
//if err != nil {
// log.Fatal(err)
//}

output, err := executeOSCmd("fern init --openapi https://xata.io/api/openapi\\?scope\\=core")
if err != nil {
log.Fatalf("not able to fern init: %v", err)
}

fmt.Println(output)

err = os.Chdir(wd)
wd, err = os.Getwd()
if err != nil {
log.Fatal(err)
}

fmt.Println(wd)

err = copyFile("xata/internal/code-gen-utils/core-generators.yml", "xata/internal/fern-core-auto/fern/api/generators.yml")
if err != nil {
log.Fatal(err)
}

// fern generate --log-level debug --local
err = os.Chdir("xata/internal/fern-core-auto")
if err != nil {
log.Fatal(err)
}

wd, err = os.Getwd()
if err != nil {
log.Fatal(err)
}

fmt.Println(wd)

err = updateCoreAPISpecs()
if err != nil {
log.Fatal(err)
}
const (
newCorePath = "xata/internal/fern-core-new"
originalCorePath = "xata/internal/fern-core"
scopeCore = "core"
scopeWorkspace = "workspace"
fernInitCoreCmd = "fern init --openapi ../../../internal/docs/core-openapi.json"
coreGeneratorsYaml = "xata/internal/code-gen-utils/core-generators.yml"
fernGenerateCmd = "fern generate --log-level debug --local"
)

output, err = executeOSCmd("fern generate --log-level debug --local")
if err != nil {
log.Fatalf("unable to generate code: %v", err)
func main() {
scope := flag.String("scope", "", "scope is one of: core or workspace")
flag.Parse()

switch *scope {
case scopeCore:
rootWD, err := os.Getwd()
if err != nil {
log.Fatalf("unable to get root wd: %v", err)
}

log.Println("creating new core api folder")
err = os.Mkdir(newCorePath, 0755)
if err != nil {
log.Fatalf("unable to create %v: %v", newCorePath, err)
}

err = os.Chdir(newCorePath)
if err != nil {
log.Fatalf("unable to change dir to %v: %v", newCorePath, err)
}

newCoreWD, err := os.Getwd()
if err != nil {
log.Fatalf("unable to get %v wd: %v", newCoreWD, err)
}

log.Println("initializing fern")
output, err := executeOSCmd(fernInitCoreCmd)
if err != nil {
log.Fatalf("unable to fern init: %v", err)
}

log.Println(output)

err = os.Chdir(rootWD)
if err != nil {
log.Fatalf("unable to change dir to root wd: %v", err)
}

log.Println("updating the generators file")
err = copyFile(coreGeneratorsYaml, newCorePath+"/fern/api/generators.yml")
if err != nil {
log.Fatal(err)
}

err = os.Chdir(newCoreWD)
if err != nil {
log.Fatalf("unable to change dir to %v: %v", newCorePath, err)
}

log.Println("generating code")
output, err = executeOSCmd(fernGenerateCmd)
if err != nil {
log.Fatalf("unable to generate code: %v", err)
}

log.Println(output)

err = os.Chdir(rootWD)
if err != nil {
log.Fatalf("unable to get root wd: %v", err)
}

err = os.RemoveAll(originalCorePath)
if err != nil {
log.Fatalf("unable to remove the original folder: %v", err)
}

err = os.Rename(newCorePath, originalCorePath)
if err != nil {
log.Fatalf("unable to rename the new folder as the original folder")
}

log.Println("success")
case scopeWorkspace:
log.Fatal("not implemented")
default:
log.Fatal("unknown scope: ", *scope)
}

fmt.Println(output)

fmt.Println("hola")
}

func executeOSCmd(cmd string) (string, error) {
Expand Down Expand Up @@ -154,7 +150,7 @@ func copyFile(srcPath, destPath string) error {

func updateCoreAPISpecs() error {
// Read the OpenAPI YAML file
inputFile := "xata/internal/fern-core-auto/fern/api/openapi/openapi.yml"
inputFile := "xata/internal/fern-core-auto/fern/api/openapi/core-openapi.yml"
openAPIData, err := os.ReadFile(inputFile)
if err != nil {
fmt.Println("Error reading OpenAPI file:", err)
Expand All @@ -163,7 +159,12 @@ func updateCoreAPISpecs() error {

// Unmarshal the OpenAPI data into a struct
var openAPI OpenAPI
if err := yaml.Unmarshal(openAPIData, &openAPI); err != nil {
//if err := yaml.Unmarshal(openAPIData, &openAPI); err != nil {
// fmt.Println("Error unmarshaling OpenAPI data:", err)
// return err
//}

if err := json.Unmarshal(openAPIData, &openAPI); err != nil {
fmt.Println("Error unmarshaling OpenAPI data:", err)
return err
}
Expand All @@ -174,14 +175,20 @@ func updateCoreAPISpecs() error {
delete(openAPI.Paths, "/db/{db_branch_name}/migrations/plan")

// Marshal the updated OpenAPI struct back to YAML
updatedOpenAPIData, err := yaml.Marshal(&openAPI)
//updatedOpenAPIData, err := yaml.Marshal(&openAPI)
//if err != nil {
// fmt.Println("Error marshaling updated OpenAPI data:", err)
// return err
//}

updatedOpenAPIData, err := json.Marshal(&openAPI)
if err != nil {
fmt.Println("Error marshaling updated OpenAPI data:", err)
return err
}

// Save the updated OpenAPI data to a new file
outputFile := "xata/internal/fern-core-auto/fern/api/openapi/openapi.yml"
outputFile := "xata/internal/fern-core-auto/fern/api/openapi/core-openapi.yml"
err = os.WriteFile(outputFile, updatedOpenAPIData, 0644)
if err != nil {
fmt.Println("Error saving updated OpenAPI file:", err)
Expand Down
1 change: 1 addition & 0 deletions xata/internal/fern-core/fern/api/openapi/core-openapi.json

Large diffs are not rendered by default.

Loading

0 comments on commit b19abbd

Please sign in to comment.