github.com/cheikhshift/buffalo@v0.9.5/generators/grift/grift.go (about)

     1  package grift
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/gobuffalo/buffalo/generators"
     9  	"github.com/gobuffalo/makr"
    10  )
    11  
    12  //New allows to create a new grift task generator
    13  func New(data makr.Data) (*makr.Generator, error) {
    14  	g := makr.New()
    15  
    16  	files, err := generators.Find(filepath.Join(generators.TemplatesPath, "grift"))
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	path := filepath.Join("grifts", data["filename"].(string))
    22  	file := files[0]
    23  
    24  	if _, err := os.Stat(path); os.IsNotExist(err) {
    25  		g.Add(makr.NewFile(path, file.Body))
    26  	} else {
    27  		template, err := ioutil.ReadFile(path)
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  
    32  		g.Add(makr.NewFile(path, string(template)+existsTmpl))
    33  	}
    34  
    35  	return g, nil
    36  }
    37  
    38  var existsTmpl = `
    39  {{ if .plainTask -}}
    40      var _ = Desc("{{.taskName}}", "TODO")
    41      var _ = Add("{{.taskName}}", func(c *Context) error {
    42          return nil
    43      })
    44  {{ else }}
    45      {{ $last := .last }}
    46      {{ range $index, $element := .parts }}
    47          {{ $isLast := eq $index $last }}
    48          {{ if not $isLast }}
    49              {{if eq $index 0}}
    50                  var _ = Namespace("{{$element}}", func(){
    51              {{ else }}
    52                  Namespace("{{$element}}", func(){
    53              {{end}}
    54          {{ else }}
    55              Desc("{{$element}}", "TODO")
    56              Add("{{$element}}", func(c *Context) error{
    57                  return nil
    58              })
    59          {{ end }}
    60      {{ end }}
    61  
    62      {{ range $index, $element := .parts }}
    63          {{ if $index }} }) {{ end }}
    64      {{ end }}
    65  
    66  {{ end }}`