Skip to content

[breaking] Refactor initialization steps #1274

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 22 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
5ad5bdb
[breaking] Split rpc Init and remove Rescan function
silvanocerza May 7, 2021
dbad881
[breaking] Refactored commands package to reflect gRPC changes
silvanocerza Jun 7, 2021
1e8764f
[breaking] Refactored cli package to reflect commands changes
silvanocerza Apr 23, 2021
93655a4
Fix instance creation for CLI commands that update indexes
silvanocerza Apr 23, 2021
9adeb3f
Fix unit tests
silvanocerza Apr 23, 2021
2079c0c
Change update indexes commands to not reload instance after
silvanocerza Jun 7, 2021
ced6b48
Fix installation of builtin tools
silvanocerza Apr 23, 2021
a9141cf
Fix integration tests
silvanocerza Apr 23, 2021
58821fd
Fix code for linting
silvanocerza Apr 23, 2021
61b40d2
Update i18n files
silvanocerza Apr 26, 2021
e0b20df
Update UPGRADING.md with breaking changes
silvanocerza Jun 7, 2021
29aacd4
Update comment with correct information
silvanocerza Apr 27, 2021
f4391fd
Using callback instead of channel+goroutine for Init
cmaglie Apr 27, 2021
0c0f8a5
Enhance platform loading step
silvanocerza Apr 27, 2021
410ecd7
Update client_example to reflect new gRPC changes
silvanocerza Apr 27, 2021
2e44c3b
Enhance Init docstring
silvanocerza Apr 29, 2021
1193930
Enhance builtin tools installation during Init
silvanocerza Apr 29, 2021
68871fc
Fix unit test
silvanocerza May 11, 2021
7b035d1
Fix integration tests
silvanocerza May 11, 2021
30280c2
[skip changelog] Fix after botched rebase
silvanocerza Jun 7, 2021
d6ed084
Fix issue when using Init to rescan installed core on already initial…
silvanocerza Jun 15, 2021
bee0970
Update generated file from .proto
silvanocerza Jun 16, 2021
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
310 changes: 174 additions & 136 deletions arduino/cores/packagemanager/loader.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ func NewPackageManager(indexDir, packagesDir, downloadDir, tempDir *paths.Path)
}
}

// Clear resets the PackageManager to its initial state
func (pm *PackageManager) Clear() {
pm.Packages = cores.NewPackages()
pm.CustomGlobalProperties = properties.NewMap()
}

// FindPlatformReleaseProvidingBoardsWithVidPid FIXMEDOC
func (pm *PackageManager) FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid string) []*cores.PlatformRelease {
res := []*cores.PlatformRelease{}
Expand Down
20 changes: 19 additions & 1 deletion arduino/cores/packagemanager/package_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ func TestFindToolsRequiredForBoard(t *testing.T) {
loadIndex("https://dl.espressif.com/dl/package_esp32_index.json")
loadIndex("http://arduino.esp8266.com/stable/package_esp8266com_index.json")
loadIndex("https://adafruit.github.io/arduino-board-index/package_adafruit_index.json")
require.NoError(t, pm.LoadHardware())
errs := pm.LoadHardware()
require.Len(t, errs, 0)
esp32, err := pm.FindBoardWithFQBN("esp32:esp32:esp32")
require.NoError(t, err)
esptool231 := pm.FindToolDependency(&cores.ToolDependency{
Expand Down Expand Up @@ -319,3 +320,20 @@ func TestIdentifyBoard(t *testing.T) {
require.Equal(t, "[test:avr:e]", fmt.Sprintf("%v", identify("0xAB00", "0xcd00")))
require.Equal(t, "[test:avr:e]", fmt.Sprintf("%v", identify("0xab00", "0xCD00")))
}

func TestPackageManagerClear(t *testing.T) {
// Create a PackageManager and load the harware
packageManager := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)
packageManager.LoadHardwareFromDirectory(customHardware)

// Creates another PackageManager but don't load the hardware
emptyPackageManager := packagemanager.NewPackageManager(customHardware, customHardware, customHardware, customHardware)

// Verifies they're not equal
require.NotEqual(t, &packageManager, &emptyPackageManager)

// Clear the first PackageManager that contains loaded hardware
packageManager.Clear()
// Verifies both PackageManagers are now equal
require.Equal(t, &packageManager, &emptyPackageManager)
}
26 changes: 17 additions & 9 deletions arduino/libraries/librariesmanager/librariesmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/pmylund/sortutil"
"github.com/sirupsen/logrus"
semver "go.bug.st/relaxed-semver"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// LibrariesManager keeps the current status of the libraries in the system
Expand Down Expand Up @@ -160,13 +162,14 @@ func (lm *LibrariesManager) AddPlatformReleaseLibrariesDir(plaftormRelease *core
}

// RescanLibraries reload all installed libraries in the system.
func (lm *LibrariesManager) RescanLibraries() error {
func (lm *LibrariesManager) RescanLibraries() []*status.Status {
statuses := []*status.Status{}
for _, dir := range lm.LibrariesDir {
if err := lm.LoadLibrariesFromDir(dir); err != nil {
return fmt.Errorf("loading libs from %s: %s", dir.Path, err)
if errs := lm.LoadLibrariesFromDir(dir); len(errs) > 0 {
statuses = append(statuses, errs...)
}
}
return nil
return statuses
}

func (lm *LibrariesManager) getUserLibrariesDir() *paths.Path {
Expand All @@ -180,21 +183,25 @@ func (lm *LibrariesManager) getUserLibrariesDir() *paths.Path {

// LoadLibrariesFromDir loads all libraries in the given directory. Returns
// nil if the directory doesn't exists.
func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) error {
func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) []*status.Status {
statuses := []*status.Status{}
subDirs, err := librariesDir.Path.ReadDir()
if os.IsNotExist(err) {
return nil
return statuses
}
if err != nil {
return fmt.Errorf("reading dir %s: %s", librariesDir.Path, err)
s := status.Newf(codes.FailedPrecondition, "reading dir %s: %s", librariesDir.Path, err)
return append(statuses, s)
}
subDirs.FilterDirs()
subDirs.FilterOutHiddenFiles()

for _, subDir := range subDirs {
library, err := libraries.Load(subDir, librariesDir.Location)
if err != nil {
return fmt.Errorf("loading library from %s: %s", subDir, err)
s := status.Newf(codes.Internal, "loading library from %s: %s", subDir, err)
statuses = append(statuses, s)
continue
}
library.ContainerPlatform = librariesDir.PlatformRelease
alternatives, ok := lm.Libraries[library.Name]
Expand All @@ -204,7 +211,8 @@ func (lm *LibrariesManager) LoadLibrariesFromDir(librariesDir *LibrariesDir) err
}
alternatives.Add(library)
}
return nil

return statuses
}

// LoadLibraryFromDir loads one single library from the libRootDir.
Expand Down
8 changes: 2 additions & 6 deletions cli/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ var attachFlags struct {
}

func runAttachCommand(cmd *cobra.Command, args []string) {
instance, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Attach board error: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
instance := instance.CreateAndInit()

var path *paths.Path
if len(args) > 1 {
Expand All @@ -64,7 +60,7 @@ func runAttachCommand(cmd *cobra.Command, args []string) {
path = initSketchPath(path)
}

if _, err = board.Attach(context.Background(), &rpc.BoardAttachRequest{
if _, err := board.Attach(context.Background(), &rpc.BoardAttachRequest{
Instance: instance,
BoardUri: args[0],
SketchPath: path.String(),
Expand Down
6 changes: 1 addition & 5 deletions cli/board/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ func initDetailsCommand() *cobra.Command {
}

func runDetailsCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf(tr("Error getting board details: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()

// remove once `board details <fqbn>` is removed
if fqbn == "" && len(args) > 0 {
Expand Down
13 changes: 2 additions & 11 deletions cli/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ var listFlags struct {
// runListCommand detects and lists the connected arduino boards
func runListCommand(cmd *cobra.Command, args []string) {
if listFlags.watch {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error detecting boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()
watchList(cmd, inst)
os.Exit(0)
}
Expand All @@ -73,12 +69,7 @@ func runListCommand(cmd *cobra.Command, args []string) {
time.Sleep(timeout)
}

inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error detecting boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

inst := instance.CreateAndInit()
ports, err := board.List(inst.GetId())
if err != nil {
feedback.Errorf("Error detecting boards: %v", err)
Expand Down
6 changes: 1 addition & 5 deletions cli/board/listall.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ var showHiddenBoard bool

// runListAllCommand list all installed boards
func runListAllCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error listing boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()

list, err := board.ListAll(context.Background(), &rpc.BoardListAllRequest{
Instance: inst,
Expand Down
6 changes: 1 addition & 5 deletions cli/board/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ var searchFlags struct {
}

func runSearchCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error searching boards: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()

res, err := board.Search(context.Background(), &rpc.BoardSearchRequest{
Instance: inst,
Expand Down
35 changes: 6 additions & 29 deletions cli/burnbootloader/burnbootloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ import (
"github.com/arduino/arduino-cli/cli/instance"
"github.com/arduino/arduino-cli/commands/upload"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
fqbn string
port string
verbose bool
verify bool
importDir string
programmer string
burnBootloader bool
fqbn string
port string
verbose bool
verify bool
programmer string
)

// NewCommand created a new `burn-bootloader` command
Expand All @@ -60,11 +56,7 @@ func NewCommand() *cobra.Command {
}

func run(command *cobra.Command, args []string) {
instance, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error during Upload: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
instance := instance.CreateAndInit()

if _, err := upload.BurnBootloader(context.Background(), &rpc.BurnBootloaderRequest{
Instance: instance,
Expand All @@ -79,18 +71,3 @@ func run(command *cobra.Command, args []string) {
}
os.Exit(0)
}

// initSketchPath returns the current working directory
func initSketchPath(sketchPath *paths.Path) *paths.Path {
if sketchPath != nil {
return sketchPath
}

wd, err := paths.Getwd()
if err != nil {
feedback.Errorf("Couldn't get current working directory: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
logrus.Infof("Reading sketch from dir: %s", wd)
return wd
}
7 changes: 2 additions & 5 deletions cli/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,7 @@ func NewCommand() *cobra.Command {
}

func run(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error creating instance: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()

var path *paths.Path
if len(args) > 0 {
Expand Down Expand Up @@ -183,6 +179,7 @@ func run(cmd *cobra.Command, args []string) {
compileErr := new(bytes.Buffer)
verboseCompile := configuration.Settings.GetString("logging.level") == "debug"
var compileRes *rpc.CompileResponse
var err error
if output.OutputFormat == "json" {
compileRes, err = compile.Compile(context.Background(), compileRequest, compileOut, compileErr, verboseCompile)
} else {
Expand Down
6 changes: 1 addition & 5 deletions cli/core/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ func initDownloadCommand() *cobra.Command {
}

func runDownloadCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error downloading: %v", err)
os.Exit(errorcodes.ErrGeneric)
}
inst := instance.CreateAndInit()

logrus.Info("Executing `arduino core download`")

Expand Down
7 changes: 1 addition & 6 deletions cli/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ func DetectSkipPostInstallValue() bool {
}

func runInstallCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error installing: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

inst := instance.CreateAndInit()
logrus.Info("Executing `arduino core install`")

platformsRefs, err := globals.ParseReferenceArgs(args, true)
Expand Down
7 changes: 1 addition & 6 deletions cli/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ var listFlags struct {
}

func runListCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error listing platforms: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

inst := instance.CreateAndInit()
logrus.Info("Executing `arduino core list`")

platforms, err := core.GetPlatforms(&rpc.PlatformListRequest{
Expand Down
12 changes: 8 additions & 4 deletions cli/core/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func initSearchCommand() *cobra.Command {
const indexUpdateInterval = "24h"

func runSearchCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error searching for platforms: %v", err)
inst, status := instance.Create()
if status != nil {
feedback.Errorf("Error creating instance: %v", status)
os.Exit(errorcodes.ErrGeneric)
}

if indexesNeedUpdating(indexUpdateInterval) {
_, err = commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
Instance: inst,
}, output.ProgressBar())
if err != nil {
Expand All @@ -77,6 +77,10 @@ func runSearchCommand(cmd *cobra.Command, args []string) {
}
}

for _, err := range instance.Init(inst) {
feedback.Errorf("Error initializing instance: %v", err)
}

arguments := strings.ToLower(strings.Join(args, " "))
logrus.Infof("Executing `arduino core search` with args: '%s'", arguments)

Expand Down
7 changes: 1 addition & 6 deletions cli/core/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ func initUninstallCommand() *cobra.Command {
}

func runUninstallCommand(cmd *cobra.Command, args []string) {
inst, err := instance.CreateInstance()
if err != nil {
feedback.Errorf("Error uninstalling: %v", err)
os.Exit(errorcodes.ErrGeneric)
}

inst := instance.CreateAndInit()
logrus.Info("Executing `arduino core uninstall`")

platformsRefs, err := globals.ParseReferenceArgs(args, true)
Expand Down
21 changes: 19 additions & 2 deletions cli/core/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,28 @@ func initUpdateIndexCommand() *cobra.Command {
}

func runUpdateIndexCommand(cmd *cobra.Command, args []string) {
instance := instance.CreateInstanceIgnorePlatformIndexErrors()
logrus.Info("Executing `arduino core update-index`")
// We don't initialize any CoreInstance when updating indexes since we don't need to.
// Also meaningless errors might be returned when calling this command with --additional-urls
// since the CLI would be searching for a corresponding file for the additional urls set
// as argument but none would be obviously found.
inst, status := instance.Create()
if status != nil {
feedback.Errorf("Error creating instance: %v", status)
os.Exit(errorcodes.ErrGeneric)
}

// In case this is the first time the CLI is run we need to update indexes
// to make it work correctly, we must do this explicitly in this command since
// we must use instance.Create instead of instance.CreateAndInit for the
// reason stated above.
if err := instance.FirstUpdate(inst); err != nil {
feedback.Errorf("Error updating indexes: %v", status)
os.Exit(errorcodes.ErrGeneric)
}

_, err := commands.UpdateIndex(context.Background(), &rpc.UpdateIndexRequest{
Instance: instance,
Instance: inst,
}, output.ProgressBar())
if err != nil {
feedback.Errorf("Error updating index: %v", err)
Expand Down
Loading