func execute(gen genex.Generator, src, name string, params map[string]interface{}) error { // Format the output. funcMap := map[string]interface{}{ "join": func(s []string) string { return strings.Join(s, ",") }, } t := template.Must(template.New("").Funcs(funcMap).Parse(src)) var buf bytes.Buffer t.Execute(&buf, params) gen.Printf("%s", buf.String()) formattedSrc := gen.Format() // Write to file. return ioutil.WriteFile(name, formattedSrc, 0644) }
func run() int { var ( args = flag.Args() g genex.Generator ) // Print the header and package clause. g.Printf("// Code generated by gen-config.\n") // Run generate for each type. filename := []string{} config := map[string]interface{}{} for _, fp := range genex.MustParseGlobs(args[:]) { if strings.HasSuffix(fp, ".tml") { if _, err := toml.DecodeFile(fp, &config); err == nil { g.Printf("// %s\n", fp) filename = append(filename, fp) } } } g.Printf("// DO NOT EDIT\n") g.Printf("\npackage %s\n", *pkg) // Generate two source files. params := map[string]interface{}{} params["ConfigDir"] = *confDir params["TypeBody"] = printer(config) params["Filename"] = func() (ret []string) { for _, v := range filename { ret = append(ret, strings.Replace(v, *confDir+"/", "", -1)) } return }() params["StructName"] = func(cfg map[string]interface{}) (ret []map[string]string) { for k, _ := range cfg { elm := map[string]string{} elm["Struct"] = snaker.SnakeToCamel(k) elm["Tag"] = fmt.Sprintf("`toml:\"%s\"`", k) ret = append(ret, elm) } return }(config) // Format the output. t := template.Must(template.New("").Parse(text)) var buf bytes.Buffer t.Execute(&buf, params) g.Printf("%s", buf.String()) src := g.Format() // Write to file. outputName := *output if outputName == "" { baseName := OUTPUT outputName = filepath.Join(*srcDir, strings.ToLower(baseName)) } err := ioutil.WriteFile(outputName, src, 0644) if err != nil { log.Fatalf("writing output: %s", err) } return 0 }
func run() int { var ( args = flag.Args() mg genex.Generator g genex.Generator ) // Print the header and package clause. mg.Printf("// Code generated by gen-routes.\n// DO NOT EDIT\n") mg.Printf("\npackage %s\n", *pkg) // Print the header and package clause. g.Printf("// Code generated by gen-routes.\n") // Set Routes from args list, filepaths := MustParseGlobs(args[:]) for _, fp := range filepaths { g.Printf("// %s\n", fp) } g.Printf("// DO NOT EDIT\n") g.Printf("\npackage %s\n", *pkg) // Generate two source files. params := map[string]interface{}{} type Element struct { // Essentials Method, Path, Controller string // Controller ControllerPack, ControllerName, ControllerFunc string // Param Param []string } data := []Element{} statics := []Static{} htmls := []HTML{} for _, routes := range list { for _, route := range routes.Endpoint.Routes { elm := Element{} elm.Param = []string{} // Essentials elm.Method = route.Method elm.Path = path.Join("/", route.Path) elm.Controller = route.Controller // Controller _, t, fn := parsePackage(route.Controller) elm.ControllerFunc = fn _, elm.ControllerPack, elm.ControllerName = parsePackage(t) // Param elm.Param = parsePatternPath(elm.Path) // To be c.Param("%s") for i := 0; i < len(elm.Param); i++ { elm.Param[i] = fmt.Sprintf("c.Param(\"%s\")", elm.Param[i]) } // append data = append(data, elm) } statics = append(statics, routes.Statics...) htmls = append(htmls, routes.HTML...) } params["data"] = data params["package"] = func() (ret []string) { ins := map[string]bool{} for _, routes := range list { for _, v := range routes.Packages { if ok, found := ins[v.Name]; found && ok { continue } ins[v.Name] = true ret = append(ret, v.Name) } } return }() params["static"] = statics params["html"] = htmls params["appName"] = filepath.Base(strings.Replace(*srcDir, "/internal/app", "", 1)) // Write to routes_gen.go outputName := *output if outputName == "" { baseName := OUTPUT outputName = filepath.Join(*srcDir, strings.ToLower(baseName)) } if !strings.HasSuffix(outputName, ".go") { outputName = outputName + ".go" } if err := execute(g, routesSource, outputName, params); err != nil { log.Fatalf("writing output: %s", err) } // Write to main_gen.go outputName = filepath.Join(*srcDir, "main_gen.go") var src string if v := os.Getenv("SOURCE_GAE"); v != "" { src = gaeSource } else { src = mainSource } if err := execute(mg, src, outputName, params); err != nil { log.Fatalf("writing output: %s", err) } return 0 }