/
main.go
178 lines (149 loc) · 4.63 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
cacheutil "github.com/bitrise-io/go-steputils/cache"
"github.com/bitrise-io/go-steputils/input"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-utils/command"
"github.com/bitrise-io/go-utils/filedownloader"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-steplib/steps-carthage/cachedcarthage"
"github.com/bitrise-steplib/steps-carthage/carthage"
"github.com/hashicorp/go-version"
"github.com/kballard/go-shellquote"
)
const (
projectDirArg = "--project-directory"
)
// FileProvider ...
type FileProvider interface {
LocalPath(path string) (string, error)
}
// Config ...
type Config struct {
GithubAccessToken stepconf.Secret `env:"github_access_token"`
CarthageCommand string `env:"carthage_command,required"`
CarthageOptions string `env:"carthage_options"`
SourceDir string `env:"BITRISE_SOURCE_DIR"`
Xcconfig string `env:"xcconfig"`
XcconfigFromEnv string `env:"XCODE_XCCONFIG_FILE"`
// Debug
VerboseLog bool `env:"verbose_log,opt[yes,no]"`
}
func fail(format string, v ...interface{}) {
log.Errorf(format, v...)
os.Exit(1)
}
func main() {
var configs Config
if err := stepconf.Parse(&configs); err != nil {
fail("Could not create config: %s", err)
}
stepconf.Print(configs)
log.SetEnableDebugLog(configs.VerboseLog)
// Environment
fmt.Println()
log.Infof("Environment:")
carthageVersion, err := getCarthageVersion()
if err != nil {
fail("Failed to get carthage version, error: %s", err)
}
log.Printf("- CarthageVersion: %s", carthageVersion.String())
swiftVersion, err := getSwiftVersion()
if err != nil {
fail("Failed to get swift version, error: %s", err)
}
log.Printf("- SwiftVersion: %s", strings.Replace(swiftVersion, "\n", "- ", -1))
// --
// Parse options
args := parseCarthageOptions(configs)
fileProvider := input.NewFileProvider(filedownloader.New(http.DefaultClient))
xconfigPath, err := parseXCConfigPath(configs.Xcconfig, configs.XcconfigFromEnv, fileProvider)
if err != nil {
fail("Failed to get xcconfig file, error: %s", err)
}
projectDir := parseProjectDir(configs.SourceDir, args)
project := cachedcarthage.NewProject(projectDir)
filecache := cacheutil.New()
stateProvider := cachedcarthage.DefaultStateProvider{}
runner := cachedcarthage.NewRunner(
configs.CarthageCommand,
args,
configs.GithubAccessToken,
xconfigPath,
cachedcarthage.NewCache(project, swiftVersion, &filecache, stateProvider),
carthage.NewCLIBuilder(),
)
if err := runner.Run(); err != nil {
fail("Failed to execute step: %s", err)
}
}
func parseXCConfigPath(pathFromStepInput string, pathFromEnv string, fileProvider FileProvider) (string, error) {
pathToUse := ""
if pathFromStepInput != "" {
localPath, err := fileProvider.LocalPath(pathFromStepInput)
if err != nil {
return "", err
}
pathToUse = localPath
}
if pathFromEnv != "" {
if pathToUse != "" {
log.Warnf("Both `xcconfig` input and `XCODE_XCCONFIG_FILE` are set. Using `xcconfig` input.")
} else {
pathToUse = pathFromEnv
}
}
return pathToUse, nil
}
func parseCarthageOptions(config Config) []string {
var customCarthageOptions []string
if config.CarthageOptions != "" {
options, err := shellquote.Split(config.CarthageOptions)
if err != nil {
fail("Failed to shell split CarthageOptions (%s), error: %s", config.CarthageOptions)
}
customCarthageOptions = options
}
return customCarthageOptions
}
func getCarthageVersion() (*version.Version, error) {
cmd := carthage.NewCLIBuilder().Append("version").Command()
out, err := cmd.RunAndReturnTrimmedCombinedOutput()
if err != nil {
return nil, err
}
// if output is multi-line, get the last line of string
// parse Version from cmd output
for _, outLine := range strings.Split(out, "\n") {
if currentVersion, err := version.NewVersion(outLine); err == nil {
return currentVersion, nil
}
}
return nil, fmt.Errorf("failed to parse `$ carthage version` output: %s", out)
}
func getSwiftVersion() (string, error) {
cmd := command.New("swift", "-version")
return cmd.RunAndReturnTrimmedCombinedOutput()
}
func parseProjectDir(originalDir string, customCarthageOptions []string) string {
projectDir := originalDir
isNextOptionProjectDir := false
for _, option := range customCarthageOptions {
if option == projectDirArg {
isNextOptionProjectDir = true
continue
}
if isNextOptionProjectDir {
projectDir = option
fmt.Println()
log.Infof("--project-directory flag found with value: %s", projectDir)
log.Printf("using %s as working directory", projectDir)
break
}
}
return projectDir
}