// Generate generates migration templates. func (g *migrationGenerator) Generate() { name := g.flag.Arg(0) if name == "" { kocha.PanicOnError(g, "abort: no NAME given") } tx := kocha.TxTypeMap[g.txType] if tx == nil { kocha.PanicOnError(g, "abort: unsupported transaction type: `%v`", g.txType) } now := Now().Format("20060102150405") data := map[string]interface{}{ "Name": kocha.ToCamelCase(name), "TimeStamp": now, "ImportPath": tx.ImportPath(), "TxType": reflect.TypeOf(tx.TransactionType()).String(), } kocha.CopyTemplate(g, filepath.Join(SkeletonDir("migration"), "migration.go.template"), filepath.Join("db", "migrations", fmt.Sprintf("%v_%v.go", now, kocha.ToSnakeCase(name))), data) initPath := filepath.Join("db", "migrations", "init.go") if _, err := os.Stat(initPath); os.IsNotExist(err) { appDir, err := kocha.FindAppDir() if err != nil { panic(err) } kocha.CopyTemplate(g, filepath.Join(SkeletonDir("migration"), "init.go.template"), initPath, map[string]interface{}{ "typeName": g.txType, "tx": strings.TrimSpace(kocha.GoString(tx)), "dbImportPath": path.Join(appDir, "db"), }) } }
// Generate generate a controller templates. func (g *controllerGenerator) Generate() { name := g.flag.Arg(0) if name == "" { kocha.PanicOnError(g, "abort: no NAME given") } camelCaseName := kocha.ToCamelCase(name) snakeCaseName := kocha.ToSnakeCase(name) data := map[string]interface{}{ "Name": camelCaseName, } kocha.CopyTemplate(g, filepath.Join(SkeletonDir("controller"), "controller.go.template"), filepath.Join("app", "controllers", snakeCaseName+".go"), data) kocha.CopyTemplate(g, filepath.Join(SkeletonDir("controller"), "view.html"), filepath.Join("app", "views", snakeCaseName+".html"), data) g.addRouteToFile(name) }
// Generate generates model templates. func (g *modelGenerator) Generate() { name := g.flag.Arg(0) if name == "" { kocha.PanicOnError(g, "abort: no NAME given") } mt := modelTypeMap[g.orm] if mt == nil { kocha.PanicOnError(g, "abort: unsupported ORM type: `%v`", g.orm) } m := mt.FieldTypeMap() var fields []modelField for _, arg := range g.flag.Args()[1:] { input := strings.Split(arg, ":") if len(input) != 2 { kocha.PanicOnError(g, "abort: invalid argument format has been specified: `%v`", strings.Join(input, ", ")) } name, t := input[0], input[1] if name == "" { kocha.PanicOnError(g, "abort: field name hasn't been specified") } ft, found := m[t] if !found { kocha.PanicOnError(g, "abort: unsupported field type: `%v`", t) } fields = append(fields, modelField{ Name: kocha.ToCamelCase(name), Type: ft.Name, Column: kocha.ToSnakeCase(name), OptionTags: ft.OptionTags, }) } camelCaseName := kocha.ToCamelCase(name) snakeCaseName := kocha.ToSnakeCase(name) data := map[string]interface{}{ "Name": camelCaseName, "Fields": fields, } templatePath, configTemplatePath := mt.TemplatePath() kocha.CopyTemplate(g, templatePath, filepath.Join("app", "models", snakeCaseName+".go"), data) initPath := filepath.Join("db", "config.go") if _, err := os.Stat(initPath); os.IsNotExist(err) { kocha.CopyTemplate(g, configTemplatePath, initPath, nil) } }
// Generate generates unit templates. func (g *unitGenerator) Generate() { name := g.flag.Arg(0) if name == "" { kocha.PanicOnError(g, "abort: no NAME given") } camelCaseName := kocha.ToCamelCase(name) snakeCaseName := kocha.ToSnakeCase(name) data := map[string]interface{}{ "Name": camelCaseName, } kocha.CopyTemplate(g, filepath.Join(SkeletonDir("unit"), "unit.go.template"), filepath.Join("app", "units", snakeCaseName+".go"), data) }
// Run execute the process for `new` command. func (c *newCommand) Run() { appPath := c.flag.Arg(0) if appPath == "" { kocha.PanicOnError(c, "abort: no APP_PATH given") } dstBasePath := filepath.Join(filepath.SplitList(build.Default.GOPATH)[0], "src", appPath) _, filename, _, _ := runtime.Caller(0) baseDir := filepath.Dir(filename) skeletonDir := filepath.Join(baseDir, "skeleton", "new") if _, err := os.Stat(filepath.Join(dstBasePath, "config", "app.go")); err == nil { kocha.PanicOnError(c, "abort: Kocha application is already exists") } data := map[string]interface{}{ "appName": filepath.Base(appPath), "appPath": appPath, "secretKey": fmt.Sprintf("%q", string(kocha.GenerateRandomKey(32))), // AES-256 "signedKey": fmt.Sprintf("%q", string(kocha.GenerateRandomKey(16))), } filepath.Walk(skeletonDir, func(path string, info os.FileInfo, err error) error { if err != nil { panic(err) } if info.IsDir() { return nil } dstPath := filepath.Join(dstBasePath, strings.TrimSuffix(strings.TrimPrefix(path, skeletonDir), ".template")) dstDir := filepath.Dir(dstPath) dirCreated, err := mkdirAllIfNotExists(dstDir) if err != nil { kocha.PanicOnError(c, "abort: failed to create directory: %v", err) } if dirCreated { kocha.PrintCreateDirectory(dstDir) } kocha.CopyTemplate(c, path, dstPath, data) return nil }) }