Skip to content

Commit 4833cf5

Browse files
committed
Create a PackageManager method to obtain the correct PlatforRelease
It may be available a bundle AVR core withing the Arduino IDE, if the user installs a specific version (even an earlier version than the bundled) then the installed core should be used.
1 parent cea0cf1 commit 4833cf5

File tree

14 files changed

+58
-32
lines changed

14 files changed

+58
-32
lines changed

Gopkg.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arduino/cores/cores.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,15 @@ func (platform *Platform) latestReleaseVersion() *semver.Version {
154154
return max
155155
}
156156

157-
// GetInstalled return one of the installed PlatformRelease
158-
// TODO: This is a temporary method to help incremental transition from
159-
// arduino-builder, it will be probably removed in the future
160-
func (platform *Platform) GetInstalled() *PlatformRelease {
157+
// GetAllInstalled returns all installed PlatformRelease
158+
func (platform *Platform) GetAllInstalled() []*PlatformRelease {
159+
res := []*PlatformRelease{}
161160
for _, release := range platform.Releases {
162161
if release.InstallDir != nil {
163-
return release
162+
res = append(res, release)
164163
}
165164
}
166-
return nil
165+
return res
167166
}
168167

169168
func (platform *Platform) String() string {

arduino/cores/packagemanager/install_uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (pm *PackageManager) IsToolRequired(toolRelease *cores.ToolRelease) bool {
103103
// Search in all installed platforms
104104
for _, targetPackage := range pm.packages.Packages {
105105
for _, platform := range targetPackage.Platforms {
106-
if platformRelease := platform.GetInstalled(); platformRelease != nil {
106+
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
107107
if platformRelease.RequiresToolRelease(toolRelease) {
108108
return true
109109
}

arduino/cores/packagemanager/package_manager.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (pm *PackageManager) FindBoardsWithVidPid(vid, pid string) []*cores.Board {
9090
res := []*cores.Board{}
9191
for _, targetPackage := range pm.packages.Packages {
9292
for _, targetPlatform := range targetPackage.Platforms {
93-
if platform := targetPlatform.GetInstalled(); platform != nil {
93+
if platform := pm.GetInstalledPlatformRelease(targetPlatform); platform != nil {
9494
for _, board := range platform.Boards {
9595
if board.HasUsbID(vid, pid) {
9696
res = append(res, board)
@@ -106,7 +106,7 @@ func (pm *PackageManager) FindBoardsWithID(id string) []*cores.Board {
106106
res := []*cores.Board{}
107107
for _, targetPackage := range pm.packages.Packages {
108108
for _, targetPlatform := range targetPackage.Platforms {
109-
if platform := targetPlatform.GetInstalled(); platform != nil {
109+
if platform := pm.GetInstalledPlatformRelease(targetPlatform); platform != nil {
110110
for _, board := range platform.Boards {
111111
if board.BoardID == id {
112112
res = append(res, board)
@@ -159,7 +159,7 @@ func (pm *PackageManager) ResolveFQBN(fqbn *cores.FQBN) (
159159
return targetPackage, nil, nil, nil, nil,
160160
fmt.Errorf("unknown platform %s:%s", targetPackage, fqbn.PlatformArch)
161161
}
162-
platformRelease := platform.GetInstalled()
162+
platformRelease := pm.GetInstalledPlatformRelease(platform)
163163
if platformRelease == nil {
164164
return targetPackage, nil, nil, nil, nil,
165165
fmt.Errorf("platform %s is not installed", platformRelease)
@@ -189,7 +189,8 @@ func (pm *PackageManager) ResolveFQBN(fqbn *cores.FQBN) (
189189
return targetPackage, platformRelease, board, buildProperties, nil,
190190
fmt.Errorf("missing package %s:%s required for build", referredPackage, platform)
191191
}
192-
buildPlatformRelease = buildPackage.Platforms[fqbn.PlatformArch].GetInstalled()
192+
buildPlatform := buildPackage.Platforms[fqbn.PlatformArch]
193+
buildPlatformRelease = pm.GetInstalledPlatformRelease(buildPlatform)
193194
}
194195

195196
// No errors... phew!
@@ -316,6 +317,29 @@ func (tr *ToolReleaseActions) Get() (*cores.ToolRelease, error) {
316317
return tr.release, nil
317318
}
318319

320+
// GetInstalledPlatformRelease returns the PlatformRelease installed (it is choosen)
321+
func (pm *PackageManager) GetInstalledPlatformRelease(platform *cores.Platform) *cores.PlatformRelease {
322+
releases := platform.GetAllInstalled()
323+
if len(releases) == 0 {
324+
return nil
325+
}
326+
best := releases[0]
327+
bestIsManaged := pm.IsManagedPlatformRelease(best)
328+
for _, candidate := range releases[1:] {
329+
candidateIsManaged := pm.IsManagedPlatformRelease(candidate)
330+
if bestIsManaged == candidateIsManaged {
331+
if candidate.Version.GreaterThan(best.Version) {
332+
best = candidate
333+
}
334+
}
335+
if !bestIsManaged && candidateIsManaged {
336+
best = candidate
337+
bestIsManaged = true
338+
}
339+
}
340+
return best
341+
}
342+
319343
func (pm *PackageManager) GetAllInstalledToolsReleases() []*cores.ToolRelease {
320344
tools := []*cores.ToolRelease{}
321345
for _, targetPackage := range pm.packages.Packages {

commands/board/listall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func runListAllCommand(cmd *cobra.Command, args []string) {
4545
list := &output.BoardList{}
4646
for _, targetPackage := range pm.GetPackages().Packages {
4747
for _, platform := range targetPackage.Platforms {
48-
platformRelease := platform.GetInstalled()
48+
platformRelease := pm.GetInstalledPlatformRelease(platform)
4949
if platformRelease == nil {
5050
continue
5151
}

commands/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib
113113
if pm != nil {
114114
for _, targetPackage := range pm.GetPackages().Packages {
115115
for _, platform := range targetPackage.Platforms {
116-
if platformRelease := platform.GetInstalled(); platformRelease != nil {
116+
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
117117
lm.AddPlatformReleaseLibrariesDir(platformRelease, libraries.PlatformBuiltIn)
118118
}
119119
}

commands/compile/compile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func run(cmd *cobra.Command, args []string) {
137137
Package: packageName,
138138
PlatformArchitecture: coreName,
139139
})
140-
if targetPlatform == nil || targetPlatform.GetInstalled() == nil {
140+
if targetPlatform == nil || pm.GetInstalledPlatformRelease(targetPlatform) == nil {
141141
errorMessage := fmt.Sprintf(
142142
"\"%[1]s:%[2]s\" platform is not installed, please install it by running \""+
143143
commands.AppName+" core install %[1]s:%[2]s\".", packageName, coreName)
@@ -146,7 +146,7 @@ func run(cmd *cobra.Command, args []string) {
146146
}
147147

148148
ctx := &types.Context{}
149-
149+
ctx.PackageManager = pm
150150
if parsedFqbn, err := cores.ParseFQBN(fqbn); err != nil {
151151
formatter.PrintError(err, "Error parsing FQBN.")
152152
os.Exit(commands.ErrBadArgument)

commands/core/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func installPlatformRelease(pm *packagemanager.PackageManager, platformRelease *
7878
log := pm.Log.WithField("platform", platformRelease)
7979

8080
platform := platformRelease.Platform
81-
installed := platform.GetInstalled()
81+
installed := pm.GetInstalledPlatformRelease(platform)
8282
if installed == nil {
8383
log.Info("Installing platform")
8484
formatter.Print("Installing " + platformRelease.String() + "...")

commands/core/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
5050
res := output.InstalledPlatformReleases{}
5151
for _, targetPackage := range pm.GetPackages().Packages {
5252
for _, platform := range targetPackage.Platforms {
53-
if platformRelease := platform.GetInstalled(); platformRelease != nil {
53+
if platformRelease := pm.GetInstalledPlatformRelease(platform); platformRelease != nil {
5454
if listFlags.updatableOnly {
5555
if latest := platform.GetLatestRelease(); latest == nil || latest == platformRelease {
5656
continue

commands/core/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func uninstallPlatformByRef(pm *packagemanager.PackageManager, platformRef *pack
5959
formatter.PrintErrorMessage("Platform not found " + platformRef.String())
6060
os.Exit(commands.ErrBadCall)
6161
}
62-
platformRelease := platform.GetInstalled()
62+
platformRelease := pm.GetInstalledPlatformRelease(platform)
6363
if platformRelease == nil {
6464
formatter.PrintErrorMessage("Platform not installed " + platformRef.String())
6565
os.Exit(commands.ErrBadCall)

0 commit comments

Comments
 (0)