Exemple #1
0
func revel_init(project string, template_dir string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)

	data := map[string]interface{}{
		"project": project,
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.go.t"),
		path.Join(project_dir, "app", "models", "model.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.validator.go.t"),
		path.Join(project_dir, "app", "models", "model.validator.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "controllers", "controller.db.go.t"),
		path.Join(project_dir, "app", "controllers", "controller.db.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "controllers", "controller.go.t"),
		path.Join(project_dir, "app", "controllers", "controller.go"), data, force); err != nil {
		return err
	}

	return nil
}
Exemple #2
0
func revel_index(project string, template_dir string, theme string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	tables, err := symbol.GetAllTables(db)
	if err != nil {
		return err
	}

	data := map[string]interface{}{
		"project": project,
		"tables":  tables,
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "conf", "routes"),
		path.Join(project_dir, "conf", "routes"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "header.html"),
		path.Join(project_dir, "app", "views", "header.html"),
		data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "footer.html"),
		path.Join(project_dir, "app", "views", "footer.html"),
		data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "index.html"),
		path.Join(project_dir, "app", "views", "index.html"),
		data, force); err != nil {
		return err
	}

	var scan = func(fn string, fileInfo os.FileInfo, inpErr error) (err error) {
		if !fileInfo.IsDir() {
			return symbol.RenderTemplate(fn,
				path.Join(project_dir, "app", "views", "widget", fileInfo.Name()),
				data, force)
		}
		return nil
	}
	return filepath.Walk(path.Join(template_dir, "revel", "views", theme, "widget"), scan)
}
Exemple #3
0
func revel_index(project string, template_dir string, theme string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	tables, err := symbol.GetAllTables(db)
	if err != nil {
		return err
	}

	data := map[string]interface{}{
		"project": project,
		"tables":  tables,
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "conf", "routes"),
		path.Join(project_dir, "conf", "routes"), data, true); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "header.html"),
		path.Join(project_dir, "app", "views", "header.html"),
		data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "footer.html"),
		path.Join(project_dir, "app", "views", "footer.html"),
		data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "index.html"),
		path.Join(project_dir, "app", "views", "index.html"),
		data, true); err != nil {
		return err
	}
	return nil
}
Exemple #4
0
func revel_view(project string, template_dir string, modules []string, theme string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	for _, name := range modules {
		tables := []*symbol.Table{}
		if name == "*" {
			tbs, err := symbol.GetAllTables(db)
			if err != nil {
				return err
			}
			tables = append(tables, tbs...)
		} else {
			tb, err := symbol.GetTable(db, name)
			if err != nil {
				return err
			}
			tables = append(tables, tb)
		}

		for _, table := range tables {
			data := map[string]interface{}{
				"project": project,
				"table":   table,
			}

			var scan = func(fn string, fileInfo os.FileInfo, inpErr error) (err error) {
				if !fileInfo.IsDir() {
					return symbol.RenderTemplate(fn,
						path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), fileInfo.Name()),
						data, force)
				}
				return nil
			}
			if err := filepath.Walk(path.Join(template_dir, "revel", "views", theme, "crud"), scan); err != nil {
				return err
			}
		}
	}

	return nil
}
Exemple #5
0
func revel_model(project string, template_dir string, moduels []string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	for _, name := range moduels {
		tables := []*symbol.Table{}
		if name == "*" {
			tbs, err := symbol.GetAllTables(db)
			if err != nil {
				return err
			}
			tables = append(tables, tbs...)
		} else {
			tb, err := symbol.GetTable(db, name)
			if err != nil {
				return err
			}
			tables = append(tables, tb)
		}

		for _, table := range tables {
			data := map[string]interface{}{
				"project": project,
				"table":   table,
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.crud.go.t"),
				path.Join(project_dir, "app", "models", fmt.Sprintf("model.%s.go", symbol.ModuleName(table.Name()))),
				data, force); err != nil {
				return err
			}
		}
	}

	return nil
}
Exemple #6
0
func revel_view(project string, template_dir string, modules []string, theme string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	for _, name := range modules {
		tables := []*symbol.Table{}
		if name == "*" {
			tbs, err := symbol.GetAllTables(db)
			if err != nil {
				return err
			}
			tables = append(tables, tbs...)
		} else {
			tb, err := symbol.GetTable(db, name)
			if err != nil {
				return err
			}
			tables = append(tables, tb)
		}

		for _, table := range tables {
			data := map[string]interface{}{
				"project": project,
				"table":   table,
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "crud/create.html"),
				path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), "create.html"),
				data, force); err != nil {
				return err
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "crud/detail.html"),
				path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), "detail.html"),
				data, force); err != nil {
				return err
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "crud/index.html"),
				path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), "index.html"),
				data, force); err != nil {
				return err
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "crud/query.html"),
				path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), "query.html"),
				data, force); err != nil {
				return err
			}

			if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "views", theme, "crud/update.html"),
				path.Join(project_dir, "app", "views", symbol.ModuleName(table.Name()), "update.html"),
				data, force); err != nil {
				return err
			}
		}
	}

	return nil
}
Exemple #7
0
func revel_init(project string, template_dir string, force bool) error {
	root_dir, err := gopath_src_dir()
	if err != nil {
		return err
	}
	project_dir := path.Join(root_dir, project)
	revel.ConfPaths = append(revel.ConfPaths, path.Join(project_dir, "conf"))

	db, err := revel_db(project_dir)
	if err != nil {
		return err
	}
	defer db.Close()

	options, err := revel_options(db)
	if err != nil {
		return err
	}

	data := map[string]interface{}{
		"project": project,
		"options": options,
	}

	if err := symbol.CopyDir(path.Join(template_dir, "revel", "public"),
		path.Join(project_dir, "public")); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.go.t"),
		path.Join(project_dir, "app", "models", "model.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.const.go.t"),
		path.Join(project_dir, "app", "models", "model.const.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "models", "model.validator.go.t"),
		path.Join(project_dir, "app", "models", "model.validator.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "controllers", "controller.widget.go.t"),
		path.Join(project_dir, "app", "controllers", "controller.widget.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "controllers", "controller.db.go.t"),
		path.Join(project_dir, "app", "controllers", "controller.db.go"), data, force); err != nil {
		return err
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "revel", "controllers", "controller.fns.go.t"),
		path.Join(project_dir, "app", "controllers", "controller.fns.go"), data, force); err != nil {
		return err
	}

	return nil
}
Exemple #8
0
func modelGenerate(ctx *cli.Context) {
	template_dir, err := default_template_dir(ctx)
	if err != nil {
		fmt.Println("scaffold model generate failed:", err)
		return
	}

	args := ctx.Args()
	if len(args) != 1 {
		fmt.Println("Usage: scaffold model generate <project/path>")
		return
	}

	project := args[0]
	root_dir, err := gopath_src_dir()
	if err != nil {
		fmt.Println("scaffold model generate failed:", err)
		return
	}
	project_dir := path.Join(root_dir, project)

	driver := ctx.String("driver")
	database := ctx.String("database")
	host := ctx.String("host")
	port := ctx.Int("port")
	user := ctx.String("username")
	pass := ctx.String("password")

	dsn := symbol.DSNFormat(host, port, user, pass, database)
	db, err := sql.Open(driver, dsn)
	if err != nil {
		fmt.Println("scaffold model generate failed:", err)
		return
	}
	defer db.Close()

	tables, err := symbol.GetAllTables(db)
	if err != nil {
		fmt.Println("scaffold model generate failed:", err)
		return
	}

	if err := symbol.RenderTemplate(path.Join(template_dir, "model", "model.go.t"),
		path.Join(project_dir, "model.go"), map[string]interface{}{
			"project": project,
			"tables":  tables,
		}, true); err != nil {
		fmt.Println("scaffold model generate failed:", err)
		return
	}

	for _, table := range tables {
		data := map[string]interface{}{
			"table": table,
		}

		dest := fmt.Sprintf("model.%s.go", symbol.ModuleName(table.Name()))
		if err := symbol.RenderTemplate(path.Join(template_dir, "model", "model.table.go.t"),
			path.Join(project_dir, dest), data, true); err != nil {
			fmt.Println("scaffold model generate failed:", err)
			return
		}
	}
}
Exemple #9
0
func Generate(ctx *cli.Context) {
	template_dir, err := src_template_dir(ctx)
	if err != nil {
		fmt.Println("scaffold generate failed:", err)
		return
	}
	if template_dir == "" {
		fmt.Println("scaffold generate failed: none templates")
		return
	}

	args := ctx.Args()
	if len(args) != 1 {
		fmt.Println("Usage: scaffold generate <project/path>")
		return
	}

	project := args[0]
	project_dir, err := dest_project_dir(project)
	if err != nil {
		fmt.Println("scaffold generate failed:", err)
		return
	}

	suffixes := ctx.GlobalStringSlice("include-template-suffix")
	ignores := ctx.GlobalStringSlice("exclude-template-suffix")
	force := ctx.GlobalBool("force")
	driver := ctx.String("driver")
	database := ctx.String("database")
	host := ctx.String("host")
	port := ctx.Int("port")
	user := ctx.String("username")
	pass := ctx.String("password")

	dsn := symbol.DSNFormat(host, port, user, pass, database)
	db, err := sql.Open(driver, dsn)
	if err != nil {
		fmt.Println("scaffold generate failed:", err)
		return
	}
	defer db.Close()

	tables, err := symbol.GetAllTables(db)
	if err != nil {
		fmt.Println("scaffold generate failed:", err)
		return
	}

	if err := filepath.Walk(template_dir, func(srcPath string, info os.FileInfo, err error) error {
		data := map[string]interface{}{
			"project": project,
			"tables":  tables,
		}
		for i, table := range tables {
			data["table"] = table
			data["index"] = i

			pathName, err := symbol.RenderString(srcPath, data)
			if err != nil {
				return err
			}

			destPath := path.Join(project_dir, strings.TrimPrefix(pathName, template_dir))

			if info.IsDir() {
				if err := os.MkdirAll(destPath, info.Mode()); err != nil {
					return err
				}
			} else {
				if hasSuffix(pathName, ignores) {
					break
				} else if hasSuffix(pathName, suffixes) {
					if err := symbol.RenderTemplate(srcPath, destPath, data, force); err != nil {
						return err
					}
				} else {
					if err := symbol.CopyFile(srcPath, destPath, force); err != nil {
						return err
					}
				}
			}

			if srcPath == pathName {
				break
			}
		}
		return nil
	}); err != nil {
		fmt.Println("scaffold generate failed:", err)
		return
	}
	if len(suffixes) == 0 {
		fmt.Println("WARNING: scaffold template suffix is unset, just copy files.")
	}
}