-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcocoapods_installer.go
121 lines (103 loc) · 3.32 KB
/
cocoapods_installer.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/bitrise-io/go-steputils/v2/ruby"
"github.com/bitrise-io/go-utils/v2/command"
"github.com/bitrise-io/go-utils/v2/errorutil"
"github.com/bitrise-io/go-utils/v2/log"
)
// CocoapodsInstaller ...
type CocoapodsInstaller struct {
rubyCmdFactory ruby.CommandFactory
logger log.Logger
}
// NewCocoapodsInstaller ...
func NewCocoapodsInstaller(rubyCmdFactory ruby.CommandFactory, logger log.Logger) CocoapodsInstaller {
return CocoapodsInstaller{
rubyCmdFactory: rubyCmdFactory,
logger: logger,
}
}
// InstallPods ...
func (i CocoapodsInstaller) InstallPods(podArg []string, podCmd string, podfileDir string, verbose bool) error {
if err := i.runPodInstall(podArg, podCmd, podfileDir, verbose); err == nil {
return nil
} else {
i.logger.Printf("")
i.logger.Warnf(errorutil.FormattedError(fmt.Errorf("Failed to install Pods: %w", err)))
i.logger.Warnf("Retrying with pod repo update...")
i.logger.Printf("")
}
if err := i.runPodRepoUpdate(podArg, podfileDir, verbose); err != nil {
return err
}
if err := i.runPodInstall(podArg, podCmd, podfileDir, verbose); err != nil {
return err
}
return nil
}
func (i CocoapodsInstaller) runPodInstall(podArg []string, podCmd string, podfileDir string, verbose bool) error {
errorFinder := &cocoapodsCmdErrorFinder{}
cmdSlice := podInstallCmdSlice(podArg, podCmd, verbose)
cmd := createPodCommand(i.rubyCmdFactory, cmdSlice, podfileDir, errorFinder)
i.logger.Donef("$ %s", cmd.PrintableCommandArgs())
return cmd.Run()
}
func (i CocoapodsInstaller) runPodRepoUpdate(podArg []string, podfileDir string, verbose bool) error {
errorFinder := &cocoapodsCmdErrorFinder{}
cmdSlice := podRepoUpdateCmdSlice(podArg, verbose)
cmd := createPodCommand(i.rubyCmdFactory, cmdSlice, podfileDir, errorFinder)
i.logger.Donef("$ %s", cmd.PrintableCommandArgs())
return cmd.Run()
}
func podInstallCmdSlice(podArg []string, podCmd string, verbose bool) []string {
cmdSlice := append(podArg, podCmd, "--no-repo-update")
if verbose {
cmdSlice = append(cmdSlice, "--verbose")
}
return cmdSlice
}
func podRepoUpdateCmdSlice(podArg []string, verbose bool) []string {
cmdSlice := append(podArg, "repo", "update")
if verbose {
cmdSlice = append(cmdSlice, "--verbose")
}
return cmdSlice
}
func createPodCommand(factory ruby.CommandFactory, args []string, dir string, errorFinder *cocoapodsCmdErrorFinder) command.Command {
return factory.Create(args[0], args[1:], &command.Opts{
Stdout: os.Stdout,
Stderr: os.Stderr,
Stdin: nil,
Env: nil,
Dir: dir,
ErrorFinder: errorFinder.findErrors,
})
}
type cocoapodsCmdErrorFinder struct {
transientProblemAlreadySeen bool
}
func (f *cocoapodsCmdErrorFinder) findErrors(out string) []string {
var errors []string
reader := strings.NewReader(out)
scanner := bufio.NewScanner(reader)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "[!] ") || strings.HasPrefix(line, "curl: ") {
errors = append(errors, line)
} else if strings.HasPrefix(line, "Warning: Transient problem: ") {
if !f.transientProblemAlreadySeen {
errors = append(errors, "Transient problem")
f.transientProblemAlreadySeen = true
}
}
}
if err := scanner.Err(); err != nil {
return nil
}
return errors
}