Skip to content

Pluggable discovery (WIP) #109

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

Closed
wants to merge 12 commits into from
Closed
1 change: 0 additions & 1 deletion Gopkg.lock

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

4 changes: 0 additions & 4 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@
branch = "master"
name = "github.com/bgentry/go-netrc"

[[constraint]]
name = "github.com/codeclysm/cc"
version = "1.2.1"

[[constraint]]
branch = "master"
name = "github.com/mitchellh/go-homedir"
Expand Down
8 changes: 4 additions & 4 deletions arduino/cores/cores.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ package cores
import (
"strings"

"github.com/arduino/go-paths-helper"
paths "github.com/arduino/go-paths-helper"

"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/go-properties-orderedmap"
"go.bug.st/relaxed-semver"
properties "github.com/arduino/go-properties-orderedmap"
semver "go.bug.st/relaxed-semver"
)

// Platform represents a platform package.
Expand Down Expand Up @@ -158,7 +158,7 @@ func (platform *Platform) latestReleaseVersion() *semver.Version {
func (platform *Platform) GetAllInstalled() []*PlatformRelease {
res := []*PlatformRelease{}
for _, release := range platform.Releases {
if release.InstallDir != nil {
if release.IsInstalled() {
res = append(res, release)
}
}
Expand Down
65 changes: 65 additions & 0 deletions arduino/cores/packagemanager/identify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* This file is part of arduino-cli.
*
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
*
* This software is released under the GNU General Public License version 3,
* which covers the main part of arduino-cli.
* The terms of this license can be found at:
* https://www.gnu.org/licenses/gpl-3.0.en.html
*
* You can be released from the requirements of the above licenses by purchasing
* a commercial license. Buying such a license is mandatory if you want to modify or
* otherwise use the software for commercial activities involving the Arduino
* software without disclosing the source code of your own applications. To purchase
* a commercial license, send an email to [email protected].
*/

package packagemanager

import (
"fmt"

"github.com/arduino/arduino-cli/arduino/cores"
properties "github.com/arduino/go-properties-orderedmap"
)

// IdentifyBoard returns a list of baords matching the provided identification properties.
func (pm *PackageManager) IdentifyBoard(idProps *properties.Map) []*cores.Board {
if idProps.Size() == 0 {
return []*cores.Board{}
}

checkSuffix := func(props *properties.Map, s string) (checked bool, found bool) {
for k, v1 := range idProps.AsMap() {
v2, ok := props.GetOk(k + s)
if !ok {
return false, false
}
if v1 != v2 {
return true, false
}
}
return false, true
}

foundBoards := []*cores.Board{}
for _, board := range pm.InstalledBoards() {
if _, found := checkSuffix(board.Properties, ""); found {
foundBoards = append(foundBoards, board)
continue
}
id := 0
for {
again, found := checkSuffix(board.Properties, fmt.Sprintf(".%d", id))
if found {
foundBoards = append(foundBoards, board)
}
if !again {
break
}
id++
}
}
return foundBoards
}
34 changes: 32 additions & 2 deletions arduino/cores/packagemanager/package_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

"github.com/arduino/arduino-cli/arduino/cores"
"github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/go-paths-helper"
paths "github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
"github.com/sirupsen/logrus"
"go.bug.st/relaxed-semver"
semver "go.bug.st/relaxed-semver"
)

// PackageManager defines the superior oracle which understands all about
Expand Down Expand Up @@ -354,6 +354,36 @@ func (pm *PackageManager) GetAllInstalledToolsReleases() []*cores.ToolRelease {
return tools
}

// InstalledPlatformReleases returns all installed PlatformReleases. This function is
// useful to range all PlatformReleases in for loops.
func (pm *PackageManager) InstalledPlatformReleases() []*cores.PlatformRelease {
platforms := []*cores.PlatformRelease{}
for _, targetPackage := range pm.packages.Packages {
for _, platform := range targetPackage.Platforms {
for _, release := range platform.GetAllInstalled() {
platforms = append(platforms, release)
}
}
}
return platforms
}

// InstalledBoards returns all installed Boards. This function is useful to range
// all Boards in for loops.
func (pm *PackageManager) InstalledBoards() []*cores.Board {
boards := []*cores.Board{}
for _, targetPackage := range pm.packages.Packages {
for _, platform := range targetPackage.Platforms {
for _, release := range platform.GetAllInstalled() {
for _, board := range release.Boards {
boards = append(boards, board)
}
}
}
}
return boards
}

func (pm *PackageManager) FindToolsRequiredForBoard(board *cores.Board) ([]*cores.ToolRelease, error) {
pm.Log.Infof("Searching tools required for board %s", board)

Expand Down
152 changes: 152 additions & 0 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//
// This file is part of arduino-cli.
//
// Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to modify or
// otherwise use the software for commercial activities involving the Arduino
// software without disclosing the source code of your own applications. To purchase
// a commercial license, send an email to [email protected].
//

package discovery

import (
"encoding/json"
"fmt"
"io"
"os/exec"
"time"

"github.com/arduino/arduino-cli/arduino/cores/packagemanager"

properties "github.com/arduino/go-properties-orderedmap"

"github.com/arduino/arduino-cli/executils"
)

// Discovery is an instance of a discovery tool
type Discovery struct {
in io.WriteCloser
out io.ReadCloser
outJSON *json.Decoder
cmd *exec.Cmd
Timeout time.Duration
}

// BoardPort is a generic port descriptor
type BoardPort struct {
Address string `json:"address"`
Label string `json:"label"`
Prefs *properties.Map `json:"prefs"`
IdentificationPrefs *properties.Map `json:"identificationPrefs"`
Protocol string `json:"protocol"`
ProtocolLabel string `json:"protocolLabel"`
}

type eventJSON struct {
EventType string `json:"eventType,required"`
Ports []*BoardPort `json:"ports"`
}

// NewFromCommandLine creates a new Discovery object
func NewFromCommandLine(args ...string) (*Discovery, error) {
cmd, err := executils.Command(args)
if err != nil {
return nil, fmt.Errorf("creating discovery process: %s", err)
}
disc := &Discovery{Timeout: time.Second}
disc.cmd = cmd
return disc, nil
}

// Start starts the specified discovery
func (d *Discovery) Start() error {
if in, err := d.cmd.StdinPipe(); err == nil {
d.in = in
} else {
return fmt.Errorf("creating stdin pipe for discovery: %s", err)
}
if out, err := d.cmd.StdoutPipe(); err == nil {
d.out = out
d.outJSON = json.NewDecoder(d.out)
} else {
return fmt.Errorf("creating stdout pipe for discovery: %s", err)
}
if err := d.cmd.Start(); err != nil {
return fmt.Errorf("starting discovery process: %s", err)
}
return nil
}

// List retrieve the port list from this discovery
func (d *Discovery) List() ([]*BoardPort, error) {
if _, err := d.in.Write([]byte("LIST\n")); err != nil {
return nil, fmt.Errorf("sending LIST command to discovery: %s", err)
}
var event eventJSON
done := make(chan bool)
timeout := false
go func() {
select {
case <-done:
case <-time.After(d.Timeout):
timeout = true
d.Close()
}
}()
if err := d.outJSON.Decode(&event); err != nil {
if timeout {
return nil, fmt.Errorf("decoding LIST command: timeout")
}
return nil, fmt.Errorf("decoding LIST command: %s", err)
}
done <- true
return event.Ports, nil
}

// Close stops the Discovery and free the resources
func (d *Discovery) Close() error {
// TODO: Send QUIT for safe close or terminate process after a small timeout
if err := d.in.Close(); err != nil {
return fmt.Errorf("closing stdin pipe: %s", err)
}
if err := d.out.Close(); err != nil {
return fmt.Errorf("closing stdout pipe: %s", err)
}
if d.cmd != nil {
d.cmd.Process.Kill()
}
return nil
}

// ExtractDiscoveriesFromPlatforms returns all Discovery from all the installed platforms.
func ExtractDiscoveriesFromPlatforms(pm *packagemanager.PackageManager) map[string]*Discovery {
res := map[string]*Discovery{}

for _, platformRelease := range pm.InstalledPlatformReleases() {
discoveries := platformRelease.Properties.SubTree("discovery").FirstLevelOf()

for name, props := range discoveries {
if pattern, has := props.GetOk("pattern"); has {
props.Merge(platformRelease.Properties)
cmdLine := props.ExpandPropsInString(pattern)
if cmdArgs, err := properties.SplitQuotedString(cmdLine, `"`, false); err != nil {
// TODO
} else if disc, err := NewFromCommandLine(cmdArgs...); err != nil {
// TODO
} else {
res[name] = disc
}
}
}
}

return res
}
Loading