env.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Implements the environment JSON file handling for serializing the
  15. // environment variables that were used in soong_build so that soong_ui can
  16. // check whether they have changed
  17. package shared
  18. import (
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "sort"
  23. )
  24. type envFileEntry struct{ Key, Value string }
  25. type envFileData []envFileEntry
  26. // Serializes the given environment variable name/value map into JSON formatted bytes by converting
  27. // to envFileEntry values and marshaling them.
  28. //
  29. // e.g. OUT_DIR = "out"
  30. // is converted to:
  31. //
  32. // {
  33. // "Key": "OUT_DIR",
  34. // "Value": "out",
  35. // },
  36. func EnvFileContents(envDeps map[string]string) ([]byte, error) {
  37. contents := make(envFileData, 0, len(envDeps))
  38. for key, value := range envDeps {
  39. contents = append(contents, envFileEntry{key, value})
  40. }
  41. sort.Sort(contents)
  42. data, err := json.MarshalIndent(contents, "", " ")
  43. if err != nil {
  44. return nil, err
  45. }
  46. data = append(data, '\n')
  47. return data, nil
  48. }
  49. // Reads and deserializes a Soong environment file located at the given file path to determine its
  50. // staleness. If any environment variable values have changed, it prints them out and returns true.
  51. // Failing to read or parse the file also causes it to return true.
  52. func StaleEnvFile(filepath string, getenv func(string) string) (bool, error) {
  53. data, err := ioutil.ReadFile(filepath)
  54. if err != nil {
  55. return true, err
  56. }
  57. var contents envFileData
  58. err = json.Unmarshal(data, &contents)
  59. if err != nil {
  60. return true, err
  61. }
  62. var changed []string
  63. for _, entry := range contents {
  64. key := entry.Key
  65. old := entry.Value
  66. cur := getenv(key)
  67. if old != cur {
  68. changed = append(changed, fmt.Sprintf("%s (%q -> %q)", key, old, cur))
  69. }
  70. }
  71. if len(changed) > 0 {
  72. fmt.Printf("environment variables changed value:\n")
  73. for _, s := range changed {
  74. fmt.Printf(" %s\n", s)
  75. }
  76. return true, nil
  77. }
  78. return false, nil
  79. }
  80. // Deserializes and environment serialized by EnvFileContents() and returns it
  81. // as a map[string]string.
  82. func EnvFromFile(envFile string) (map[string]string, error) {
  83. result := make(map[string]string)
  84. data, err := ioutil.ReadFile(envFile)
  85. if err != nil {
  86. return result, err
  87. }
  88. var contents envFileData
  89. err = json.Unmarshal(data, &contents)
  90. if err != nil {
  91. return result, err
  92. }
  93. for _, entry := range contents {
  94. result[entry.Key] = entry.Value
  95. }
  96. return result, nil
  97. }
  98. // Implements sort.Interface so that we can use sort.Sort on envFileData arrays.
  99. func (e envFileData) Len() int {
  100. return len(e)
  101. }
  102. func (e envFileData) Less(i, j int) bool {
  103. return e[i].Key < e[j].Key
  104. }
  105. func (e envFileData) Swap(i, j int) {
  106. e[i], e[j] = e[j], e[i]
  107. }
  108. var _ sort.Interface = envFileData{}