func Resource(rootPath, app, resourceName string) { if ok, err := utils.FileExists(path.Join(rootPath, "app.go")); !ok || err != nil { fmt.Printf("Could not find app.go. Are you at the root of your project?") os.Exit(1) } collection := db.Pluralize(db.CamelCaseToUnderscore(resourceName)) if collection[len(collection)-1] != 's' { collection += "s" } // Write the model to the apps/APP/models.go file. tpl := StringReplace(tplModel, map[string]string{ "name": resourceName, "collection": collection, }) FileReplaceToken(path.Join(rootPath, "apps", app, "models.go"), "APPKIT:APP_MODELS", tpl) // Write the resource hook struct to apps/APP/resources.go . tpl = StringReplace(tplResourceHooks, map[string]string{ "name": resourceName, }) FileReplaceToken(path.Join(rootPath, "apps", app, "resources.go"), "APPKIT:RESOURCES", tpl) // Now, finally, write the .RegisterResource to apps/APP/appname.go Build(). tpl = StringReplace(tplRegisterResource, map[string]string{ "model": resourceName, "hooks": resourceName + "Resource", }) FileReplaceToken(path.Join(rootPath, "apps", app, app+".go"), "APPKIT:APP_BUILD", tpl) }
func App(rootPath, appName string, resources []string) { if ok, err := utils.FileExists(path.Join(rootPath, "app.go")); !ok || err != nil { fmt.Printf("Could not find app.go. Are you at the root of your project?") os.Exit(1) } pkgPath := determinePkgPath(rootPath) // Build appName.go file. tpl := StringReplace(tplApp, map[string]string{ "pkg": appName, }) utils.WriteFile(path.Join(rootPath, "apps", appName, appName+".go"), []byte(tpl), true) // Build models.go file. tpl = StringReplace(tplModels, map[string]string{ "pkg": appName, }) utils.WriteFile(path.Join(rootPath, "apps", appName, "models.go"), []byte(tpl), true) // Build resources.go file. tpl = StringReplace(tplResources, map[string]string{ "pkg": appName, }) utils.WriteFile(path.Join(rootPath, "apps", appName, "resources.go"), []byte(tpl), true) // Add pkg import and build call to app.go. appPath := path.Join(rootPath, "app.go") FileReplaceToken(appPath, "APPKIT:APP_IMPORTS", fmt.Sprintf(` app_%v "%v/apps/%v"`, appName, pkgPath, appName)) FileReplaceToken(appPath, "APPKIT:APP_APPS", fmt.Sprintf(" app_%v.Build(app)", appName)) // Now, build resources. for _, resource := range resources { Resource(rootPath, appName, resource) } }
func Backend(rootPath, pkg, backend string) { if ok, err := utils.FileExists(path.Join(rootPath, "app.go")); !ok || err != nil { fmt.Printf("Could not find app.go. Are you at the root of your project?") os.Exit(1) } var buildImports []string var build string switch backend { case "postgres", "postgresql": buildImports = []string{`_ "github.com/lib/pq"`, `"github.com/theduke/go-dukedb/backends/sql"`} build = StringReplace(tplBackendSql, map[string]string{ "name": "postgres", "upperCaseBackend": strings.ToUpper(string(backend[0])) + backend[1:], }) case "mysql": case "memory": default: fmt.Printf("Unknown backend type: %v\n", backend) os.Exit(1) } migrations := StringReplace(tplMigrationsFile, map[string]string{ "pkg": pkg, "backend": backend, "upperCaseBackend": strings.ToUpper(string(backend[0])) + backend[1:], }) buildMigrations := StringReplace(tplBuildMigrations, map[string]string{ "name": backend, "upperCaseName": strings.ToUpper(string(backend[0])) + backend[1:], }) appPath := path.Join(rootPath, "app.go") imports := "" for _, val := range buildImports { imports += "\t" + val + "\n" } // Add imports to app.go. if !FileReplaceToken(appPath, "APPKIT:APP_IMPORTS", imports) { fmt.Printf("APPKIT:APP_IMPORTS token not found in %v\n", appPath) os.Exit(1) } // Add build code to BuildBackends() function in app.go if !FileReplaceToken(appPath, "APPKIT:APP_BACKENDS", build) { fmt.Printf("APPKIT:APP_BACKENDS token not found in %v\n", appPath) os.Exit(1) } // Add code to BuildMigratins() function in app.go. if !FileReplaceToken(appPath, "APPKIT:APP_MIGRATIONS", buildMigrations) { fmt.Printf("APPKIT:APP_MIGRATIONS token not found in %v\n", appPath) os.Exit(1) } // Create migrations file. utils.WriteFile(path.Join(rootPath, backend+"_"+"migrations.go"), []byte(migrations), false) }