Skip to content

Commit 8375a6d

Browse files
authored
[skip-changelog] Some small refactorings (#1711)
* Updated relaxed-semver library to v0.9.0 * Fixed errors translations * Make architecture explicit in loadPlatform * Extracted subrotuine loadToolReleaseFromDirectory * Added cores.InstallPlatformInDirectory(..) helper function * legacy: allow using a preconfigured library manager in build
1 parent 4a626a0 commit 8375a6d

File tree

11 files changed

+84
-67
lines changed

11 files changed

+84
-67
lines changed

.licenses/go/go.bug.st/relaxed-semver.dep.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: go.bug.st/relaxed-semver
3-
version: v0.0.0-20190922224835-391e10178d18
3+
version: v0.9.0
44
type: go
55
summary:
66
homepage: https://pkg.go.dev/go.bug.st/relaxed-semver
@@ -9,7 +9,7 @@ licenses:
99
- sources: LICENSE
1010
text: |2+
1111
12-
Copyright (c) 2018, Cristian Maglie.
12+
Copyright (c) 2018-2022, Cristian Maglie.
1313
All rights reserved.
1414
1515
Redistribution and use in source and binary forms, with or without

arduino/cores/packagemanager/install_uninstall.go

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/arduino/arduino-cli/arduino/cores"
2424
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
2525
"github.com/arduino/arduino-cli/executils"
26+
"github.com/arduino/go-paths-helper"
2627
"github.com/pkg/errors"
2728
)
2829

@@ -33,6 +34,11 @@ func (pm *PackageManager) InstallPlatform(platformRelease *cores.PlatformRelease
3334
"hardware",
3435
platformRelease.Platform.Architecture,
3536
platformRelease.Version.String())
37+
return pm.InstallPlatformInDirectory(platformRelease, destDir)
38+
}
39+
40+
// InstallPlatformInDirectory installs a specific release of a platform in a specific directory.
41+
func (pm *PackageManager) InstallPlatformInDirectory(platformRelease *cores.PlatformRelease, destDir *paths.Path) error {
3642
if err := platformRelease.Resource.Install(pm.DownloadDir, pm.TempDir, destDir); err != nil {
3743
return errors.Errorf(tr("installing platform %[1]s: %[2]s"), platformRelease, err)
3844
}

arduino/cores/packagemanager/loader.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,13 @@ func (pm *PackageManager) loadPlatforms(targetPackage *cores.Package, packageDir
157157
// Filter out directories like .git and similar things
158158
platformsDirs.FilterOutPrefix(".")
159159
for _, platformPath := range platformsDirs {
160+
targetArchitecture := platformPath.Base()
161+
160162
// Tools are not a platform
161-
if platformPath.Base() == "tools" {
163+
if targetArchitecture == "tools" {
162164
continue
163165
}
164-
if err := pm.loadPlatform(targetPackage, platformPath); err != nil {
166+
if err := pm.loadPlatform(targetPackage, targetArchitecture, platformPath); err != nil {
165167
merr = append(merr, err)
166168
}
167169
}
@@ -172,14 +174,12 @@ func (pm *PackageManager) loadPlatforms(targetPackage *cores.Package, packageDir
172174
// loadPlatform loads a single platform and all its installed releases given a platformPath.
173175
// platformPath must be a directory.
174176
// Returns a gRPC Status error in case of failures.
175-
func (pm *PackageManager) loadPlatform(targetPackage *cores.Package, platformPath *paths.Path) error {
177+
func (pm *PackageManager) loadPlatform(targetPackage *cores.Package, architecture string, platformPath *paths.Path) error {
176178
// This is not a platform
177179
if platformPath.IsNotDir() {
178180
return errors.New(tr("path is not a platform directory: %s", platformPath))
179181
}
180182

181-
architecture := platformPath.Base()
182-
183183
// There are two possible platform directory structures:
184184
// - ARCHITECTURE/boards.txt
185185
// - ARCHITECTURE/VERSION/boards.txt
@@ -618,19 +618,28 @@ func (pm *PackageManager) loadToolReleasesFromTool(tool *cores.Tool, toolPath *p
618618
toolVersions.FilterDirs()
619619
toolVersions.FilterOutHiddenFiles()
620620
for _, versionPath := range toolVersions {
621-
if toolReleasePath, err := versionPath.Abs(); err == nil {
622-
version := semver.ParseRelaxed(versionPath.Base())
623-
release := tool.GetOrCreateRelease(version)
624-
release.InstallDir = toolReleasePath
625-
pm.Log.WithField("tool", release).Infof("Loaded tool")
626-
} else {
621+
version := semver.ParseRelaxed(versionPath.Base())
622+
if err := pm.loadToolReleaseFromDirectory(tool, version, versionPath); err != nil {
627623
return err
628624
}
629625
}
630626

631627
return nil
632628
}
633629

630+
func (pm *PackageManager) loadToolReleaseFromDirectory(tool *cores.Tool, version *semver.RelaxedVersion, toolReleasePath *paths.Path) error {
631+
if absToolReleasePath, err := toolReleasePath.Abs(); err != nil {
632+
return errors.New(tr("error opening %s", absToolReleasePath))
633+
} else if !absToolReleasePath.IsDir() {
634+
return errors.New(tr("%s is not a directory", absToolReleasePath))
635+
} else {
636+
toolRelease := tool.GetOrCreateRelease(version)
637+
toolRelease.InstallDir = absToolReleasePath
638+
pm.Log.WithField("tool", toolRelease).Infof("Loaded tool")
639+
return nil
640+
}
641+
}
642+
634643
// LoadToolsFromBundleDirectories FIXMEDOC
635644
func (pm *PackageManager) LoadToolsFromBundleDirectories(dirs paths.PathList) []error {
636645
var merr []error

arduino/discovery/discovery_client/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
305305
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
306306
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
307307
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
308-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
308+
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
309309
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
310310
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
311311
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=

arduino/resources/install.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,43 @@ import (
3434
func (release *DownloadResource) Install(downloadDir, tempPath, destDir *paths.Path) error {
3535
// Check the integrity of the package
3636
if ok, err := release.TestLocalArchiveIntegrity(downloadDir); err != nil {
37-
return fmt.Errorf(tr("testing local archive integrity: %s"), err)
37+
return fmt.Errorf(tr("testing local archive integrity: %s", err))
3838
} else if !ok {
3939
return fmt.Errorf(tr("checking local archive integrity"))
4040
}
4141

4242
// Create a temporary dir to extract package
4343
if err := tempPath.MkdirAll(); err != nil {
44-
return fmt.Errorf(tr("creating temp dir for extraction: %s"), err)
44+
return fmt.Errorf(tr("creating temp dir for extraction: %s", err))
4545
}
4646
tempDir, err := tempPath.MkTempDir("package-")
4747
if err != nil {
48-
return fmt.Errorf(tr("creating temp dir for extraction: %s"), err)
48+
return fmt.Errorf(tr("creating temp dir for extraction: %s", err))
4949
}
5050
defer tempDir.RemoveAll()
5151

5252
// Obtain the archive path and open it
5353
archivePath, err := release.ArchivePath(downloadDir)
5454
if err != nil {
55-
return fmt.Errorf(tr("getting archive path: %s"), err)
55+
return fmt.Errorf(tr("getting archive path: %s", err))
5656
}
5757
file, err := os.Open(archivePath.String())
5858
if err != nil {
59-
return fmt.Errorf(tr("opening archive file: %s"), err)
59+
return fmt.Errorf(tr("opening archive file: %s", err))
6060
}
6161
defer file.Close()
6262

6363
// Extract into temp directory
6464
ctx, cancel := cleanup.InterruptableContext(context.Background())
6565
defer cancel()
6666
if err := extract.Archive(ctx, file, tempDir.String(), nil); err != nil {
67-
return fmt.Errorf(tr("extracting archive: %s"), err)
67+
return fmt.Errorf(tr("extracting archive: %s", err))
6868
}
6969

7070
// Check package content and find package root dir
7171
root, err := findPackageRoot(tempDir)
7272
if err != nil {
73-
return fmt.Errorf(tr("searching package root dir: %s"), err)
73+
return fmt.Errorf(tr("searching package root dir: %s", err))
7474
}
7575

7676
// Ensure container dir exists
@@ -91,7 +91,7 @@ func (release *DownloadResource) Install(downloadDir, tempPath, destDir *paths.P
9191

9292
// Move/rename the extracted root directory in the destination directory
9393
if err := root.Rename(destDir); err != nil {
94-
return fmt.Errorf(tr("moving extracted archive to destination dir: %s"), err)
94+
return fmt.Errorf(tr("moving extracted archive to destination dir: %s", err))
9595
}
9696

9797
// TODO
@@ -115,7 +115,7 @@ func IsDirEmpty(path *paths.Path) (bool, error) {
115115
func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
116116
files, err := parent.ReadDir()
117117
if err != nil {
118-
return nil, fmt.Errorf(tr("reading package root dir: %s"), err)
118+
return nil, fmt.Errorf(tr("reading package root dir: %s", err))
119119
}
120120
var root *paths.Path
121121
for _, file := range files {
@@ -125,7 +125,7 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
125125
if root == nil {
126126
root = file
127127
} else {
128-
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'"), root, file)
128+
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", root, file))
129129
}
130130
}
131131
if root == nil {

client_example/go.sum

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
288288
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
289289
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
290290
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
291-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
291+
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
292292
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
293293
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
294294
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=

docsgen/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ require (
6666
github.com/xanzy/ssh-agent v0.2.1 // indirect
6767
go.bug.st/cleanup v1.0.0 // indirect
6868
go.bug.st/downloader/v2 v2.1.1 // indirect
69-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 // indirect
69+
go.bug.st/relaxed-semver v0.9.0 // indirect
7070
go.bug.st/serial v1.3.2 // indirect
7171
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
7272
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect

docsgen/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
359359
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
360360
go.bug.st/downloader/v2 v2.1.1 h1:nyqbUizo3E2IxCCm4YFac4FtSqqFpqWP+Aae5GCMuw4=
361361
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
362-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 h1:F1qxtaFuewctYc/SsHRn+Q7Dtwi+yJGPgVq8YLtQz98=
363-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
362+
go.bug.st/relaxed-semver v0.9.0 h1:qt0T8W70VCurvsbxRK25fQwiTOFjkzwC/fDOpyPnchQ=
363+
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
364364
go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
365365
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
366366
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ require (
3838
github.com/stretchr/testify v1.7.0
3939
go.bug.st/cleanup v1.0.0
4040
go.bug.st/downloader/v2 v2.1.1
41-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
41+
go.bug.st/relaxed-semver v0.9.0
4242
go.bug.st/serial v1.3.2
4343
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
4444
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
359359
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
360360
go.bug.st/downloader/v2 v2.1.1 h1:nyqbUizo3E2IxCCm4YFac4FtSqqFpqWP+Aae5GCMuw4=
361361
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
362-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 h1:F1qxtaFuewctYc/SsHRn+Q7Dtwi+yJGPgVq8YLtQz98=
363-
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
362+
go.bug.st/relaxed-semver v0.9.0 h1:qt0T8W70VCurvsbxRK25fQwiTOFjkzwC/fDOpyPnchQ=
363+
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
364364
go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
365365
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
366366
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=

legacy/builder/libraries_loader.go

+38-36
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,53 @@ import (
2626
type LibrariesLoader struct{}
2727

2828
func (s *LibrariesLoader) Run(ctx *types.Context) error {
29-
lm := librariesmanager.NewLibraryManager(nil, nil)
30-
ctx.LibrariesManager = lm
29+
if ctx.LibrariesManager == nil {
30+
lm := librariesmanager.NewLibraryManager(nil, nil)
31+
ctx.LibrariesManager = lm
3132

32-
builtInLibrariesFolders := ctx.BuiltInLibrariesDirs
33-
if err := builtInLibrariesFolders.ToAbs(); err != nil {
34-
return errors.WithStack(err)
35-
}
36-
for _, folder := range builtInLibrariesFolders {
37-
lm.AddLibrariesDir(folder, libraries.IDEBuiltIn)
38-
}
33+
builtInLibrariesFolders := ctx.BuiltInLibrariesDirs
34+
if err := builtInLibrariesFolders.ToAbs(); err != nil {
35+
return errors.WithStack(err)
36+
}
37+
for _, folder := range builtInLibrariesFolders {
38+
lm.AddLibrariesDir(folder, libraries.IDEBuiltIn)
39+
}
3940

40-
actualPlatform := ctx.ActualPlatform
41-
platform := ctx.TargetPlatform
42-
if actualPlatform != platform {
43-
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
44-
}
45-
lm.AddPlatformReleaseLibrariesDir(platform, libraries.PlatformBuiltIn)
41+
actualPlatform := ctx.ActualPlatform
42+
platform := ctx.TargetPlatform
43+
if actualPlatform != platform {
44+
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
45+
}
46+
lm.AddPlatformReleaseLibrariesDir(platform, libraries.PlatformBuiltIn)
4647

47-
librariesFolders := ctx.OtherLibrariesDirs
48-
if err := librariesFolders.ToAbs(); err != nil {
49-
return errors.WithStack(err)
50-
}
51-
for _, folder := range librariesFolders {
52-
lm.AddLibrariesDir(folder, libraries.User)
53-
}
48+
librariesFolders := ctx.OtherLibrariesDirs
49+
if err := librariesFolders.ToAbs(); err != nil {
50+
return errors.WithStack(err)
51+
}
52+
for _, folder := range librariesFolders {
53+
lm.AddLibrariesDir(folder, libraries.User)
54+
}
5455

55-
if errs := lm.RescanLibraries(); len(errs) > 0 {
56-
// With the refactoring of the initialization step of the CLI we changed how
57-
// errors are returned when loading platforms and libraries, that meant returning a list of
58-
// errors instead of a single one to enhance the experience for the user.
59-
// I have no intention right now to start a refactoring of the legacy package too, so
60-
// here's this shitty solution for now.
61-
// When we're gonna refactor the legacy package this will be gone.
62-
return errors.WithStack(errs[0].Err())
63-
}
56+
if errs := lm.RescanLibraries(); len(errs) > 0 {
57+
// With the refactoring of the initialization step of the CLI we changed how
58+
// errors are returned when loading platforms and libraries, that meant returning a list of
59+
// errors instead of a single one to enhance the experience for the user.
60+
// I have no intention right now to start a refactoring of the legacy package too, so
61+
// here's this shitty solution for now.
62+
// When we're gonna refactor the legacy package this will be gone.
63+
return errors.WithStack(errs[0].Err())
64+
}
6465

65-
for _, dir := range ctx.LibraryDirs {
66-
// Libraries specified this way have top priority
67-
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
68-
return err
66+
for _, dir := range ctx.LibraryDirs {
67+
// Libraries specified this way have top priority
68+
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
69+
return err
70+
}
6971
}
7072
}
7173

7274
resolver := librariesresolver.NewCppResolver()
73-
if err := resolver.ScanFromLibrariesManager(lm); err != nil {
75+
if err := resolver.ScanFromLibrariesManager(ctx.LibrariesManager); err != nil {
7476
return errors.WithStack(err)
7577
}
7678
ctx.LibrariesResolver = resolver

0 commit comments

Comments
 (0)