Skip to content

Commit 394dce7

Browse files
committed
Inital import
0 parents  commit 394dce7

File tree

8 files changed

+384
-0
lines changed

8 files changed

+384
-0
lines changed

build/build.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package build
2+
3+
import "github.com/arduino/arduino-cli/opts"
4+
5+
type options struct {
6+
GetPrefs []string `long:"get-pref" description:"Prints the value of the given preference to the standard output stream. When the value does not exist, nothing is printed.\nIf no preference is given as parameter, it prints all preferences."`
7+
Board string `long:"board" description:"Select the board to compile for."`
8+
InstallBoards string `long:"install-boards" description:"Fetches available board support (platform) list and install the specified one, along with its related tools."`
9+
InstallLibrary string `long:"install-library" description:"Fetches available libraries list and install the specified one."`
10+
}
11+
12+
/*
13+
func (*options) Usage() string {
14+
return "[--board package:arch:board[:parameters]] [--pref name=value] [-v|--verbose] [--preserve-temp-files] <FILE.ino>"
15+
}
16+
*/
17+
18+
// Options for the build module
19+
var Options options
20+
21+
func init() {
22+
opts.Parser.AddCommand("build", "Build a sketch", "", &Options)
23+
}

config/config.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"os/user"
6+
"path/filepath"
7+
"runtime"
8+
)
9+
10+
// GetDefaultArduinoFolder returns the default data folder for Arduino platform
11+
func GetDefaultArduinoFolder() (string, error) {
12+
usr, err := user.Current()
13+
if err != nil {
14+
return "", err
15+
}
16+
switch runtime.GOOS {
17+
case "linux":
18+
return filepath.Join(usr.HomeDir, ".arduino15"), nil
19+
case "darwin":
20+
return filepath.Join(usr.HomeDir, "Library", "arduino15"), nil
21+
default:
22+
return "", fmt.Errorf("Unsupported OS: %s", runtime.GOOS)
23+
}
24+
}

libraries/index.go

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package libraries
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
)
7+
8+
// Index represents the content of a library_index.json file
9+
type Index struct {
10+
Libraries []IndexRelease
11+
}
12+
13+
// IndexRelease is an entry of a library_index.json
14+
type IndexRelease struct {
15+
Name string
16+
Version string
17+
Author string
18+
Maintainer string
19+
Sentence string
20+
Paragraph string
21+
Website string
22+
Category string
23+
Architectures []string
24+
Types []string
25+
URL string
26+
ArchiveFileName string
27+
Size int
28+
Checksum string
29+
}
30+
31+
// LoadLibrariesIndex reads a library_index.json from a file and returns
32+
// the corresponding LibrariesIndex structure
33+
func LoadLibrariesIndex(libFile string) (*Index, error) {
34+
libBuff, err := ioutil.ReadFile(libFile)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
var index Index
40+
if err := json.Unmarshal(libBuff, &index); err != nil {
41+
return nil, err
42+
}
43+
44+
return &index, nil
45+
}
46+
47+
// ExtractRelease create a new Release with the information contained
48+
// in this index element
49+
func (indexLib *IndexRelease) ExtractRelease() *Release {
50+
return &Release{
51+
Version: indexLib.Version,
52+
URL: indexLib.URL,
53+
ArchiveFileName: indexLib.ArchiveFileName,
54+
Size: indexLib.Size,
55+
Checksum: indexLib.Checksum,
56+
}
57+
}
58+
59+
// ExtractLibrary create a new Library with the information contained
60+
// in this index element
61+
func (indexLib *IndexRelease) ExtractLibrary() *Library {
62+
release := indexLib.ExtractRelease()
63+
return &Library{
64+
Name: indexLib.Name,
65+
Author: indexLib.Author,
66+
Maintainer: indexLib.Maintainer,
67+
Sentence: indexLib.Sentence,
68+
Paragraph: indexLib.Paragraph,
69+
Website: indexLib.Website,
70+
Category: indexLib.Category,
71+
Architectures: indexLib.Architectures,
72+
Types: indexLib.Types,
73+
Releases: []*Release{release},
74+
Latest: release,
75+
}
76+
}

libraries/libraries.go

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package libraries
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
7+
"path/filepath"
8+
9+
"strings"
10+
11+
"github.com/arduino/arduino-cli/config"
12+
"github.com/arduino/arduino-cli/opts"
13+
)
14+
15+
type libsCommand struct {
16+
List libsListCommand `command:"list" description:"List/search available libraries"`
17+
}
18+
19+
/*
20+
func (*libsCommand) Usage() string {
21+
return "[--board package:arch:board[:parameters]] [--pref name=value] [-v|--verbose] [--preserve-temp-files] <FILE.ino>"
22+
}
23+
*/
24+
25+
var options libsCommand
26+
27+
func init() {
28+
opts.Parser.AddCommand("libs", "Manages libraries", "alsdkmaslkdmaslkdm", &options)
29+
}
30+
31+
type libsListCommand struct {
32+
Long []bool `long:"long" short:"l" description:"Output detailed information about libraries (use twice for even more verbose output)"`
33+
}
34+
35+
func (*libsListCommand) Usage() string {
36+
return "[-l] [-l] [<libname>]"
37+
}
38+
39+
func (r *Release) String() string {
40+
res := " Release: " + r.Version + "\n"
41+
res += " URL: " + r.URL + "\n"
42+
res += " ArchiveFileName: " + r.ArchiveFileName + "\n"
43+
res += " Size: " + strconv.Itoa(r.Size) + "\n"
44+
res += " Checksum: " + r.Checksum + "\n"
45+
return res
46+
}
47+
48+
func (l *Library) String() string {
49+
res := "Name: " + l.Name + "\n"
50+
res += " Author: " + l.Author + "\n"
51+
res += " Maintainer: " + l.Maintainer + "\n"
52+
res += " Sentence: " + l.Sentence + "\n"
53+
res += " Paragraph: " + l.Paragraph + "\n"
54+
res += " Website: " + l.Website + "\n"
55+
res += " Category: " + l.Category + "\n"
56+
res += " Architecture: " + strings.Join(l.Architectures, ", ") + "\n"
57+
res += " Types: " + strings.Join(l.Types, ", ") + "\n"
58+
res += " Versions: " + strings.Join(l.Versions(), ", ") + "\n"
59+
return res
60+
}
61+
62+
func (opts *libsListCommand) Execute(args []string) error {
63+
fmt.Println("libs list:", args)
64+
fmt.Println("long =", opts.Long)
65+
66+
baseFolder, err := config.GetDefaultArduinoFolder()
67+
if err != nil {
68+
return fmt.Errorf("Could not determine data folder: %s", err)
69+
}
70+
71+
libFile := filepath.Join(baseFolder, "library_index.json")
72+
index, err := LoadLibrariesIndex(libFile)
73+
if err != nil {
74+
return fmt.Errorf("Could not read library index: %s", err)
75+
}
76+
77+
libraries, err := CreateStatusContextFromIndex(index, nil, nil)
78+
if err != nil {
79+
return fmt.Errorf("Could not synchronize library status: %s", err)
80+
}
81+
82+
for _, name := range libraries.Names() {
83+
if len(opts.Long) > 0 {
84+
lib := libraries.Libraries[name]
85+
fmt.Print(lib)
86+
if len(opts.Long) > 1 {
87+
for _, r := range lib.Releases {
88+
fmt.Print(r)
89+
}
90+
}
91+
fmt.Println()
92+
} else {
93+
fmt.Println(name)
94+
}
95+
}
96+
return nil
97+
}

libraries/status.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package libraries
2+
3+
import "github.com/pmylund/sortutil"
4+
5+
// StatusContext keeps the current status of the libraries in the system
6+
// (the list of libraries, revisions, installed paths, etc.)
7+
type StatusContext struct {
8+
Libraries map[string]*Library
9+
}
10+
11+
// Library represents a library in the system
12+
type Library struct {
13+
Name string
14+
Author string
15+
Maintainer string
16+
Sentence string
17+
Paragraph string
18+
Website string
19+
Category string
20+
Architectures []string
21+
Types []string
22+
Releases []*Release
23+
Installed *Release
24+
Latest *Release
25+
}
26+
27+
// Release represents a release of a library
28+
type Release struct {
29+
Version string
30+
URL string
31+
ArchiveFileName string
32+
Size int
33+
Checksum string
34+
}
35+
36+
// Versions returns an array of all versions available of the library
37+
func (l *Library) Versions() []string {
38+
res := make([]string, len(l.Releases))
39+
i := 0
40+
for _, r := range l.Releases {
41+
res[i] = r.Version
42+
i++
43+
}
44+
sortutil.CiAsc(res)
45+
return res
46+
}
47+
48+
// AddLibrary adds an IndexRelease to the status context
49+
func (l *StatusContext) AddLibrary(indexLib *IndexRelease) {
50+
name := indexLib.Name
51+
if l.Libraries[name] == nil {
52+
l.Libraries[name] = indexLib.ExtractLibrary()
53+
} else {
54+
release := indexLib.ExtractRelease()
55+
lib := l.Libraries[name]
56+
lib.Releases = append(lib.Releases, release)
57+
if lib.Latest.Version < release.Version {
58+
lib.Latest = release
59+
}
60+
}
61+
}
62+
63+
// Names returns an array with all the names of the registered libraries
64+
func (l *StatusContext) Names() []string {
65+
res := make([]string, len(l.Libraries))
66+
i := 0
67+
for n := range l.Libraries {
68+
res[i] = n
69+
i++
70+
}
71+
sortutil.CiAsc(res)
72+
return res
73+
}
74+
75+
func CreateStatusContextFromIndex(index *Index, sketchbookPaths []string, corePaths []string) (*StatusContext, error) {
76+
// Start with an empty status context
77+
libraries := StatusContext{
78+
Libraries: map[string]*Library{},
79+
}
80+
for _, lib := range index.Libraries {
81+
// Add all indexed libraries in the status context
82+
libraries.AddLibrary(&lib)
83+
}
84+
return &libraries, nil
85+
}

main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
_ "github.com/arduino/arduino-cli/build"
8+
_ "github.com/arduino/arduino-cli/libraries"
9+
"github.com/arduino/arduino-cli/opts"
10+
_ "github.com/arduino/arduino-cli/upload"
11+
)
12+
13+
const version = "0.1.0"
14+
15+
type versionOpts struct {
16+
}
17+
18+
func (*versionOpts) Execute(args []string) error {
19+
fmt.Println("arduino-cli " + version)
20+
return nil
21+
}
22+
23+
func main() {
24+
opts.Parser.AddCommand("version", "Prints version info", "", &versionOpts{})
25+
args, err := opts.Parse()
26+
if err != nil {
27+
os.Exit(1)
28+
}
29+
30+
fmt.Println("remaining args: ", args)
31+
}

opts/opts.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package opts
2+
3+
import "github.com/jessevdk/go-flags"
4+
5+
// GeneralOpts is a struct that defines the command line options that are generic for the application
6+
type GeneralOpts struct {
7+
// Slice of bool will append 'true' each time the option is encountered (can be set multiple times, like -vvv)
8+
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information."`
9+
}
10+
11+
// General contains the current settings for the generic command line options
12+
var General GeneralOpts
13+
14+
// Parser is the main parser for the command line options
15+
var Parser *flags.Parser
16+
17+
func init() {
18+
Parser = flags.NewParser(&General, flags.Default)
19+
}
20+
21+
// Parse perform command line parsing
22+
func Parse() ([]string, error) {
23+
args, err := Parser.Parse()
24+
return args, err
25+
}

upload/upload.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package upload
2+
3+
import (
4+
"github.com/arduino/arduino-cli/opts"
5+
)
6+
7+
// Options for the command line parser
8+
type options struct {
9+
Board string `long:"board" description:"Select the board to compile for." optional:"yes"`
10+
}
11+
12+
/*
13+
func (*Options) Usage() string {
14+
return "[--board package:arch:board[:parameters]] [--pref name=value] [-v|--verbose] [--preserve-temp-files] <FILE.ino>"
15+
}
16+
*/
17+
18+
// Options for the build module
19+
var Options options
20+
21+
func init() {
22+
opts.Parser.AddCommand("upload", "Upload a sketch", "", &Options)
23+
}

0 commit comments

Comments
 (0)