github.com/wawandco/ox@v0.13.6-0.20230809142027-913b3d837f2a/plugins/tools/ox/resource/resource.go (about)

     1  package resource
     2  
     3  import (
     4  	_ "embed"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"github.com/gobuffalo/flect/name"
    10  
    11  	"github.com/wawandco/ox/internal/info"
    12  	"github.com/wawandco/ox/internal/source"
    13  	"github.com/wawandco/ox/plugins/tools/ox/model"
    14  	"github.com/wawandco/ox/plugins/tools/soda/fizz"
    15  )
    16  
    17  var (
    18  	//go:embed templates/action.go.tmpl
    19  	actionTemplate string
    20  	//go:embed templates/action_test.go.tmpl
    21  	actionTestTemplate string
    22  	//go:embed templates/index.html.tmpl
    23  	indexHTMLTemplate string
    24  	//go:embed templates/new.html.tmpl
    25  	newHTMLTemplate string
    26  	//go:embed templates/edit.html.tmpl
    27  	editHTMLTemplate string
    28  	//go:embed templates/show.html.tmpl
    29  	showHTMLTemplate string
    30  	//go:embed templates/form.html.tmpl
    31  	formHTMLTemplate string
    32  )
    33  
    34  // Resource model struct
    35  type Resource struct {
    36  	Actions  []name.Ident
    37  	Name     name.Ident
    38  	Model    model.Model
    39  	ModelPkg string
    40  	Args     []string
    41  
    42  	originalArgs []string
    43  	originalName string
    44  	root         string
    45  }
    46  
    47  // New creates a new instance of Resource
    48  func New(root string, args []string) *Resource {
    49  	module := info.ModuleName()
    50  	if module == "" {
    51  		module = root + "/app/models"
    52  	}
    53  
    54  	modelsPath := filepath.Join(root, "app", "models")
    55  	model := model.New(modelsPath, args[0], args[1:])
    56  	actions := []name.Ident{
    57  		name.New("list"),
    58  		name.New("show"),
    59  		name.New("new"),
    60  		name.New("create"),
    61  		name.New("edit"),
    62  		name.New("update"),
    63  		name.New("destroy"),
    64  	}
    65  
    66  	return &Resource{
    67  		Actions:  actions,
    68  		Args:     args[1:],
    69  		Model:    model,
    70  		ModelPkg: module + "/app/models",
    71  		Name:     name.New(args[0]),
    72  
    73  		originalArgs: args[0:],
    74  		originalName: args[0],
    75  		root:         root,
    76  	}
    77  }
    78  
    79  // GenerateActions generates the actions for the resource
    80  func (r *Resource) GenerateActions() error {
    81  	actionName := r.Name.Proper().Pluralize().Underscore().String()
    82  	dirPath := filepath.Join(r.root, "app", "actions")
    83  	actions := map[string]string{
    84  		actionName:           actionTemplate,
    85  		actionName + "_test": actionTestTemplate,
    86  	}
    87  
    88  	for name, content := range actions {
    89  		filename := name + ".go"
    90  		path := filepath.Join(dirPath, filename)
    91  		err := source.Build(path, content, r)
    92  		if err != nil {
    93  			return err
    94  		}
    95  	}
    96  
    97  	return nil
    98  }
    99  
   100  // GenerateModel generates the migrations for the resource
   101  func (r *Resource) GenerateMigrations() error {
   102  	migrationPath := filepath.Join(r.root, "migrations")
   103  	creator := fizz.Creator{}
   104  
   105  	if err := creator.Create(migrationPath, r.originalName, r.originalArgs); err != nil {
   106  		return fmt.Errorf("failed creating migrations: %w", err)
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  // GenerateModel generates the model for the resource
   113  func (r *Resource) GenerateModel() error {
   114  	if err := r.Model.Create(); err != nil {
   115  		return fmt.Errorf("error creating model: %w", err)
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  // GenerateModel generates the templates for the resource
   122  func (r *Resource) GenerateTemplates() error {
   123  	templates := map[string]string{
   124  		"index": indexHTMLTemplate,
   125  		"new":   newHTMLTemplate,
   126  		"edit":  editHTMLTemplate,
   127  		"show":  showHTMLTemplate,
   128  		"form":  formHTMLTemplate,
   129  	}
   130  
   131  	dirPath := filepath.Join(r.root, "app", "templates", r.Name.Underscore().String())
   132  	if _, err := os.Stat(dirPath); os.IsNotExist(err) {
   133  		err = os.Mkdir(dirPath, 0755)
   134  		if err != nil {
   135  			return err
   136  		}
   137  	}
   138  
   139  	for name, content := range templates {
   140  		filename := name + ".plush.html"
   141  		path := filepath.Join(dirPath, filename)
   142  
   143  		err := source.Build(path, content, r)
   144  		if err != nil {
   145  			return err
   146  		}
   147  	}
   148  
   149  	return nil
   150  }