Skip to content

Commit 43edad7

Browse files
committed
Realigning with the Java IDE and fixing lib list error
1 parent f1f5533 commit 43edad7

File tree

5 files changed

+114
-29
lines changed

5 files changed

+114
-29
lines changed

cmd/arduino_core.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@
3030
package cmd
3131

3232
import (
33+
"fmt"
34+
"io/ioutil"
35+
"os"
36+
"path/filepath"
37+
"strings"
38+
39+
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
40+
"github.com/bcmi-labs/arduino-cli/common"
3341
"github.com/spf13/cobra"
42+
"github.com/zieckey/goini"
3443
)
3544

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

5564
func executeCoreListCommand(cmd *cobra.Command, args []string) {
65+
if arduinoLibFlags.updateIndex {
66+
execUpdateListIndex(cmd, args)
67+
return
68+
}
69+
70+
libHome, err := common.GetDefaultLibFolder()
71+
if err != nil {
72+
formatter.Print("Cannot get libraries folder")
73+
return
74+
}
75+
76+
//prettyPrints.corestatus(status)
77+
dir, err := os.Open(libHome)
78+
if err != nil {
79+
formatter.Print("Cannot open libraries folder")
80+
return
81+
}
82+
83+
dirFiles, err := dir.Readdir(0)
84+
if err != nil {
85+
formatter.Print("Cannot read into libraries folder")
86+
return
87+
}
88+
89+
cores := make([]string, 0, 10)
90+
91+
//TODO: optimize this algorithm
92+
// time complexity O(libraries_to_install(from RAM) *
93+
// library_folder_number(from DISK) *
94+
// library_folder_file_number (from DISK))
95+
//TODO : remove only one version
96+
for _, file := range dirFiles {
97+
if file.IsDir() {
98+
indexFile := filepath.Join(libHome, file.Name(), "library.properties")
99+
_, err = os.Stat(indexFile)
100+
if os.IsNotExist(err) {
101+
fileName := file.Name()
102+
//replacing underscore in foldernames with spaces.
103+
fileName = strings.Replace(fileName, "_", " ", -1)
104+
fileName = strings.Replace(fileName, "-", " v. ", -1)
105+
//I use folder name
106+
cores = append(cores, fileName)
107+
} else {
108+
// I use library.properties file
109+
content, err := ioutil.ReadFile(indexFile)
110+
if err != nil {
111+
fileName := file.Name()
112+
//replacing underscore in foldernames with spaces.
113+
fileName = strings.Replace(fileName, "_", " ", -1)
114+
fileName = strings.Replace(fileName, "-", " v. ", -1)
115+
//I use folder name
116+
cores = append(cores, fileName)
117+
continue
118+
}
119+
120+
ini := goini.New()
121+
err = ini.Parse(content, "\n", "=")
122+
if err != nil {
123+
formatter.Print(err)
124+
}
125+
Name, ok := ini.Get("name")
126+
if !ok {
127+
fileName := file.Name()
128+
//replacing underscore in foldernames with spaces.
129+
fileName = strings.Replace(fileName, "_", " ", -1)
130+
fileName = strings.Replace(fileName, "-", " v. ", -1)
131+
//I use folder name
132+
cores = append(cores, fileName)
133+
continue
134+
}
135+
Version, ok := ini.Get("version")
136+
if !ok {
137+
fileName := file.Name()
138+
//replacing underscore in foldernames with spaces.
139+
fileName = strings.Replace(fileName, "_", " ", -1)
140+
fileName = strings.Replace(fileName, "-", " v. ", -1)
141+
//I use folder name
142+
cores = append(cores, fileName)
143+
continue
144+
}
145+
cores = append(cores, fmt.Sprintf("%-10s v. %s", Name, Version))
146+
}
147+
}
148+
}
56149

150+
if len(cores) < 1 {
151+
formatter.Print("No core installed")
152+
} else {
153+
//pretty prints installed libraries
154+
for _, item := range cores {
155+
formatter.Print(item)
156+
}
157+
}
57158
}

cmd/arduino_lib.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func executeListCommand(command *cobra.Command, args []string) {
473473
return
474474
}
475475

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

478478
//TODO: optimize this algorithm
479479
// time complexity O(libraries_to_install(from RAM) *
@@ -490,7 +490,7 @@ func executeListCommand(command *cobra.Command, args []string) {
490490
fileName = strings.Replace(fileName, "_", " ", -1)
491491
fileName = strings.Replace(fileName, "-", " v. ", -1)
492492
//I use folder name
493-
libs = append(libs, fileName)
493+
libs[fileName] = "Unknown Version"
494494
} else {
495495
// I use library.properties file
496496
content, err := ioutil.ReadFile(indexFile)
@@ -500,7 +500,7 @@ func executeListCommand(command *cobra.Command, args []string) {
500500
fileName = strings.Replace(fileName, "_", " ", -1)
501501
fileName = strings.Replace(fileName, "-", " v. ", -1)
502502
//I use folder name
503-
libs = append(libs, fileName)
503+
libs[fileName] = "Unknown Version"
504504
continue
505505
}
506506

@@ -516,7 +516,7 @@ func executeListCommand(command *cobra.Command, args []string) {
516516
fileName = strings.Replace(fileName, "_", " ", -1)
517517
fileName = strings.Replace(fileName, "-", " v. ", -1)
518518
//I use folder name
519-
libs = append(libs, fileName)
519+
libs[fileName] = "Unknown Version"
520520
continue
521521
}
522522
Version, ok := ini.Get("version")
@@ -526,21 +526,18 @@ func executeListCommand(command *cobra.Command, args []string) {
526526
fileName = strings.Replace(fileName, "_", " ", -1)
527527
fileName = strings.Replace(fileName, "-", " v. ", -1)
528528
//I use folder name
529-
libs = append(libs, fileName)
529+
libs[fileName] = "Unknown Version"
530530
continue
531531
}
532-
libs = append(libs, fmt.Sprintf("%-10s v. %s", Name, Version))
532+
libs[Name] = fmt.Sprintf("v.%s", Version)
533533
}
534534
}
535535
}
536536

537537
if len(libs) < 1 {
538538
formatter.Print("No library installed")
539539
} else {
540-
//pretty prints installed libraries
541-
for _, item := range libs {
542-
formatter.Print(item)
543-
}
540+
formatter.Print(output.LibResultsFromMap(libs))
544541
}
545542
}
546543

common/functions.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ import (
3939
"path/filepath"
4040
"runtime"
4141

42-
"github.com/sirupsen/logrus"
43-
42+
"github.com/bcmi-labs/arduino-cli/cmd/formatter"
4443
pb "gopkg.in/cheggaaa/pb.v1"
4544
)
4645

@@ -89,14 +88,14 @@ func GetDefaultArduinoHomeFolder() (string, error) {
8988
func GetFolder(folder string, messageName string) (string, error) {
9089
_, err := os.Stat(folder)
9190
if os.IsNotExist(err) {
92-
logrus.Infof("Cannot find default %s folder, attemping to create it ...", messageName)
91+
formatter.Print(fmt.Sprintf("Cannot find default %s folder, attemping to create it ...", messageName))
9392
err = os.MkdirAll(folder, 0755)
9493
if err != nil {
95-
logrus.Infoln("ERROR")
96-
logrus.Infof("Cannot create %s folder\n", messageName)
94+
formatter.Print("ERROR")
95+
formatter.PrintErrorMessage(fmt.Sprintf("Cannot create %s folder\n", messageName))
9796
return "", err
9897
}
99-
logrus.Infoln("OK")
98+
formatter.Print("OK")
10099
}
101100
return folder, nil
102101
}

libraries/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ func getDownloadCacheFolder() (string, error) {
125125
return "", err
126126
}
127127

128-
stagingFolder := filepath.Join(libFolder, "staging")
128+
stagingFolder := filepath.Join(libFolder, "staging", "libraries")
129129
return common.GetFolder(stagingFolder, "libraries cache")
130130
}

libraries/install_uninstall.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,3 @@ func prepareInstall(library *Library, body []byte, version string) (*zip.Reader,
150150
}
151151
return archive, nil
152152
}
153-
154-
// getLibFolder returns the destination folder of the downloaded specified library.
155-
// It creates the folder if does not find it.
156-
func getLibFolder(library *Library) (string, error) {
157-
baseFolder, err := common.GetDefaultLibFolder()
158-
if err != nil {
159-
return "", err
160-
}
161-
162-
libFolder := filepath.Join(baseFolder, fmt.Sprintf("%s-%s", library.Name, library.Latest().Version))
163-
return common.GetFolder(libFolder, "library")
164-
}

0 commit comments

Comments
 (0)