Skip to content

Commit

Permalink
ws wip
Browse files Browse the repository at this point in the history
  • Loading branch information
omerdemirok committed Nov 11, 2023
1 parent b19abbd commit c861a50
Show file tree
Hide file tree
Showing 527 changed files with 12,514 additions and 1,734 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ help: ## Display help
.PHONY: generate-core-code
generate-core-code:
go run xata/internal/core_code_gen.go -scope=core

.PHONY: generate-core-workspace
generate-core-workspace:
go run xata/internal/core_code_gen.go -scope=workspace
11 changes: 11 additions & 0 deletions xata/internal/code-gen-utils/workspace-generators.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default-group: local
groups:
local:
generators:
- name: fernapi/fern-go-sdk
version: 0.0.14
output:
location: local-file-system
path: ../../generated/go
config:
importPath: github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go
202 changes: 122 additions & 80 deletions xata/internal/core_code_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,34 @@ type OpenAPI struct {
XTagGroups []map[string]any `yaml:"x-tagGroups" json:"x-tagGroups"`
}

type scope int

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"
core scope = iota
workspace
)

const (
originalCorePath = "xata/internal/fern-core"
coreAPIspecs = "internal/docs/core-openapi.json"
workspaceAPIspecs = "internal/docs/workspace-openapi.json"
originalWorkspacePath = "xata/internal/fern-workspace"
scopeCore = "core"
scopeWorkspace = "workspace"
coreGeneratorsYamlFile = "core-generators.yml"
workspaceGeneratorsYamlFile = "workspace-generators.yml"
utilsPath = "xata/internal/code-gen-utils/"

fernGenerateCmd = "fern generate --log-level debug --local"
fernInitCmd = "fern init --openapi ../../../"
newSuffix = "-new"
)

var (
newCorePath = originalCorePath + newSuffix
newWorkspacePath = originalWorkspacePath + newSuffix
coreGeneratorsYamlPath = utilsPath + coreGeneratorsYamlFile
workspaceGeneratorsYamlPath = utilsPath + workspaceGeneratorsYamlFile
)

func main() {
Expand All @@ -38,80 +58,104 @@ func main() {

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

log.Println("creating new core api folder")
err = os.Mkdir(newCorePath, 0755)
log.Println("success generating core code")
case scopeWorkspace:
err := generateFernCode(workspace, newWorkspacePath, originalWorkspacePath, workspaceAPIspecs, workspaceGeneratorsYamlPath)
if err != nil {
log.Fatalf("unable to create %v: %v", newCorePath, err)
log.Fatal(err)
}

err = os.Chdir(newCorePath)
if err != nil {
log.Fatalf("unable to change dir to %v: %v", newCorePath, err)
}
log.Println("success generating workspace code")
default:
log.Fatal("unknown scope: ", *scope)
}
}

newCoreWD, err := os.Getwd()
if err != nil {
log.Fatalf("unable to get %v wd: %v", newCoreWD, err)
}
func generateFernCode(scope scope, newPath, originalPath, apiSPECS, generatorsYAML string) error {
rootWD, err := os.Getwd()
if err != nil {
return fmt.Errorf("unable to get root wd: %v", err)
}

log.Println("initializing fern")
output, err := executeOSCmd(fernInitCoreCmd)
if err != nil {
log.Fatalf("unable to fern init: %v", err)
}
log.Println("creating new folder")
err = os.Mkdir(newPath, 0755)
if err != nil {
return fmt.Errorf("unable to create %v: %v", newPath, err)
}

log.Println(output)
err = os.Chdir(newPath)
if err != nil {
return fmt.Errorf("unable to change dir to %v: %v", newPath, err)
}

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

log.Println("updating the generators file")
err = copyFile(coreGeneratorsYaml, newCorePath+"/fern/api/generators.yml")
if err != nil {
log.Fatal(err)
}
log.Println("initializing fern")
output, err := executeOSCmd(fernInitCmd + apiSPECS)
if err != nil {
return fmt.Errorf("unable to fern init: %v", err)
}

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

log.Println("generating code")
output, err = executeOSCmd(fernGenerateCmd)
err = os.Chdir(rootWD)
if err != nil {
return fmt.Errorf("unable to change dir to root wd: %v", err)
}

switch scope {
case core:
log.Println("no action needed for core")
case workspace:
err = updateWorkspaceAPISpecs(newWorkspacePath + "/fern/api/openapi/workspace-openapi.json")
if err != nil {
log.Fatalf("unable to generate code: %v", err)
return fmt.Errorf("unable to update workspace API specs: %v", err)
}
}

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

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

err = os.RemoveAll(originalCorePath)
if err != nil {
log.Fatalf("unable to remove the original folder: %v", err)
}
log.Println("generating code")
output, err = executeOSCmd(fernGenerateCmd)
if err != nil {
return fmt.Errorf("unable to generate code: %v", err)
}

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

log.Println("success")
case scopeWorkspace:
log.Fatal("not implemented")
default:
log.Fatal("unknown scope: ", *scope)
err = os.Chdir(rootWD)
if err != nil {
return fmt.Errorf("unable to get root wd: %v", err)
}

err = os.RemoveAll(originalPath)
if err != nil {
return fmt.Errorf("unable to remove the original folder: %v", err)
}

err = os.Rename(newPath, originalPath)
if err != nil {
return fmt.Errorf("unable to rename the new folder as the original folder")
}

return nil
}

func executeOSCmd(cmd string) (string, error) {
Expand Down Expand Up @@ -148,38 +192,37 @@ func copyFile(srcPath, destPath string) error {
return nil
}

func updateCoreAPISpecs() error {
func updateWorkspaceAPISpecs(filePath string) error {
// Read the OpenAPI YAML file
inputFile := "xata/internal/fern-core-auto/fern/api/openapi/core-openapi.yml"
openAPIData, err := os.ReadFile(inputFile)
openAPIData, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading OpenAPI file:", err)
fmt.Println("error reading OpenAPI file:", err)
return err
}

// Unmarshal the OpenAPI data into a struct
var openAPI OpenAPI
//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)
if err = json.Unmarshal(openAPIData, &openAPI); err != nil {
fmt.Println("error unmarshaling OpenAPI data:", err)
return err
}

// Remove path definitions under /foo
log.Println("removing deprecated path definitions")
delete(openAPI.Paths, "/db/{db_branch_name}/migrations")
delete(openAPI.Paths, "/db/{db_branch_name}/migrations/execute")
delete(openAPI.Paths, "/db/{db_branch_name}/migrations/plan")
columnEnums := openAPI.Components["schemas"].(map[string]any)["Column"].(map[string]any)["properties"].(map[string]any)["type"].(map[string]any)["enum"].([]any)
var newColumnEnums []string
for _, e := range columnEnums {
if e.(string) == "file[]" {
newColumnEnums = append(newColumnEnums, "fileArray")
} else {
newColumnEnums = append(newColumnEnums, e.(string))
}
}

// Marshal the updated OpenAPI struct back to YAML
//updatedOpenAPIData, err := yaml.Marshal(&openAPI)
//if err != nil {
// fmt.Println("Error marshaling updated OpenAPI data:", err)
// return err
//}
openAPI.Components["schemas"].(map[string]any)["Column"].(map[string]any)["properties"].(map[string]any)["type"].(map[string]any)["enum"] = newColumnEnums

updatedOpenAPIData, err := json.Marshal(&openAPI)
if err != nil {
Expand All @@ -188,13 +231,12 @@ func updateCoreAPISpecs() error {
}

// Save the updated OpenAPI data to a new file
outputFile := "xata/internal/fern-core-auto/fern/api/openapi/core-openapi.yml"
err = os.WriteFile(outputFile, updatedOpenAPIData, 0644)
err = os.WriteFile(filePath, updatedOpenAPIData, 0644)
if err != nil {
fmt.Println("Error saving updated OpenAPI file:", err)
return err
}

fmt.Println("OpenAPI file updated and saved as", outputFile)
log.Print("OpenAPI file updated and saved as", filePath)
return nil
}
2 changes: 1 addition & 1 deletion xata/internal/fern-core/generated/go/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was auto-generated by Fern from our API Definition.

// Authentication and API Key management.
// Manage user invites.
package api
11 changes: 11 additions & 0 deletions xata/internal/fern-workspace-back/fern/api/generators.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default-group: local
groups:
local:
generators:
- name: fernapi/fern-go-sdk
version: 0.0.14
output:
location: local-file-system
path: ../../generated/go
config:
importPath: github.com/xataio/xata-go/xata/internal/fern-workspace/generated/go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Apache-2.0

// This file was auto-generated by Fern from our API Definition.

package api

// AddGitBranchesEntryRequest is an in-lined request used by the AddGitBranchesEntry endpoint.
type AddGitBranchesEntryRequest struct {
// The name of the Git branch.
GitBranch string `json:"gitBranch"`
XataBranch BranchName `json:"xataBranch"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: Apache-2.0

// This file was auto-generated by Fern from our API Definition.

package api

type AddGitBranchesEntryResponse struct {
// Warning message
Warning *string `json:"warning,omitempty"`
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: Apache-2.0

// This file was auto-generated by Fern from our API Definition.

package api

type AddTableColumnResponse struct {
// <span style="white-space: nowrap">`non-empty`</span>
MigrationId string `json:"migrationID"`
ParentMigrationId string `json:"parentMigrationID"`
Status MigrationStatus `json:"status,omitempty"`
}
Loading

0 comments on commit c861a50

Please sign in to comment.