cmd/entc: add generate.go file to env init (#402)

* cmd/entc: add generate.go file to env init

* doc: update getting-started and codegen documentation
This commit is contained in:
Ariel Mashraki
2020-03-24 19:23:30 +02:00
committed by GitHub
parent abee904420
commit a2ea5bfbee
8 changed files with 127 additions and 42 deletions

View File

@@ -27,8 +27,8 @@ func main() {
cmd.AddCommand(
func() *cobra.Command {
var (
path string
cmd = &cobra.Command{
target string
cmd = &cobra.Command{
Use: "init [flags] [schemas]",
Short: "initialize an environment with zero or more schemas",
Example: examples(
@@ -44,21 +44,12 @@ func main() {
return nil
},
Run: func(cmd *cobra.Command, names []string) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
}
err := initEnv(target, names)
failOnErr(err)
for _, name := range names {
b := bytes.NewBuffer(nil)
failOnErr(tmpl.Execute(b, name))
target := filepath.Join(path, strings.ToLower(name+".go"))
failOnErr(ioutil.WriteFile(target, b.Bytes(), 0644))
}
},
}
)
cmd.Flags().StringVar(&path, "target", "ent/schema", "target directory for schemas")
cmd.Flags().StringVar(&target, "target", defaultSchema, "target directory for schemas")
return cmd
}(),
&cobra.Command{
@@ -119,28 +110,6 @@ func main() {
cmd.Execute()
}
// schema template for the "init" command.
var tmpl = template.Must(template.New("schema").
Parse(`package schema
import "github.com/facebookincubator/ent"
// {{ . }} holds the schema definition for the {{ . }} entity.
type {{ . }} struct {
ent.Schema
}
// Fields of the {{ . }}.
func ({{ . }}) Fields() []ent.Field {
return nil
}
// Edges of the {{ . }}.
func ({{ . }}) Edges() []ent.Edge {
return nil
}
`))
// custom implementation for pflag.
type idType field.Type
@@ -179,6 +148,69 @@ func (idType) String() string {
return field.TypeInt.String()
}
// initEnv initialize an environment for ent codegen.
func initEnv(target string, names []string) error {
if err := createDir(target); err != nil {
return err
}
for _, name := range names {
b := bytes.NewBuffer(nil)
failOnErr(tmpl.Execute(b, name))
target := filepath.Join(target, strings.ToLower(name+".go"))
failOnErr(ioutil.WriteFile(target, b.Bytes(), 0644))
}
return nil
}
func createDir(target string) error {
_, err := os.Stat(target)
if err == nil || !os.IsNotExist(err) {
return err
}
if err := os.MkdirAll(target, os.ModePerm); err != nil {
return fmt.Errorf("creating schema directory: %v", err)
}
if target != defaultSchema {
return nil
}
if err := ioutil.WriteFile("ent/generate.go", []byte(genFile), 0644); err != nil {
return fmt.Errorf("creating generate.go file: %v", err)
}
return nil
}
// schema template for the "init" command.
var tmpl = template.Must(template.New("schema").
Parse(`package schema
import "github.com/facebookincubator/ent"
// {{ . }} holds the schema definition for the {{ . }} entity.
type {{ . }} struct {
ent.Schema
}
// Fields of the {{ . }}.
func ({{ . }}) Fields() []ent.Field {
return nil
}
// Edges of the {{ . }}.
func ({{ . }}) Edges() []ent.Edge {
return nil
}
`))
const (
// default schema package path.
defaultSchema = "ent/schema"
// ent/generate.go file used for "go generate" command.
genFile = `package ent
//go:generate go run github.com/facebookincubator/ent/cmd/entc generate ./schema
`
)
func failOnErr(err error) {
if err != nil {
fmt.Fprint(os.Stderr, err.Error())

35
cmd/entc/entc_test.go Normal file
View File

@@ -0,0 +1,35 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (
"bytes"
"os"
"os/exec"
"testing"
"github.com/stretchr/testify/require"
)
func TestCmd(t *testing.T) {
defer os.RemoveAll("ent")
cmd := exec.Command("go", "run", "github.com/facebookincubator/ent/cmd/entc", "init", "User")
stderr := bytes.NewBuffer(nil)
cmd.Stderr = stderr
require.NoError(t, cmd.Run(), stderr.String())
_, err := os.Stat("ent/generate.go")
require.NoError(t, err)
_, err = os.Stat("ent/schema/user.go")
require.NoError(t, err)
cmd = exec.Command("go", "run", "github.com/facebookincubator/ent/cmd/entc", "generate", "./ent/schema")
stderr = bytes.NewBuffer(nil)
cmd.Stderr = stderr
require.NoError(t, cmd.Run(), stderr.String())
_, err = os.Stat("ent/user.go")
require.NoError(t, err)
}

View File

@@ -1,3 +1,7 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (

View File

@@ -1,3 +1,7 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (

View File

@@ -1,3 +1,7 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (

View File

@@ -1,3 +1,7 @@
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package main
import (