Skip to content

Added 'lib examples' command #905

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added 'examples' field in rpc.Library
  • Loading branch information
cmaglie committed Aug 21, 2020
commit 730b5a7c29cb4130c0960537bae0fe75e18f31b7
1 change: 1 addition & 0 deletions arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Library struct {
Version *semver.Version
License string
Properties *properties.Map
Examples paths.PathList
}

func (library *Library) String() string {
Expand Down
54 changes: 54 additions & 0 deletions arduino/libraries/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
semver "go.bug.st/relaxed-semver"
)

Expand Down Expand Up @@ -94,6 +95,9 @@ func makeNewLibrary(libraryDir *paths.Path, location LibraryLocation) (*Library,
library.Version = v
}

if err := addExamples(library); err != nil {
return nil, errors.Errorf("scanning examples: %s", err)
}
library.Name = libraryDir.Base()
library.RealName = strings.TrimSpace(libProperties.Get("name"))
library.Author = strings.TrimSpace(libProperties.Get("author"))
Expand Down Expand Up @@ -122,6 +126,56 @@ func makeLegacyLibrary(path *paths.Path, location LibraryLocation) (*Library, er
IsLegacy: true,
Version: semver.MustParse(""),
}
if err := addExamples(library); err != nil {
return nil, errors.Errorf("scanning examples: %s", err)
}
addUtilityDirectory(library)
return library, nil
}

func addExamples(lib *Library) error {
files, err := lib.InstallDir.ReadDir()
if err != nil {
return err
}
examples := paths.NewPathList()
for _, file := range files {
name := strings.ToLower(file.Base())
if name != "example" && name != "examples" {
continue
}
if !file.IsDir() {
continue
}
if err := addExamplesToPathList(file, &examples); err != nil {
return err
}
break
}

lib.Examples = examples
return nil
}

func addExamplesToPathList(examplesPath *paths.Path, list *paths.PathList) error {
files, err := examplesPath.ReadDir()
if err != nil {
return err
}
for _, file := range files {
if isExample(file) {
list.Add(file)
} else if file.IsDir() {
if err := addExamplesToPathList(file, list); err != nil {
return err
}
}
}
return nil
}

// isExample returns true if examplePath contains an example
func isExample(examplePath *paths.Path) bool {
mainIno := examplePath.Join(examplePath.Base() + ".ino")
return mainIno.Exist() && mainIno.IsNotDir()
}
1 change: 1 addition & 0 deletions commands/lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func GetOutputLibrary(lib *libraries.Library) *rpc.Library {
IsLegacy: lib.IsLegacy,
Version: lib.Version.String(),
License: lib.License,
Examples: lib.Examples.AsStrings(),
}
}

Expand Down
54 changes: 32 additions & 22 deletions rpc/commands/lib.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/commands/lib.proto
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ message Library {
LibraryLocation location = 24;
// The library format type.
LibraryLayout layout = 25;
// The example sketches provided by the library
repeated string examples = 26;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider moving the examples from Library to LibraryRelease.

For me, a Library is the unique identifier (with its name) and it has 1..* LibraryReleases.

A Library can have, for example, two releases, 1.0.0 and 1.1.0.

The 1.0.0 version can have two examples:

[
  '/path/to/example/A',
  '/path/to/example/B'
]

And the latest, 1.1.0 LibraryRelease version can have an update, so it has comes with three examples:

[
  '/path/to/example/A',
  '/path/to/example/B',
  '/path/to/example/NewFeatureFrom_1_1_0',
]

Possible related: #928

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for merging this PR as-is; we can handle #928 later together with the currently unnecessary instance.

}

enum LibraryLayout {
Expand Down