github.com/corylanou/buffalo@v0.8.0/generators/action/action.go (about)

     1  package action
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path/filepath"
     7  	"strings"
     8  
     9  	"github.com/gobuffalo/buffalo/generators"
    10  	"github.com/gobuffalo/makr"
    11  	"github.com/markbates/inflect"
    12  )
    13  
    14  var runningTests bool
    15  
    16  // New action generator
    17  func New(name string, actions []string, data makr.Data) (*makr.Generator, error) {
    18  	g := makr.New()
    19  
    20  	filePath := filepath.Join("actions", fmt.Sprintf("%v.go", data["filename"]))
    21  	actionsTemplate := buildActionsTemplate(filePath)
    22  	testFilePath := filepath.Join("actions", fmt.Sprintf("%v_test.go", data["filename"]))
    23  	testsTemplate := buildTestsTemplate(testFilePath)
    24  	actionsToAdd := findActionsToAdd(name, filePath, actions)
    25  	testsToAdd := findTestsToAdd(name, testFilePath, actions)
    26  
    27  	data["actions"] = actionsToAdd
    28  	data["tests"] = testsToAdd
    29  
    30  	g.Add(makr.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["filename"])), actionsTemplate))
    31  	g.Add(makr.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["filename"])), testsTemplate))
    32  	g.Add(&makr.Func{
    33  		Should: func(data makr.Data) bool { return true },
    34  		Runner: func(root string, data makr.Data) error {
    35  			routes := []string{}
    36  			for _, a := range actions {
    37  				routes = append(routes, fmt.Sprintf("app.GET(\"/%s/%s\", %s)", name, a, data["namespace"].(string)+inflect.Camelize(a)))
    38  			}
    39  			return generators.AddInsideAppBlock(routes...)
    40  		},
    41  	})
    42  	addTemplateFiles(actionsToAdd, data)
    43  
    44  	if !runningTests {
    45  		g.Add(makr.NewCommand(makr.GoFmt()))
    46  	}
    47  	return g, nil
    48  }
    49  
    50  func buildActionsTemplate(filePath string) string {
    51  	actionsTemplate := rActionFileT
    52  	fileContents, err := ioutil.ReadFile(filePath)
    53  	if err == nil {
    54  		actionsTemplate = string(fileContents)
    55  	}
    56  
    57  	actionsTemplate = actionsTemplate + `
    58  {{ range $action := .actions }}
    59  // {{$.namespace}}{{camelize $action}} default implementation.
    60  func {{$.namespace}}{{camelize $action}}(c buffalo.Context) error {
    61  	return c.Render(200, r.HTML("{{$.filename}}/{{underscore $action}}.html"))
    62  }
    63  {{end}}`
    64  	return actionsTemplate
    65  }
    66  
    67  func buildTestsTemplate(filePath string) string {
    68  	testsTemplate := `package actions_test
    69  
    70  import (
    71  	"testing"
    72  
    73  	"github.com/stretchr/testify/require"
    74  )
    75  	`
    76  	fileContents, err := ioutil.ReadFile(filePath)
    77  	if err == nil {
    78  		testsTemplate = string(fileContents)
    79  	}
    80  
    81  	testsTemplate = testsTemplate + `
    82  {{ range $action := .tests}}
    83  func (as *ActionSuite) Test_{{$.namespace}}_{{camelize $action}}() {
    84  	as.Fail("Not Implemented!")
    85  }
    86  
    87  {{end}}`
    88  	return testsTemplate
    89  }
    90  
    91  func addTemplateFiles(actionsToAdd []string, data makr.Data) {
    92  	for _, action := range actionsToAdd {
    93  		vg := makr.New()
    94  		viewPath := filepath.Join("templates", fmt.Sprintf("%s", data["filename"]), fmt.Sprintf("%s.html", inflect.Underscore(action)))
    95  		vg.Add(makr.NewFile(viewPath, rViewT))
    96  		vg.Run(".", makr.Data{
    97  			"namespace": data["namespace"],
    98  			"action":    inflect.Camelize(action),
    99  		})
   100  	}
   101  }
   102  
   103  func findActionsToAdd(name, path string, actions []string) []string {
   104  	fileContents, err := ioutil.ReadFile(path)
   105  	if err != nil {
   106  		fileContents = []byte("")
   107  	}
   108  
   109  	actionsToAdd := []string{}
   110  
   111  	for _, action := range actions {
   112  		funcSignature := fmt.Sprintf("func %s%s(c buffalo.Context) error", inflect.Camelize(name), inflect.Camelize(action))
   113  		if strings.Contains(string(fileContents), funcSignature) {
   114  			fmt.Printf("--> [warning] skipping %v%v since it already exists\n", inflect.Camelize(name), inflect.Camelize(action))
   115  			continue
   116  		}
   117  
   118  		actionsToAdd = append(actionsToAdd, action)
   119  	}
   120  
   121  	return actionsToAdd
   122  }
   123  
   124  func findTestsToAdd(name, path string, actions []string) []string {
   125  	fileContents, err := ioutil.ReadFile(path)
   126  	if err != nil {
   127  		fileContents = []byte("")
   128  	}
   129  
   130  	actionsToAdd := []string{}
   131  
   132  	for _, action := range actions {
   133  		funcSignature := fmt.Sprintf("func Test_%v_%v(c buffalo.Context) error", inflect.Camelize(name), inflect.Camelize(action))
   134  		if strings.Contains(string(fileContents), funcSignature) {
   135  			fmt.Printf("--> [warning] skipping Test_%v_%v since it already exists\n", inflect.Camelize(name), inflect.Camelize(action))
   136  			continue
   137  		}
   138  
   139  		actionsToAdd = append(actionsToAdd, action)
   140  	}
   141  
   142  	return actionsToAdd
   143  }