Skip to content

Commit

Permalink
optimized lib stuff, no more maps, using directly output_structs
Browse files Browse the repository at this point in the history
  • Loading branch information
saniales committed Jul 25, 2017
1 parent 3d9bf68 commit 3b2a1b0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 47 deletions.
18 changes: 14 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
"showLog": true
},
{
"name": "Launch LIB LIST",
"name": "Launch LIB LIST JSON",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"env": {},
"args": ["lib", "list"],
"args": ["lib", "list", "--format", "json"],
"showLog": true
},
{
Expand All @@ -41,13 +41,23 @@
"showLog": true
},
{
"name": "Launch LIB INSTALL",
"name": "Launch LIB INSTALL JSON",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"env": {},
"args": ["lib", "install", "YoutubeApi"],
"args": ["lib", "install", "YoutubeApi", "--format", "json"],
"showLog": true
},
{
"name": "Launch LIB UNINSTALL JSON",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/main.go",
"env": {},
"args": ["lib", "uninstall", "YoutubeApi", "--format", "json"],
"showLog": true
},
{
Expand Down
4 changes: 2 additions & 2 deletions cmd/arduino_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func executeCoreListCommand(cmd *cobra.Command, args []string) {
InstalledCores: make([]output.InstalledStuff, 0, 5),
InstalledTools: make([]output.InstalledStuff, 0, 5),
}
getInstalledCores(packageName, &(pkg.InstalledCores))
getInstalledTools(packageName, &(pkg.InstalledTools))
getInstalledCores(packageName, &pkg.InstalledCores)
getInstalledTools(packageName, &pkg.InstalledTools)
pkgs.InstalledPackages = append(pkgs.InstalledPackages, pkg)
}

Expand Down
113 changes: 79 additions & 34 deletions cmd/arduino_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ func executeDownloadCommand(cmd *cobra.Command, args []string) error {
libraryResults := parallelLibDownloads(libs, true, "Downloaded")

for libFail, err := range failed {
libraryResults[libFail] = err
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: libFail,
Error: err.Error(),
})
}

formatter.Print(output.LibResultsFromMap(libraryResults))

formatter.Print(libraryResults)
return nil
}

Expand All @@ -192,15 +194,16 @@ func purgeInvalidLibraries(libnames map[string]string, status libraries.StatusCo
fails := make(map[string]error, len(libnames))

for libraryName, version := range libnames {
library, valid := libnames[libraryName]
_, valid := libnames[libraryName]
if !valid {
fails[libraryName] = errors.New("Library Not Found")
} else {
release := status.Libraries[library].GetVersion(version)
library := status.Libraries[libraryName]
release := library.GetVersion(version)
if release == nil {
fails[libraryName] = errors.New("Version Not Found")
} else { // replaces "latest" with latest version too
items[status.Libraries[library]] = release.Version
items[library] = release.Version
}
}
}
Expand All @@ -212,9 +215,11 @@ func purgeInvalidLibraries(libnames map[string]string, status libraries.StatusCo
//
// forced is used to force download if cached.
// OkStatus is used to tell the overlying process result ("Downloaded", "Installed", etc...)
func parallelLibDownloads(items map[*libraries.Library]string, forced bool, OkStatus string) map[string]interface{} {
func parallelLibDownloads(items map[*libraries.Library]string, forced bool, OkStatus string) output.LibProcessResults {
itemC := len(items)
libraryResults := make(map[string]interface{}, itemC)
libraryResults := output.LibProcessResults{
Libraries: make([]output.LibProcessResult, 0, itemC),
}

tasks := make(map[string]task.Wrapper, len(items))
progressBars := make([]*pb.ProgressBar, 0, len(items))
Expand Down Expand Up @@ -247,9 +252,15 @@ func parallelLibDownloads(items map[*libraries.Library]string, forced bool, OkSt

for libraryName, result := range results {
if result.Error != nil {
libraryResults[libraryName] = result.Error
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: libraryName,
Error: result.Error.Error(),
})
} else {
libraryResults[libraryName] = OkStatus
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: libraryName,
Status: OkStatus,
})
}
}
}
Expand Down Expand Up @@ -283,20 +294,28 @@ func executeInstallCommand(cmd *cobra.Command, args []string) error {
libs, failed := purgeInvalidLibraries(parseLibArgs(args), status)

libraryResults := parallelLibDownloads(libs, false, "Installed")
for fail, reason := range failed {
libraryResults[fail] = reason
for libFail, reason := range failed {
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: libFail,
Error: reason.Error(),
})
}
for library, version := range libs {
err = libraries.InstallLib(library, version)
if err != nil {
libraryResults[library.Name] = err
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: library.Name,
Error: err.Error(),
})
} else {
libraryResults[library.Name] = "Installed"
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: library.Name,
Status: "Installed",
})
}
}

formatter.Print(output.LibResultsFromMap(libraryResults))

formatter.Print(libraryResults)
return nil
}

Expand All @@ -322,18 +341,24 @@ func executeUninstallCommand(cmd *cobra.Command, args []string) error {
return nil
}

libraryResults := make(map[string]interface{}, len(args))
libraryResults := output.LibProcessResults{
Libraries: make([]output.LibProcessResult, 0, 10),
}
for _, arg := range args {
libraryResults[arg] = errors.New("Not Found or Not Installed")
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: arg,
Error: "Not Found or Not Installed",
})
}
//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 {
for _, library := range args {
for _, library := range args {
var result *output.LibProcessResult
for _, file := range dirFiles {
if file.IsDir() {
indexFile := filepath.Join(libFolder, file.Name(), "library.properties")
_, err = os.Stat(indexFile)
Expand All @@ -343,53 +368,73 @@ func executeUninstallCommand(cmd *cobra.Command, args []string) error {
fileName = strings.Replace(fileName, "_", " ", -1)
//I use folder name
if strings.Contains(fileName, library) {
result = &output.LibProcessResult{
LibraryName: library,
}
//found
err = libraries.Uninstall(filepath.Join(libFolder, fileName))
if err != nil {
libraryResults[library] = err
result.Error = err.Error()
libraryResults.Libraries = append(libraryResults.Libraries, *result)
} else {
libraryResults[library] = "Uninstalled"
result.Error = "Uninstalled"
libraryResults.Libraries = append(libraryResults.Libraries, *result)
}
break
}
} else {
} else if err == nil {
// I use library.properties file
content, err := os.OpenFile(indexFile, os.O_RDONLY, 0666)
if err != nil {
libraryResults[library] = err
continue
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: library,
Error: err.Error(),
})
break
}

// create map from content
scanner := bufio.NewScanner(content)
for scanner.Scan() {
lines := strings.SplitN(scanner.Text(), "=", 2)
// NOTE: asserting that if there is a library.properties, there is always the
// name of the library.
if lines[0] == "name" {
if strings.Contains(lines[1], library) {
result = &output.LibProcessResult{
LibraryName: library,
}
//found
err = libraries.Uninstall(filepath.Join(libFolder, file.Name()))
if err != nil {
libraryResults[library] = err
result.Error = err.Error()
libraryResults.Libraries = append(libraryResults.Libraries, *result)
} else {
libraryResults[library] = "Uninstalled"
result.Status = "Uninstalled"
libraryResults.Libraries = append(libraryResults.Libraries, *result)
}
}
break
}
}

if err := scanner.Err(); err != nil {
libraryResults[library] = err
} else if _, ok := libraryResults[library].(error); ok {
libraryResults[library] = errors.New("name not found in library.properties")
err = scanner.Err()
if err != nil {
result.Error = err.Error()
libraryResults.Libraries = append(libraryResults.Libraries, *result)
}
break
}
if result == nil {
libraryResults.Libraries = append(libraryResults.Libraries, output.LibProcessResult{
LibraryName: library,
Error: "\"name\" field not found in library.properties file of the library",
})
}
}
}
}
if len(libraryResults) > 0 {
formatter.Print(output.LibResultsFromMap(libraryResults))
if len(libraryResults.Libraries) > 0 {
formatter.Print(libraryResults)
}

return nil
Expand Down
5 changes: 5 additions & 0 deletions cmd/output/core_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type InstalledPackageList struct {
InstalledPackages []InstalledPackage `json:"packages,required"`
}

//CoreSearchResults represents a result of a search of cores.
type CoreSearchResults struct {
Cores []interface{} `json:"cores,required"`
}

func (is InstalledStuff) String() string {
return fmt.Sprintln(" Name:", is.Name) +
fmt.Sprintln(" Versions:", is.Versions)
Expand Down
8 changes: 4 additions & 4 deletions common/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
package common

import (
"errors"
"fmt"
"os"
"os/user"
Expand All @@ -49,13 +48,14 @@ func GetFolder(folder string, label string, createIfMissing bool) (string, error
err = os.MkdirAll(folder, 0755)
if err != nil {
formatter.Print("ERROR")
formatter.PrintErrorMessage(fmt.Sprintf("Cannot create %s folder\n", label))
formatter.PrintErrorMessage(fmt.Sprintf("Folder %s missing and cannot create it", label))
return "", err
}
formatter.Print("OK")
} else if err != nil {
formatter.PrintErrorMessage(fmt.Sprintf("Cannot get %s folder\n", label))
return "", errors.New("Cannot get folder, it does not exist")
msgFormat := "Cannot get %s folder, it does not exist"
formatter.PrintErrorMessage(fmt.Sprintf(msgFormat, label))
return "", fmt.Errorf(msgFormat, label)
}
return folder, nil
}
Expand Down
6 changes: 3 additions & 3 deletions libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (r Release) ExpectedChecksum() string {
}

// Versions returns an array of all versions available of the library
func (l *Library) Versions() semver.Versions {
func (l Library) Versions() semver.Versions {
res := make(semver.Versions, len(l.Releases))
i := 0
for version := range l.Releases {
Expand All @@ -204,15 +204,15 @@ func (l *Library) Versions() semver.Versions {
// nil if not found.
//
// If version == "latest" then release.Version contains the latest version.
func (l *Library) GetVersion(version string) *Release {
func (l Library) GetVersion(version string) *Release {
if version == "latest" {
return l.Releases[l.latestVersion()]
}
return l.Releases[version]
}

// Latest obtains the latest version of a library.
func (l *Library) Latest() *Release {
func (l Library) Latest() *Release {
return l.GetVersion(l.latestVersion())
}

Expand Down

0 comments on commit 3b2a1b0

Please sign in to comment.