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 {
if err := pm.LoadHardwareFromDirectories(dirs); err != nil {
return err
}
dirs, err = configs.BundleToolsDirectories()
dirs, err = config.BundleToolsDirectories()
if err != nil {
return fmt.Errorf("getting hardware directory: %s", err)
}
......
......@@ -102,7 +102,7 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib
Config.DownloadsDir())
// Add IDE builtin libraries dir
if bundledLibsDir := configs.IDEBundledLibrariesDir(); bundledLibsDir != nil {
if bundledLibsDir := Config.IDEBundledLibrariesDir(); bundledLibsDir != nil {
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
}
......
......@@ -31,7 +31,6 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/commands/core"
"github.com/arduino/arduino-cli/common/formatter"
"github.com/arduino/arduino-cli/configs"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-map"
"github.com/sirupsen/logrus"
......@@ -163,7 +162,7 @@ func run(cmd *cobra.Command, args []string) {
os.Exit(commands.ErrCoreConfig)
}
if toolsDir, err := configs.BundleToolsDirectories(); err == nil {
if toolsDir, err := commands.Config.BundleToolsDirectories(); err == nil {
ctx.ToolsDirs = toolsDir
} else {
formatter.PrintError(err, "Cannot get bundled tools directories.")
......
......@@ -114,7 +114,7 @@ func initConfigs() {
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
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")
err := commands.Config.LoadFromDesktopIDEPreferences()
if err != nil {
......
......@@ -34,6 +34,14 @@ type Configuration struct {
// SketchbookDir represents the current root of the sketchbooks tree (defaulted to `$HOME/Arduino`).
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
......
......@@ -18,9 +18,6 @@
package configs
import (
"os"
"path/filepath"
"github.com/arduino/go-paths-helper"
)
......@@ -28,10 +25,10 @@ import (
func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
res := paths.PathList{}
if IsBundledInDesktopIDE() {
bundledHardwareDir := filepath.Join(*arduinoIDEDirectory, "hardware")
if info, err := os.Stat(bundledHardwareDir); err == nil && info.IsDir() {
res.Add(paths.New(bundledHardwareDir))
if config.IsBundledInDesktopIDE() {
bundledHardwareDir := config.ArduinoIDEDirectory.Join("hardware")
if bundledHardwareDir.IsDir() {
res.Add(bundledHardwareDir)
}
}
......@@ -47,13 +44,13 @@ func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
}
// BundleToolsDirectories returns all paths that may contains bundled-tools.
func BundleToolsDirectories() (paths.PathList, error) {
func (config *Configuration) BundleToolsDirectories() (paths.PathList, error) {
res := paths.PathList{}
if IsBundledInDesktopIDE() {
bundledToolsDir := filepath.Join(*arduinoIDEDirectory, "hardware", "tools")
if info, err := os.Stat(bundledToolsDir); err == nil && info.IsDir() {
res = append(res, paths.New(bundledToolsDir))
if config.IsBundledInDesktopIDE() {
bundledToolsDir := config.ArduinoIDEDirectory.Join("hardware", "tools")
if bundledToolsDir.IsDir() {
res = append(res, bundledToolsDir)
}
}
......
......@@ -21,7 +21,6 @@ import (
"errors"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/arduino/go-paths-helper"
......@@ -30,15 +29,14 @@ import (
"github.com/sirupsen/logrus"
)
var arduinoIDEDirectory *string
// IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE.
func IsBundledInDesktopIDE() bool {
if arduinoIDEDirectory != nil {
return *arduinoIDEDirectory != ""
func (config *Configuration) IsBundledInDesktopIDE() bool {
if config.IDEBundledCheckResult != nil {
return *config.IDEBundledCheckResult
}
empty := ""
arduinoIDEDirectory = &empty
res := false
config.IDEBundledCheckResult = &res
logrus.Info("Checking if CLI is Bundled into the IDE")
executable, err := os.Executable()
......@@ -46,25 +44,27 @@ func IsBundledInDesktopIDE() bool {
logrus.WithError(err).Warn("Cannot get executable path")
return false
}
executable, err = filepath.EvalSymlinks(executable)
if err != nil {
logrus.WithError(err).Warn("Cannot get executable path (symlinks error)")
executablePath := paths.New(executable)
if err := executablePath.FollowSymLink(); err != nil {
logrus.WithError(err).Warn("Cannot get executable path")
return false
}
ideDir := filepath.Dir(executable)
ideDir := executablePath.Parent()
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 {
filePath := filepath.Join(ideDir, test)
_, err := os.Stat(filePath)
if !os.IsNotExist(err) {
arduinoIDEDirectory = &ideDir
break
if !ideDir.Join(test).Exist() {
return false
}
}
return *arduinoIDEDirectory != ""
config.ArduinoIDEDirectory = ideDir
res = true
return true
}
// LoadFromDesktopIDEPreferences loads the config from the Desktop IDE preferences.txt file
......@@ -127,9 +127,9 @@ func proxyConfigsFromIDEPrefs(props properties.Map) error {
// IDEBundledLibrariesDir returns the libraries directory bundled in
// the Arduino IDE. If there is no Arduino IDE or the directory doesn't
// exists then nil is returned
func IDEBundledLibrariesDir() *paths.Path {
if IsBundledInDesktopIDE() {
libDir := paths.New(*arduinoIDEDirectory, "libraries")
func (config *Configuration) IDEBundledLibrariesDir() *paths.Path {
if config.IsBundledInDesktopIDE() {
libDir := config.ArduinoIDEDirectory.Join("libraries")
if libDir.IsDir() {
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