예제 #1
0
func FileReplace(path string, data map[string]string) {
	rawContent, err := utils.ReadFile(path)
	checkErr(err)

	rawContent = []byte(StringReplace(string(rawContent), data))
	checkErr(utils.WriteFile(path, rawContent, false))
}
예제 #2
0
func Bootstrap(rootPath, name, pkgUrl, backend string) bool {
	pkgPath := determinePkgPath(rootPath)

	appPath := path.Join(rootPath, "apps")

	if err := os.MkdirAll(appPath, 0777); err != nil {
		fmt.Printf("Could not create root directory %v: %v", rootPath, err)
		return false
	}

	// Create app.go file.
	tpl := StringReplace(tplMainApp, map[string]string{
		"pkg":     name,
		"url":     pkgPath,
		"userPkg": pkgPath + "/apps/users",
	})
	utils.WriteFile(path.Join(rootPath, "app.go"), []byte(tpl), false)

	// Create the main.go file.
	tpl = StringReplace(tplMain, map[string]string{
		"pkg": name,
		"url": pkgPath,
	})
	utils.WriteFile(path.Join(rootPath, name, "main.go"), []byte(tpl), true)

	// Create users app.
	tpl = StringReplace(tplUserProfile, map[string]string{})
	utils.WriteFile(path.Join(rootPath, "apps", "users", "models.go"), []byte(tpl), true)

	// Create backend, if any.
	if backend != "" {
		Backend(rootPath, name, backend)
	}

	// Write example config.
	utils.WriteFile(path.Join(rootPath, name, "config.yaml"), []byte(tplConfig), false)

	return true
}
예제 #3
0
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)
	}
}
예제 #4
0
func FileReplaceToken(path, token, newContent string) bool {
	contents, err := utils.ReadFile(path)
	if err != nil {
		return false
	}

	newContent, ok := ReplaceToken(token, string(contents), newContent)
	if !ok {
		return false
	}

	contents = []byte(newContent)

	if err := utils.WriteFile(path, []byte(contents), false); err != nil {
		return false
	}

	return true
}
예제 #5
0
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)
}