Пример #1
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
}
Пример #2
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
}
Пример #3
0
		wr := bytes.NewBuffer([]byte(""))
		if err := tmpl.Render(wr, map[string]interface{}{
			"mode":    mode,
			"name":    name,
			"value":   value,
			"options": options,
		}); err != nil {
			return template.HTML(err.Error())
		}

		return template.HTML(wr.String())
	}

	Many = func(belong string, many string, relation string, mode string, id int64, value interface{}, options ...interface{}) template.HTML {
		tmpl, err := revel.MainTemplateLoader.Template(fmt.Sprintf("view.%s/many.html", symbol.ModuleName(many)))
		if err != nil {
			return template.HTML(fmt.Sprintf("view.%s/many.html not exist.", symbol.ModuleName(many)))
		}

		wr := bytes.NewBuffer([]byte(""))
		if err := tmpl.Render(wr, map[string]interface{}{
			"belong":   belong,
			"many":     many,
			"relation": relation,
			"mode":     mode,
			"id":       id,
			"value":    value,
			"options":  options,
		}); err != nil {
			return template.HTML(err.Error())
Пример #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,
			}

			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
}
Пример #5
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
		}
	}
}