github.com/orderbynull/buffalo@v0.11.1/generators/grift/generator.go (about)

     1  package grift
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/gobuffalo/buffalo/meta"
     7  	"github.com/markbates/inflect"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // Generator for creating a new grift task
    12  type Generator struct {
    13  	App        meta.App       `json:"app"`
    14  	Name       inflect.Name   `json:"name"`
    15  	Parts      []inflect.Name `json:"parts"`
    16  	Args       []string       `json:"args"`
    17  	Namespaced bool           `json:"namespaced"`
    18  }
    19  
    20  // Last checks if the name is the last of the parts
    21  func (g Generator) Last(n inflect.Name) bool {
    22  	return g.Parts[len(g.Parts)-1] == n
    23  }
    24  
    25  // New generator for grift tasks
    26  func New(args ...string) (Generator, error) {
    27  	g := Generator{
    28  		App:   meta.New("."),
    29  		Args:  args,
    30  		Parts: []inflect.Name{},
    31  	}
    32  	if len(args) > 0 {
    33  		g.Namespaced = strings.Contains(args[0], ":")
    34  
    35  		for _, n := range strings.Split(args[0], ":") {
    36  			g.Parts = append(g.Parts, inflect.Name(n))
    37  		}
    38  		g.Name = inflect.Name(g.Parts[len(g.Parts)-1])
    39  	}
    40  
    41  	return g, g.Validate()
    42  }
    43  
    44  // Validate the generator
    45  func (g Generator) Validate() error {
    46  	if len(g.Args) < 1 {
    47  		return errors.New("you need to provide a name for the grift task")
    48  	}
    49  	return nil
    50  }