func main() { var rt reflect.Type runtime.LockOSThread() outFilePath := filepath.Join(os.Args[1], "-gen-refsids.go") outSrc := "package cdom\n\n" // collect all struct types and catch the HasSid struct type for _, rt = range cdr.Types { if rt.Kind() == reflect.Struct { if !spref(rt.Name(), "Lib", "Base", "Has", "Ref") { allStructs[rt.Name()] = rt } if rt.Name() == "HasSid" { hasSidType = rt } } } // map all direct and indirect type dependencies on HasSid type mapTypeDeps(hasSidType) for rt, _ = range typeDeps { testResolvers(rt) } for rt, _ = range typeDeps { if rt != hasSidType { outSrc += writeResolverMethods(rt) } } for _, rt = range cdr.Types { if (rt.Kind() == reflect.Struct) && !spref(rt.Name(), "Sid", "Base") { outSrc += writeAccessorMethods(rt) } } uio.WriteTextFile(outFilePath, outSrc[:len(outSrc)-1]+"\n") }
// Attempts to xml.Unmarshal() all files in the "infiles" sub-directory of the specified directory path into the interface{} structure returned by the specified constructor. // For each such input file, then attempts to xml.MarshalIndent() said structure back into a new output XML file with the same name, in the "outfiles" sub-directory of the specified directory path. func TestViaRemarshal(dirPath string, makeEmptyDoc func() interface{}) { var dirPathInFiles = filepath.Join(dirPath, "infiles") var dirPathOutFiles = filepath.Join(dirPath, "outfiles") var loadXmlDocFile = func(filename string, keepRecursing bool) bool { log.Printf("Loading %s", filename) doc, dataOrig := makeEmptyDoc(), uio.ReadBinaryFile(filename, true) err := xml.Unmarshal(dataOrig, doc) if err != nil { panic(err) } if OnDocLoaded != nil { OnDocLoaded(doc) } outFileName := filepath.Join(dirPathOutFiles, filepath.Base(filename)) log.Printf("Writing %s", outFileName) dataFaks, err := xml.MarshalIndent(doc, "", "\t") if err != nil { panic(err) } uio.WriteTextFile(outFileName, strings.Trim(string(dataFaks), " \r\n\t")) log.Printf("Verifying...") if errs := verifyDocs(dataOrig, dataFaks); len(errs) > 0 { for _, err = range errs { log.Printf("%v", err) } } return keepRecursing } uio.WalkDirectory(dirPathInFiles, "", loadXmlDocFile, true) }
func (me *Schema) MakeGoPkgSrcFile() (goOutFilePath string, err error) { var goOutDirPath = filepath.Join(filepath.Dir(me.loadLocalPath), goPkgPrefix+filepath.Base(me.loadLocalPath)+goPkgSuffix) goOutFilePath = filepath.Join(goOutDirPath, path.Base(me.loadUri)+".go") var bag = newPkgBag(me) for _, inc := range me.XMLIncludedSchemas { bag.Schema = inc inc.makePkg(bag) } bag.Schema = me me.hasElemAnnotation.makePkg(bag) bag.appendFmt(true, "") me.makePkg(bag) if err = uio.EnsureDirExists(filepath.Dir(goOutFilePath)); err == nil { err = uio.WriteTextFile(goOutFilePath, bag.assembleSource()) } return }
func main() { // pkgreflect -novars=true -norecurs=true -nofuncs=false -gofile=-pkgreflect.go $dir runtime.LockOSThread() srcDirPath := os.Args[1] impPath := strings.Trim(strings.Replace(strings.Replace(strings.ToLower(srcDirPath), strings.ToLower(ugo.GopathSrc()), "", -1), "\\", "/", -1), "/") srcFilePath := filepath.Join(srcDirPath, "-pkgreflect.go") outDirPath := filepath.Join(srcDirPath, "pkgreflect") outFilePath := filepath.Join(outDirPath, "-gen-pkgreflect.go") if raw, err := exec.Command("pkgreflect", "-novars=true", "-norecurs=true", "-nofuncs=false", "-gofile=-pkgreflect.go", srcDirPath).CombinedOutput(); err != nil { print(err) panic(err) } else { if len(raw) > 0 { println(string(raw)) } src := uio.ReadTextFile(srcFilePath, true, "") os.Remove(srcFilePath) src = strings.Replace(src, "package cdom", "package pkgreflect", -1) src = strings.Replace(src, "import \"reflect\"", "import \"reflect\"\nimport cdom \""+impPath+"\"", -1) src = strings.Replace(src, "((*", "((*cdom.", -1) src = strings.Replace(src, "ValueOf(", "ValueOf(cdom.", -1) uio.WriteTextFile(outFilePath, src) } }
func main() { const ( srcImpLib = ` func libs_%s(xn *xmlx.Node) { var ( lib *cdom.Lib%sDefs def *cdom.%sDef id string ) for _, ln := range xcns(xn, "library_%s") { id = xas(ln, "id") if lib = cdom.All%sDefLibs[id]; lib == nil { lib = cdom.All%sDefLibs.AddNew(id) } for _, def = range objs_%sDef(ln, "%s") { if def != nil { lib.Add(def) } } lib.SetDirty() } } ` srcImpObj = ` func obj_%s(xn *xmlx.Node, n string) (obj *cdom.%s) { if (xn != nil) && (len(n) > 0) { xn = xcn(xn, n) } if xn != nil { obj = init_%s(xn) } return } ` srcImpInitCtor = ` func init_%s(xn *xmlx.Node) (obj *cdom.%s) { obj = cdom.New%s() ` srcImpInitNew = ` func init_%s(xn *xmlx.Node) (obj *cdom.%s) { obj = new(cdom.%s) ` srcImpN = ` func objs_%s(xn *xmlx.Node, n string) (objs []*cdom.%s) { xns := xcns(xn, n) objs = make([]*cdom.%s, len(xns)) for i, xn := range xns { objs[i] = obj_%s(xn, "") } return } ` srcLoad = ` func load_%s(xn *xmlx.Node, obj *cdom.%s) { } ` ) var ( i int ok, canDirty bool srcLibs, srcInits, srcObjs, srcLoads string ctorFunc reflect.Value ) has := []string{"Asset", "Extras", "FxParamDefs", "ID", "Inputs", "Name", "ParamDefs", "ParamInsts", "Sid", "Sources", "Techniques"} flag.Parse() for n, t := range cdr.Types { if canDirty = false; !(strings.HasPrefix(n, "Lib") || strings.HasPrefix(n, "Mesh") || strings.HasPrefix(n, "Base") || strings.HasSuffix(n, "Base") || strings.HasPrefix(n, "Has") || strings.HasPrefix(n, "Ref")) { srcObjs += fmt.Sprintf(srcImpObj, n, n, n) if ctorFunc, ok = cdr.Functions["New"+n]; ok && ctorFunc.Type().NumIn() == 0 { srcInits += fmt.Sprintf(srcImpInitCtor, n, n, n) } else { srcInits += fmt.Sprintf(srcImpInitNew, n, n, n) } if t.Kind() == reflect.Struct { for i = 0; i < t.NumField(); i++ { if strings.HasPrefix(t.Field(i).Name, "Base") { canDirty = true } } if canDirty { if strings.HasSuffix(n, "Def") || strings.HasSuffix(n, "Inst") { srcInits += "\tobj.Init()\n" } if strings.HasSuffix(n, "Inst") { srcInits += "\tsetInstDefRef(xn, &obj.BaseInst)\n" } } for _, h := range has { if _, ok = t.FieldByName("Has" + h); ok { srcInits += fmt.Sprintf("\thas_%s(xn, &obj.Has%s)\n", h, h) } } } srcInits += fmt.Sprintf("\n\tload_%s(xn, obj)", n) if canDirty { srcInits += "\n\tobj.SetDirty()" } srcInits += "\n\treturn\n}\n" srcObjs += fmt.Sprintf(srcImpN, n, n, n, n) srcLoads += fmt.Sprintf(srcLoad, n, n) } } for _, lm := range libs { // animations Animation Animation animations Animation Animation Animation animation srcLibs += fmt.Sprintf(srcImpLib, lm.xnPlural, lm.tn, lm.tn, lm.xnPlural, lm.tn, lm.tn, lm.tn, lm.xnSingular) } srcLibs += "\nfunc libs_All (xn *xmlx.Node) {\n" for _, lm := range libs { srcLibs += fmt.Sprintf("\tlibs_%s(xn)\n", lm.xnPlural) } srcLibs += "}\n" uio.WriteTextFile(filepath.Join(*outDirPath, "-skel-libs.txt"), srcLibs) uio.WriteTextFile(filepath.Join(*outDirPath, "-skel-inits.txt"), srcInits) uio.WriteTextFile(filepath.Join(*outDirPath, "-skel-objs.txt"), srcObjs) uio.WriteTextFile(filepath.Join(*outDirPath, "-skel-loads.txt"), srcLoads) }