Skip to content

Commit

Permalink
Moved path-creation utility in own package. Renamed some common paths.
Browse files Browse the repository at this point in the history
This simplifies the overall codebase and avoids passing anonymous functions
to discover paths, for example the following snippet:

  // DownloadIndex is a function to download a generic index.
  func DownloadIndex(indexPathFunc func() (string, error), URL string) error {
    file, err := indexPathFunc()
    ...

becomes:

  func DownloadIndex(indexPath pathutils.Path, URL string) error {
    file, err := indexPath.Get()
    ...

This commit also renames some of the paths defined in the commons package
removing the Default prefix, since them are initialized with the default
but they are updated with the actual path at runtime.
  • Loading branch information
cmaglie committed Feb 21, 2018
1 parent 96139c2 commit 3f5245c
Show file tree
Hide file tree
Showing 27 changed files with 263 additions and 151 deletions.
4 changes: 2 additions & 2 deletions commands/board/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func runAttachCommand(cmd *cobra.Command, args []string) {

time.Sleep(duration)

homeFolder, err := common.GetDefaultArduinoHomeFolder()
homeFolder, err := common.ArduinoHomeFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot Parse Board Index file.")
os.Exit(commands.ErrCoreConfig)
}

packageFolder, err := common.GetDefaultPkgFolder()
packageFolder, err := common.PackagesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot Parse Board Index file.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var listCommand = &cobra.Command{
// runListCommand detects and lists the connected arduino boards
// (either via serial or network ports).
func runListCommand(cmd *cobra.Command, args []string) {
packageFolder, err := common.GetDefaultPkgFolder()
packageFolder, err := common.PackagesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot Parse Board Index file.")
os.Exit(commands.ErrCoreConfig)
Expand Down
4 changes: 2 additions & 2 deletions commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestLibDownload(t *testing.T) {
defer cleanTempRedirect(t, tempFile)

// getting the paths to create the want path of the want object.
stagingFolder, err := common.GetDownloadCacheFolder("libraries")
stagingFolder, err := common.DownloadCacheFolder("libraries").Get()
require.NoError(t, err, "Getting cache folder")

// desired output
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestCoreDownload(t *testing.T) {
defer cleanTempRedirect(t, tempFile)

// getting the paths to create the want path of the want object.
stagingFolder, err := common.GetDownloadCacheFolder("packages")
stagingFolder, err := common.DownloadCacheFolder("packages").Get()
require.NoError(t, err, "Getting cache folder")

// desired output
Expand Down
22 changes: 17 additions & 5 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,28 @@ func run(cmd *cobra.Command, args []string) {
ctx := &types.Context{}

ctx.FQBN = fullyQualifiedBoardName
ctx.SketchLocation = filepath.Join(common.SketchbookFolder, sketchName)
sketchbookPath, err := common.SketchbookFolder.Get()
if err != nil {
formatter.PrintError(err, "Getting sketchbook folder")
os.Exit(commands.ErrCoreConfig)
}
ctx.SketchLocation = filepath.Join(sketchbookPath, sketchName)

packagesFolder, err := common.GetDefaultPkgFolder()
packagesFolder, err := common.PackagesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get packages folder.")
os.Exit(commands.ErrCoreConfig)
}
ctx.HardwareFolders = []string{packagesFolder}

toolsFolder, err := common.GetDefaultToolsFolder(packageName)
toolsFolder, err := common.ToolsFolder(packageName).Get()
if err != nil {
formatter.PrintError(err, "Cannot get tools folder.")
os.Exit(commands.ErrCoreConfig)
}
ctx.ToolsFolders = []string{toolsFolder}

librariesFolder, err := common.GetDefaultLibFolder()
librariesFolder, err := common.LibrariesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get libraries folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down Expand Up @@ -222,8 +227,15 @@ func run(cmd *cobra.Command, args []string) {

// Will be deprecated.
ctx.ArduinoAPIVersion = "10600"

// Check if Arduino IDE is installed and get it's libraries location.
ideProperties, err := properties.Load(filepath.Join(common.ArduinoDataFolder, "preferences.txt"))
dataFolder, err := common.ArduinoDataFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot locate arduino data folder.")
os.Exit(commands.ErrCoreConfig)
}

ideProperties, err := properties.Load(filepath.Join(dataFolder, "preferences.txt"))
if err == nil {
lastIdeSubProperties := ideProperties.SubTree("last").SubTree("ide")
// Preferences can contain records from previous IDE versions. Find the latest one.
Expand Down
9 changes: 5 additions & 4 deletions commands/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/bcmi-labs/arduino-cli/common/formatter/output"
"github.com/bcmi-labs/arduino-cli/common/formatter/pretty_print"
"github.com/bcmi-labs/arduino-cli/cores"
"github.com/bcmi-labs/arduino-cli/pathutils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -55,17 +56,17 @@ var command = &cobra.Command{

// getInstalledCores gets the installed cores and puts them in the output struct.
func getInstalledCores(packageName string, cores *[]output.InstalledStuff) {
getInstalledStuff(packageName, cores, common.GetDefaultCoresFolder)
getInstalledStuff(cores, common.CoresFolder(packageName))
}

// getInstalledTools gets the installed tools and puts them in the output struct.
func getInstalledTools(packageName string, tools *[]output.InstalledStuff) {
getInstalledStuff(packageName, tools, common.GetDefaultToolsFolder)
getInstalledStuff(tools, common.ToolsFolder(packageName))
}

// getInstalledStuff is a generic procedure to get installed cores or tools and put them in an output struct.
func getInstalledStuff(packageName string, stuff *[]output.InstalledStuff, defaultFolderFunc func(string) (string, error)) {
stuffHome, err := defaultFolderFunc(packageName)
func getInstalledStuff(stuff *[]output.InstalledStuff, folder pathutils.Path) {
stuffHome, err := folder.Get()
if err != nil {
logrus.WithError(err).Warn("Cannot get default folder")
return
Expand Down
4 changes: 2 additions & 2 deletions commands/core/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
WithField("Version", item.Release.VersionName()).
Info("Installing tool")

toolRoot, err := common.GetDefaultToolsFolder(item.Package)
toolRoot, err := common.ToolsFolder(item.Package).Get()
if err != nil {
formatter.PrintError(err, "Cannot get tool install path, try again.")
os.Exit(commands.ErrCoreConfig)
Expand Down Expand Up @@ -138,7 +138,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
WithField("Version", item.Release.VersionName()).
Info("Installing core")

coreRoot, err := common.GetDefaultCoresFolder(item.Package)
coreRoot, err := common.CoresFolder(item.Package).Get()
if err != nil {
formatter.PrintError(err, "Cannot get core install path, try again.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/core/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var listCommand = &cobra.Command{

func runListCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino core list`")
pkgHome, err := common.GetDefaultPkgFolder()
pkgHome, err := common.PackagesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get packages folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func runInstallCommand(cmd *cobra.Command, args []string) {
logrus.Info("Download finished")

logrus.Info("Installing")
folder, err := common.GetDefaultLibFolder()
folder, err := common.LibrariesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get default lib install path.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var listCommand = &cobra.Command{
func runListCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino lib list`")

libHome, err := common.GetDefaultLibFolder()
libHome, err := common.LibrariesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get libraries folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/lib/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func runUninstallCommand(cmd *cobra.Command, args []string) {
logrus.Info("Preparing")
libs := libraries.ParseArgs(args)

libFolder, err := common.GetDefaultLibFolder()
libFolder, err := common.LibrariesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get default libraries folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down
12 changes: 7 additions & 5 deletions commands/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"os"
"strings"

"github.com/bcmi-labs/arduino-cli/pathutils"

"github.com/bcmi-labs/arduino-cli/commands"
"github.com/bcmi-labs/arduino-cli/commands/board"
"github.com/bcmi-labs/arduino-cli/commands/compile"
Expand Down Expand Up @@ -170,19 +172,19 @@ func initConfigs() {
}
logrus.Info("Configuration set")
commands.GlobalFlags.Configs = c
common.ArduinoDataFolder = commands.GlobalFlags.Configs.ArduinoDataFolder
common.ArduinoIDEFolder = configs.ArduinoIDEFolder
common.SketchbookFolder = commands.GlobalFlags.Configs.SketchbookPath
common.ArduinoDataFolder = pathutils.NewConstPath(commands.GlobalFlags.Configs.ArduinoDataFolder)
common.ArduinoIDEFolder = pathutils.NewConstPath(configs.ArduinoIDEFolder)
common.SketchbookFolder = pathutils.NewConstPath(commands.GlobalFlags.Configs.SketchbookPath)
}

func initViper() {
logrus.Info("Initiating viper config")

defHome, err := common.GetDefaultArduinoHomeFolder()
defHome, err := common.ArduinoHomeFolder.Get()
if err != nil {
commands.ErrLogrus.WithError(err).Warn("Cannot get default Arduino Home")
}
defArduinoData, err := common.GetDefaultArduinoFolder()
defArduinoData, err := common.ArduinoDataFolder.Get()
if err != nil {
logrus.WithError(err).Warn("Cannot get default Arduino folder")
}
Expand Down
2 changes: 1 addition & 1 deletion commands/sketch/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var syncCommand = &cobra.Command{
func runSyncCommand(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino sketch sync`")

sketchbook, err := common.GetDefaultArduinoHomeFolder()
sketchbook, err := common.ArduinoHomeFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get sketchbook folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down
2 changes: 1 addition & 1 deletion commands/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var command = &cobra.Command{

func run(cmd *cobra.Command, args []string) {
logrus.Info("Executing `arduino validate`")
packagesFolder, err := common.GetDefaultPkgFolder()
packagesFolder, err := common.PackagesFolder.Get()
if err != nil {
formatter.PrintError(err, "Cannot get packages folder.")
os.Exit(commands.ErrCoreConfig)
Expand Down
6 changes: 4 additions & 2 deletions common/net_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ import (
"net/http"
"os"
"time"

"github.com/bcmi-labs/arduino-cli/pathutils"
)

// DownloadIndex is a function to download a generic index.
func DownloadIndex(indexPathFunc func() (string, error), URL string) error {
file, err := indexPathFunc()
func DownloadIndex(indexPath pathutils.Path, URL string) error {
file, err := indexPath.Get()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 3f5245c

Please sign in to comment.