github.com/brycereitano/goa@v0.0.0-20170315073847-8ffa6c85e265/goagen/gen_controller/generator.go (about)

     1  package gencontroller
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/goadesign/goa/design"
    10  	"github.com/goadesign/goa/goagen/codegen"
    11  	"github.com/goadesign/goa/goagen/gen_main"
    12  	"github.com/goadesign/goa/goagen/utils"
    13  )
    14  
    15  //NewGenerator returns an initialized instance of a JavaScript Client Generator
    16  func NewGenerator(options ...Option) *Generator {
    17  	g := &Generator{}
    18  
    19  	for _, option := range options {
    20  		option(g)
    21  	}
    22  
    23  	return g
    24  }
    25  
    26  // Generator is the application code generator.
    27  type Generator struct {
    28  	API       *design.APIDefinition // The API definition
    29  	OutDir    string                // Path to output directory
    30  	DesignPkg string                // Path to design package, only used to mark generated files.
    31  	AppPkg    string                // Name of generated "app" package
    32  	Force     bool                  // Whether to override existing files
    33  	Pkg       string                // Name of the generated package
    34  	Resource  string                // Name of the generated file
    35  	genfiles  []string              // Generated files
    36  }
    37  
    38  // Generate is the generator entry point called by the meta generator.
    39  func Generate() (files []string, err error) {
    40  	var (
    41  		outDir, designPkg, appPkg, ver, res, pkg string
    42  		force                                    bool
    43  	)
    44  
    45  	set := flag.NewFlagSet("controller", flag.PanicOnError)
    46  	set.StringVar(&outDir, "out", "", "")
    47  	set.StringVar(&designPkg, "design", "", "")
    48  	set.StringVar(&appPkg, "app-pkg", "app", "")
    49  	set.StringVar(&pkg, "pkg", "main", "")
    50  	set.StringVar(&res, "res", "", "")
    51  	set.StringVar(&ver, "version", "", "")
    52  	set.BoolVar(&force, "force", false, "")
    53  	set.Bool("notest", false, "")
    54  	set.Parse(os.Args[1:])
    55  
    56  	if err := codegen.CheckVersion(ver); err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	g := &Generator{OutDir: outDir, DesignPkg: designPkg, AppPkg: appPkg, Force: force, API: design.Design, Pkg: pkg, Resource: res}
    61  
    62  	return g.Generate()
    63  }
    64  
    65  // Generate produces the skeleton controller service factory.
    66  func (g *Generator) Generate() (_ []string, err error) {
    67  	if g.API == nil {
    68  		return nil, fmt.Errorf("missing API definition, make sure design is properly initialized")
    69  	}
    70  
    71  	go utils.Catch(nil, func() { g.Cleanup() })
    72  
    73  	defer func() {
    74  		if err != nil {
    75  			g.Cleanup()
    76  		}
    77  	}()
    78  
    79  	if g.AppPkg == "" {
    80  		g.AppPkg = "app"
    81  	}
    82  	elems := strings.Split(g.AppPkg, "/")
    83  	pkgName := elems[len(elems)-1]
    84  	codegen.Reserved[pkgName] = true
    85  
    86  	err = g.API.IterateResources(func(r *design.ResourceDefinition) error {
    87  		var (
    88  			filename string
    89  			err      error
    90  		)
    91  		if g.Resource == "" || g.Resource == r.Name {
    92  			filename, err = genmain.GenerateController(g.Force, g.AppPkg, g.OutDir, g.Pkg, r.Name, r)
    93  		}
    94  
    95  		if err != nil {
    96  			return err
    97  		}
    98  		g.genfiles = append(g.genfiles, filename)
    99  
   100  		return nil
   101  	})
   102  
   103  	return g.genfiles, err
   104  }
   105  
   106  // Cleanup removes all the files generated by this generator during the last invokation of Generate.
   107  func (g *Generator) Cleanup() {
   108  	for _, f := range g.genfiles {
   109  		os.Remove(f)
   110  	}
   111  	g.genfiles = nil
   112  }