env.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  50. // path to determine its staleness. If any environment variable values have
  51. // changed, it prints and returns changed environment variable values and
  52. // returns true.
  53. // Failing to read or parse the file also causes it to return true.
  54. func StaleEnvFile(filepath string, getenv func(string) string) (isStale bool,
  55. changedEnvironmentVariable []string, err error) {
  56. data, err := ioutil.ReadFile(filepath)
  57. if err != nil {
  58. return true, nil, err
  59. }
  60. var contents envFileData
  61. err = json.Unmarshal(data, &contents)
  62. if err != nil {
  63. return true, nil, err
  64. }
  65. var changed []string
  66. for _, entry := range contents {
  67. key := entry.Key
  68. old := entry.Value
  69. cur := getenv(key)
  70. if old != cur {
  71. changed = append(changed, fmt.Sprintf("%s (%q -> %q)", key, old, cur))
  72. changedEnvironmentVariable = append(changedEnvironmentVariable, key)
  73. }
  74. }
  75. if len(changed) > 0 {
  76. fmt.Printf("environment variables changed value:\n")
  77. for _, s := range changed {
  78. fmt.Printf(" %s\n", s)
  79. }
  80. return true, changedEnvironmentVariable, nil
  81. }
  82. return false, nil, nil
  83. }
  84. // Deserializes and environment serialized by EnvFileContents() and returns it
  85. // as a map[string]string.
  86. func EnvFromFile(envFile string) (map[string]string, error) {
  87. result := make(map[string]string)
  88. data, err := ioutil.ReadFile(envFile)
  89. if err != nil {
  90. return result, err
  91. }
  92. var contents envFileData
  93. err = json.Unmarshal(data, &contents)
  94. if err != nil {
  95. return result, err
  96. }
  97. for _, entry := range contents {
  98. result[entry.Key] = entry.Value
  99. }
  100. return result, nil
  101. }
  102. // Implements sort.Interface so that we can use sort.Sort on envFileData arrays.
  103. func (e envFileData) Len() int {
  104. return len(e)
  105. }
  106. func (e envFileData) Less(i, j int) bool {
  107. return e[i].Key < e[j].Key
  108. }
  109. func (e envFileData) Swap(i, j int) {
  110. e[i], e[j] = e[j], e[i]
  111. }
  112. var _ sort.Interface = envFileData{}