github.com/whyrusleeping/gx@v0.14.3/gxutil/pkgfile.go (about) 1 package gxutil 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "os" 9 10 log "github.com/whyrusleeping/stump" 11 ) 12 13 type PackageBase struct { 14 Name string `json:"name,omitempty"` 15 Author string `json:"author,omitempty"` 16 Description string `json:"description,omitempty"` 17 Keywords []string `json:"keywords,omitempty"` 18 Version string `json:"version,omitempty"` 19 Dependencies []*Dependency `json:"gxDependencies,omitempty"` 20 Bin string `json:"bin,omitempty"` 21 Build string `json:"build,omitempty"` 22 Test string `json:"test,omitempty"` 23 ReleaseCmd string `json:"releaseCmd,omitempty"` 24 SubtoolRequired bool `json:"subtoolRequired,omitempty"` 25 Language string `json:"language,omitempty"` 26 License string `json:"license"` 27 Bugs BugsObj `json:"bugs"` 28 GxVersion string `json:"gxVersion"` 29 } 30 31 type BugsObj struct { 32 Url string `json:"url,omitempty"` 33 } 34 35 type Package struct { 36 PackageBase 37 38 Gx json.RawMessage `json:"gx,omitempty"` 39 } 40 41 // Dependency represents a dependency of a package 42 type Dependency struct { 43 Author string `json:"author,omitempty"` 44 Name string `json:"name,omitempty"` 45 Hash string `json:"hash"` 46 Version string `json:"version,omitempty"` 47 } 48 49 func LoadPackageFile(pkg interface{}, fname string) error { 50 data, err := ioutil.ReadFile(fname) 51 if err != nil { 52 return err 53 } 54 55 var pkgmap map[string]interface{} 56 if err := json.Unmarshal(data, &pkgmap); err != nil { 57 return err 58 } 59 60 if url, ok := pkgmap["bugs"].(string); ok { 61 pkgmap["bugs"] = map[string]interface{}{ 62 "url": url, 63 } 64 changed, err := json.Marshal(pkgmap) 65 if err != nil { 66 return err 67 } 68 data = changed 69 } 70 71 return json.Unmarshal(data, pkg) 72 } 73 74 func SavePackageFile(pkg interface{}, fname string) error { 75 data, err := ioutil.ReadFile(fname) 76 if err != nil { 77 if os.IsNotExist(err) { 78 return writeJson(pkg, fname) 79 } 80 return err 81 } 82 83 var current map[string]interface{} 84 if err := json.Unmarshal(data, ¤t); err != nil { 85 return err 86 } 87 88 buf := new(bytes.Buffer) 89 enc := json.NewEncoder(buf) 90 enc.SetEscapeHTML(false) 91 if err := enc.Encode(pkg); err != nil { 92 return err 93 } 94 95 var modified map[string]interface{} 96 if err := json.NewDecoder(buf).Decode(&modified); err != nil { 97 return err 98 } 99 100 return writeJson(mergeMaps(current, modified), fname) 101 } 102 103 func writeJson(i interface{}, fname string) error { 104 fi, err := os.Create(fname) 105 if err != nil { 106 return err 107 } 108 defer fi.Close() 109 110 buf := new(bytes.Buffer) 111 enc := json.NewEncoder(buf) 112 enc.SetEscapeHTML(false) 113 if err := enc.Encode(i); err != nil { 114 return err 115 } 116 117 out := new(bytes.Buffer) 118 if json.Indent(out, buf.Bytes(), "", " ") != nil { 119 return err 120 } 121 122 _, err = fi.Write(out.Bytes()) 123 fi.WriteString("\n") 124 return err 125 } 126 127 // FindDep returns a reference to the named dependency in this package file 128 func (pkg *PackageBase) FindDep(ref string) *Dependency { 129 for _, d := range pkg.Dependencies { 130 if d.Hash == ref || d.Name == ref { 131 return d 132 } 133 } 134 return nil 135 } 136 137 func (pkg *PackageBase) ForEachDep(cb func(dep *Dependency, pkg *Package) error) error { 138 log.VLog(" - foreachdep: %s", pkg.Name) 139 for _, dep := range pkg.Dependencies { 140 var cpkg Package 141 err := LoadPackage(&cpkg, pkg.Language, dep.Hash) 142 if err != nil { 143 if os.IsNotExist(err) { 144 log.VLog("LoadPackage error: ", err) 145 return fmt.Errorf("package %s (%s) not found", dep.Name, dep.Hash) 146 } 147 return err 148 } 149 150 err = cb(dep, &cpkg) 151 if err != nil { 152 return err 153 } 154 } 155 156 return nil 157 }