Skip to content

Commit 6a201aa

Browse files
authored
Enhance alphabetic sorting and place deprecated platform at the bottom (#1278)
* updated tests * add first draft of sorting * finished/fixed implementation of tests * implementation of sorting function is now correct * removed sorting from the cli (previously added in commands) and add [DEPRECATED] to the name * fixed deprecation notice not being printed if installed.json was already present * fix formatting
1 parent 15e81ed commit 6a201aa

File tree

22 files changed

+265
-84
lines changed

22 files changed

+265
-84
lines changed

Diff for: arduino/cores/cores.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Platform struct {
3434
Releases map[string]*PlatformRelease // The Releases of this platform, labeled by version.
3535
Package *Package `json:"-"`
3636
ManuallyInstalled bool // true if the Platform has been installed without the CLI
37+
Deprecated bool // true if the Platform has been deprecated
3738
}
3839

3940
// PlatformReleaseHelp represents the help URL for this Platform release

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

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type indexPlatformRelease struct {
5050
Name string `json:"name,required"`
5151
Architecture string `json:"architecture"`
5252
Version *semver.Version `json:"version,required"`
53+
Deprecated bool `json:"deprecated"`
5354
Category string `json:"category"`
5455
URL string `json:"url"`
5556
ArchiveFileName string `json:"archiveFileName,required"`
@@ -167,6 +168,7 @@ func IndexFromPlatformRelease(pr *cores.PlatformRelease) Index {
167168
Name: pr.Platform.Name,
168169
Architecture: pr.Platform.Architecture,
169170
Version: pr.Version,
171+
Deprecated: pr.Platform.Deprecated,
170172
Category: pr.Platform.Category,
171173
URL: pr.Resource.URL,
172174
ArchiveFileName: pr.Resource.ArchiveFileName,
@@ -205,6 +207,13 @@ func (inPlatformRelease indexPlatformRelease) extractPlatformIn(outPackage *core
205207
// FIXME: shall we use the Name and Category of the latest release? or maybe move Name and Category in PlatformRelease?
206208
outPlatform.Name = inPlatformRelease.Name
207209
outPlatform.Category = inPlatformRelease.Category
210+
// If the Platform is installed before deprecation installed.json file does not include "deprecated" field.
211+
// The installed.json is read during loading phase of an installed Platform, if the deprecated field is not found
212+
// the package_index.json field would be overwritten and the deprecation info would be lost.
213+
// This check prevents that behaviour.
214+
if !outPlatform.Deprecated {
215+
outPlatform.Deprecated = inPlatformRelease.Deprecated
216+
}
208217

209218
size, err := inPlatformRelease.Size.Int64()
210219
if err != nil {

Diff for: cli/core/list.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
package core
1717

1818
import (
19+
"fmt"
1920
"os"
20-
"sort"
2121

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

8888
t := table.New()
8989
t.SetHeader("ID", "Installed", "Latest", "Name")
90-
sort.Slice(ir.platforms, func(i, j int) bool {
91-
return ir.platforms[i].Id < ir.platforms[j].Id
92-
})
9390
for _, p := range ir.platforms {
94-
t.AddRow(p.Id, p.Installed, p.Latest, p.Name)
91+
name := p.Name
92+
if p.Deprecated {
93+
name = fmt.Sprintf("[DEPRECATED] %s", name)
94+
}
95+
t.AddRow(p.Id, p.Installed, p.Latest, name)
9596
}
9697

9798
return t.Render()

Diff for: cli/core/search.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ package core
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"os"
2122
"path"
22-
"sort"
2323
"strings"
2424
"time"
2525

@@ -108,11 +108,12 @@ func (sr searchResults) String() string {
108108
if len(sr.platforms) > 0 {
109109
t := table.New()
110110
t.SetHeader("ID", "Version", "Name")
111-
sort.Slice(sr.platforms, func(i, j int) bool {
112-
return sr.platforms[i].Id < sr.platforms[j].Id
113-
})
114111
for _, item := range sr.platforms {
115-
t.AddRow(item.GetId(), item.GetLatest(), item.GetName())
112+
name := item.GetName()
113+
if item.Deprecated {
114+
name = fmt.Sprintf("[DEPRECATED] %s", name)
115+
}
116+
t.AddRow(item.GetId(), item.GetLatest(), name)
116117
}
117118
return t.Render()
118119
}

Diff for: commands/core.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func PlatformReleaseToRPC(platformRelease *cores.PlatformRelease) *rpc.Platform
5959
Boards: boards,
6060
Latest: platformRelease.Version.String(),
6161
ManuallyInstalled: platformRelease.Platform.ManuallyInstalled,
62+
Deprecated: platformRelease.Platform.Deprecated,
6263
}
6364

6465
return result

Diff for: commands/core/list.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package core
1717

1818
import (
19+
"sort"
20+
1921
"github.com/arduino/arduino-cli/commands"
2022
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2123
"github.com/pkg/errors"
@@ -67,6 +69,15 @@ func GetPlatforms(req *rpc.PlatformListRequest) ([]*rpc.Platform, error) {
6769
}
6870
}
6971
}
70-
72+
// Sort result alphabetically and put deprecated platforms at the bottom
73+
sort.Slice(res, func(i, j int) bool {
74+
return res[i].Name < res[j].Name
75+
})
76+
sort.SliceStable(res, func(i, j int) bool {
77+
if !res[i].Deprecated && res[j].Deprecated {
78+
return true
79+
}
80+
return false
81+
})
7182
return res, nil
7283
}

Diff for: commands/core/search.go

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package core
1818
import (
1919
"errors"
2020
"regexp"
21+
"sort"
2122
"strings"
2223

2324
"github.com/arduino/arduino-cli/arduino/cores"
@@ -113,5 +114,17 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
113114
for i, platformRelease := range res {
114115
out[i] = commands.PlatformReleaseToRPC(platformRelease)
115116
}
117+
// Sort result alphabetically and put deprecated platforms at the bottom
118+
sort.Slice(
119+
out, func(i, j int) bool {
120+
return out[i].Name < out[j].Name
121+
})
122+
sort.SliceStable(
123+
out, func(i, j int) bool {
124+
if !out[i].Deprecated && out[j].Deprecated {
125+
return true
126+
}
127+
return false
128+
})
116129
return &rpc.PlatformSearchResponse{SearchOutput: out}, nil
117130
}

Diff for: commands/core/search_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,38 @@ func TestPlatformSearch(t *testing.T) {
277277
},
278278
})
279279
}
280+
281+
func TestPlatformSearchSorting(t *testing.T) {
282+
dataDir := paths.TempDir().Join("test", "data_dir")
283+
downloadDir := paths.TempDir().Join("test", "staging")
284+
os.Setenv("ARDUINO_DATA_DIR", dataDir.String())
285+
os.Setenv("ARDUINO_DOWNLOADS_DIR", downloadDir.String())
286+
dataDir.MkdirAll()
287+
downloadDir.MkdirAll()
288+
defer paths.TempDir().Join("test").RemoveAll()
289+
err := paths.New("testdata").Join("package_index.json").CopyTo(dataDir.Join("package_index.json"))
290+
require.Nil(t, err)
291+
292+
configuration.Settings = configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())
293+
294+
inst, err := instance.CreateInstance()
295+
require.Nil(t, err)
296+
require.NotNil(t, inst)
297+
298+
res, err := PlatformSearch(&rpc.PlatformSearchRequest{
299+
Instance: inst,
300+
SearchArgs: "",
301+
AllVersions: false,
302+
})
303+
require.Nil(t, err)
304+
require.NotNil(t, res)
305+
306+
require.Len(t, res.SearchOutput, 3)
307+
require.Equal(t, res.SearchOutput[0].Name, "Arduino AVR Boards")
308+
require.Equal(t, res.SearchOutput[0].Deprecated, false)
309+
require.Equal(t, res.SearchOutput[1].Name, "RK002")
310+
require.Equal(t, res.SearchOutput[1].Deprecated, false)
311+
require.Equal(t, res.SearchOutput[2].Name, "Platform")
312+
require.Equal(t, res.SearchOutput[2].Deprecated, true)
313+
314+
}

Diff for: commands/core/testdata/package_index.json

+33-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@
130130
}
131131
],
132132
"tools": []
133-
}
133+
},
134+
{
135+
"name": "Package",
136+
"tools": [],
137+
"email": "[email protected]",
138+
"maintainer": "Arduino",
139+
"help": {
140+
"online": "https://github.com/Arduino/arduino-cli"
141+
},
142+
"websiteURL": "https://github.com/Arduino/arduino-cli",
143+
"platforms": [
144+
{
145+
"category": "Test",
146+
"help": {
147+
"online": "https://github.com/Arduino/arduino-cli"
148+
},
149+
"url": "https://raw.githubusercontent.com/arduino/arduino-cli/master/test/testdata/core.zip",
150+
"checksum": "SHA-256:6a338cf4d6d501176a2d352c87a8d72ac7488b8c5b82cdf2a4e2cef630391092",
151+
"name": "Platform",
152+
"version": "1.2.3",
153+
"deprecated": true,
154+
"architecture": "x86",
155+
"archiveFileName": "core.zip",
156+
"size": "486",
157+
"toolsDependencies": [],
158+
"boards": [
159+
{
160+
"name": "MyBoard"
161+
}
162+
]
163+
}
164+
]
165+
}
134166
]
135167
}

Diff for: rpc/cc/arduino/cli/commands/v1/board.pb.go

+2-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/cc/arduino/cli/commands/v1/commands.pb.go

+2-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/cc/arduino/cli/commands/v1/common.pb.go

+15-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: rpc/cc/arduino/cli/commands/v1/common.proto

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ message Platform {
7676
// If true this Platform has been installed manually in the user' sketchbook
7777
// hardware folder
7878
bool manually_installed = 9;
79+
// If true this Platform has been deprecated
80+
bool deprecated = 10;
7981
}
8082

8183
message Board {

Diff for: rpc/cc/arduino/cli/commands/v1/compile.pb.go

+2-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)