Skip to content

Commit d9d07e4

Browse files
author
Luca Bianconi
committed
Merge branch 'master' into feat/purge-build-cache
2 parents 7bf832d + 2b8d6d7 commit d9d07e4

File tree

32 files changed

+329
-104
lines changed

32 files changed

+329
-104
lines changed

Diff for: arduino/builder/sketch_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package builder_test
1717

1818
import (
1919
"fmt"
20-
"io/ioutil"
2120
"os"
2221
"path/filepath"
2322
"runtime"
@@ -31,7 +30,7 @@ import (
3130
)
3231

3332
func tmpDirOrDie() *paths.Path {
34-
dir, err := ioutil.TempDir(os.TempDir(), "builder_test")
33+
dir, err := os.MkdirTemp(os.TempDir(), "builder_test")
3534
if err != nil {
3635
panic(fmt.Sprintf("error creating tmp dir: %v", err))
3736
}
@@ -44,7 +43,7 @@ func TestSaveSketch(t *testing.T) {
4443
sketchFile := filepath.Join("testdata", sketchName)
4544
tmp := tmpDirOrDie()
4645
defer tmp.RemoveAll()
47-
source, err := ioutil.ReadFile(sketchFile)
46+
source, err := os.ReadFile(sketchFile)
4847
if err != nil {
4948
t.Fatalf("unable to read golden file %s: %v", sketchFile, err)
5049
}

Diff for: arduino/cores/packagemanager/package_manager.go

+54-21
Original file line numberDiff line numberDiff line change
@@ -614,29 +614,58 @@ func (pme *Explorer) GetTool(toolID string) *cores.Tool {
614614
}
615615
}
616616

617-
// FindToolsRequiredForBoard FIXMEDOC
618-
func (pme *Explorer) FindToolsRequiredForBoard(board *cores.Board) ([]*cores.ToolRelease, error) {
619-
pme.log.Infof("Searching tools required for board %s", board)
620-
621-
// core := board.Properties["build.core"]
622-
platform := board.PlatformRelease
623-
624-
// maps "PACKAGER:TOOL" => ToolRelease
625-
foundTools := map[string]*cores.ToolRelease{}
626-
627-
// a Platform may not specify required tools (because it's a platform that comes from a
628-
// user/hardware dir without a package_index.json) then add all available tools
629-
for _, targetPackage := range pme.packages {
630-
for _, tool := range targetPackage.Tools {
631-
rel := tool.GetLatestInstalled()
632-
if rel != nil {
633-
foundTools[rel.Tool.Name] = rel
617+
// FindToolsRequiredForBuild returns the list of ToolReleases needed to build for the specified
618+
// plaftorm. The buildPlatform may be different depending on the selected board.
619+
func (pme *Explorer) FindToolsRequiredForBuild(platform, buildPlatform *cores.PlatformRelease) ([]*cores.ToolRelease, error) {
620+
621+
// maps tool name => all available ToolRelease
622+
allToolsAlternatives := map[string][]*cores.ToolRelease{}
623+
for _, tool := range pme.GetAllInstalledToolsReleases() {
624+
alternatives := allToolsAlternatives[tool.Tool.Name]
625+
alternatives = append(alternatives, tool)
626+
allToolsAlternatives[tool.Tool.Name] = alternatives
627+
}
628+
629+
// selectBest select the tool with best matching, applying the following rules
630+
// in order of priority:
631+
// - the tool comes from the requested packager
632+
// - the tool comes from the build platform packager
633+
// - the tool has the greatest version
634+
// - the tool packager comes first in alphabetic order
635+
packagerPriority := map[string]int{}
636+
packagerPriority[platform.Platform.Package.Name] = 2
637+
if buildPlatform != nil {
638+
packagerPriority[buildPlatform.Platform.Package.Name] = 1
639+
}
640+
selectBest := func(tools []*cores.ToolRelease) *cores.ToolRelease {
641+
selected := tools[0]
642+
for _, tool := range tools[1:] {
643+
if packagerPriority[tool.Tool.Package.Name] != packagerPriority[selected.Tool.Package.Name] {
644+
if packagerPriority[tool.Tool.Package.Name] > packagerPriority[selected.Tool.Package.Name] {
645+
selected = tool
646+
}
647+
continue
648+
}
649+
if !tool.Version.Equal(selected.Version) {
650+
if tool.Version.GreaterThan(selected.Version) {
651+
selected = tool
652+
}
653+
continue
654+
}
655+
if tool.Tool.Package.Name < selected.Tool.Package.Name {
656+
selected = tool
634657
}
635658
}
659+
return selected
636660
}
637661

638-
// replace the default tools above with the specific required by the current platform
662+
// First select the specific tools required by the current platform
639663
requiredTools := []*cores.ToolRelease{}
664+
// The Sorting of the tool dependencies is required because some platforms may depends
665+
// on more than one version of the same tool. For example adafruit:samd has both
666+
// [email protected] and [email protected]. To allow the runtime property
667+
// {runtime.tools.bossac.path} to be correctly set to the 1.8.0 version we must ensure
668+
// that the returned array is sorted by version.
640669
platform.ToolDependencies.Sort()
641670
for _, toolDep := range platform.ToolDependencies {
642671
pme.log.WithField("tool", toolDep).Infof("Required tool")
@@ -645,11 +674,15 @@ func (pme *Explorer) FindToolsRequiredForBoard(board *cores.Board) ([]*cores.Too
645674
return nil, fmt.Errorf(tr("tool release not found: %s"), toolDep)
646675
}
647676
requiredTools = append(requiredTools, tool)
648-
delete(foundTools, tool.Tool.Name)
677+
delete(allToolsAlternatives, tool.Tool.Name)
649678
}
650679

651-
for _, toolRel := range foundTools {
652-
requiredTools = append(requiredTools, toolRel)
680+
// Since a Platform may not specify the required tools (because it's a platform that comes
681+
// from a user/hardware dir without a package_index.json) then add all available tools giving
682+
// priority to tools coming from the same packager or referenced packager
683+
for _, tools := range allToolsAlternatives {
684+
tool := selectBest(tools)
685+
requiredTools = append(requiredTools, tool)
653686
}
654687
return requiredTools, nil
655688
}

Diff for: arduino/cores/packagemanager/package_manager_test.go

+43-2
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
287287
loadIndex("https://dl.espressif.com/dl/package_esp32_index.json")
288288
loadIndex("http://arduino.esp8266.com/stable/package_esp8266com_index.json")
289289
loadIndex("https://adafruit.github.io/arduino-board-index/package_adafruit_index.json")
290+
loadIndex("https://test.com/package_test_index.json") // this is not downloaded, it just picks the "local cached" file package_test_index.json
291+
290292
// We ignore the errors returned since they might not be necessarily blocking
291293
// but just warnings for the user, like in the case a board is not loaded
292294
// because of malformed menus
@@ -310,8 +312,13 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
310312
})
311313
require.NotNil(t, esptool0413)
312314

315+
testPlatform := pme.FindPlatformRelease(&packagemanager.PlatformReference{
316+
Package: "test",
317+
PlatformArchitecture: "avr",
318+
PlatformVersion: semver.MustParse("1.1.0")})
319+
313320
testConflictingToolsInDifferentPackages := func() {
314-
tools, err := pme.FindToolsRequiredForBoard(esp32)
321+
tools, err := pme.FindToolsRequiredForBuild(esp32.PlatformRelease, nil)
315322
require.NoError(t, err)
316323
require.Contains(t, tools, esptool231)
317324
require.NotContains(t, tools, esptool0413)
@@ -331,10 +338,44 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
331338
testConflictingToolsInDifferentPackages()
332339
testConflictingToolsInDifferentPackages()
333340

341+
{
342+
// Test buildPlatform dependencies
343+
arduinoBossac180 := pme.FindToolDependency(&cores.ToolDependency{
344+
ToolPackager: "arduino",
345+
ToolName: "bossac",
346+
ToolVersion: semver.ParseRelaxed("1.8.0-48-gb176eee"),
347+
})
348+
require.NotNil(t, arduinoBossac180)
349+
testBossac175 := pme.FindToolDependency(&cores.ToolDependency{
350+
ToolPackager: "test",
351+
ToolName: "bossac",
352+
ToolVersion: semver.ParseRelaxed("1.7.5"),
353+
})
354+
require.NotNil(t, testBossac175)
355+
356+
tools, err := pme.FindToolsRequiredForBuild(esp32.PlatformRelease, nil)
357+
require.NoError(t, err)
358+
require.Contains(t, tools, esptool231)
359+
require.NotContains(t, tools, esptool0413)
360+
// When building without testPlatform dependency, arduino:bossac should be selected
361+
// since it has the higher version
362+
require.NotContains(t, tools, testBossac175)
363+
require.Contains(t, tools, arduinoBossac180)
364+
365+
tools, err = pme.FindToolsRequiredForBuild(esp32.PlatformRelease, testPlatform)
366+
require.NoError(t, err)
367+
require.Contains(t, tools, esptool231)
368+
require.NotContains(t, tools, esptool0413)
369+
// When building with testPlatform dependency, test:bossac should be selected
370+
// because it has dependency priority
371+
require.Contains(t, tools, testBossac175)
372+
require.NotContains(t, tools, arduinoBossac180)
373+
}
374+
334375
feather, err := pme.FindBoardWithFQBN("adafruit:samd:adafruit_feather_m0_express")
335376
require.NoError(t, err)
336377
require.NotNil(t, feather)
337-
featherTools, err := pme.FindToolsRequiredForBoard(feather)
378+
featherTools, err := pme.FindToolsRequiredForBuild(feather.PlatformRelease, nil)
338379
require.NoError(t, err)
339380
require.NotNil(t, featherTools)
340381

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"packages": [
3+
{
4+
"name": "test",
5+
"websiteURL": "https://test.com",
6+
"email": "[email protected]",
7+
"help": {
8+
"online": "https://test.com"
9+
},
10+
"maintainer": "Test",
11+
"platforms": [
12+
{
13+
"architecture": "avr",
14+
"boards": [],
15+
"name": "Test AVR Boards",
16+
"category": "Test",
17+
"version": "1.1.0",
18+
"archiveFileName": "test-1.1.0.tar.bz2",
19+
"checksum": "SHA-256:4e72d4267d9a8d86874edcd021dc661854a5136c0eed947a6fe10366bc51e373",
20+
"help": {
21+
"online": "https://test.com"
22+
},
23+
"url": "https://test.com/test-1.1.0.tar.bz2",
24+
"toolsDependencies": [],
25+
"size": "12345"
26+
}
27+
],
28+
"tools": [
29+
{
30+
"name": "bossac",
31+
"version": "1.7.5",
32+
"systems": [
33+
{
34+
"host": "i386-apple-darwin11",
35+
"checksum": "MD5:603bcce8e59683ac27054b3197a53254",
36+
"size": "96372129",
37+
"archiveFileName": "bossac.tar.bz2",
38+
"url": "https://test.com/bossac.tar.bz2"
39+
}
40+
]
41+
}
42+
]
43+
}
44+
]
45+
}

Diff for: arduino/cores/packagemanager/testdata/data_dir_1/packages/test/hardware/avr/1.1.0/boards.txt

Whitespace-only changes.

Diff for: arduino/cores/packagemanager/testdata/data_dir_1/packages/test/tools/bossac/1.7.5/.keep

Whitespace-only changes.

Diff for: arduino/httpclient/httpclient_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package httpclient
1717

1818
import (
1919
"fmt"
20-
"io/ioutil"
20+
"io"
2121
"net/http"
2222
"net/http/httptest"
2323
"net/url"
@@ -42,7 +42,7 @@ func TestUserAgentHeader(t *testing.T) {
4242
response, err := client.Do(request)
4343
require.NoError(t, err)
4444

45-
b, err := ioutil.ReadAll(response.Body)
45+
b, err := io.ReadAll(response.Body)
4646
require.NoError(t, err)
4747

4848
require.Equal(t, "test-user-agent", string(b))

Diff for: arduino/resources/checksums.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"fmt"
2525
"hash"
2626
"io"
27-
"io/ioutil"
2827
"os"
2928
"path/filepath"
3029
"strings"
@@ -154,7 +153,7 @@ func computeDirChecksum(root string) (string, error) {
154153

155154
// CheckDirChecksum reads checksum from the package.json and compares it with a recomputed value.
156155
func CheckDirChecksum(root string) (bool, error) {
157-
packageJSON, err := ioutil.ReadFile(filepath.Join(root, packageFileName))
156+
packageJSON, err := os.ReadFile(filepath.Join(root, packageFileName))
158157
if err != nil {
159158
return false, err
160159
}

Diff for: arduino/resources/helpers_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
package resources
1717

1818
import (
19-
"io/ioutil"
2019
"net/http"
2120
"net/http/httptest"
21+
"os"
2222
"strings"
2323
"testing"
2424

@@ -67,7 +67,7 @@ func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
6767
// User-Agent: arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019-06-12 11:11:11.111
6868
// Accept-Encoding: gzip
6969

70-
b, err := ioutil.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name
70+
b, err := os.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name
7171
require.NoError(t, err)
7272

7373
requestLines := strings.Split(string(b), "\r\n")

Diff for: client_example/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23-
"io/ioutil"
2423
"log"
2524
"os"
2625
"path"
@@ -53,7 +52,7 @@ func main() {
5352
// To avoid polluting an existing arduino-cli installation, the example
5453
// client uses a temp folder to keep cores, libraries and the like.
5554
// You can point `dataDir` to a location that better fits your needs.
56-
dataDir, err = ioutil.TempDir("", "arduino-rpc-client")
55+
dataDir, err = os.MkdirTemp("", "arduino-rpc-client")
5756
if err != nil {
5857
log.Fatal(err)
5958
}

Diff for: commands/daemon/daemon.go

+7-28
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,7 @@ func (s *ArduinoCoreServerImpl) UpdateLibrariesIndex(req *rpc.UpdateLibrariesInd
159159
err := commands.UpdateLibrariesIndex(stream.Context(), req,
160160
func(p *rpc.DownloadProgress) { stream.Send(&rpc.UpdateLibrariesIndexResponse{DownloadProgress: p}) },
161161
)
162-
if err != nil {
163-
return convertErrorToRPCStatus(err)
164-
}
165-
return stream.Send(&rpc.UpdateLibrariesIndexResponse{})
162+
return convertErrorToRPCStatus(err)
166163
}
167164

168165
// Create FIXMEDOC
@@ -357,10 +354,7 @@ func (s *ArduinoCoreServerImpl) LibraryInstall(req *rpc.LibraryInstallRequest, s
357354
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryInstallResponse{Progress: p}) },
358355
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryInstallResponse{TaskProgress: p}) },
359356
)
360-
if err != nil {
361-
return convertErrorToRPCStatus(err)
362-
}
363-
return stream.Send(&rpc.LibraryInstallResponse{})
357+
return convertErrorToRPCStatus(err)
364358
}
365359

366360
// LibraryUpgrade FIXMEDOC
@@ -370,21 +364,15 @@ func (s *ArduinoCoreServerImpl) LibraryUpgrade(req *rpc.LibraryUpgradeRequest, s
370364
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryUpgradeResponse{Progress: p}) },
371365
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeResponse{TaskProgress: p}) },
372366
)
373-
if err != nil {
374-
return convertErrorToRPCStatus(err)
375-
}
376-
return stream.Send(&rpc.LibraryUpgradeResponse{})
367+
return convertErrorToRPCStatus(err)
377368
}
378369

379370
// LibraryUninstall FIXMEDOC
380371
func (s *ArduinoCoreServerImpl) LibraryUninstall(req *rpc.LibraryUninstallRequest, stream rpc.ArduinoCoreService_LibraryUninstallServer) error {
381372
err := lib.LibraryUninstall(stream.Context(), req,
382373
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUninstallResponse{TaskProgress: p}) },
383374
)
384-
if err != nil {
385-
return convertErrorToRPCStatus(err)
386-
}
387-
return stream.Send(&rpc.LibraryUninstallResponse{})
375+
return convertErrorToRPCStatus(err)
388376
}
389377

390378
// LibraryUpgradeAll FIXMEDOC
@@ -393,10 +381,7 @@ func (s *ArduinoCoreServerImpl) LibraryUpgradeAll(req *rpc.LibraryUpgradeAllRequ
393381
func(p *rpc.DownloadProgress) { stream.Send(&rpc.LibraryUpgradeAllResponse{Progress: p}) },
394382
func(p *rpc.TaskProgress) { stream.Send(&rpc.LibraryUpgradeAllResponse{TaskProgress: p}) },
395383
)
396-
if err != nil {
397-
return convertErrorToRPCStatus(err)
398-
}
399-
return stream.Send(&rpc.LibraryUpgradeAllResponse{})
384+
return convertErrorToRPCStatus(err)
400385
}
401386

402387
// LibraryResolveDependencies FIXMEDOC
@@ -429,10 +414,7 @@ func (s *ArduinoCoreServerImpl) ZipLibraryInstall(req *rpc.ZipLibraryInstallRequ
429414
stream.Context(), req,
430415
func(p *rpc.TaskProgress) { stream.Send(&rpc.ZipLibraryInstallResponse{TaskProgress: p}) },
431416
)
432-
if err != nil {
433-
return convertErrorToRPCStatus(err)
434-
}
435-
return stream.Send(&rpc.ZipLibraryInstallResponse{})
417+
return convertErrorToRPCStatus(err)
436418
}
437419

438420
// GitLibraryInstall FIXMEDOC
@@ -441,10 +423,7 @@ func (s *ArduinoCoreServerImpl) GitLibraryInstall(req *rpc.GitLibraryInstallRequ
441423
stream.Context(), req,
442424
func(p *rpc.TaskProgress) { stream.Send(&rpc.GitLibraryInstallResponse{TaskProgress: p}) },
443425
)
444-
if err != nil {
445-
return convertErrorToRPCStatus(err)
446-
}
447-
return stream.Send(&rpc.GitLibraryInstallResponse{})
426+
return convertErrorToRPCStatus(err)
448427
}
449428

450429
// EnumerateMonitorPortSettings FIXMEDOC

0 commit comments

Comments
 (0)