-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
202 lines (170 loc) · 5.86 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/bitrise-io/go-android/cache"
"github.com/bitrise-io/go-android/gradle"
utilscache "github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/env"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/sliceutil"
"github.com/kballard/go-shellquote"
)
// Config ...
type Config struct {
ProjectLocation string `env:"project_location,dir"`
ReportPathPattern string `env:"report_path_pattern"`
Variant string `env:"variant"`
Module string `env:"module"`
Arguments string `env:"arguments"`
CacheLevel string `env:"cache_level,opt[none,only_deps,all]"`
DeployDir string `env:"BITRISE_DEPLOY_DIR,dir"`
}
var logger = log.NewLogger(false)
func getArtifacts(gradleProject gradle.Project, started time.Time, pattern string) (artifacts []gradle.Artifact, err error) {
artifacts, err = gradleProject.FindArtifacts(started, pattern, true)
if err != nil {
return
}
if len(artifacts) == 0 {
if !started.IsZero() {
logger.Warnf("No artifacts found with pattern: %s that has modification time after: %s", pattern, started)
logger.Warnf("Retrying without modtime check....")
logger.Println()
return getArtifacts(gradleProject, time.Time{}, pattern)
}
logger.Warnf("No artifacts found with pattern: %s without modtime check", pattern)
logger.Warnf("If you have changed default report export path in your gradle files then you might need to change ReportPathPattern accordingly.")
}
return
}
func filterVariants(module, variant string, variantsMap gradle.Variants) (gradle.Variants, error) {
// if module set: drop all the other modules
if module != "" {
v, ok := variantsMap[module]
if !ok {
return nil, fmt.Errorf("module not found: %s", module)
}
variantsMap = gradle.Variants{module: v}
}
// if variant not set: use all variants
if variant == "" {
return variantsMap, nil
}
filteredVariants := gradle.Variants{}
for m, variants := range variantsMap {
for _, v := range variants {
if strings.ToLower(v) == strings.ToLower(variant) {
filteredVariants[m] = append(filteredVariants[m], v)
}
}
}
if len(filteredVariants) == 0 {
return nil, fmt.Errorf("variant: %s not found in any module", variant)
}
return filteredVariants, nil
}
func mainE(config Config, cmdFactory command.Factory, logger log.Logger) error {
gradleProject, err := gradle.NewProject(config.ProjectLocation, cmdFactory)
if err != nil {
return fmt.Errorf("Process config: failed to open project, error: %s", err)
}
lintTask := gradleProject.GetTask("lint")
args, err := shellquote.Split(config.Arguments)
if err != nil {
return fmt.Errorf("Process config: failed to parse arguments, error: %s", err)
}
logger.Infof("Variants:")
fmt.Println()
variants, err := lintTask.GetVariants(args...)
if err != nil {
return fmt.Errorf("Run: failed to fetch variants, error: %s", err)
}
filteredVariants, err := filterVariants(config.Module, config.Variant, variants)
if err != nil {
failf("Process config: failed to find buildable variants, error: %s", err)
}
for module, variants := range variants {
logger.Printf("%s:", module)
for _, variant := range variants {
if sliceutil.IsStringInSlice(variant, filteredVariants[module]) {
logger.Donef("✓ %s", strings.TrimSuffix(variant, "UnitTest"))
continue
}
logger.Printf("- %s", strings.TrimSuffix(variant, "UnitTest"))
}
}
fmt.Println()
started := time.Now()
logger.Infof("Run lint:")
lintCommand := lintTask.GetCommand(filteredVariants, args...)
fmt.Println()
logger.Donef("$ " + lintCommand.PrintableCommandArgs())
fmt.Println()
taskError := lintCommand.Run()
if taskError != nil {
logger.Errorf("Run: lint task failed, error: %v", taskError)
}
fmt.Println()
logger.Infof("Exporting artifacts:")
fmt.Println()
artifacts, err := getArtifacts(gradleProject, started, config.ReportPathPattern)
if err != nil {
return fmt.Errorf("Export outputs: failed to find artifacts, error: %v", err)
}
if len(artifacts) > 0 {
for _, artifact := range artifacts {
exists, err := pathutil.IsPathExists(
filepath.Join(config.DeployDir, artifact.Name),
)
if err != nil {
return fmt.Errorf("Export outputs: failed to check path, error: %v", err)
}
artifactName := filepath.Base(artifact.Path)
if exists {
timestamp := time.Now().
Format("20060102150405")
ext := filepath.Ext(artifact.Name)
name := strings.TrimSuffix(filepath.Base(artifact.Name), ext)
artifact.Name = fmt.Sprintf("%s-%s%s", name, timestamp, ext)
}
logger.Printf(" Export [ %s => $BITRISE_DEPLOY_DIR/%s ]", artifactName, artifact.Name)
if err := artifact.Export(config.DeployDir); err != nil {
logger.Warnf("failed to export artifacts, error: %v", err)
}
}
} else {
logger.Warnf("No artifacts found with pattern: %s", config.ReportPathPattern)
logger.Warnf("If you have changed default report file paths with lintOptions/htmlOutput or lintOptions/xmlOutput")
logger.Warnf("in your gradle files then you might need to change ReportPathPattern accordingly.")
}
return taskError
}
func failf(f string, args ...interface{}) {
logger.Errorf(f, args...)
os.Exit(1)
}
func main() {
var config Config
if err := stepconf.Parse(&config); err != nil {
failf("Process config: couldn't create step config: %v\n", err)
}
stepconf.Print(config)
fmt.Println()
cmdFactory := command.NewFactory(env.NewRepository())
if err := mainE(config, cmdFactory, logger); err != nil {
failf("%s", err)
}
fmt.Println()
logger.Infof("Collecting cache:")
if warning := cache.Collect(config.ProjectLocation, utilscache.Level(config.CacheLevel), cmdFactory); warning != nil {
logger.Warnf("%s", warning)
}
logger.Donef(" Done")
}