-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (118 loc) · 3.81 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
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/deploy"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/env"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/execmd"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/gh"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/gradle"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/trigger"
"github.com/bitrise-steplib/bitrise-step-build-orchestrator/util"
)
func DisplayInfo() {
log.Infof("=== Display environment info ===")
execmd.ExecuteCommand("go", "version")
execmd.ExecuteCommand("git", "--version")
execmd.ExecuteCommand("adb", "--version")
execmd.ExecuteRelativeCommand("./gradlew", "--version")
}
type PathConfig struct {
Module string `env:"module,required"`
Type string `env:"module_type,opt[feature,root,testing,design]"`
Forced bool `env:"forced"`
}
func checkIfTestsExist(testPath string) bool {
log.Infof("Checking for tests in %s", testPath)
var root = fmt.Sprintf("./%s", testPath)
if _, err := os.Stat(root); !os.IsNotExist(err) {
log.Infof("OK. Found %s!\n", testPath)
return true
}
return false
}
func isSkippable(module string) bool {
var cfg PathConfig
if err := stepconf.Parse(&cfg); err != nil {
util.Failf("Issue with an input: %s", err)
}
if cfg.Forced {
log.Infof("Forcing build")
return false
}
if _, detected := os.LookupEnv("TARGET_MODULE"); detected {
log.Infof("TARGET_MODULE detected, forcing build")
return false
}
var testModuleDir string
var testPath string
var targetModule string
switch cfg.Type {
case "root":
testModuleDir = module
testPath = fmt.Sprintf("%s/src/androidTest", testModuleDir)
targetModule = module
case "testing":
testModuleDir = strings.TrimSuffix(module, "-tests")
testPath = fmt.Sprintf("testing/%s/src/androidTest", testModuleDir)
targetModule = "testing"
case "design":
testModuleDir = strings.TrimSuffix(strings.TrimPrefix(module, "design-"), "-tests")
testPath = fmt.Sprintf("design/%s/src/androidTest", testModuleDir)
targetModule = "design"
default:
testModuleDir = strings.TrimPrefix(module, "feature-")
testPath = fmt.Sprintf("features/%s/src/androidTest", testModuleDir)
targetModule = module
}
exists := checkIfTestsExist(testPath)
if !exists {
log.Errorf("No tests detected in %s. Skipping build", module)
return true
}
modules := gh.GetChangedModules()
if modules[targetModule] == false {
log.Errorf("No changes detected in %s. Skipping build", targetModule)
return true
}
log.Infof("Changes to module %s found. Running tests.", targetModule)
return false
}
func buildAndTrigger() {
timestamp()
gradle.Assemble()
timestamp()
gradle.PrepareForDeploy()
timestamp()
env.SetTargetEnv()
timestamp()
deploy.Deploy()
timestamp()
trigger.TriggerWorkflow()
timestamp()
}
var startTime int64 = 0
func timestamp() {
log.Infof("[Time] %d", time.Now().UnixNano() / int64(time.Millisecond) - startTime)
}
func main() {
startTime = time.Now().UnixNano() / int64(time.Millisecond)
timestamp()
var cfg PathConfig
if err := stepconf.Parse(&cfg); err != nil {
util.Failf("Issue with an input: %s", err)
}
timestamp()
DisplayInfo()
if isSkippable(cfg.Module) {
os.Exit(0)
}
log.Infof("Building %s", cfg.Module)
timestamp()
buildAndTrigger()
os.Exit(0)
}