Skip to content

Commit

Permalink
Replace direct access to the package manager with a call to PlatformS…
Browse files Browse the repository at this point in the history
…earch function
  • Loading branch information
MatteoPologruto committed Sep 13, 2023
1 parent f7b3c30 commit 11b4de8
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
8 changes: 4 additions & 4 deletions arduino/cores/packagemanager/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ func (platform *PlatformReference) String() string {

// FindPlatform returns the Platform matching the PlatformReference or nil if not found.
// The PlatformVersion field of the reference is ignored.
func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string) *cores.Platform {
targetPackage, ok := pme.packages[platformPackage]
func (pme *Explorer) FindPlatform(ref *PlatformReference) *cores.Platform {
targetPackage, ok := pme.packages[ref.Package]
if !ok {
return nil
}
platform, ok := targetPackage.Platforms[platformArchitecture]
platform, ok := targetPackage.Platforms[ref.PlatformArchitecture]
if !ok {
return nil
}
Expand All @@ -57,7 +57,7 @@ func (pme *Explorer) FindPlatform(platformPackage, platformArchitecture string)

// FindPlatformRelease returns the PlatformRelease matching the PlatformReference or nil if not found
func (pme *Explorer) FindPlatformRelease(ref *PlatformReference) *cores.PlatformRelease {
platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture)
platform := pme.FindPlatform(ref)
if platform == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion arduino/cores/packagemanager/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades(
}

// Search the latest version for all specified platforms
platform := pme.FindPlatform(platformRef.Package, platformRef.PlatformArchitecture)
platform := pme.FindPlatform(platformRef)
if platform == nil {
return nil, &arduino.PlatformNotFoundError{Platform: platformRef.String()}
}
Expand Down
5 changes: 4 additions & 1 deletion arduino/cores/packagemanager/package_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,10 @@ func TestBoardOrdering(t *testing.T) {
pme, release := pm.NewExplorer()
defer release()

pl := pme.FindPlatform("arduino", "avr")
pl := pme.FindPlatform(&PlatformReference{
Package: "arduino",
PlatformArchitecture: "avr",
})
require.NotNil(t, pl)
plReleases := pl.GetAllInstalled()
require.NotEmpty(t, plReleases)
Expand Down
2 changes: 1 addition & 1 deletion commands/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func platformUninstall(ctx context.Context, req *rpc.PlatformUninstallRequest, t
PlatformArchitecture: req.Architecture,
}
if ref.PlatformVersion == nil {
platform := pme.FindPlatform(ref.Package, ref.PlatformArchitecture)
platform := pme.FindPlatform(ref)
if platform == nil {
return &arduino.PlatformNotFoundError{Platform: ref.String()}
}
Expand Down
5 changes: 4 additions & 1 deletion commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,10 @@ func runProgramAction(pme *packagemanager.Explorer,
Property: fmt.Sprintf("%s.tool.%s", action, port.Protocol), // TODO: Can be done better, maybe inline getToolID(...)
Value: uploadToolID}
} else if len(split) == 2 {
p := pme.FindPlatform(split[0], boardPlatform.Platform.Architecture)
p := pme.FindPlatform(&packagemanager.PlatformReference{
Package: split[0],
PlatformArchitecture: boardPlatform.Platform.Architecture,
})
if p == nil {
return nil, &arduino.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
}
Expand Down
11 changes: 5 additions & 6 deletions internal/cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"strings"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/compile"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/commands/upload"
"github.com/arduino/arduino-cli/configuration"
Expand Down Expand Up @@ -362,13 +362,12 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
panic(tr("Platform ID is not correct"))
}

pme, release := commands.GetPackageManagerExplorer(compileRequest)
platform := pme.FindPlatform(split[0], split[1])
release()

if profileArg.String() == "" {
res.Error += fmt.Sprintln()
if platform != nil {
if platform, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
Instance: inst,
SearchArgs: platformErr.Platform,
AllVersions: false}); platform != nil {
suggestion := fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform)
res.Error += tr("Try running %s", suggestion)
} else {
Expand Down
11 changes: 5 additions & 6 deletions internal/cli/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"strings"

"github.com/arduino/arduino-cli/arduino"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/core"
sk "github.com/arduino/arduino-cli/commands/sketch"
"github.com/arduino/arduino-cli/commands/upload"
"github.com/arduino/arduino-cli/i18n"
Expand Down Expand Up @@ -129,12 +129,11 @@ func runUploadCommand(command *cobra.Command, args []string) {
panic(tr("Platform ID is not correct"))
}

pme, release := commands.GetPackageManagerExplorer(&rpc.UploadRequest{Instance: inst})
platform := pme.FindPlatform(split[0], split[1])
release()

msg += "\n"
if platform != nil {
if platform, _ := core.PlatformSearch(&rpc.PlatformSearchRequest{
Instance: inst,
SearchArgs: platformErr.Platform,
AllVersions: false}); platform != nil {
msg += tr("Try running %s", fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform))
} else {
msg += tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)
Expand Down

0 comments on commit 11b4de8

Please sign in to comment.