Skip to content

Commit d33e77d

Browse files
Make it possible to mutate Package props when loading indexes from differente places
1 parent e7a8321 commit d33e77d

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

Diff for: arduino/cores/packageindex/index.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ var tr = i18n.Tr
142142

143143
// MergeIntoPackages converts the Index data into a cores.Packages and merge them
144144
// with the existing contents of the cores.Packages passed as parameter.
145-
func (index Index) MergeIntoPackages(outPackages cores.Packages) {
145+
func (index Index) MergeIntoPackages(outPackages cores.Packages, mutators ...cores.PackageMutator) {
146146
for _, inPackage := range index.Packages {
147-
inPackage.extractPackageIn(outPackages, index.IsTrusted)
147+
inPackage.extractPackageIn(outPackages, index.IsTrusted, mutators...)
148148
}
149149
}
150150

@@ -243,7 +243,7 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
243243
}
244244
}
245245

246-
func (inPackage indexPackage) extractPackageIn(outPackages cores.Packages, trusted bool) {
246+
func (inPackage indexPackage) extractPackageIn(outPackages cores.Packages, trusted bool, mutators ...cores.PackageMutator) {
247247
outPackage := outPackages.GetOrCreatePackage(inPackage.Name)
248248
outPackage.Maintainer = inPackage.Maintainer
249249
outPackage.WebsiteURL = inPackage.WebsiteURL
@@ -258,6 +258,12 @@ func (inPackage indexPackage) extractPackageIn(outPackages cores.Packages, trust
258258
for _, inPlatform := range inPackage.Platforms {
259259
inPlatform.extractPlatformIn(outPackage, trusted)
260260
}
261+
262+
// Apply mutations in case we need to override the above behaviour. For example this is used to change the `indexed`
263+
// property when the `LoadPackageIndexFromFile` or `LoadPackageIndex` func is loading the global index files.
264+
for _, mutator := range mutators {
265+
mutator(outPackage)
266+
}
261267
}
262268

263269
func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *cores.Package, trusted bool) error {

Diff for: arduino/cores/packagemanager/install_uninstall.go

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades(
5252
if installed == nil {
5353
return &arduino.PlatformNotFoundError{Platform: platformRef.String()}
5454
}
55+
if !installed.Platform.Package.IsIndexed() {
56+
taskCB(&rpc.TaskProgress{Name: tr("WARNING missing package index for %s, future updates cannot be guaranteed", platform.String())})
57+
}
5558
latest := platform.GetLatestRelease()
5659
if !latest.Version.GreaterThan(installed.Version) {
5760
return &arduino.PlatformAlreadyAtTheLatestVersionError{}

Diff for: arduino/cores/packagemanager/package_manager.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ func (pmb *Builder) LoadPackageIndex(URL *url.URL) error {
451451
p.URL = URL.String()
452452
}
453453

454-
index.MergeIntoPackages(pmb.packages)
454+
index.MergeIntoPackages(pmb.packages, cores.WithIndexedStatus(true))
455455
return nil
456456
}
457457

@@ -462,7 +462,15 @@ func (pmb *Builder) LoadPackageIndexFromFile(indexPath *paths.Path) (*packageind
462462
return nil, fmt.Errorf(tr("loading json index file %[1]s: %[2]s"), indexPath, err)
463463
}
464464

465-
index.MergeIntoPackages(pmb.packages)
465+
// When the global indexes are loading packages, we're setting the `indexed` property at true. Other indexing
466+
// coming from installed.json must not alter the status of that property.
467+
switch indexPath.Base() {
468+
case "installed.json":
469+
index.MergeIntoPackages(pmb.packages)
470+
default:
471+
index.MergeIntoPackages(pmb.packages, cores.WithIndexedStatus(true))
472+
}
473+
466474
return index, nil
467475
}
468476

Diff for: arduino/cores/status.go

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ type Package struct {
4747
Tools map[string]*Tool // The tools in the system.
4848
Help PackageHelp `json:"-"`
4949
Packages Packages `json:"-"`
50+
indexed bool
51+
}
52+
53+
// PackageMutator is a func used to modify the property of the given *Package
54+
type PackageMutator func(p *Package)
55+
56+
// WithIndexedStatus used to modify the indexed propriety of the *Package
57+
func WithIndexedStatus(indexed bool) PackageMutator {
58+
return func(p *Package) { p.indexed = indexed }
5059
}
5160

5261
// GetOrCreatePackage returns the specified Package or creates an empty one
@@ -185,6 +194,11 @@ func (targetPackage *Package) GetOrCreateTool(name string) *Tool {
185194
return tool
186195
}
187196

197+
// IsIndexed returns true if the package is loaded from package_index.json
198+
func (targetPackage *Package) IsIndexed() bool {
199+
return targetPackage.indexed
200+
}
201+
188202
func (targetPackage *Package) String() string {
189203
return targetPackage.Name
190204
}

Diff for: internal/integrationtest/core/core_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -1026,3 +1026,19 @@ func TestCoreBrokenDependency(t *testing.T) {
10261026
require.Error(t, err)
10271027
require.Contains(t, string(stderr), "try contacting [email protected]")
10281028
}
1029+
1030+
func TestCoreUpgradeWarningWithPackageInstalledButNotIndexed(t *testing.T) {
1031+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
1032+
defer env.CleanUp()
1033+
1034+
// update index
1035+
url := "https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json"
1036+
_, _, err := cli.Run("core", "update-index", "--additional-urls="+url)
1037+
require.NoError(t, err)
1038+
// install 3rd-party core outdated version
1039+
_, _, err = cli.Run("core", "install", "esp32:[email protected]", "--additional-urls="+url)
1040+
require.NoError(t, err)
1041+
// upgrade without index fires a warning
1042+
stdout, _, _ := cli.Run("core", "upgrade", "esp32:esp32")
1043+
require.Contains(t, string(stdout), "WARNING missing package index for esp32:esp32, future updates cannot be guaranteed")
1044+
}

0 commit comments

Comments
 (0)