Skip to content

Commit

Permalink
Realigning with the Java IDE and fixing lib list error
Browse files Browse the repository at this point in the history
  • Loading branch information
saniales committed Jul 17, 2017
1 parent f1f5533 commit 43edad7
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 29 deletions.
101 changes: 101 additions & 0 deletions cmd/arduino_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/bcmi-labs/arduino-cli/cmd/formatter"
"github.com/bcmi-labs/arduino-cli/common"
"github.com/spf13/cobra"
"github.com/zieckey/goini"
)

var arduinoCoreCmd = &cobra.Command{
Expand All @@ -53,5 +62,97 @@ func init() {
}

func executeCoreListCommand(cmd *cobra.Command, args []string) {
if arduinoLibFlags.updateIndex {
execUpdateListIndex(cmd, args)
return
}

libHome, err := common.GetDefaultLibFolder()
if err != nil {
formatter.Print("Cannot get libraries folder")
return
}

//prettyPrints.corestatus(status)
dir, err := os.Open(libHome)
if err != nil {
formatter.Print("Cannot open libraries folder")
return
}

dirFiles, err := dir.Readdir(0)
if err != nil {
formatter.Print("Cannot read into libraries folder")
return
}

cores := make([]string, 0, 10)

//TODO: optimize this algorithm
// time complexity O(libraries_to_install(from RAM) *
// library_folder_number(from DISK) *
// library_folder_file_number (from DISK))
//TODO : remove only one version
for _, file := range dirFiles {
if file.IsDir() {
indexFile := filepath.Join(libHome, file.Name(), "library.properties")
_, err = os.Stat(indexFile)
if os.IsNotExist(err) {
fileName := file.Name()
//replacing underscore in foldernames with spaces.
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
cores = append(cores, fileName)
} else {
// I use library.properties file
content, err := ioutil.ReadFile(indexFile)
if err != nil {
fileName := file.Name()
//replacing underscore in foldernames with spaces.
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
cores = append(cores, fileName)
continue
}

ini := goini.New()
err = ini.Parse(content, "\n", "=")
if err != nil {
formatter.Print(err)
}
Name, ok := ini.Get("name")
if !ok {
fileName := file.Name()
//replacing underscore in foldernames with spaces.
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
cores = append(cores, fileName)
continue
}
Version, ok := ini.Get("version")
if !ok {
fileName := file.Name()
//replacing underscore in foldernames with spaces.
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
cores = append(cores, fileName)
continue
}
cores = append(cores, fmt.Sprintf("%-10s v. %s", Name, Version))
}
}
}

if len(cores) < 1 {
formatter.Print("No core installed")
} else {
//pretty prints installed libraries
for _, item := range cores {
formatter.Print(item)
}
}
}
17 changes: 7 additions & 10 deletions cmd/arduino_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func executeListCommand(command *cobra.Command, args []string) {
return
}

libs := make([]string, 0, 10)
libs := make(map[string]interface{}, 10)

//TODO: optimize this algorithm
// time complexity O(libraries_to_install(from RAM) *
Expand All @@ -490,7 +490,7 @@ func executeListCommand(command *cobra.Command, args []string) {
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
libs = append(libs, fileName)
libs[fileName] = "Unknown Version"
} else {
// I use library.properties file
content, err := ioutil.ReadFile(indexFile)
Expand All @@ -500,7 +500,7 @@ func executeListCommand(command *cobra.Command, args []string) {
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
libs = append(libs, fileName)
libs[fileName] = "Unknown Version"
continue
}

Expand All @@ -516,7 +516,7 @@ func executeListCommand(command *cobra.Command, args []string) {
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
libs = append(libs, fileName)
libs[fileName] = "Unknown Version"
continue
}
Version, ok := ini.Get("version")
Expand All @@ -526,21 +526,18 @@ func executeListCommand(command *cobra.Command, args []string) {
fileName = strings.Replace(fileName, "_", " ", -1)
fileName = strings.Replace(fileName, "-", " v. ", -1)
//I use folder name
libs = append(libs, fileName)
libs[fileName] = "Unknown Version"
continue
}
libs = append(libs, fmt.Sprintf("%-10s v. %s", Name, Version))
libs[Name] = fmt.Sprintf("v.%s", Version)
}
}
}

if len(libs) < 1 {
formatter.Print("No library installed")
} else {
//pretty prints installed libraries
for _, item := range libs {
formatter.Print(item)
}
formatter.Print(output.LibResultsFromMap(libs))
}
}

Expand Down
11 changes: 5 additions & 6 deletions common/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ import (
"path/filepath"
"runtime"

"github.com/sirupsen/logrus"

"github.com/bcmi-labs/arduino-cli/cmd/formatter"
pb "gopkg.in/cheggaaa/pb.v1"
)

Expand Down Expand Up @@ -89,14 +88,14 @@ func GetDefaultArduinoHomeFolder() (string, error) {
func GetFolder(folder string, messageName string) (string, error) {
_, err := os.Stat(folder)
if os.IsNotExist(err) {
logrus.Infof("Cannot find default %s folder, attemping to create it ...", messageName)
formatter.Print(fmt.Sprintf("Cannot find default %s folder, attemping to create it ...", messageName))
err = os.MkdirAll(folder, 0755)
if err != nil {
logrus.Infoln("ERROR")
logrus.Infof("Cannot create %s folder\n", messageName)
formatter.Print("ERROR")
formatter.PrintErrorMessage(fmt.Sprintf("Cannot create %s folder\n", messageName))
return "", err
}
logrus.Infoln("OK")
formatter.Print("OK")
}
return folder, nil
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ func getDownloadCacheFolder() (string, error) {
return "", err
}

stagingFolder := filepath.Join(libFolder, "staging")
stagingFolder := filepath.Join(libFolder, "staging", "libraries")
return common.GetFolder(stagingFolder, "libraries cache")
}
12 changes: 0 additions & 12 deletions libraries/install_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,3 @@ func prepareInstall(library *Library, body []byte, version string) (*zip.Reader,
}
return archive, nil
}

// getLibFolder returns the destination folder of the downloaded specified library.
// It creates the folder if does not find it.
func getLibFolder(library *Library) (string, error) {
baseFolder, err := common.GetDefaultLibFolder()
if err != nil {
return "", err
}

libFolder := filepath.Join(baseFolder, fmt.Sprintf("%s-%s", library.Name, library.Latest().Version))
return common.GetFolder(libFolder, "library")
}

0 comments on commit 43edad7

Please sign in to comment.