Commit 4f935b80 authored by Cristian Maglie's avatar Cristian Maglie

Rationalized configurations about IDE and bundled cores

parent 4833cf51
...@@ -39,7 +39,7 @@ func (pm *PackageManager) LoadHardware(config *configs.Configuration) error { ...@@ -39,7 +39,7 @@ func (pm *PackageManager) LoadHardware(config *configs.Configuration) error {
if err := pm.LoadHardwareFromDirectories(dirs); err != nil { if err := pm.LoadHardwareFromDirectories(dirs); err != nil {
return err return err
} }
dirs, err = configs.BundleToolsDirectories() dirs, err = config.BundleToolsDirectories()
if err != nil { if err != nil {
return fmt.Errorf("getting hardware directory: %s", err) return fmt.Errorf("getting hardware directory: %s", err)
} }
......
...@@ -102,7 +102,7 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib ...@@ -102,7 +102,7 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib
Config.DownloadsDir()) Config.DownloadsDir())
// Add IDE builtin libraries dir // Add IDE builtin libraries dir
if bundledLibsDir := configs.IDEBundledLibrariesDir(); bundledLibsDir != nil { if bundledLibsDir := Config.IDEBundledLibrariesDir(); bundledLibsDir != nil {
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn) lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
} }
......
...@@ -31,7 +31,6 @@ import ( ...@@ -31,7 +31,6 @@ import (
"github.com/arduino/arduino-cli/commands" "github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/core" "github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/common/formatter" "github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/configs"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-map" properties "github.com/arduino/go-properties-map"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
...@@ -163,7 +162,7 @@ func run(cmd *cobra.Command, args []string) { ...@@ -163,7 +162,7 @@ func run(cmd *cobra.Command, args []string) {
os.Exit(commands.ErrCoreConfig) os.Exit(commands.ErrCoreConfig)
} }
if toolsDir, err := configs.BundleToolsDirectories(); err == nil { if toolsDir, err := commands.Config.BundleToolsDirectories(); err == nil {
ctx.ToolsDirs = toolsDir ctx.ToolsDirs = toolsDir
} else { } else {
formatter.PrintError(err, "Cannot get bundled tools directories.") formatter.PrintError(err, "Cannot get bundled tools directories.")
......
...@@ -114,7 +114,7 @@ func initConfigs() { ...@@ -114,7 +114,7 @@ func initConfigs() {
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil { if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
logrus.WithError(err).Warn("Did not manage to get config file, using default configuration") logrus.WithError(err).Warn("Did not manage to get config file, using default configuration")
} }
if configs.IsBundledInDesktopIDE() { if commands.Config.IsBundledInDesktopIDE() {
logrus.Info("CLI is bundled into the IDE") logrus.Info("CLI is bundled into the IDE")
err := commands.Config.LoadFromDesktopIDEPreferences() err := commands.Config.LoadFromDesktopIDEPreferences()
if err != nil { if err != nil {
......
...@@ -34,6 +34,14 @@ type Configuration struct { ...@@ -34,6 +34,14 @@ type Configuration struct {
// SketchbookDir represents the current root of the sketchbooks tree (defaulted to `$HOME/Arduino`). // SketchbookDir represents the current root of the sketchbooks tree (defaulted to `$HOME/Arduino`).
SketchbookDir *paths.Path SketchbookDir *paths.Path
// ArduinoIDEDirectory is the directory of the Arduino IDE if the CLI runs togheter with it.
ArduinoIDEDirectory *paths.Path
// IDEBundledCheckResult contains the result of the check to see if the CLI is bundled with the IDE:
// the field is true if the CLI is bundled with the Arduino IDE, false if the CLI is running
// standalone or nil if the detection has not been performed.
IDEBundledCheckResult *bool
} }
// NewConfiguration returns a new Configuration with the default values // NewConfiguration returns a new Configuration with the default values
......
...@@ -18,9 +18,6 @@ ...@@ -18,9 +18,6 @@
package configs package configs
import ( import (
"os"
"path/filepath"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
) )
...@@ -28,10 +25,10 @@ import ( ...@@ -28,10 +25,10 @@ import (
func (config *Configuration) HardwareDirectories() (paths.PathList, error) { func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
res := paths.PathList{} res := paths.PathList{}
if IsBundledInDesktopIDE() { if config.IsBundledInDesktopIDE() {
bundledHardwareDir := filepath.Join(*arduinoIDEDirectory, "hardware") bundledHardwareDir := config.ArduinoIDEDirectory.Join("hardware")
if info, err := os.Stat(bundledHardwareDir); err == nil && info.IsDir() { if bundledHardwareDir.IsDir() {
res.Add(paths.New(bundledHardwareDir)) res.Add(bundledHardwareDir)
} }
} }
...@@ -47,13 +44,13 @@ func (config *Configuration) HardwareDirectories() (paths.PathList, error) { ...@@ -47,13 +44,13 @@ func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
} }
// BundleToolsDirectories returns all paths that may contains bundled-tools. // BundleToolsDirectories returns all paths that may contains bundled-tools.
func BundleToolsDirectories() (paths.PathList, error) { func (config *Configuration) BundleToolsDirectories() (paths.PathList, error) {
res := paths.PathList{} res := paths.PathList{}
if IsBundledInDesktopIDE() { if config.IsBundledInDesktopIDE() {
bundledToolsDir := filepath.Join(*arduinoIDEDirectory, "hardware", "tools") bundledToolsDir := config.ArduinoIDEDirectory.Join("hardware", "tools")
if info, err := os.Stat(bundledToolsDir); err == nil && info.IsDir() { if bundledToolsDir.IsDir() {
res = append(res, paths.New(bundledToolsDir)) res = append(res, bundledToolsDir)
} }
} }
......
...@@ -21,7 +21,6 @@ import ( ...@@ -21,7 +21,6 @@ import (
"errors" "errors"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
...@@ -30,15 +29,14 @@ import ( ...@@ -30,15 +29,14 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
var arduinoIDEDirectory *string
// IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE. // IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE.
func IsBundledInDesktopIDE() bool { func (config *Configuration) IsBundledInDesktopIDE() bool {
if arduinoIDEDirectory != nil { if config.IDEBundledCheckResult != nil {
return *arduinoIDEDirectory != "" return *config.IDEBundledCheckResult
} }
empty := ""
arduinoIDEDirectory = &empty res := false
config.IDEBundledCheckResult = &res
logrus.Info("Checking if CLI is Bundled into the IDE") logrus.Info("Checking if CLI is Bundled into the IDE")
executable, err := os.Executable() executable, err := os.Executable()
...@@ -46,25 +44,27 @@ func IsBundledInDesktopIDE() bool { ...@@ -46,25 +44,27 @@ func IsBundledInDesktopIDE() bool {
logrus.WithError(err).Warn("Cannot get executable path") logrus.WithError(err).Warn("Cannot get executable path")
return false return false
} }
executable, err = filepath.EvalSymlinks(executable) executablePath := paths.New(executable)
if err != nil { if err := executablePath.FollowSymLink(); err != nil {
logrus.WithError(err).Warn("Cannot get executable path (symlinks error)") logrus.WithError(err).Warn("Cannot get executable path")
return false return false
} }
ideDir := filepath.Dir(executable) ideDir := executablePath.Parent()
logrus.Info("Candidate IDE Directory: ", ideDir) logrus.Info("Candidate IDE Directory: ", ideDir)
tests := []string{"tools-builder", "Examples/01.Basics/Blink"} tests := []string{
"tools-builder",
"examples/01.Basics/Blink",
}
for _, test := range tests { for _, test := range tests {
filePath := filepath.Join(ideDir, test) if !ideDir.Join(test).Exist() {
_, err := os.Stat(filePath) return false
if !os.IsNotExist(err) {
arduinoIDEDirectory = &ideDir
break
} }
} }
return *arduinoIDEDirectory != "" config.ArduinoIDEDirectory = ideDir
res = true
return true
} }
// LoadFromDesktopIDEPreferences loads the config from the Desktop IDE preferences.txt file // LoadFromDesktopIDEPreferences loads the config from the Desktop IDE preferences.txt file
...@@ -127,9 +127,9 @@ func proxyConfigsFromIDEPrefs(props properties.Map) error { ...@@ -127,9 +127,9 @@ func proxyConfigsFromIDEPrefs(props properties.Map) error {
// IDEBundledLibrariesDir returns the libraries directory bundled in // IDEBundledLibrariesDir returns the libraries directory bundled in
// the Arduino IDE. If there is no Arduino IDE or the directory doesn't // the Arduino IDE. If there is no Arduino IDE or the directory doesn't
// exists then nil is returned // exists then nil is returned
func IDEBundledLibrariesDir() *paths.Path { func (config *Configuration) IDEBundledLibrariesDir() *paths.Path {
if IsBundledInDesktopIDE() { if config.IsBundledInDesktopIDE() {
libDir := paths.New(*arduinoIDEDirectory, "libraries") libDir := config.ArduinoIDEDirectory.Join("libraries")
if libDir.IsDir() { if libDir.IsDir() {
return libDir return libDir
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment