-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathproperty_cache.go
105 lines (86 loc) · 2.98 KB
/
property_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"encoding/gob"
"fmt"
"os"
"path/filepath"
)
// variantKey specifies a build variant in an Android Gradle project.
type variantKey struct {
// Dir indicates the project directory where "build.gradle" resides.
Dir string
// Module indicates the name of the sub-module. Can be an empty string.
Module string
// Variant is the name of the build variant in an Android application module.
// When there are no product flavors, there are only two build variants: "debug" and "release".
Variant string
}
// propertyCache is a map used for caching the variant properties extracted from the Gradle scripts,
// so that apps can be launched more quickly without running Gradle tasks.
type propertyCache map[variantKey]variantProperties
func getPropertyCache(cacheFile string) (propertyCache, error) {
return readPropertyCacheMap(cacheFile)
}
// Clears the cache entry from the given cacheFile.
func clearPropertyCacheEntry(key variantKey, cacheFile string) error {
cache, err := getPropertyCache(cacheFile)
if err != nil {
return err
}
delete(cache, key)
return writePropertyCacheMap(cache, cacheFile)
}
// Adds a new entry in the property cache located at cacheFile and save the cache back to the file.
func writePropertyCacheEntry(key variantKey, props variantProperties, cacheFile string) error {
cache, err := getPropertyCache(cacheFile)
if err != nil {
return err
}
cache[key] = props
return writePropertyCacheMap(cache, cacheFile)
}
// Reads the property cache map from the given file using gob-encoding.
func readPropertyCacheMap(filename string) (propertyCache, error) {
f, err := os.Open(filename)
if err != nil {
// If the file does not exist, return an empty map without an error.
if os.IsNotExist(err) {
return propertyCache{}, nil
}
// An unexpected error occurred and should be returned.
return nil, err
}
defer f.Close()
decoder := gob.NewDecoder(f)
result := propertyCache{}
// Decoding might fail when the cache file is somehow corrupted, or when the cache schema is
// updated. In such cases, move on after resetting the cache file instead of exiting the app.
if err := decoder.Decode(&result); err != nil {
fmt.Fprintln(os.Stderr, "WARNING: Could not decode the property cache file. Resetting the cache.")
if err := os.Remove(f.Name()); err != nil {
return nil, err
}
return propertyCache{}, nil
}
return result, nil
}
// Writes the property cache map to the given file using gob-encoding.
func writePropertyCacheMap(cache propertyCache, filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
encoder := gob.NewEncoder(f)
return encoder.Encode(cache)
}
func getDefaultCacheFilePath() (string, error) {
configDir, err := getConfigDir()
if err != nil {
return "", err
}
return filepath.Join(configDir, "id_cache"), nil
}