Exemple #1
0
//generate the code for mapper tag
func (m *MapperCommentCodeGen) Generate(appName, serverName string, alias map[string]string) string {
	pkgName := alias[m.ImportPath]
	var code string
	info := m.ActionInfo
	httpMethods := strings.Join(m.HttpMethods, `", "`)
	url := m.UrlExpr
	params := map[string]interface{}{
		"info":        info,
		"httpMethods": httpMethods,
		"serverName":  serverName,
		"alias":       alias,
		"url":         url,
		"q":           "`",
	}
	if info.RecvName == "" {
		params["funcOrMethod"] = "FuncMapper"
		params["action"] = pkgName + "." + info.ActionName
		code = peony.ExecuteTemplate(mapperTemplate, params)
	} else {
		params["funcOrMethod"] = "MethodMapper"
		params["action"] = fmt.Sprintf("(*%s.%s).%s", pkgName, info.RecvName, info.Name)
		code = peony.ExecuteTemplate(mapperTemplate, params)
	}
	return code
}
Exemple #2
0
func genSource(dir, filename, tpl string, args map[string]interface{}) {
	code := peony.ExecuteTemplate(template.Must(template.New("").Parse(tpl)), args)
	finfo, err := os.Stat(dir)
	if err != nil {
		if os.IsNotExist(err) {
			err = os.Mkdir(dir, 0777)
			if err != nil {
				log.Fatalln("create dir error:", dir)
				return
			}
		} else {
			log.Fatalln("stat error:", err)
		}
	} else if !finfo.IsDir() {
		log.Fatalln("Not dir, shoul be a dir.")
	}
	filepath := path.Join(dir, filename)
	os.Remove(filepath)
	var file *os.File
	file, err = os.Create(filepath)

	if err != nil {
		log.Fatalln("Open file error:", err)
	}
	defer file.Close()
	_, err = file.WriteString(code)
	if err != nil {
		log.Fatalln("Write source eror:", err)
	}

}
Exemple #3
0
func tarshell(binName, appName string, tarWriter *tar.Writer) {
	now := time.Now()
	args := map[string]string{"BinName": binName}
	var targzWriteScript = func(s, n string) {
		val := peony.ExecuteTemplate(template.Must(template.New("").Parse(s)), args)
		tarh := &tar.Header{
			Name:    filepath.Join(appName, "bin", n),
			Size:    int64(len(val)),
			Mode:    int64(0766),
			ModTime: now,
		}
		targzWrite(tarWriter, tarh, bytes.NewReader([]byte(val)))
	}
	targzWriteScript(shell, "run.sh")
	targzWriteScript(cmd, "run.bat")
}