-
Notifications
You must be signed in to change notification settings - Fork 13
/
refresh.go
177 lines (152 loc) · 5.17 KB
/
refresh.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
package runtime
import (
"github.com/ActiveState/cli/internal/analytics"
"github.com/ActiveState/cli/internal/constants"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
configMediator "github.com/ActiveState/cli/internal/mediators/config"
"github.com/ActiveState/cli/internal/output"
"github.com/ActiveState/cli/internal/rtutils"
"github.com/ActiveState/cli/internal/runbits/rationalize"
"github.com/ActiveState/cli/pkg/platform/authentication"
"github.com/ActiveState/cli/pkg/platform/model"
bpModel "github.com/ActiveState/cli/pkg/platform/model/buildplanner"
"github.com/ActiveState/cli/pkg/platform/runtime"
"github.com/ActiveState/cli/pkg/platform/runtime/setup/events"
"github.com/ActiveState/cli/pkg/platform/runtime/target"
"github.com/ActiveState/cli/pkg/project"
"github.com/go-openapi/strfmt"
"github.com/imacks/bitflags-go"
)
func init() {
configMediator.RegisterOption(constants.AsyncRuntimeConfig, configMediator.Bool, false)
}
type Opts int
const (
OptNone Opts = 1 << iota
OptMinimalUI // Only print progress output, don't decorate the UI in any other way
OptNoUI // Don't print progress output, don't decorate the UI in any other way
OptOrderChanged // Indicate that the order has changed, and the runtime should be refreshed regardless of internal dirty checking mechanics
)
type Configurable interface {
GetString(key string) string
GetBool(key string) bool
}
var overrideAsyncTriggers = map[target.Trigger]bool{
target.TriggerRefresh: true,
target.TriggerExec: true,
target.TriggerActivate: true,
target.TriggerShell: true,
target.TriggerScript: true,
target.TriggerDeploy: true,
target.TriggerUse: true,
}
// SolveAndUpdate should be called after runtime mutations.
func SolveAndUpdate(
auth *authentication.Auth,
out output.Outputer,
an analytics.Dispatcher,
proj *project.Project,
customCommitID *strfmt.UUID,
trigger target.Trigger,
svcm *model.SvcModel,
cfg Configurable,
opts Opts,
) (_ *runtime.Runtime, rerr error) {
defer rationalizeError(auth, proj, &rerr)
if proj == nil {
return nil, rationalize.ErrNoProject
}
if proj.IsHeadless() {
return nil, rationalize.ErrHeadless
}
if cfg.GetBool(constants.AsyncRuntimeConfig) && !overrideAsyncTriggers[trigger] {
logging.Debug("Skipping runtime solve due to async runtime")
return nil, nil
}
target := target.NewProjectTarget(proj, customCommitID, trigger)
rt, err := runtime.New(target, an, svcm, auth, cfg, out)
if err != nil {
return nil, locale.WrapError(err, "err_packages_update_runtime_init", "Could not initialize runtime.")
}
if !bitflags.Has(opts, OptOrderChanged) && !bitflags.Has(opts, OptMinimalUI) && !rt.NeedsUpdate() {
out.Notice(locale.Tl("pkg_already_uptodate", "Requested dependencies are already configured and installed."))
return rt, nil
}
if rt.NeedsUpdate() && !bitflags.Has(opts, OptMinimalUI) {
if !rt.HasCache() {
out.Notice(output.Title(locale.T("install_runtime")))
out.Notice(locale.T("install_runtime_info"))
} else {
out.Notice(output.Title(locale.T("update_runtime")))
out.Notice(locale.T("update_runtime_info"))
}
}
if rt.NeedsUpdate() {
pg := NewRuntimeProgressIndicator(out)
defer rtutils.Closer(pg.Close, &rerr)
err := rt.SolveAndUpdate(pg)
if err != nil {
return nil, locale.WrapError(err, "err_packages_update_runtime_install", "Could not install dependencies.")
}
}
return rt, nil
}
func Solve(
auth *authentication.Auth,
out output.Outputer,
an analytics.Dispatcher,
proj *project.Project,
customCommitID *strfmt.UUID,
trigger target.Trigger,
svcm *model.SvcModel,
cfg Configurable,
opts Opts,
) (_ *runtime.Runtime, _ *bpModel.Commit, rerr error) {
var spinner *output.Spinner
if !bitflags.Has(opts, OptMinimalUI) {
spinner = output.StartSpinner(out, locale.T("progress_solve_preruntime"), constants.TerminalAnimationInterval)
}
defer func() {
if spinner == nil {
return
}
if rerr != nil {
spinner.Stop(locale.T("progress_fail"))
} else {
spinner.Stop(locale.T("progress_success"))
}
}()
rtTarget := target.NewProjectTarget(proj, customCommitID, trigger)
rt, err := runtime.New(rtTarget, an, svcm, auth, cfg, out)
if err != nil {
return nil, nil, locale.WrapError(err, "err_packages_update_runtime_init", "Could not initialize runtime.")
}
setup := rt.Setup(&events.VoidHandler{})
commit, err := setup.Solve()
if err != nil {
return nil, nil, errs.Wrap(err, "Solve failed")
}
return rt, commit, nil
}
// UpdateByReference will update the given runtime if necessary. This is functionally the same as SolveAndUpdateByReference
// except that it does not do its own solve.
func UpdateByReference(
rt *runtime.Runtime,
commit *bpModel.Commit,
auth *authentication.Auth,
proj *project.Project,
out output.Outputer,
) (rerr error) {
defer rationalizeError(auth, proj, &rerr)
if rt.NeedsUpdate() {
pg := NewRuntimeProgressIndicator(out)
defer rtutils.Closer(pg.Close, &rerr)
err := rt.Setup(pg).Update(commit)
if err != nil {
return locale.WrapError(err, "err_packages_update_runtime_install", "Could not install dependencies.")
}
}
return nil
}