Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
rewrites resource generator with genny (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates authored Jan 8, 2019
1 parent 6f9f8b4 commit 15da6ad
Show file tree
Hide file tree
Showing 49 changed files with 1,221 additions and 504 deletions.
4 changes: 1 addition & 3 deletions buffalo/cmd/filetests/generate_resource_nested_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
},
{
"path": "locales/admin/planes.en-us.yaml",
"contains": [
"translation: \"Plane was successfully created.\""
]
"absent": true
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
},
{
"path": "locales/admin/users.en-us.yaml",
"contains": [
"translation: \"AdminUser was successfully created.\""
]
"absent": true
},
{
"path": "models/admin_user.go",
Expand Down
61 changes: 36 additions & 25 deletions buffalo/cmd/generate/resource.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package generate

import (
"github.com/pkg/errors"
"context"

"github.com/gobuffalo/buffalo/generators/resource"
"github.com/gobuffalo/flect/name"
"github.com/gobuffalo/makr"
"github.com/gobuffalo/buffalo/genny/resource"
"github.com/gobuffalo/genny"
"github.com/gobuffalo/genny/movinglater/attrs"
"github.com/gobuffalo/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

var resourceOptions = struct {
SkipMigration bool
SkipModel bool
SkipTemplates bool
ModelName string
Name string
}{}
*resource.Options
Verbose bool
DryRun bool
}{
Options: &resource.Options{},
}

// ResourceCmd generates a new actions/resource file and a stub test.
var ResourceCmd = &cobra.Command{
Expand All @@ -24,35 +26,44 @@ var ResourceCmd = &cobra.Command{
Aliases: []string{"r"},
Short: "Generate a new actions/resource file",
RunE: func(cmd *cobra.Command, args []string) error {
o, err := resource.New(resourceOptions.Name, args...)
if err != nil {
return errors.WithStack(err)
if len(args) == 0 {
return errors.New("you must supply a name")
}
ctx := context.Background()
run := genny.WetRunner(ctx)
if resourceOptions.DryRun {
run = genny.DryRunner(ctx)
}
if o.App.AsAPI {
resourceOptions.SkipTemplates = true

if resourceOptions.Verbose {
lg := logger.New(logger.DebugLevel)
run.Logger = lg
}

if len(resourceOptions.Name) == 0 {
resourceOptions.Name = args[0]
}
o.SkipModel = resourceOptions.SkipModel
o.SkipMigration = resourceOptions.SkipMigration
o.SkipTemplates = resourceOptions.SkipTemplates
if resourceOptions.ModelName != "" {
o.UseModel = true
o.Model = name.New(resourceOptions.ModelName)
ats, err := attrs.ParseArgs(args[0:]...)
if err != nil {
return errors.WithStack(err)
}
resourceOptions.Attrs = ats

if err := o.Validate(); err != nil {
if err := run.WithNew(resource.New(resourceOptions.Options)); err != nil {
return err
}

return o.Run(".", makr.Data{})
return run.Run()
},
}

func init() {
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipMigration, "skip-migration", "s", false, "tells resource generator not-to add model migration")
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipModel, "skip-model", "", false, "tells resource generator not to generate model nor migrations")
ResourceCmd.Flags().BoolVarP(&resourceOptions.SkipTemplates, "skip-templates", "", false, "tells resource generator not to generate templates for the resource")
ResourceCmd.Flags().StringVarP(&resourceOptions.ModelName, "use-model", "", "", "tells resource generator to reference an existing model in generated code")
ResourceCmd.Flags().StringVarP(&resourceOptions.Model, "use-model", "", "", "tells resource generator to reference an existing model in generated code")
ResourceCmd.Flags().StringVarP(&resourceOptions.Name, "name", "n", "", "allows to define a different model name for the resource being generated.")
ResourceCmd.Flags().BoolVarP(&resourceOptions.DryRun, "dry-run", "d", false, "dry run")
ResourceCmd.Flags().BoolVarP(&resourceOptions.Verbose, "verbose", "v", false, "verbosely print out the go get commands")
}

const resourceExamples = `$ buffalo g resource users
Expand Down
61 changes: 0 additions & 61 deletions generators/resource/generator.go

This file was deleted.

34 changes: 0 additions & 34 deletions generators/resource/generator_test.go

This file was deleted.

37 changes: 0 additions & 37 deletions generators/resource/props.go

This file was deleted.

85 changes: 0 additions & 85 deletions generators/resource/resource.go

This file was deleted.

Loading

0 comments on commit 15da6ad

Please sign in to comment.