Skip to content

Some small refactorings #1711

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 6 commits into from
Apr 15, 2022
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
4 changes: 2 additions & 2 deletions .licenses/go/go.bug.st/relaxed-semver.dep.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: go.bug.st/relaxed-semver
version: v0.0.0-20190922224835-391e10178d18
version: v0.9.0
type: go
summary:
homepage: https://pkg.go.dev/go.bug.st/relaxed-semver
Expand All @@ -9,7 +9,7 @@ licenses:
- sources: LICENSE
text: |2+

Copyright (c) 2018, Cristian Maglie.
Copyright (c) 2018-2022, Cristian Maglie.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
6 changes: 6 additions & 0 deletions arduino/cores/packagemanager/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-cli/executils"
"github.com/arduino/go-paths-helper"
"github.com/pkg/errors"
)

Expand All @@ -33,6 +34,11 @@ func (pm *PackageManager) InstallPlatform(platformRelease *cores.PlatformRelease
"hardware",
platformRelease.Platform.Architecture,
platformRelease.Version.String())
return pm.InstallPlatformInDirectory(platformRelease, destDir)
}

// InstallPlatformInDirectory installs a specific release of a platform in a specific directory.
func (pm *PackageManager) InstallPlatformInDirectory(platformRelease *cores.PlatformRelease, destDir *paths.Path) error {
if err := platformRelease.Resource.Install(pm.DownloadDir, pm.TempDir, destDir); err != nil {
return errors.Errorf(tr("installing platform %[1]s: %[2]s"), platformRelease, err)
}
Expand Down
31 changes: 20 additions & 11 deletions arduino/cores/packagemanager/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,13 @@ func (pm *PackageManager) loadPlatforms(targetPackage *cores.Package, packageDir
// Filter out directories like .git and similar things
platformsDirs.FilterOutPrefix(".")
for _, platformPath := range platformsDirs {
targetArchitecture := platformPath.Base()

// Tools are not a platform
if platformPath.Base() == "tools" {
if targetArchitecture == "tools" {
continue
}
if err := pm.loadPlatform(targetPackage, platformPath); err != nil {
if err := pm.loadPlatform(targetPackage, targetArchitecture, platformPath); err != nil {
merr = append(merr, err)
}
}
Expand All @@ -172,14 +174,12 @@ func (pm *PackageManager) loadPlatforms(targetPackage *cores.Package, packageDir
// loadPlatform loads a single platform and all its installed releases given a platformPath.
// platformPath must be a directory.
// Returns a gRPC Status error in case of failures.
func (pm *PackageManager) loadPlatform(targetPackage *cores.Package, platformPath *paths.Path) error {
func (pm *PackageManager) loadPlatform(targetPackage *cores.Package, architecture string, platformPath *paths.Path) error {
// This is not a platform
if platformPath.IsNotDir() {
return errors.New(tr("path is not a platform directory: %s", platformPath))
}

architecture := platformPath.Base()

// There are two possible platform directory structures:
// - ARCHITECTURE/boards.txt
// - ARCHITECTURE/VERSION/boards.txt
Expand Down Expand Up @@ -618,19 +618,28 @@ func (pm *PackageManager) loadToolReleasesFromTool(tool *cores.Tool, toolPath *p
toolVersions.FilterDirs()
toolVersions.FilterOutHiddenFiles()
for _, versionPath := range toolVersions {
if toolReleasePath, err := versionPath.Abs(); err == nil {
version := semver.ParseRelaxed(versionPath.Base())
release := tool.GetOrCreateRelease(version)
release.InstallDir = toolReleasePath
pm.Log.WithField("tool", release).Infof("Loaded tool")
} else {
version := semver.ParseRelaxed(versionPath.Base())
if err := pm.loadToolReleaseFromDirectory(tool, version, versionPath); err != nil {
return err
}
}

return nil
}

func (pm *PackageManager) loadToolReleaseFromDirectory(tool *cores.Tool, version *semver.RelaxedVersion, toolReleasePath *paths.Path) error {
if absToolReleasePath, err := toolReleasePath.Abs(); err != nil {
return errors.New(tr("error opening %s", absToolReleasePath))
} else if !absToolReleasePath.IsDir() {
return errors.New(tr("%s is not a directory", absToolReleasePath))
} else {
toolRelease := tool.GetOrCreateRelease(version)
toolRelease.InstallDir = absToolReleasePath
pm.Log.WithField("tool", toolRelease).Infof("Loaded tool")
return nil
}
}

// LoadToolsFromBundleDirectories FIXMEDOC
func (pm *PackageManager) LoadToolsFromBundleDirectories(dirs paths.PathList) []error {
var merr []error
Expand Down
2 changes: 1 addition & 1 deletion arduino/discovery/discovery_client/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
Expand Down
20 changes: 10 additions & 10 deletions arduino/resources/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,43 @@ import (
func (release *DownloadResource) Install(downloadDir, tempPath, destDir *paths.Path) error {
// Check the integrity of the package
if ok, err := release.TestLocalArchiveIntegrity(downloadDir); err != nil {
return fmt.Errorf(tr("testing local archive integrity: %s"), err)
return fmt.Errorf(tr("testing local archive integrity: %s", err))
} else if !ok {
return fmt.Errorf(tr("checking local archive integrity"))
}

// Create a temporary dir to extract package
if err := tempPath.MkdirAll(); err != nil {
return fmt.Errorf(tr("creating temp dir for extraction: %s"), err)
return fmt.Errorf(tr("creating temp dir for extraction: %s", err))
}
tempDir, err := tempPath.MkTempDir("package-")
if err != nil {
return fmt.Errorf(tr("creating temp dir for extraction: %s"), err)
return fmt.Errorf(tr("creating temp dir for extraction: %s", err))
}
defer tempDir.RemoveAll()

// Obtain the archive path and open it
archivePath, err := release.ArchivePath(downloadDir)
if err != nil {
return fmt.Errorf(tr("getting archive path: %s"), err)
return fmt.Errorf(tr("getting archive path: %s", err))
}
file, err := os.Open(archivePath.String())
if err != nil {
return fmt.Errorf(tr("opening archive file: %s"), err)
return fmt.Errorf(tr("opening archive file: %s", err))
}
defer file.Close()

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

// Check package content and find package root dir
root, err := findPackageRoot(tempDir)
if err != nil {
return fmt.Errorf(tr("searching package root dir: %s"), err)
return fmt.Errorf(tr("searching package root dir: %s", err))
}

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

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

// TODO
Expand All @@ -115,7 +115,7 @@ func IsDirEmpty(path *paths.Path) (bool, error) {
func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
files, err := parent.ReadDir()
if err != nil {
return nil, fmt.Errorf(tr("reading package root dir: %s"), err)
return nil, fmt.Errorf(tr("reading package root dir: %s", err))
}
var root *paths.Path
for _, file := range files {
Expand All @@ -125,7 +125,7 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
if root == nil {
root = file
} else {
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'"), root, file)
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", root, file))
}
}
if root == nil {
Expand Down
2 changes: 1 addition & 1 deletion client_example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45/go.mod h1:dRSl/CVCTf56CkXgJMDOdSwNfo2g1orOGE/gBGdvjZw=
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
Expand Down
2 changes: 1 addition & 1 deletion docsgen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ require (
github.com/xanzy/ssh-agent v0.2.1 // indirect
go.bug.st/cleanup v1.0.0 // indirect
go.bug.st/downloader/v2 v2.1.1 // indirect
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 // indirect
go.bug.st/relaxed-semver v0.9.0 // indirect
go.bug.st/serial v1.3.2 // indirect
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions docsgen/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
go.bug.st/downloader/v2 v2.1.1 h1:nyqbUizo3E2IxCCm4YFac4FtSqqFpqWP+Aae5GCMuw4=
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 h1:F1qxtaFuewctYc/SsHRn+Q7Dtwi+yJGPgVq8YLtQz98=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
go.bug.st/relaxed-semver v0.9.0 h1:qt0T8W70VCurvsbxRK25fQwiTOFjkzwC/fDOpyPnchQ=
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/stretchr/testify v1.7.0
go.bug.st/cleanup v1.0.0
go.bug.st/downloader/v2 v2.1.1
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18
go.bug.st/relaxed-semver v0.9.0
go.bug.st/serial v1.3.2
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ go.bug.st/cleanup v1.0.0 h1:XVj1HZxkBXeq3gMT7ijWUpHyIC1j8XAoNSyQ06CskgA=
go.bug.st/cleanup v1.0.0/go.mod h1:EqVmTg2IBk4znLbPD28xne3abjsJftMdqqJEjhn70bk=
go.bug.st/downloader/v2 v2.1.1 h1:nyqbUizo3E2IxCCm4YFac4FtSqqFpqWP+Aae5GCMuw4=
go.bug.st/downloader/v2 v2.1.1/go.mod h1:VZW2V1iGKV8rJL2ZEGIDzzBeKowYv34AedJz13RzVII=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18 h1:F1qxtaFuewctYc/SsHRn+Q7Dtwi+yJGPgVq8YLtQz98=
go.bug.st/relaxed-semver v0.0.0-20190922224835-391e10178d18/go.mod h1:Cx1VqMtEhE9pIkEyUj3LVVVPkv89dgW8aCKrRPDR/uE=
go.bug.st/relaxed-semver v0.9.0 h1:qt0T8W70VCurvsbxRK25fQwiTOFjkzwC/fDOpyPnchQ=
go.bug.st/relaxed-semver v0.9.0/go.mod h1:ug0/W/RPYUjliE70Ghxg77RDHmPxqpo7SHV16ijss7Q=
go.bug.st/serial v1.3.2 h1:6BFZZd/wngoL5PPYYTrFUounF54SIkykHpT98eq6zvk=
go.bug.st/serial v1.3.2/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
go.bug.st/serial.v1 v0.0.0-20180827123349-5f7892a7bb45 h1:mACY1anK6HNCZtm/DK2Rf2ZPHggVqeB0+7rY9Gl6wyI=
Expand Down
74 changes: 38 additions & 36 deletions legacy/builder/libraries_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,53 @@ import (
type LibrariesLoader struct{}

func (s *LibrariesLoader) Run(ctx *types.Context) error {
lm := librariesmanager.NewLibraryManager(nil, nil)
ctx.LibrariesManager = lm
if ctx.LibrariesManager == nil {
lm := librariesmanager.NewLibraryManager(nil, nil)
ctx.LibrariesManager = lm

builtInLibrariesFolders := ctx.BuiltInLibrariesDirs
if err := builtInLibrariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
}
for _, folder := range builtInLibrariesFolders {
lm.AddLibrariesDir(folder, libraries.IDEBuiltIn)
}
builtInLibrariesFolders := ctx.BuiltInLibrariesDirs
if err := builtInLibrariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
}
for _, folder := range builtInLibrariesFolders {
lm.AddLibrariesDir(folder, libraries.IDEBuiltIn)
}

actualPlatform := ctx.ActualPlatform
platform := ctx.TargetPlatform
if actualPlatform != platform {
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
}
lm.AddPlatformReleaseLibrariesDir(platform, libraries.PlatformBuiltIn)
actualPlatform := ctx.ActualPlatform
platform := ctx.TargetPlatform
if actualPlatform != platform {
lm.AddPlatformReleaseLibrariesDir(actualPlatform, libraries.ReferencedPlatformBuiltIn)
}
lm.AddPlatformReleaseLibrariesDir(platform, libraries.PlatformBuiltIn)

librariesFolders := ctx.OtherLibrariesDirs
if err := librariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
}
for _, folder := range librariesFolders {
lm.AddLibrariesDir(folder, libraries.User)
}
librariesFolders := ctx.OtherLibrariesDirs
if err := librariesFolders.ToAbs(); err != nil {
return errors.WithStack(err)
}
for _, folder := range librariesFolders {
lm.AddLibrariesDir(folder, libraries.User)
}

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

for _, dir := range ctx.LibraryDirs {
// Libraries specified this way have top priority
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
return err
for _, dir := range ctx.LibraryDirs {
// Libraries specified this way have top priority
if err := lm.LoadLibraryFromDir(dir, libraries.Unmanaged); err != nil {
return err
}
}
}

resolver := librariesresolver.NewCppResolver()
if err := resolver.ScanFromLibrariesManager(lm); err != nil {
if err := resolver.ScanFromLibrariesManager(ctx.LibrariesManager); err != nil {
return errors.WithStack(err)
}
ctx.LibrariesResolver = resolver
Expand Down