//生成代码 // 会把Object上面所有的公开函数都拿去生成一遍 func MustGenerateCode(req *GenerateRequest) { config := reflectToTplConfig(req) outBs := tplGenerateCode(config) outB := []byte(outBs) outB1, err := kmgFormat.Source(outB) if err == nil { outB = outB1 } kmgFile.MustWriteFileWithMkdir(req.OutFilePath, outB) return }
func ResourceBuild(req *ResourceUploadRequest) { if req.Name == "" { panic(`[ResourceBuild] req.Name == ""`) } tmpDirPath := kmgConfig.DefaultEnv().PathInTmp("kmgViewResource_build/" + req.Name) kmgFile.MustDelete(tmpDirPath) response := resourceBuildToDir(req.ImportPathList, tmpDirPath) req.Qiniu.MustUploadFromFile(tmpDirPath, req.QiniuPrefix) packageName := filepath.Base(filepath.Dir(req.OutGoFilePath)) urlPrefix := req.Qiniu.GetSchemeAndDomain() + "/" + req.QiniuPrefix //jsUrl:=urlPrefix+"/"+response.JsFileName //cssUrl:=urlPrefix+"/"+response.CssFileName // 不可以使用 fmt.Sprintf("%#v",generated) 会导出私有变量. //generated:=&Generated{ // Name: req.Name, // GeneratedJsFileUrl: jsUrl, // GeneratedCssFileUrl: cssUrl, // GeneratedUrlPrefix: urlPrefix, // RequestImportList: req.ImportPathList, //} outGoContent := []byte(`package ` + packageName + ` import ( "github.com/bronze1man/kmg/kmgView/kmgViewResource" "sync" ) var ` + req.Name + `Once sync.Once var ` + req.Name + `generated *kmgViewResource.Generated func get` + req.Name + `ViewResource() *kmgViewResource.Generated{ ` + req.Name + `Once.Do(func(){ ` + req.Name + `generated = &kmgViewResource.Generated{ Name: ` + fmt.Sprintf("%#v", req.Name) + `, GeneratedJsFileName: ` + fmt.Sprintf("%#v", response.JsFileName) + `, GeneratedCssFileName: ` + fmt.Sprintf("%#v", response.CssFileName) + `, GeneratedUrlPrefix: ` + fmt.Sprintf("%#v", urlPrefix) + `, RequestImportList: ` + fmt.Sprintf("%#v", req.ImportPathList) + `, } ` + req.Name + `generated.Init() }) return ` + req.Name + `generated } `) outGoContent, err := kmgFormat.Source(outGoContent) if err != nil { panic(err) } kmgFile.MustWriteFile(req.OutGoFilePath, outGoContent) }