Skip to content

Enhance alphabetic sorting and place deprecated platform at the bottom #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Platform struct {
Releases map[string]*PlatformRelease // The Releases of this platform, labeled by version.
Package *Package `json:"-"`
ManuallyInstalled bool // true if the Platform has been installed without the CLI
Deprecated bool // true if the Platform has been deprecated
}

// PlatformReleaseHelp represents the help URL for this Platform release
Expand Down
9 changes: 9 additions & 0 deletions arduino/cores/packageindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type indexPlatformRelease struct {
Name string `json:"name,required"`
Architecture string `json:"architecture"`
Version *semver.Version `json:"version,required"`
Deprecated bool `json:"deprecated"`
Category string `json:"category"`
URL string `json:"url"`
ArchiveFileName string `json:"archiveFileName,required"`
Expand Down Expand Up @@ -167,6 +168,7 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
Name: pr.Platform.Name,
Architecture: pr.Platform.Architecture,
Version: pr.Version,
Deprecated: pr.Platform.Deprecated,
Category: pr.Platform.Category,
URL: pr.Resource.URL,
ArchiveFileName: pr.Resource.ArchiveFileName,
Expand Down Expand Up @@ -205,6 +207,13 @@ func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *core
// FIXME: shall we use the Name and Category of the latest release? or maybe move Name and Category in PlatformRelease?
outPlatform.Name = inPlatformRelease.Name
outPlatform.Category = inPlatformRelease.Category
// If the Platform is installed before deprecation installed.json file does not include "deprecated" field.
// The installed.json is read during loading phase of an installed Platform, if the deprecated field is not found
// the package_index.json field would be overwritten and the deprecation info would be lost.
// This check prevents that behaviour.
if !outPlatform.Deprecated {
outPlatform.Deprecated = inPlatformRelease.Deprecated
}

size, err := inPlatformRelease.Size.Int64()
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
package core

import (
"fmt"
"os"
"sort"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
Expand Down Expand Up @@ -87,11 +87,12 @@ func (ir installedResult) String() string {

t := table.New()
t.SetHeader("ID", "Installed", "Latest", "Name")
sort.Slice(ir.platforms, func(i, j int) bool {
return ir.platforms[i].Id < ir.platforms[j].Id
})
for _, p := range ir.platforms {
t.AddRow(p.Id, p.Installed, p.Latest, p.Name)
name := p.Name
if p.Deprecated {
name = fmt.Sprintf("[DEPRECATED] %s", name)
}
t.AddRow(p.Id, p.Installed, p.Latest, name)
}

return t.Render()
Expand Down
11 changes: 6 additions & 5 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package core

import (
"context"
"fmt"
"os"
"path"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -108,11 +108,12 @@ func (sr searchResults) String() string {
if len(sr.platforms) > 0 {
t := table.New()
t.SetHeader("ID", "Version", "Name")
sort.Slice(sr.platforms, func(i, j int) bool {
return sr.platforms[i].Id < sr.platforms[j].Id
})
for _, item := range sr.platforms {
t.AddRow(item.GetId(), item.GetLatest(), item.GetName())
name := item.GetName()
if item.Deprecated {
name = fmt.Sprintf("[DEPRECATED] %s", name)
}
t.AddRow(item.GetId(), item.GetLatest(), name)
}
return t.Render()
}
Expand Down
1 change: 1 addition & 0 deletions commands/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func PlatformReleaseToRPC(platformRelease *cores.PlatformRelease) *rpc.Platform
Boards: boards,
Latest: platformRelease.Version.String(),
ManuallyInstalled: platformRelease.Platform.ManuallyInstalled,
Deprecated: platformRelease.Platform.Deprecated,
}

return result
Expand Down
13 changes: 12 additions & 1 deletion commands/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package core

import (
"sort"

"github.com/arduino/arduino-cli/commands"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -67,6 +69,15 @@ func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, error) {
}
}
}

// Sort result alphabetically and put deprecated platforms at the bottom
sort.Slice(res, func(i, j int) bool {
return res[i].Name < res[j].Name
})
sort.SliceStable(res, func(i, j int) bool {
if !res[i].Deprecated && res[j].Deprecated {
return true
}
return false
})
return res, nil
}
13 changes: 13 additions & 0 deletions commands/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core
import (
"errors"
"regexp"
"sort"
"strings"

"github.com/arduino/arduino-cli/arduino/cores"
Expand Down Expand Up @@ -113,5 +114,17 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
for i, platformRelease := range res {
out[i] = commands.PlatformReleaseToRPC(platformRelease)
}
// Sort result alphabetically and put deprecated platforms at the bottom
sort.Slice(
out, func(i, j int) bool {
return out[i].Name < out[j].Name
})
sort.SliceStable(
out, func(i, j int) bool {
if !out[i].Deprecated && out[j].Deprecated {
return true
}
return false
})
return &rpc.PlatformSearchResponse{SearchOutput: out}, nil
}
35 changes: 35 additions & 0 deletions commands/core/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,38 @@ func TestPlatformSearch(t *testing.T) {
},
})
}

func TestPlatformSearchSorting(t *testing.T) {
dataDir := paths.TempDir().Join("test", "data_dir")
downloadDir := paths.TempDir().Join("test", "staging")
os.Setenv("ARDUINO_DATA_DIR", dataDir.String())
os.Setenv("ARDUINO_DOWNLOADS_DIR", downloadDir.String())
dataDir.MkdirAll()
downloadDir.MkdirAll()
defer paths.TempDir().Join("test").RemoveAll()
err := paths.New("testdata").Join("package_index.json").CopyTo(dataDir.Join("package_index.json"))
require.Nil(t, err)

configuration.Settings = configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())

inst, err := instance.CreateInstance()
require.Nil(t, err)
require.NotNil(t, inst)

res, err := PlatformSearch(&rpc.PlatformSearchRequest{
Instance: inst,
SearchArgs: "",
AllVersions: false,
})
require.Nil(t, err)
require.NotNil(t, res)

require.Len(t, res.SearchOutput, 3)
require.Equal(t, res.SearchOutput[0].Name, "Arduino AVR Boards")
require.Equal(t, res.SearchOutput[0].Deprecated, false)
require.Equal(t, res.SearchOutput[1].Name, "RK002")
require.Equal(t, res.SearchOutput[1].Deprecated, false)
require.Equal(t, res.SearchOutput[2].Name, "Platform")
require.Equal(t, res.SearchOutput[2].Deprecated, true)

}
34 changes: 33 additions & 1 deletion commands/core/testdata/package_index.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,38 @@
}
],
"tools": []
}
},
{
"name": "Package",
"tools": [],
"email": "test@example.com",
"maintainer": "Arduino",
"help": {
"online": "https://github.com/Arduino/arduino-cli"
},
"websiteURL": "https://github.com/Arduino/arduino-cli",
"platforms": [
{
"category": "Test",
"help": {
"online": "https://github.com/Arduino/arduino-cli"
},
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/core.zip",
"checksum": "SHA-256:6a338cf4d6d501176a2d352c87a8d72ac7488b8c5b82cdf2a4e2cef630391092",
"name": "Platform",
"version": "1.2.3",
"deprecated": true,
"architecture": "x86",
"archiveFileName": "core.zip",
"size": "486",
"toolsDependencies": [],
"boards": [
{
"name": "MyBoard"
}
]
}
]
}
]
}
9 changes: 2 additions & 7 deletions rpc/cc/arduino/cli/commands/v1/board.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions rpc/cc/arduino/cli/commands/v1/commands.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions rpc/cc/arduino/cli/commands/v1/common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/cc/arduino/cli/commands/v1/common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ message Platform {
// If true this Platform has been installed manually in the user' sketchbook
// hardware folder
bool manually_installed = 9;
// If true this Platform has been deprecated
bool deprecated = 10;
}

message Board {
Expand Down
9 changes: 2 additions & 7 deletions rpc/cc/arduino/cli/commands/v1/compile.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading