github.com/bketelsen/buffalo@v0.9.5/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  	handlersToAdd := findHandlersToAdd(name, filepath.Join("actions", "app.go"), actions)
    27  
    28  	data["actions"] = actionsToAdd
    29  	data["tests"] = testsToAdd
    30  
    31  	g.Add(makr.NewFile(filepath.Join("actions", fmt.Sprintf("%s.go", data["filename"])), actionsTemplate))
    32  	g.Add(makr.NewFile(filepath.Join("actions", fmt.Sprintf("%s_test.go", data["filename"])), testsTemplate))
    33  	g.Add(&makr.Func{
    34  		Should: func(data makr.Data) bool { return true },
    35  		Runner: func(root string, data makr.Data) error {
    36  			routes := []string{}
    37  			for _, a := range handlersToAdd {
    38  				routes = append(routes, fmt.Sprintf("app.%s(\"/%s/%s\", %s)", data["method"], name, a, data["namespace"].(string)+inflect.Camelize(a)))
    39  			}
    40  			return generators.AddInsideAppBlock(routes...)
    41  		},
    42  	})
    43  
    44  	if skipTemplates := data["skipTemplate"].(bool); !skipTemplates {
    45  		addTemplateFiles(actionsToAdd, data)
    46  	}
    47  
    48  	return g, nil
    49  }
    50  
    51  func buildActionsTemplate(filePath string) string {
    52  	actionsTemplate := rActionFileT
    53  	fileContents, err := ioutil.ReadFile(filePath)
    54  	if err == nil {
    55  		actionsTemplate = string(fileContents)
    56  	}
    57  
    58  	actionsTemplate = actionsTemplate + `
    59  {{ range $action := .actions }}
    60  // {{$.namespace}}{{camelize $action}} default implementation.
    61  func {{$.namespace}}{{camelize $action}}(c buffalo.Context) error {
    62  	return c.Render(200, r.HTML("{{$.filename}}/{{underscore $action}}.html"))
    63  }
    64  {{end}}`
    65  	return actionsTemplate
    66  }
    67  
    68  func buildTestsTemplate(filePath string) string {
    69  	testsTemplate := `package actions
    70  
    71  import (
    72  	"testing"
    73  
    74  	"github.com/stretchr/testify/require"
    75  )
    76  	`
    77  	fileContents, err := ioutil.ReadFile(filePath)
    78  	if err == nil {
    79  		testsTemplate = string(fileContents)
    80  	}
    81  
    82  	testsTemplate = testsTemplate + `
    83  {{ range $action := .tests}}
    84  func (as *ActionSuite) Test_{{$.namespace}}_{{camelize $action}}() {
    85  	as.Fail("Not Implemented!")
    86  }
    87  
    88  {{end}}`
    89  	return testsTemplate
    90  }
    91  
    92  func addTemplateFiles(actionsToAdd []string, data makr.Data) {
    93  	for _, action := range actionsToAdd {
    94  		vg := makr.New()
    95  		viewPath := filepath.Join("templates", fmt.Sprintf("%s", data["filename"]), fmt.Sprintf("%s.html", inflect.Underscore(action)))
    96  		vg.Add(makr.NewFile(viewPath, rViewT))
    97  		vg.Run(".", makr.Data{
    98  			"namespace": data["namespace"],
    99  			"action":    inflect.Camelize(action),
   100  		})
   101  	}
   102  }
   103  
   104  func findActionsToAdd(name, path string, actions []string) []string {
   105  	fileContents, err := ioutil.ReadFile(path)
   106  	if err != nil {
   107  		fileContents = []byte("")
   108  	}
   109  
   110  	actionsToAdd := []string{}
   111  
   112  	for _, action := range actions {
   113  		funcSignature := fmt.Sprintf("func %s%s(c buffalo.Context) error", inflect.Camelize(name), inflect.Camelize(action))
   114  		if strings.Contains(string(fileContents), funcSignature) {
   115  			fmt.Printf("--> [warning] skipping %v%v since it already exists\n", inflect.Camelize(name), inflect.Camelize(action))
   116  			continue
   117  		}
   118  
   119  		actionsToAdd = append(actionsToAdd, action)
   120  	}
   121  
   122  	return actionsToAdd
   123  }
   124  
   125  func findHandlersToAdd(name, path string, actions []string) []string {
   126  	fileContents, err := ioutil.ReadFile(path)
   127  	if err != nil {
   128  		fileContents = []byte("")
   129  	}
   130  
   131  	handlersToAdd := []string{}
   132  
   133  	for _, action := range actions {
   134  		funcSignature := fmt.Sprintf("app.GET(\"/%s/%s\", %s)", name, action, inflect.Camelize(inflect.Pluralize(name)+"_"+action))
   135  		if strings.Contains(string(fileContents), funcSignature) {
   136  			fmt.Printf("--> [warning] skipping %s from app.go since it already exists\n", funcSignature)
   137  			continue
   138  		}
   139  
   140  		handlersToAdd = append(handlersToAdd, action)
   141  	}
   142  
   143  	return handlersToAdd
   144  }
   145  
   146  func findTestsToAdd(name, path string, actions []string) []string {
   147  	fileContents, err := ioutil.ReadFile(path)
   148  	if err != nil {
   149  		fileContents = []byte("")
   150  	}
   151  
   152  	actionsToAdd := []string{}
   153  
   154  	for _, action := range actions {
   155  		funcSignature := fmt.Sprintf("func (as *ActionSuite) Test_%v_%v() {", inflect.Camelize(name), inflect.Camelize(action))
   156  		if strings.Contains(string(fileContents), funcSignature) {
   157  			fmt.Printf("--> [warning] skipping Test_%v_%v since it already exists\n", inflect.Camelize(name), inflect.Camelize(action))
   158  			continue
   159  		}
   160  
   161  		actionsToAdd = append(actionsToAdd, action)
   162  	}
   163  
   164  	return actionsToAdd
   165  }