forked from Jguer/yay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_install.go
419 lines (351 loc) · 10.6 KB
/
local_install.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Experimental code for install local with dependency refactoring
// Not at feature parity with install.go
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/Jguer/yay/v11/pkg/db"
"github.com/Jguer/yay/v11/pkg/dep"
"github.com/Jguer/yay/v11/pkg/download"
"github.com/Jguer/yay/v11/pkg/metadata"
"github.com/Jguer/yay/v11/pkg/multierror"
"github.com/Jguer/yay/v11/pkg/settings"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/settings/parser"
"github.com/Jguer/yay/v11/pkg/text"
"github.com/leonelquinteros/gotext"
"github.com/pkg/errors"
gosrc "github.com/Morganamilo/go-srcinfo"
mapset "github.com/deckarep/golang-set/v2"
)
var ErrInstallRepoPkgs = errors.New(gotext.Get("error installing repo packages"))
func installLocalPKGBUILD(
ctx context.Context,
cmdArgs *parser.Arguments,
dbExecutor db.Executor,
) error {
aurCache, err := metadata.NewAURCache(filepath.Join(config.BuildDir, "aur.json"))
if err != nil {
return errors.Wrap(err, gotext.Get("failed to retrieve aur Cache"))
}
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, gotext.Get("failed to retrieve working directory"))
}
if len(cmdArgs.Targets) > 1 {
return errors.New(gotext.Get("only one target is allowed"))
}
if len(cmdArgs.Targets) == 1 {
wd = cmdArgs.Targets[0]
}
pkgbuild, err := gosrc.ParseFile(filepath.Join(wd, ".SRCINFO"))
if err != nil {
return errors.Wrap(err, gotext.Get("failed to parse .SRCINFO"))
}
grapher := dep.NewGrapher(dbExecutor, aurCache, false, settings.NoConfirm, os.Stdout)
graph, err := grapher.GraphFromSrcInfo(wd, pkgbuild)
if err != nil {
return err
}
topoSorted := graph.TopoSortedLayerMap()
fmt.Println(topoSorted, len(topoSorted))
preparer := &Preparer{
dbExecutor: dbExecutor,
cmdBuilder: config.Runtime.CmdBuilder,
config: config,
}
installer := &Installer{dbExecutor: dbExecutor}
err = preparer.Present(os.Stdout, topoSorted)
if err != nil {
return err
}
cleanFunc := preparer.ShouldCleanMakeDeps(ctx)
if cleanFunc != nil {
installer.AddPostInstallHook(cleanFunc)
}
pkgBuildDirs, err := preparer.PrepareWorkspace(ctx, topoSorted)
if err != nil {
return err
}
err = installer.Install(ctx, cmdArgs, topoSorted, pkgBuildDirs)
if err != nil {
if errHook := installer.RunPostInstallHooks(ctx); errHook != nil {
text.Errorln(errHook)
}
return err
}
return installer.RunPostInstallHooks(ctx)
}
type Preparer struct {
dbExecutor db.Executor
cmdBuilder exe.ICmdBuilder
config *settings.Configuration
pkgBuildDirs []string
makeDeps []string
}
func (preper *Preparer) ShouldCleanMakeDeps(ctx context.Context) PostInstallHookFunc {
if len(preper.makeDeps) == 0 {
return nil
}
switch preper.config.RemoveMake {
case "yes":
break
case "no":
return nil
default:
if !text.ContinueTask(os.Stdin, gotext.Get("Remove make dependencies after install?"), false, settings.NoConfirm) {
return nil
}
}
return func(ctx context.Context) error {
return removeMake(ctx, preper.config.Runtime.CmdBuilder, preper.makeDeps)
}
}
func (preper *Preparer) Present(w io.Writer, targets []map[string]*dep.InstallInfo) error {
pkgsBySourceAndReason := map[string]map[string][]string{}
for _, layer := range targets {
for pkgName, info := range layer {
source := dep.SourceNames[info.Source]
reason := dep.ReasonNames[info.Reason]
pkgStr := text.Cyan(fmt.Sprintf("%s-%s", pkgName, info.Version))
if _, ok := pkgsBySourceAndReason[source]; !ok {
pkgsBySourceAndReason[source] = map[string][]string{}
}
pkgsBySourceAndReason[source][reason] = append(pkgsBySourceAndReason[source][reason], pkgStr)
if info.Reason == dep.MakeDep {
preper.makeDeps = append(preper.makeDeps, pkgName)
}
}
}
for source, pkgsByReason := range pkgsBySourceAndReason {
for reason, pkgs := range pkgsByReason {
fmt.Fprintf(w, text.Bold("%s %s (%d):")+" %s\n",
source,
reason,
len(pkgs),
strings.Join(pkgs, ", "))
}
}
return nil
}
func (preper *Preparer) PrepareWorkspace(ctx context.Context, targets []map[string]*dep.InstallInfo) (map[string]string, error) {
aurBases := mapset.NewThreadUnsafeSet[string]()
pkgBuildDirs := make(map[string]string, 0)
for _, layer := range targets {
for pkgName, info := range layer {
if info.Source == dep.AUR {
pkgBase := *info.AURBase
aurBases.Add(pkgBase)
pkgBuildDirs[pkgName] = filepath.Join(config.BuildDir, pkgBase)
} else if info.Source == dep.SrcInfo {
pkgBuildDirs[pkgName] = *info.SrcinfoPath
}
}
}
if _, errA := download.AURPKGBUILDRepos(ctx,
preper.cmdBuilder, aurBases.ToSlice(), config.AURURL, config.BuildDir, false); errA != nil {
return nil, errA
}
if errP := downloadPKGBUILDSourceFanout(ctx, config.Runtime.CmdBuilder,
preper.pkgBuildDirs, false, config.MaxConcurrentDownloads); errP != nil {
text.Errorln(errP)
}
return pkgBuildDirs, nil
}
type (
PostInstallHookFunc func(ctx context.Context) error
Installer struct {
dbExecutor db.Executor
postInstallHooks []PostInstallHookFunc
}
)
func (installer *Installer) AddPostInstallHook(hook PostInstallHookFunc) {
installer.postInstallHooks = append(installer.postInstallHooks, hook)
}
func (Installer *Installer) RunPostInstallHooks(ctx context.Context) error {
var errMulti multierror.MultiError
for _, hook := range Installer.postInstallHooks {
if err := hook(ctx); err != nil {
errMulti.Add(err)
}
}
return errMulti.Return()
}
func (installer *Installer) Install(ctx context.Context,
cmdArgs *parser.Arguments,
targets []map[string]*dep.InstallInfo,
pkgBuildDirs map[string]string,
) error {
// Reorganize targets into layers of dependencies
for i := len(targets) - 1; i >= 0; i-- {
err := installer.handleLayer(ctx, cmdArgs, targets[i], pkgBuildDirs)
if err != nil {
// rollback
return err
}
}
return nil
}
func (installer *Installer) handleLayer(ctx context.Context,
cmdArgs *parser.Arguments, layer map[string]*dep.InstallInfo, pkgBuildDirs map[string]string,
) error {
// Install layer
nameToBaseMap := make(map[string]string, 0)
syncDeps, syncExp := mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()
aurDeps, aurExp := mapset.NewThreadUnsafeSet[string](), mapset.NewThreadUnsafeSet[string]()
for name, info := range layer {
switch info.Source {
case dep.SrcInfo:
fallthrough
case dep.AUR:
nameToBaseMap[name] = *info.AURBase
switch info.Reason {
case dep.Explicit:
if cmdArgs.ExistsArg("asdeps", "asdep") {
aurDeps.Add(name)
} else {
aurExp.Add(name)
}
case dep.CheckDep:
fallthrough
case dep.MakeDep:
fallthrough
case dep.Dep:
aurDeps.Add(name)
}
case dep.Sync:
switch info.Reason {
case dep.Explicit:
if cmdArgs.ExistsArg("asdeps", "asdep") {
syncDeps.Add(name)
} else {
syncExp.Add(name)
}
case dep.CheckDep:
fallthrough
case dep.MakeDep:
fallthrough
case dep.Dep:
syncDeps.Add(name)
}
}
}
fmt.Println(syncDeps, syncExp)
errShow := installer.installSyncPackages(ctx, cmdArgs, syncDeps, syncExp)
if errShow != nil {
return ErrInstallRepoPkgs
}
errAur := installer.installAURPackages(ctx, cmdArgs, aurDeps, aurExp, nameToBaseMap, pkgBuildDirs, false)
return errAur
}
func (installer *Installer) installAURPackages(ctx context.Context,
cmdArgs *parser.Arguments,
aurDepNames, aurExpNames mapset.Set[string],
nameToBase, pkgBuildDirsByBase map[string]string,
installIncompatible bool,
) error {
deps, exp := make([]string, 0, aurDepNames.Cardinality()), make([]string, 0, aurExpNames.Cardinality())
for _, name := range aurDepNames.Union(aurExpNames).ToSlice() {
base := nameToBase[name]
dir := pkgBuildDirsByBase[base]
args := []string{"--nobuild", "-fC"}
if installIncompatible {
args = append(args, "--ignorearch")
}
// pkgver bump
if err := config.Runtime.CmdBuilder.Show(
config.Runtime.CmdBuilder.BuildMakepkgCmd(ctx, dir, args...)); err != nil {
return errors.New(gotext.Get("error making: %s", base))
}
pkgdests, _, errList := parsePackageList(ctx, dir)
if errList != nil {
return errList
}
args = []string{"-cf", "--noconfirm", "--noextract", "--noprepare", "--holdver"}
if installIncompatible {
args = append(args, "--ignorearch")
}
if errMake := config.Runtime.CmdBuilder.Show(
config.Runtime.CmdBuilder.BuildMakepkgCmd(ctx,
dir, args...)); errMake != nil {
return errors.New(gotext.Get("error making: %s", base))
}
names, err := installer.getNewTargets(pkgdests, name)
if err != nil {
return err
}
isDep := installer.isDep(cmdArgs, aurExpNames, name)
if isDep {
deps = append(deps, names...)
} else {
exp = append(exp, names...)
}
}
if err := doInstall(ctx, cmdArgs, deps, exp); err != nil {
return errors.New(fmt.Sprintf(gotext.Get("error installing:")+" %v %v", deps, exp))
}
return nil
}
func (*Installer) isDep(cmdArgs *parser.Arguments, aurExpNames mapset.Set[string], name string) bool {
switch {
case cmdArgs.ExistsArg("asdeps", "asdep"):
return true
case cmdArgs.ExistsArg("asexplicit", "asexp"):
return false
case aurExpNames.Contains(name):
return false
}
return true
}
func (installer *Installer) getNewTargets(pkgdests map[string]string, name string,
) ([]string, error) {
pkgdest, ok := pkgdests[name]
names := make([]string, 0, 2)
if !ok {
return nil, errors.New(gotext.Get("could not find PKGDEST for: %s", name))
}
if _, errStat := os.Stat(pkgdest); os.IsNotExist(errStat) {
return nil, errors.New(
gotext.Get(
"the PKGDEST for %s is listed by makepkg but does not exist: %s",
name, pkgdest))
}
names = append(names, name)
debugName := pkgdest + "-debug"
pkgdestDebug, ok := pkgdests[debugName]
if ok {
if _, errStat := os.Stat(pkgdestDebug); errStat == nil {
names = append(names, debugName)
}
}
return names, nil
}
func (*Installer) installSyncPackages(ctx context.Context, cmdArgs *parser.Arguments,
syncDeps, // repo targets that are deps
syncExp mapset.Set[string], // repo targets that are exp
) error {
repoTargets := syncDeps.Union(syncExp).ToSlice()
if len(repoTargets) == 0 {
return nil
}
arguments := cmdArgs.Copy()
arguments.DelArg("asdeps", "asdep")
arguments.DelArg("asexplicit", "asexp")
arguments.DelArg("i", "install")
arguments.Op = "S"
arguments.ClearTargets()
arguments.AddTarget(repoTargets...)
errShow := config.Runtime.CmdBuilder.Show(config.Runtime.CmdBuilder.BuildPacmanCmd(ctx,
arguments, config.Runtime.Mode, settings.NoConfirm))
if errD := asdeps(ctx, cmdArgs, syncDeps.ToSlice()); errD != nil {
return errD
}
if errE := asexp(ctx, cmdArgs, syncExp.ToSlice()); errE != nil {
return errE
}
return errShow
}