convert.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright 2018 Harvey OS Team
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. 3. Neither the name of the copyright holder nor the names of its contributors
  11. may be used to endorse or promote products derived from this software
  12. without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  22. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  23. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package main
  26. import (
  27. "encoding/json"
  28. "flag"
  29. "io/ioutil"
  30. "log"
  31. "os"
  32. "path/filepath"
  33. )
  34. var config = struct {
  35. Harvey string
  36. Args []string
  37. Except map[string]bool
  38. CmdName string
  39. FullPath string
  40. Src string
  41. Uroot string
  42. Cwd string
  43. Bbsh string
  44. Goroot string
  45. Gosrcroot string
  46. Arch string
  47. Goos string
  48. Gopath string
  49. TempDir string
  50. Go string
  51. Debug bool
  52. Fail bool
  53. }{
  54. Harvey: os.Getenv("HARVEY"),
  55. Except: map[string]bool{"sysconf.json": true},
  56. }
  57. type fixup func(string, map[string]interface{})
  58. func cflags(n string, jsmap map[string]interface{}) {
  59. if _, ok := jsmap["Cflags"]; ok {
  60. log.Printf("Deleting Cflags from %v", n)
  61. delete(jsmap, "Cflags")
  62. // TODO: once we have another ARCH, use it.
  63. a := []string{"/amd64/include/cflags.json"}
  64. switch tval := jsmap["Include"].(type) {
  65. case []interface{}:
  66. for _, v := range tval {
  67. a = append(a, v.(string))
  68. }
  69. }
  70. jsmap["Include"] = a
  71. }
  72. }
  73. func removeempty(n string, jsmap map[string]interface{}) {
  74. for key, val := range jsmap {
  75. switch tval := val.(type) {
  76. case map[string]interface{}:
  77. log.Printf("%s: tval %s", n, tval)
  78. if len(tval) == 0 {
  79. delete(jsmap, key)
  80. }
  81. case []interface{}:
  82. if len(tval) == 0 {
  83. delete(jsmap, key)
  84. }
  85. }
  86. }
  87. }
  88. func checkname(n string, jsmap map[string]interface{}) {
  89. if _, ok := jsmap["Name"]; !ok {
  90. log.Printf("File %v has no \"Name\" key", n)
  91. }
  92. }
  93. func one(n string, f ...fixup) error {
  94. buf, err := ioutil.ReadFile(n)
  95. if err != nil {
  96. return err
  97. }
  98. var jsmap map[string]interface{}
  99. if err := json.Unmarshal(buf, &jsmap); err != nil {
  100. return err
  101. }
  102. if config.Debug {
  103. log.Printf("%v: %v", n, jsmap)
  104. }
  105. mapname := jsmap["Name"].(string)
  106. delete(jsmap, "Name")
  107. var nmap = make(map[string]map[string]interface{})
  108. nmap[mapname] = jsmap
  109. buf, err = json.MarshalIndent(nmap, "", "\t")
  110. if err != nil {
  111. return err
  112. }
  113. buf = append(buf, '\n')
  114. if config.Debug {
  115. os.Stdout.Write(buf)
  116. } else {
  117. ioutil.WriteFile(n, buf, 0666)
  118. }
  119. return nil
  120. }
  121. func init() {
  122. if config.Harvey == "" {
  123. log.Fatalf("Please set $HARVEY")
  124. }
  125. if len(config.Args) == 0 {
  126. config.Args = []string{config.Harvey}
  127. }
  128. }
  129. func main() {
  130. var err error
  131. flag.BoolVar(&config.Debug, "d", true, "Enable debug prints")
  132. flag.Parse()
  133. config.Args = flag.Args()
  134. for _, n := range config.Args {
  135. err = filepath.Walk(n, func(name string, fi os.FileInfo, err error) error {
  136. if err != nil {
  137. return err
  138. }
  139. if fi.IsDir() {
  140. return nil
  141. }
  142. n := fi.Name()
  143. if len(n) < 5 || n[len(n)-5:] != ".json" {
  144. return nil
  145. }
  146. if config.Except[fi.Name()] {
  147. return nil
  148. }
  149. todo := []fixup{removeempty, checkname}
  150. log.Printf("process %s", name)
  151. err = one(name, todo...)
  152. if err != nil {
  153. log.Printf("%s: %s\n", name, err)
  154. return err
  155. }
  156. return nil
  157. })
  158. if err != nil {
  159. log.Fatalf("%v", err)
  160. }
  161. }
  162. }