Home | History | Annotate | Download | only in env
      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 
     15 // env implements the environment JSON file handling for the soong_env command line tool run before
     16 // the builder and for the env writer in the builder.
     17 package env
     18 
     19 import (
     20 	"encoding/json"
     21 	"fmt"
     22 	"io/ioutil"
     23 	"os"
     24 	"sort"
     25 )
     26 
     27 type envFileEntry struct{ Key, Value string }
     28 type envFileData []envFileEntry
     29 
     30 func WriteEnvFile(filename string, envDeps map[string]string) error {
     31 	contents := make(envFileData, 0, len(envDeps))
     32 	for key, value := range envDeps {
     33 		contents = append(contents, envFileEntry{key, value})
     34 	}
     35 
     36 	sort.Sort(contents)
     37 
     38 	data, err := json.MarshalIndent(contents, "", "    ")
     39 	if err != nil {
     40 		return err
     41 	}
     42 
     43 	data = append(data, '\n')
     44 
     45 	err = ioutil.WriteFile(filename, data, 0664)
     46 	if err != nil {
     47 		return err
     48 	}
     49 
     50 	return nil
     51 }
     52 
     53 func StaleEnvFile(filename string) (bool, error) {
     54 	data, err := ioutil.ReadFile(filename)
     55 	if err != nil {
     56 		return true, err
     57 	}
     58 
     59 	var contents envFileData
     60 
     61 	err = json.Unmarshal(data, &contents)
     62 	if err != nil {
     63 		return true, err
     64 	}
     65 
     66 	var changed []string
     67 	for _, entry := range contents {
     68 		key := entry.Key
     69 		old := entry.Value
     70 		cur := os.Getenv(key)
     71 		if old != cur {
     72 			changed = append(changed, fmt.Sprintf("%s (%q -> %q)", key, old, cur))
     73 		}
     74 	}
     75 
     76 	if len(changed) > 0 {
     77 		fmt.Printf("environment variables changed value:\n")
     78 		for _, s := range changed {
     79 			fmt.Printf("   %s\n", s)
     80 		}
     81 		return true, nil
     82 	}
     83 
     84 	return false, nil
     85 }
     86 
     87 func (e envFileData) Len() int {
     88 	return len(e)
     89 }
     90 
     91 func (e envFileData) Less(i, j int) bool {
     92 	return e[i].Key < e[j].Key
     93 }
     94 
     95 func (e envFileData) Swap(i, j int) {
     96 	e[i], e[j] = e[j], e[i]
     97 }
     98