Skip to content

Commit e3d9639

Browse files
author
Massimiliano Pippi
committed
add directories module
1 parent b404718 commit e3d9639

File tree

3 files changed

+178
-11
lines changed

3 files changed

+178
-11
lines changed

configuration/configuration.go

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020
"path/filepath"
2121
"runtime"
22+
"strings"
2223

2324
"github.com/arduino/arduino-cli/cli/feedback"
2425
"github.com/arduino/go-win32-utils"
@@ -27,17 +28,21 @@ import (
2728
)
2829

2930
// Init initialize defaults and read the configuration file
30-
func Init() {
31+
func Init(configPath string) {
3132
// Config file metadata
3233
viper.SetConfigName("arduino-cli")
3334

35+
// Get default data path if none was provided
36+
if configPath == "" {
37+
configPath = getDefaultArduinoDataDir()
38+
}
39+
3440
// Add paths where to search for a config file
35-
configPath := GetDefaultArduinoDataDir()
3641
logrus.Infof("Checking for config file in: %s", configPath)
3742
viper.AddConfigPath(configPath)
3843

3944
// Set configuration defaults
40-
setDefaults()
45+
setDefaults(configPath, getDefaultSketchbookDir())
4146

4247
// Attempt to read config file
4348
if err := viper.ReadInConfig(); err != nil {
@@ -50,11 +55,17 @@ func Init() {
5055

5156
// Bind env vars
5257
viper.SetEnvPrefix("ARDUINO")
58+
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
5359
viper.AutomaticEnv()
60+
61+
// Bind env aliases to keep backward compatibility
62+
viper.BindEnv("directories.Sketchbook", "ARDUINO_SKETCHBOOK_DIR")
63+
viper.BindEnv("directories.Downloads", "ARDUINO_DOWNLOADS_DIR")
64+
viper.BindEnv("directories.Data", "ARDUINO_DATA_DIR")
5465
}
5566

56-
// GetDefaultArduinoDataDir returns the full path to the default arduino folder
57-
func GetDefaultArduinoDataDir() string {
67+
// getDefaultArduinoDataDir returns the full path to the default arduino folder
68+
func getDefaultArduinoDataDir() string {
5869
userHomeDir, err := os.UserHomeDir()
5970
if err != nil {
6071
logrus.Errorf("Unable to get user home dir: %v", err)
@@ -77,3 +88,78 @@ func GetDefaultArduinoDataDir() string {
7788
return "."
7889
}
7990
}
91+
92+
// getDefaultSketchbookDir returns the full path to the default sketchbook folder
93+
func getDefaultSketchbookDir() string {
94+
userHomeDir, err := os.UserHomeDir()
95+
if err != nil {
96+
logrus.Errorf("Unable to get user home dir: %v", err)
97+
return "."
98+
}
99+
100+
switch runtime.GOOS {
101+
case "linux":
102+
return filepath.Join(userHomeDir, "Arduino")
103+
case "darwin":
104+
return filepath.Join(userHomeDir, "Documents", "Arduino")
105+
case "windows":
106+
documentsPath, err := win32.GetDocumentsFolder()
107+
if err != nil {
108+
logrus.Errorf("Unable to get Documents Folder: %v", err)
109+
return "."
110+
}
111+
return filepath.Join(documentsPath, "Arduino")
112+
default:
113+
return "."
114+
}
115+
}
116+
117+
// IsBundledInDesktopIDE returns true if the CLI is bundled with the Arduino IDE.
118+
func IsBundledInDesktopIDE() bool {
119+
// value is cached the first time we run the check
120+
if viper.IsSet("IDE.Bundled") {
121+
return viper.GetBool("IDE.Bundled")
122+
}
123+
124+
viper.Set("IDE.Bundled", false)
125+
viper.Set("IDE.Portable", false)
126+
127+
logrus.Info("Checking if CLI is Bundled into the IDE")
128+
executable, err := os.Executable()
129+
if err != nil {
130+
logrus.WithError(err).Warn("Cannot get executable path")
131+
return viper.GetBool("IDE.Bundled")
132+
}
133+
134+
executablePath, err := filepath.EvalSymlinks(executable)
135+
if err != nil {
136+
logrus.WithError(err).Warn("Cannot get executable path")
137+
return viper.GetBool("IDE.Bundled")
138+
}
139+
140+
ideDir := filepath.Dir(executablePath)
141+
logrus.Info("Candidate IDE Directory: ", ideDir)
142+
143+
// We check an arbitrary number of folders that are part of the IDE
144+
// install tree
145+
tests := []string{
146+
"tools-builder",
147+
"examples/01.Basics/Blink",
148+
"portable",
149+
}
150+
151+
for _, test := range tests {
152+
if _, err := os.Stat(filepath.Join(ideDir, test)); err != nil {
153+
// the test folder doesn't exist or is not accessible
154+
return viper.GetBool("IDE.Bundled")
155+
}
156+
157+
if test == "portable" {
158+
logrus.Info("IDE is portable")
159+
viper.Set("IDE.Portable", true)
160+
}
161+
}
162+
163+
viper.Set("IDE.Directory", ideDir)
164+
return true
165+
}

configuration/defaults.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import (
2121
"github.com/spf13/viper"
2222
)
2323

24-
func setDefaults() {
25-
urls := []string{"https://downloads.arduino.cc/packages/package_index.json"}
26-
viper.SetDefault("BoardManagerAdditionalUrls", urls)
24+
func setDefaults(dataDir, sketchBookDir string) {
25+
// board manager settings
26+
viper.SetDefault("board_manager.additional_urls", []string{})
2727

28-
dataDir := GetDefaultArduinoDataDir()
29-
viper.SetDefault("DataDir", dataDir)
30-
viper.SetDefault("DownloadsDir", filepath.Join(dataDir, "staging"))
28+
// arduino directories
29+
viper.SetDefault("directories.Data", dataDir)
30+
viper.SetDefault("directories.Downloads", filepath.Join(dataDir, "staging"))
31+
viper.SetDefault("directories.Packages", filepath.Join(dataDir, "packages"))
32+
viper.SetDefault("directories.SketchBook", sketchBookDir)
33+
viper.SetDefault("directories.Libraries", filepath.Join(sketchBookDir, "libraries"))
3134
}

configuration/directories.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2019 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package configuration
17+
18+
import (
19+
"github.com/arduino/go-paths-helper"
20+
"github.com/spf13/viper"
21+
)
22+
23+
// HardwareDirectories returns all paths that may contains hardware packages.
24+
func HardwareDirectories() paths.PathList {
25+
res := paths.PathList{}
26+
27+
if IsBundledInDesktopIDE() {
28+
ideDir := paths.New(viper.GetString("IDE.Directory"))
29+
bundledHardwareDir := ideDir.Join("hardware")
30+
if bundledHardwareDir.IsDir() {
31+
res.Add(bundledHardwareDir)
32+
}
33+
}
34+
35+
if viper.IsSet("directories.Packages") {
36+
res.Add(paths.New(viper.GetString("directories.Packages")))
37+
}
38+
39+
if viper.IsSet("directories.Sketchbook") {
40+
skDir := paths.New(viper.GetString("directories.Sketchbook"))
41+
hwDir := skDir.Join("hardware")
42+
if hwDir.IsDir() {
43+
res.Add(hwDir)
44+
}
45+
}
46+
47+
return res
48+
}
49+
50+
// BundleToolsDirectories returns all paths that may contains bundled-tools.
51+
func BundleToolsDirectories() paths.PathList {
52+
res := paths.PathList{}
53+
54+
if IsBundledInDesktopIDE() {
55+
ideDir := paths.New(viper.GetString("IDE.Directory"))
56+
bundledToolsDir := ideDir.Join("hardware", "tools")
57+
if bundledToolsDir.IsDir() {
58+
res = append(res, bundledToolsDir)
59+
}
60+
}
61+
62+
return res
63+
}
64+
65+
// IDEBundledLibrariesDir returns the libraries directory bundled in
66+
// the Arduino IDE. If there is no Arduino IDE or the directory doesn't
67+
// exists then nil is returned
68+
func IDEBundledLibrariesDir() *paths.Path {
69+
if IsBundledInDesktopIDE() {
70+
ideDir := paths.New(viper.GetString("IDE.Directory"))
71+
libDir := ideDir.Join("libraries")
72+
if libDir.IsDir() {
73+
return libDir
74+
}
75+
}
76+
77+
return nil
78+
}

0 commit comments

Comments
 (0)