Skip to content

Commit

Permalink
draft
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Jan 12, 2025
1 parent 5040510 commit 237d69b
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 165 deletions.
2 changes: 1 addition & 1 deletion commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
// Load libraries
for _, pack := range pme.GetPackages() {
for _, platform := range pack.Platforms {
if platformRelease := pme.GetInstalledPlatformRelease(platform); platformRelease != nil {
if platformRelease := pme.GetBestInstalledPlatformRelease(platform); platformRelease != nil {
lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
PlatformRelease: platformRelease,
Path: platformRelease.GetLibrariesDir(),
Expand Down
2 changes: 1 addition & 1 deletion commands/service_board_listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *arduinoCoreServerImpl) BoardListAll(ctx context.Context, req *rpc.Board
list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}}
for _, targetPackage := range toSortedPackageArray(pme.GetPackages()) {
for _, platform := range toSortedPlatformArray(targetPackage.Platforms) {
installedPlatformRelease := pme.GetInstalledPlatformRelease(platform)
installedPlatformRelease := pme.GetBestInstalledPlatformRelease(platform)
// We only want to list boards for installed platforms
if installedPlatformRelease == nil {
continue
Expand Down
2 changes: 1 addition & 1 deletion commands/service_board_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *arduinoCoreServerImpl) BoardSearch(ctx context.Context, req *rpc.BoardS
for _, targetPackage := range pme.GetPackages() {
for _, platform := range targetPackage.Platforms {
latestPlatformRelease := platform.GetLatestCompatibleRelease()
installedPlatformRelease := pme.GetInstalledPlatformRelease(platform)
installedPlatformRelease := pme.GetBestInstalledPlatformRelease(platform)

if latestPlatformRelease == nil && installedPlatformRelease == nil {
continue
Expand Down
3 changes: 3 additions & 0 deletions commands/service_platform_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ func (s *arduinoCoreServerImpl) PlatformInstall(req *rpc.PlatformInstallRequest,
PlatformArchitecture: req.GetArchitecture(),
PlatformVersion: version,
}
fmt.Println(ref)
platformRelease, tools, err := pme.FindPlatformReleaseDependencies(ref)
if err != nil {
return &cmderrors.PlatformNotFoundError{Platform: ref.String(), Cause: err}
}

// Prerequisite checks before install
fmt.Println("A")
if platformRelease.IsInstalled() {
taskCB(&rpc.TaskProgress{Name: i18n.Tr("Platform %s already installed", platformRelease), Completed: true})
return nil
}

fmt.Println("B")
if req.GetNoOverwrite() {
if installed := pme.GetInstalledPlatformRelease(platformRelease.Platform); installed != nil {
return fmt.Errorf("%s: %s",
Expand Down
5 changes: 4 additions & 1 deletion commands/service_platform_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (s *arduinoCoreServerImpl) PlatformSearch(_ context.Context, req *rpc.Platf
res = pme.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
} else {
searchArgs := utils.SearchTermsFromQueryString(req.GetSearchArgs())
for _, targetPackage := range pme.GetPackages() {
for _, targetPackage := range pme.AllPackages() {
for _, platform := range targetPackage.Platforms {
if platform == nil {
continue
Expand Down Expand Up @@ -91,6 +91,9 @@ func (s *arduinoCoreServerImpl) PlatformSearch(_ context.Context, req *rpc.Platf
if latestCompatible := platform.GetLatestCompatibleRelease(); latestCompatible != nil {
rpcPlatformSummary.LatestVersion = latestCompatible.Version.String()
}
if _, has := platform.GetManuallyInstalledRelease(); has {
rpcPlatformSummary.HasManuallyInstalledRelease = true
}
for _, platformRelease := range platform.GetAllReleases() {
rpcPlatformRelease := platformReleaseToRPC(platformRelease)
rpcPlatformSummary.Releases[rpcPlatformRelease.GetVersion()] = rpcPlatformRelease
Expand Down
2 changes: 1 addition & 1 deletion commands/service_platform_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func platformUninstall(_ context.Context, req *rpc.PlatformUninstallRequest, tas
if platform == nil {
return &cmderrors.PlatformNotFoundError{Platform: ref.String()}
}
platformRelease := pme.GetInstalledPlatformRelease(platform)
platformRelease := pme.GetBestInstalledPlatformRelease(platform)
if platformRelease == nil {
return &cmderrors.PlatformNotFoundError{Platform: ref.String()}
}
Expand Down
2 changes: 1 addition & 1 deletion commands/service_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (s *arduinoCoreServerImpl) runProgramAction(ctx context.Context, pme *packa
return nil, &cmderrors.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
}
uploadToolID = split[1]
uploadToolPlatform = pme.GetInstalledPlatformRelease(p)
uploadToolPlatform = pme.GetBestInstalledPlatformRelease(p)
if uploadToolPlatform == nil {
return nil, &cmderrors.PlatformNotFoundError{Platform: split[0] + ":" + boardPlatform.Platform.Architecture}
}
Expand Down
15 changes: 11 additions & 4 deletions internal/arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Platform struct {
Architecture string // The name of the architecture of this package.
Releases map[semver.NormalizedString]*PlatformRelease // The Releases of this platform, labeled by version.
Package *Package `json:"-"`
ManuallyInstalled bool // true if the Platform has been installed without the CLI
ManuallyInstalled bool // true if the Platform exists due to a manually installed release
Deprecated bool // true if the latest PlatformRelease of this Platform has been deprecated
Indexed bool // true if the Platform has been indexed from additional-urls
Latest *semver.Version `json:"-"`
Expand Down Expand Up @@ -238,10 +238,10 @@ func (d *MonitorDependency) String() string {
// GetOrCreateRelease returns the specified release corresponding the provided version,
// or creates a new one if not found.
func (platform *Platform) GetOrCreateRelease(version *semver.Version) *PlatformRelease {
var tag semver.NormalizedString
if version != nil {
tag = version.NormalizedString()
if version == nil {
version = semver.MustParse("")
}
tag := version.NormalizedString()
if release, ok := platform.Releases[tag]; ok {
return release
}
Expand All @@ -257,6 +257,13 @@ func (platform *Platform) GetOrCreateRelease(version *semver.Version) *PlatformR
return release
}

// GetManuallyInstalledRelease returns (*PlatformRelease, true) if the Platform has
// a manually installed release or (nil, false) otherwise.
func (platform *Platform) GetManuallyInstalledRelease() (*PlatformRelease, bool) {
res, ok := platform.Releases[semver.MustParse("").NormalizedString()]
return res, ok
}

// FindReleaseWithVersion returns the specified release corresponding the provided version,
// or nil if not found.
func (platform *Platform) FindReleaseWithVersion(version *semver.Version) *PlatformRelease {
Expand Down
5 changes: 4 additions & 1 deletion internal/arduino/cores/packageindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *core
outPlatform := outPackage.GetOrCreatePlatform(inPlatformRelease.Architecture)
// If the variable `isInstallJSON` is false it means that the index we're reading is coming from the additional-urls.
// Therefore, the `outPlatform.Indexed` will be set at `true`.
outPlatform.Indexed = outPlatform.Indexed || !isInstallJSON
if !isInstallJSON {
outPlatform.Indexed = true
outPlatform.ManuallyInstalled = false
}

// If the latest platform release is deprecated, then deprecate the whole platform.
if outPlatform.Latest == nil || outPlatform.Latest.LessThan(inPlatformRelease.Version) {
Expand Down
4 changes: 3 additions & 1 deletion internal/arduino/cores/packagemanager/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"runtime"

"github.com/arduino/arduino-cli/commands/cmderrors"
Expand All @@ -41,6 +42,7 @@ func (pme *Explorer) DownloadAndInstallPlatformUpgrades(
skipPostInstall bool,
skipPreUninstall bool,
) (*cores.PlatformRelease, error) {
fmt.Println("D")
if platformRef.PlatformVersion != nil {
return nil, &cmderrors.InvalidArgumentError{Message: i18n.Tr("Upgrade doesn't accept parameters with version")}
}
Expand Down Expand Up @@ -439,7 +441,7 @@ func (pme *Explorer) IsToolRequired(toolRelease *cores.ToolRelease) bool {
// Search in all installed platforms
for _, targetPackage := range pme.packages {
for _, platform := range targetPackage.Platforms {
if platformRelease := pme.GetInstalledPlatformRelease(platform); platformRelease != nil {
if platformRelease := pme.GetBestInstalledPlatformRelease(platform); platformRelease != nil {
if platformRelease.RequiresToolRelease(toolRelease) {
return true
}
Expand Down
6 changes: 4 additions & 2 deletions internal/arduino/cores/packagemanager/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,11 @@ func (pm *Builder) loadPlatform(targetPackage *cores.Package, architecture strin
// 2. Inside the sketchbook/hardware/PACKAGER/ARCHITECTURE directory:
// - ARCHITECTURE/boards.txt

version := semver.MustParse("")
platform := targetPackage.GetOrCreatePlatform(architecture)
release := platform.GetOrCreateRelease(version)
if !platform.Indexed {
platform.ManuallyInstalled = true
}
release := platform.GetOrCreateRelease(nil)
if err := pm.loadPlatformRelease(release, platformPath); err != nil {
return fmt.Errorf("%s: %w", i18n.Tr("loading platform release %s", release), err)
}
Expand Down
34 changes: 29 additions & 5 deletions internal/arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package packagemanager

import (
"cmp"
"errors"
"net/url"
"os"
Expand Down Expand Up @@ -229,6 +230,17 @@ func (pme *Explorer) GetPackages() cores.Packages {
return pme.packages
}

func (pme *Explorer) AllPackages() []*cores.Package {
packages := make([]*cores.Package, 0, len(pme.packages))
for _, p := range pme.packages {
packages = append(packages, p)
}
slices.SortFunc(packages, func(i, j *cores.Package) int {
return cmp.Compare(i.String(), j.String())
})
return packages
}

// GetCustomGlobalProperties returns the user defined custom global
// properties for installed platforms.
func (pme *Explorer) GetCustomGlobalProperties() *properties.Map {
Expand Down Expand Up @@ -260,7 +272,7 @@ func (pme *Explorer) FindBoardsWithVidPid(vid, pid string) []*cores.Board {
res := []*cores.Board{}
for _, targetPackage := range pme.packages {
for _, targetPlatform := range targetPackage.Platforms {
if platform := pme.GetInstalledPlatformRelease(targetPlatform); platform != nil {
if platform := pme.GetBestInstalledPlatformRelease(targetPlatform); platform != nil {
for _, board := range platform.Boards {
if board.HasUsbID(vid, pid) {
res = append(res, board)
Expand All @@ -277,7 +289,7 @@ func (pme *Explorer) FindBoardsWithID(id string) []*cores.Board {
res := []*cores.Board{}
for _, targetPackage := range pme.packages {
for _, targetPlatform := range targetPackage.Platforms {
if platform := pme.GetInstalledPlatformRelease(targetPlatform); platform != nil {
if platform := pme.GetBestInstalledPlatformRelease(targetPlatform); platform != nil {
for _, board := range platform.Boards {
if board.BoardID == id {
res = append(res, board)
Expand Down Expand Up @@ -336,7 +348,7 @@ func (pme *Explorer) ResolveFQBN(fqbn *fqbn.FQBN) (
return targetPackage, nil, nil, nil, nil,
errors.New(i18n.Tr("unknown platform %s:%s", targetPackage, fqbn.Architecture))
}
boardPlatformRelease := pme.GetInstalledPlatformRelease(platform)
boardPlatformRelease := pme.GetBestInstalledPlatformRelease(platform)
if boardPlatformRelease == nil {
return targetPackage, nil, nil, nil, nil,
errors.New(i18n.Tr("platform %s is not installed", platform))
Expand Down Expand Up @@ -467,7 +479,7 @@ func (pme *Explorer) determineReferencedPlatformRelease(boardBuildProperties *pr
return "", nil, "", nil,
errors.New(i18n.Tr("missing platform %[1]s:%[2]s referenced by board %[3]s", referredPackageName, fqbn.Architecture, fqbn))
}
referredPlatformRelease = pme.GetInstalledPlatformRelease(referredPlatform)
referredPlatformRelease = pme.GetBestInstalledPlatformRelease(referredPlatform)
if referredPlatformRelease == nil {
return "", nil, "", nil,
errors.New(i18n.Tr("missing platform release %[1]s:%[2]s referenced by board %[3]s", referredPackageName, fqbn.Architecture, fqbn))
Expand Down Expand Up @@ -626,8 +638,20 @@ func (tr *ToolReleaseActions) Get() (*cores.ToolRelease, error) {
return tr.release, nil
}

// GetInstalledPlatformRelease returns the PlatformRelease installed (it is chosen)
// GetInstalledPlatformRelease return the PlatformRelease installed through the package manager
// or nil if the PlatformRelease is not installed. Platforms installed manually are ignored.
func (pme *Explorer) GetInstalledPlatformRelease(platform *cores.Platform) *cores.PlatformRelease {
for _, release := range platform.GetAllInstalled() {
if release.IsInstalled() && pme.IsManagedPlatformRelease(release) {
return release
}
}
return nil
}

// GetBestInstalledPlatformRelease returns the PlatformRelease installed (it is chosen between
// the platform installed through the package manager and the one installed manually)
func (pme *Explorer) GetBestInstalledPlatformRelease(platform *cores.Platform) *cores.PlatformRelease {
releases := platform.GetAllInstalled()
if len(releases) == 0 {
return nil
Expand Down
10 changes: 6 additions & 4 deletions internal/cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func GetList(ctx context.Context, srv rpc.ArduinoCoreServiceServer, inst *rpc.In

result := []*rpc.PlatformSummary{}
for _, platform := range platforms.GetSearchOutput() {
if platform.GetInstalledVersion() == "" && !platform.GetMetadata().GetManuallyInstalled() {
if platform.GetInstalledVersion() == "" && !platform.GetHasManuallyInstalledRelease() {
continue
}
if updatableOnly && platform.GetInstalledVersion() == platform.GetLatestVersion() {
Expand Down Expand Up @@ -117,10 +117,12 @@ func (ir coreListResult) String() string {
t.SetHeader(i18n.Tr("ID"), i18n.Tr("Installed"), i18n.Tr("Latest"), i18n.Tr("Name"))
for _, platform := range ir.Platforms {
latestVersion := platform.LatestVersion.String()
if latestVersion == "" {
latestVersion = "n/a"
if platform.HasManuallyInstalledRelease {
t.AddRow(platform.Id, i18n.Tr("(in sketchbook)"), "", platform.GetPlatformName())
}
if !platform.HasManuallyInstalledRelease || platform.InstalledVersion.String() != "" {
t.AddRow(platform.Id, platform.InstalledVersion, latestVersion, platform.GetPlatformName())
}
t.AddRow(platform.Id, platform.InstalledVersion, latestVersion, platform.GetPlatformName())
}

return t.Render()
Expand Down
26 changes: 14 additions & 12 deletions internal/cli/feedback/result/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ func NewPlatformSummary(in *rpc.PlatformSummary) *PlatformSummary {
releases.SortKeys((*semver.Version).CompareTo)

return &PlatformSummary{
Id: in.GetMetadata().GetId(),
Maintainer: in.GetMetadata().GetMaintainer(),
Website: in.GetMetadata().GetWebsite(),
Email: in.GetMetadata().GetEmail(),
ManuallyInstalled: in.GetMetadata().GetManuallyInstalled(),
Deprecated: in.GetMetadata().GetDeprecated(),
Indexed: in.GetMetadata().GetIndexed(),
Releases: releases,
InstalledVersion: semver.MustParse(in.GetInstalledVersion()),
LatestVersion: semver.MustParse(in.GetLatestVersion()),
Id: in.GetMetadata().GetId(),
Maintainer: in.GetMetadata().GetMaintainer(),
Website: in.GetMetadata().GetWebsite(),
Email: in.GetMetadata().GetEmail(),
ManuallyInstalled: in.GetMetadata().GetManuallyInstalled(),
Deprecated: in.GetMetadata().GetDeprecated(),
Indexed: in.GetMetadata().GetIndexed(),
Releases: releases,
InstalledVersion: semver.MustParse(in.GetInstalledVersion()),
LatestVersion: semver.MustParse(in.GetLatestVersion()),
HasManuallyInstalledRelease: in.GetHasManuallyInstalledRelease(),
}
}

Expand All @@ -65,8 +66,9 @@ type PlatformSummary struct {

Releases orderedmap.Map[*semver.Version, *PlatformRelease] `json:"releases,omitempty"`

InstalledVersion *semver.Version `json:"installed_version,omitempty"`
LatestVersion *semver.Version `json:"latest_version,omitempty"`
InstalledVersion *semver.Version `json:"installed_version,omitempty"`
LatestVersion *semver.Version `json:"latest_version,omitempty"`
HasManuallyInstalledRelease bool `json:"has_manually_installed_release,omitempty"`
}

// GetLatestRelease returns the latest relase of this platform or nil if none available.
Expand Down
Loading

0 comments on commit 237d69b

Please sign in to comment.