-
Notifications
You must be signed in to change notification settings - Fork 19
/
cache.go
128 lines (106 loc) · 3.36 KB
/
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"strings"
"github.com/bitrise-io/bitrise-init/scanners/android"
androidCache "github.com/bitrise-io/go-android/v2/cache"
"github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/env"
)
type depsFunc func(dir string) ([]string, []string, error)
func (f FastlaneRunner) cacheDeps(opts RunOpts) {
if opts.EnableCache {
f.logger.Println()
f.logger.Infof("Collecting cache")
var depsFuncs = []depsFunc{
f.cocoapodsDeps,
f.carthageDeps,
f.androidDeps,
}
c := cache.New()
for _, depFunc := range depsFuncs {
includes, excludes, err := depFunc(opts.WorkDir)
f.logger.Debugf("%s found include path:\n%s\nexclude paths:\n%s", f.functionName(depFunc), strings.Join(includes, "\n"), strings.Join(excludes, "\n"))
if err != nil {
f.logger.Warnf("failed to collect dependencies: %s", err.Error())
continue
}
for _, item := range includes {
c.IncludePath(item)
}
for _, item := range excludes {
c.ExcludePath(item)
}
}
if err := c.Commit(); err != nil {
f.logger.Warnf("failed to commit paths to cache: %s", err)
}
}
}
func (f FastlaneRunner) functionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func (f FastlaneRunner) iosDeps(dir string, buildDirName, lockFileName string) ([]string, []string, error) {
files, err := pathutil.ListPathInDirSortedByComponents(dir, false)
if err != nil {
return nil, nil, fmt.Errorf("failed to search for files in (%s), error: %s", dir, err)
}
locks, err := pathutil.FilterPaths(files, pathutil.BaseFilter(lockFileName, true))
if err != nil {
return nil, nil, err
}
buildDirToLockFile := map[string]string{}
for _, lock := range locks {
buildDir := filepath.Join(filepath.Dir(lock), buildDirName)
exist, err := pathutil.IsPathExists(buildDir)
if err != nil {
return nil, nil, err
}
if exist {
buildDirToLockFile[buildDir] = lock
}
}
if len(buildDirToLockFile) > 1 {
var locks []string
for _, lock := range buildDirToLockFile {
locks = append(locks, lock)
}
log.Debugf("Multiple %s found: %s", lockFileName, strings.Join(locks, ", "))
}
var include []string
for buildDir, lockFile := range buildDirToLockFile {
include = append(include, fmt.Sprintf("%s -> %s", buildDir, lockFile))
}
return include, nil, nil
}
func (f FastlaneRunner) cocoapodsDeps(dir string) ([]string, []string, error) {
return f.iosDeps(dir, "Pods", "Podfile.lock")
}
func (f FastlaneRunner) carthageDeps(dir string) ([]string, []string, error) {
return f.iosDeps(dir, "Carthage", "Cartfile.resolved")
}
func (f FastlaneRunner) androidDeps(dir string) ([]string, []string, error) {
scanner := android.NewScanner()
detected, err := scanner.DetectPlatform(dir)
if err != nil {
return nil, nil, err
}
log.Debugf("android platform detected: %v", detected)
var include []string
var exclude []string
for _, project := range scanner.Projects {
i, e, err := androidCache.NewAndroidGradleCacheItemCollector(command.NewFactory(env.NewRepository())).Collect(project.RelPath, cache.LevelDeps)
if err != nil {
return nil, nil, err
}
include = append(include, i...)
exclude = append(exclude, e...)
}
return include, exclude, err
}