Skip to content

Commit

Permalink
DRAFT
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Apr 17, 2024
1 parent cdcec9b commit af4513f
Show file tree
Hide file tree
Showing 70 changed files with 3,594 additions and 1,331 deletions.
20 changes: 10 additions & 10 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
"github.com/arduino/arduino-cli/internal/arduino/resources"
"github.com/arduino/arduino-cli/internal/arduino/sketch"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateReque
}

// Setup downloads directory
downloadsDir := configuration.DownloadsDir(s.settings)
downloadsDir := s.settings.DownloadsDir()
if downloadsDir.NotExist() {
err := downloadsDir.MkdirAll()
if err != nil {
Expand All @@ -81,8 +80,8 @@ func (s *arduinoCoreServerImpl) Create(ctx context.Context, req *rpc.CreateReque
}

// Setup data directory
dataDir := configuration.DataDir(s.settings)
packagesDir := configuration.PackagesDir(s.settings)
dataDir := s.settings.DataDir()
packagesDir := s.settings.PackagesDir()
if packagesDir.NotExist() {
err := packagesDir.MkdirAll()
if err != nil {
Expand Down Expand Up @@ -192,7 +191,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor
}
}

if err := firstUpdate(ctx, s, req.GetInstance(), configuration.DataDir(s.settings), downloadCallback, allPackageIndexUrls); err != nil {
if err := firstUpdate(ctx, s, req.GetInstance(), s.settings.DataDir(), downloadCallback, allPackageIndexUrls); err != nil {
e := &cmderrors.InitFailedError{
Code: codes.InvalidArgument,
Cause: err,
Expand Down Expand Up @@ -245,7 +244,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor

// Load Platforms
if profile == nil {
for _, err := range pmb.LoadHardware(s.settings) {
for _, err := range pmb.LoadHardware() {
s := &cmderrors.PlatformLoadingError{Cause: err}
responseError(s.GRPCStatus())
}
Expand Down Expand Up @@ -349,7 +348,7 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor

if profile == nil {
// Add directories of libraries bundled with IDE
if bundledLibsDir := configuration.IDEBuiltinLibrariesDir(s.settings); bundledLibsDir != nil {
if bundledLibsDir := s.settings.IDEBuiltinLibrariesDir(); bundledLibsDir != nil {
lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
Path: bundledLibsDir,
Location: libraries.IDEBuiltIn,
Expand All @@ -358,14 +357,14 @@ func (s *arduinoCoreServerImpl) Init(req *rpc.InitRequest, stream rpc.ArduinoCor

// Add libraries directory from config file
lmb.AddLibrariesDir(librariesmanager.LibrariesDir{
Path: configuration.LibrariesDir(s.settings),
Path: s.settings.LibrariesDir(),
Location: libraries.User,
})
} else {
// Load libraries required for profile
for _, libraryRef := range profile.Libraries {
uid := libraryRef.InternalUniqueIdentifier()
libRoot := configuration.ProfilesCacheDir(s.settings).Join(uid)
libRoot := s.settings.ProfilesCacheDir().Join(uid)
libDir := libRoot.Join(libraryRef.Library)

if !libDir.IsDir() {
Expand Down Expand Up @@ -548,7 +547,7 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
Message: &rpc.UpdateIndexResponse_DownloadProgress{DownloadProgress: p},
})
}
indexpath := configuration.DataDir(s.settings)
indexpath := s.settings.DataDir()

urls := []string{globals.DefaultIndexURL}
if !req.GetIgnoreCustomPackageIndexes() {
Expand Down Expand Up @@ -614,6 +613,7 @@ func (s *arduinoCoreServerImpl) UpdateIndex(req *rpc.UpdateIndexRequest, stream
downloadCB.Start(u, tr("Downloading index: %s", filepath.Base(URL.Path)))
downloadCB.End(false, tr("Invalid network configuration: %s", err))
failed = true
continue
}

if strings.HasSuffix(URL.Host, "arduino.cc") && strings.HasSuffix(URL.Path, ".json") {
Expand Down
2 changes: 1 addition & 1 deletion commands/internal/instances/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func Create(dataDir, packagesDir, downloadsDir *paths.Path, extraUserAgent strin
}
tempDir := dataDir.Join("tmp")

pm := packagemanager.NewBuilder(dataDir, packagesDir, downloadsDir, tempDir, userAgent, downloaderConfig).Build()
pm := packagemanager.NewBuilder(dataDir, packagesDir, nil, downloadsDir, tempDir, userAgent, downloaderConfig).Build()
lm, _ := librariesmanager.NewBuilder().Build()

instance := &coreInstance{
Expand Down
18 changes: 10 additions & 8 deletions commands/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ import (
"context"

"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/i18n"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/arduino-cli/version"
)

// NewArduinoCoreServer returns an implementation of the ArduinoCoreService gRPC service
// that uses the provided version string.
func NewArduinoCoreServer(version string, settings *configuration.Settings) rpc.ArduinoCoreServiceServer {
return &arduinoCoreServerImpl{
versionString: version,
settings: settings,
}
func NewArduinoCoreServer() rpc.ArduinoCoreServiceServer {
settings := configuration.NewSettings()

// Setup i18n
i18n.Init(settings.Locale())

return &arduinoCoreServerImpl{settings: settings}
}

type arduinoCoreServerImpl struct {
rpc.UnsafeArduinoCoreServiceServer // Force compile error for unimplemented methods

versionString string

// Settings holds configurations of the CLI and the gRPC consumers
settings *configuration.Settings
}

// Version returns the version of the Arduino CLI
func (s *arduinoCoreServerImpl) Version(ctx context.Context, req *rpc.VersionRequest) (*rpc.VersionResponse, error) {
return &rpc.VersionResponse{Version: s.versionString}, nil
return &rpc.VersionResponse{Version: version.VersionInfo.VersionString}, nil
}
24 changes: 16 additions & 8 deletions commands/service_board_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,59 +48,67 @@ func TestGetByVidPid(t *testing.T) {
defer ts.Close()

vidPidURL = ts.URL
res, err := apiByVidPid("0xf420", "0XF069", configuration.Init(""))
settings := configuration.NewSettings()
res, err := apiByVidPid("0xf420", "0XF069", settings)
require.Nil(t, err)
require.Len(t, res, 1)
require.Equal(t, "Arduino/Genuino MKR1000", res[0].GetName())
require.Equal(t, "arduino:samd:mkr1000", res[0].GetFqbn())

// wrong vid (too long), wrong pid (not an hex value)

_, err = apiByVidPid("0xfffff", "0xDEFG", configuration.Init(""))
_, err = apiByVidPid("0xfffff", "0xDEFG", settings)
require.NotNil(t, err)
}

func TestGetByVidPidNotFound(t *testing.T) {
settings := configuration.NewSettings()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
defer ts.Close()

vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069", configuration.Init(""))
res, err := apiByVidPid("0x0420", "0x0069", settings)
require.NoError(t, err)
require.Empty(t, res)
}

func TestGetByVidPid5xx(t *testing.T) {
settings := configuration.NewSettings()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("500 - Ooooops!"))
}))
defer ts.Close()

vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069", configuration.Init(""))
res, err := apiByVidPid("0x0420", "0x0069", settings)
require.NotNil(t, err)
require.Equal(t, "the server responded with status 500 Internal Server Error", err.Error())
require.Len(t, res, 0)
}

func TestGetByVidPidMalformedResponse(t *testing.T) {
settings := configuration.NewSettings()

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "{}")
}))
defer ts.Close()

vidPidURL = ts.URL
res, err := apiByVidPid("0x0420", "0x0069", configuration.Init(""))
res, err := apiByVidPid("0x0420", "0x0069", settings)
require.NotNil(t, err)
require.Equal(t, "wrong format in server response", err.Error())
require.Len(t, res, 0)
}

func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
items, err := identifyViaCloudAPI(properties.NewMap(), configuration.Init(""))
settings := configuration.NewSettings()
items, err := identifyViaCloudAPI(properties.NewMap(), settings)
require.NoError(t, err)
require.Empty(t, items)
}
Expand All @@ -112,7 +120,7 @@ func TestBoardIdentifySorting(t *testing.T) {
defer paths.TempDir().Join("test").RemoveAll()

// We don't really care about the paths in this case
pmb := packagemanager.NewBuilder(dataDir, dataDir, dataDir, dataDir, "test", downloader.GetDefaultConfig())
pmb := packagemanager.NewBuilder(dataDir, dataDir, nil, dataDir, dataDir, "test", downloader.GetDefaultConfig())

// Create some boards with identical VID:PID combination
pack := pmb.GetOrCreatePackage("packager")
Expand Down Expand Up @@ -148,7 +156,7 @@ func TestBoardIdentifySorting(t *testing.T) {
pme, release := pm.NewExplorer()
defer release()

settings := configuration.Init("")
settings := configuration.NewSettings()
res, err := identify(pme, &discovery.Port{Properties: idPrefs}, settings)
require.NoError(t, err)
require.NotNil(t, res)
Expand Down
3 changes: 1 addition & 2 deletions commands/service_cache_clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package commands
import (
"context"

"github.com/arduino/arduino-cli/internal/cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
)

// CleanDownloadCacheDirectory clean the download cache directory (where archives are downloaded).
func (s *arduinoCoreServerImpl) CleanDownloadCacheDirectory(ctx context.Context, req *rpc.CleanDownloadCacheDirectoryRequest) (*rpc.CleanDownloadCacheDirectoryResponse, error) {
cachePath := configuration.DownloadsDir(s.settings)
cachePath := s.settings.DownloadsDir()
err := cachePath.RemoveAll()
if err != nil {
return nil, err
Expand Down
13 changes: 6 additions & 7 deletions commands/service_compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/arduino/arduino-cli/internal/arduino/sketch"
"github.com/arduino/arduino-cli/internal/arduino/utils"
"github.com/arduino/arduino-cli/internal/buildcache"
"github.com/arduino/arduino-cli/internal/cli/configuration"
"github.com/arduino/arduino-cli/internal/inventory"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
paths "github.com/arduino/go-paths-helper"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
ctx := stream.Context()
syncSend := NewSynchronizedSend(stream.Send)

exportBinaries := s.settings.GetBool("sketch.always_export_binaries")
exportBinaries := s.settings.SketchAlwaysExportBinaries()
if e := req.ExportBinaries; e != nil {
exportBinaries = *e
}
Expand Down Expand Up @@ -175,8 +174,8 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
// cache is purged after compilation to not remove entries that might be required

defer maybePurgeBuildCache(
s.settings.GetUint("build_cache.compilations_before_purge"),
s.settings.GetDuration("build_cache.ttl").Abs())
s.settings.GetCompilationsBeforeBuildCachePurge(),
s.settings.GetBuildCacheTTL().Abs())

var coreBuildCachePath *paths.Path
if req.GetBuildCachePath() == "" {
Expand All @@ -198,7 +197,7 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu

actualPlatform := buildPlatform
otherLibrariesDirs := paths.NewPathList(req.GetLibraries()...)
otherLibrariesDirs.Add(configuration.LibrariesDir(s.settings))
otherLibrariesDirs.Add(s.settings.LibrariesDir())

var libsManager *librariesmanager.LibrariesManager
if pme.GetProfile() != nil {
Expand Down Expand Up @@ -231,9 +230,9 @@ func (s *arduinoCoreServerImpl) Compile(req *rpc.CompileRequest, stream rpc.Ardu
coreBuildCachePath,
int(req.GetJobs()),
req.GetBuildProperties(),
configuration.HardwareDirectories(s.settings),
s.settings.HardwareDirectories(),
otherLibrariesDirs,
configuration.IDEBuiltinLibrariesDir(s.settings),
s.settings.IDEBuiltinLibrariesDir(),
fqbn,
req.GetClean(),
req.GetSourceOverride(),
Expand Down
2 changes: 1 addition & 1 deletion commands/service_debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestGetCommandLine(t *testing.T) {
sketchPath := paths.New("testdata", "debug", sketch)
require.NoError(t, sketchPath.ToAbs())

pmb := packagemanager.NewBuilder(nil, nil, nil, nil, "test", downloader.GetDefaultConfig())
pmb := packagemanager.NewBuilder(nil, nil, nil, nil, nil, "test", downloader.GetDefaultConfig())
pmb.LoadHardwareFromDirectory(customHardware)
pmb.LoadHardwareFromDirectory(dataDir)

Expand Down
24 changes: 19 additions & 5 deletions commands/service_platform_search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"testing"

"github.com/arduino/arduino-cli/internal/cli/configuration"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-paths-helper"
"github.com/stretchr/testify/require"
Expand All @@ -36,9 +35,17 @@ func TestPlatformSearch(t *testing.T) {
err := paths.New("testdata", "platform", "package_index.json").CopyTo(dataDir.Join("package_index.json"))
require.Nil(t, err)

settings := configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())
srv := NewArduinoCoreServer("", settings)
ctx := context.Background()
srv := NewArduinoCoreServer()

conf, err := paths.TempDir().Join("test", "arduino-cli.yaml").ReadFile()
require.NoError(t, err)
_, err = srv.ConfigurationOpen(ctx, &rpc.ConfigurationOpenRequest{
Format: "yaml",
EncodedSettings: string(conf),
})
require.NoError(t, err)

createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
require.NoError(t, err)

Expand Down Expand Up @@ -337,9 +344,16 @@ func TestPlatformSearchSorting(t *testing.T) {
err := paths.New("testdata", "platform", "package_index.json").CopyTo(dataDir.Join("package_index.json"))
require.Nil(t, err)

settings := configuration.Init(paths.TempDir().Join("test", "arduino-cli.yaml").String())
srv := NewArduinoCoreServer("", settings)
ctx := context.Background()
srv := NewArduinoCoreServer()

conf, err := paths.TempDir().Join("test", "arduino-cli.yaml").ReadFile()
require.NoError(t, err)
_, err = srv.ConfigurationOpen(ctx, &rpc.ConfigurationOpenRequest{
Format: "yaml",
EncodedSettings: string(conf),
})
require.NoError(t, err)

createResp, err := srv.Create(ctx, &rpc.CreateRequest{})
require.NoError(t, err)
Expand Down
Loading

0 comments on commit af4513f

Please sign in to comment.