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

     1  package resource
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/wawandco/ox/internal/log"
     8  )
     9  
    10  // Generator allows to identify resource as a plugin
    11  type Generator struct{}
    12  
    13  // Name returns the name of the plugin
    14  func (g Generator) Name() string {
    15  	return "buffalo/generate-resource"
    16  }
    17  
    18  // InvocationName is used to identify the generator when
    19  // the generate command is called.
    20  func (g Generator) InvocationName() string {
    21  	return "resource"
    22  }
    23  
    24  // Generate generates the actions, model, templates and migration files from the given attrs
    25  // app/actions/[name].go
    26  // app/actions/[name]_test.go
    27  // app/models/[name].go
    28  // app/models/[name]_test.go
    29  // app/templates/[name]/index.plush.html
    30  // app/templates/[name]/new.plush.html
    31  // app/templates/[name]/edit.plush.html
    32  // app/templates/[name]/show.plush.html
    33  // migrations/[name].up.fizz
    34  // migrations/[name].down.fizz
    35  func (g Generator) Generate(ctx context.Context, root string, args []string) error {
    36  	if len(args) < 3 {
    37  		return fmt.Errorf("no name specified, please use `ox generate resource [name]`")
    38  	}
    39  
    40  	resource := New(root, args[2:])
    41  
    42  	// Generating Templates
    43  	log.Info("Generating Actions...\n")
    44  	if err := resource.GenerateActions(); err != nil {
    45  		return fmt.Errorf("generating actions error: %w", err)
    46  	}
    47  
    48  	// Generating Templates
    49  	log.Info("Generating Templates...\n")
    50  	if err := resource.GenerateTemplates(); err != nil {
    51  		return fmt.Errorf("generating templates error: %w", err)
    52  	}
    53  
    54  	// Generating Model
    55  	log.Info("Generating Model...\n")
    56  	if err := resource.GenerateModel(); err != nil {
    57  		return fmt.Errorf("generating model error: %w", err)
    58  	}
    59  
    60  	// // Generating Migration
    61  	log.Info("Generating Migrations...\n")
    62  	if err := resource.GenerateMigrations(); err != nil {
    63  		return fmt.Errorf("generating migrations error: %w", err)
    64  	}
    65  
    66  	log.Infof("%s resource has been generated successfully \n", resource.originalName)
    67  
    68  	return nil
    69  }