Skip to content

Commit 4f935b8

Browse files
committed
Rationalized configurations about IDE and bundled cores
1 parent 4833cf5 commit 4f935b8

File tree

7 files changed

+43
-39
lines changed

7 files changed

+43
-39
lines changed

arduino/cores/packagemanager/loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (pm *PackageManager) LoadHardware(config *configs.Configuration) error {
3939
if err := pm.LoadHardwareFromDirectories(dirs); err != nil {
4040
return err
4141
}
42-
dirs, err = configs.BundleToolsDirectories()
42+
dirs, err = config.BundleToolsDirectories()
4343
if err != nil {
4444
return fmt.Errorf("getting hardware directory: %s", err)
4545
}

commands/commands.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func InitLibraryManager(pm *packagemanager.PackageManager) *librariesmanager.Lib
102102
Config.DownloadsDir())
103103

104104
// Add IDE builtin libraries dir
105-
if bundledLibsDir := configs.IDEBundledLibrariesDir(); bundledLibsDir != nil {
105+
if bundledLibsDir := Config.IDEBundledLibrariesDir(); bundledLibsDir != nil {
106106
lm.AddLibrariesDir(bundledLibsDir, libraries.IDEBuiltIn)
107107
}
108108

commands/compile/compile.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/arduino/arduino-cli/commands"
3232
"github.com/arduino/arduino-cli/commands/core"
3333
"github.com/arduino/arduino-cli/common/formatter"
34-
"github.com/arduino/arduino-cli/configs"
3534
"github.com/arduino/go-paths-helper"
3635
properties "github.com/arduino/go-properties-map"
3736
"github.com/sirupsen/logrus"
@@ -163,7 +162,7 @@ func run(cmd *cobra.Command, args []string) {
163162
os.Exit(commands.ErrCoreConfig)
164163
}
165164

166-
if toolsDir, err := configs.BundleToolsDirectories(); err == nil {
165+
if toolsDir, err := commands.Config.BundleToolsDirectories(); err == nil {
167166
ctx.ToolsDirs = toolsDir
168167
} else {
169168
formatter.PrintError(err, "Cannot get bundled tools directories.")

commands/root/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func initConfigs() {
114114
if err := commands.Config.LoadFromYAML(commands.Config.ConfigFile); err != nil {
115115
logrus.WithError(err).Warn("Did not manage to get config file, using default configuration")
116116
}
117-
if configs.IsBundledInDesktopIDE() {
117+
if commands.Config.IsBundledInDesktopIDE() {
118118
logrus.Info("CLI is bundled into the IDE")
119119
err := commands.Config.LoadFromDesktopIDEPreferences()
120120
if err != nil {

configs/configuration.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ type Configuration struct {
3434

3535
// SketchbookDir represents the current root of the sketchbooks tree (defaulted to `$HOME/Arduino`).
3636
SketchbookDir *paths.Path
37+
38+
// ArduinoIDEDirectory is the directory of the Arduino IDE if the CLI runs togheter with it.
39+
ArduinoIDEDirectory *paths.Path
40+
41+
// IDEBundledCheckResult contains the result of the check to see if the CLI is bundled with the IDE:
42+
// the field is true if the CLI is bundled with the Arduino IDE, false if the CLI is running
43+
// standalone or nil if the detection has not been performed.
44+
IDEBundledCheckResult *bool
3745
}
3846

3947
// NewConfiguration returns a new Configuration with the default values

configs/hardware_directories.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,17 @@
1818
package configs
1919

2020
import (
21-
"os"
22-
"path/filepath"
23-
2421
"github.com/arduino/go-paths-helper"
2522
)
2623

2724
// HardwareDirectories returns all paths that may contains hardware packages.
2825
func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
2926
res := paths.PathList{}
3027

31-
if IsBundledInDesktopIDE() {
32-
bundledHardwareDir := filepath.Join(*arduinoIDEDirectory, "hardware")
33-
if info, err := os.Stat(bundledHardwareDir); err == nil && info.IsDir() {
34-
res.Add(paths.New(bundledHardwareDir))
28+
if config.IsBundledInDesktopIDE() {
29+
bundledHardwareDir := config.ArduinoIDEDirectory.Join("hardware")
30+
if bundledHardwareDir.IsDir() {
31+
res.Add(bundledHardwareDir)
3532
}
3633
}
3734

@@ -47,13 +44,13 @@ func (config *Configuration) HardwareDirectories() (paths.PathList, error) {
4744
}
4845

4946
// BundleToolsDirectories returns all paths that may contains bundled-tools.
50-
func BundleToolsDirectories() (paths.PathList, error) {
47+
func (config *Configuration) BundleToolsDirectories() (paths.PathList, error) {
5148
res := paths.PathList{}
5249

53-
if IsBundledInDesktopIDE() {
54-
bundledToolsDir := filepath.Join(*arduinoIDEDirectory, "hardware", "tools")
55-
if info, err := os.Stat(bundledToolsDir); err == nil && info.IsDir() {
56-
res = append(res, paths.New(bundledToolsDir))
50+
if config.IsBundledInDesktopIDE() {
51+
bundledToolsDir := config.ArduinoIDEDirectory.Join("hardware", "tools")
52+
if bundledToolsDir.IsDir() {
53+
res = append(res, bundledToolsDir)
5754
}
5855
}
5956

configs/preferences_txt_serializer.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"errors"
2222
"net/url"
2323
"os"
24-
"path/filepath"
2524
"strings"
2625

2726
"github.com/arduino/go-paths-helper"
@@ -30,41 +29,42 @@ import (
3029
"github.com/sirupsen/logrus"
3130
)
3231

33-
var arduinoIDEDirectory *string
34-
3532
// IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE.
36-
func IsBundledInDesktopIDE() bool {
37-
if arduinoIDEDirectory != nil {
38-
return *arduinoIDEDirectory != ""
33+
func (config *Configuration) IsBundledInDesktopIDE() bool {
34+
if config.IDEBundledCheckResult != nil {
35+
return *config.IDEBundledCheckResult
3936
}
40-
empty := ""
41-
arduinoIDEDirectory = &empty
37+
38+
res := false
39+
config.IDEBundledCheckResult = &res
4240

4341
logrus.Info("Checking if CLI is Bundled into the IDE")
4442
executable, err := os.Executable()
4543
if err != nil {
4644
logrus.WithError(err).Warn("Cannot get executable path")
4745
return false
4846
}
49-
executable, err = filepath.EvalSymlinks(executable)
50-
if err != nil {
51-
logrus.WithError(err).Warn("Cannot get executable path (symlinks error)")
47+
executablePath := paths.New(executable)
48+
if err := executablePath.FollowSymLink(); err != nil {
49+
logrus.WithError(err).Warn("Cannot get executable path")
5250
return false
5351
}
54-
ideDir := filepath.Dir(executable)
52+
ideDir := executablePath.Parent()
5553
logrus.Info("Candidate IDE Directory: ", ideDir)
5654

57-
tests := []string{"tools-builder", "Examples/01.Basics/Blink"}
55+
tests := []string{
56+
"tools-builder",
57+
"examples/01.Basics/Blink",
58+
}
5859
for _, test := range tests {
59-
filePath := filepath.Join(ideDir, test)
60-
_, err := os.Stat(filePath)
61-
if !os.IsNotExist(err) {
62-
arduinoIDEDirectory = &ideDir
63-
break
60+
if !ideDir.Join(test).Exist() {
61+
return false
6462
}
6563
}
6664

67-
return *arduinoIDEDirectory != ""
65+
config.ArduinoIDEDirectory = ideDir
66+
res = true
67+
return true
6868
}
6969

7070
// LoadFromDesktopIDEPreferences loads the config from the Desktop IDE preferences.txt file
@@ -127,9 +127,9 @@ func proxyConfigsFromIDEPrefs(props properties.Map) error {
127127
// IDEBundledLibrariesDir returns the libraries directory bundled in
128128
// the Arduino IDE. If there is no Arduino IDE or the directory doesn't
129129
// exists then nil is returned
130-
func IDEBundledLibrariesDir() *paths.Path {
131-
if IsBundledInDesktopIDE() {
132-
libDir := paths.New(*arduinoIDEDirectory, "libraries")
130+
func (config *Configuration) IDEBundledLibrariesDir() *paths.Path {
131+
if config.IsBundledInDesktopIDE() {
132+
libDir := config.ArduinoIDEDirectory.Join("libraries")
133133
if libDir.IsDir() {
134134
return libDir
135135
}

0 commit comments

Comments
 (0)