Skip to content

Commit a1e4e02

Browse files
committed
adding headers
1 parent 055890c commit a1e4e02

File tree

6 files changed

+187
-59
lines changed

6 files changed

+187
-59
lines changed

common/interfaces.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
130
package common
231

332
// Index represents a generic index parsed from an index file.

cores/cores.go

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"fmt"
3434
"strings"
3535

36-
"github.com/pmylund/sortutil"
36+
"github.com/blang/semver"
3737
)
3838

3939
// Core represents a core package.
@@ -60,20 +60,23 @@ func (core *Core) GetVersion(version string) *Release {
6060
}
6161

6262
// Versions returns all the version numbers in this Core Package.
63-
func (core *Core) Versions() []string {
64-
versions := make([]string, len(core.Releases))
65-
i := 0
66-
for version := range core.Releases {
67-
versions[i] = version
68-
i++
63+
func (core *Core) Versions() semver.Versions {
64+
versions := make(semver.Versions, 0, len(core.Releases))
65+
66+
for _, release := range core.Releases {
67+
temp, err := semver.Make(release.Version)
68+
if err == nil {
69+
versions = append(versions, temp)
70+
}
6971
}
70-
sortutil.CiAsc(versions)
72+
7173
return versions
7274
}
7375

7476
// Latest obtains latest version of a core package.
7577
func (core *Core) Latest() *Release {
76-
return core.GetVersion(core.latestVersion())
78+
latest := core.latestVersion()
79+
return core.GetVersion(latest)
7780
}
7881

7982
// latestVersion obtains latest version number.
@@ -82,35 +85,36 @@ func (core *Core) Latest() *Release {
8285
func (core *Core) latestVersion() string {
8386
versions := core.Versions()
8487
if len(versions) > 0 {
85-
return versions[0]
88+
max := versions[0]
89+
for i := 1; i < len(versions); i++ {
90+
if versions[i].GT(max) {
91+
max = versions[i]
92+
}
93+
}
94+
return fmt.Sprint(max)
8695
}
8796
return ""
8897
}
8998

90-
func (core *Core) String(verbosity int) (res string) {
91-
if verbosity > 0 {
92-
res += fmt.Sprintf("Name : %s\n", core.Name)
93-
res += fmt.Sprintf("Architecture: %s\n", core.Architecture)
94-
res += fmt.Sprintf("Category : %s\n", core.Category)
95-
if verbosity > 1 {
96-
res += "Releases:\n"
97-
for _, release := range core.Releases {
98-
res += fmt.Sprintf("%s\n", release.String())
99-
}
100-
} else {
101-
res += fmt.Sprintf("Releases : %s", core.Versions())
99+
func (core *Core) String() string {
100+
res := fmt.Sprintln("Name :", core.Name)
101+
res += fmt.Sprintln("Architecture:", core.Architecture)
102+
res += fmt.Sprintln("Category :", core.Category)
103+
if core.Releases != nil && len(core.Releases) > 0 {
104+
res += "Releases:\n"
105+
for _, release := range core.Releases {
106+
res += fmt.Sprintln(release)
102107
}
103-
} else {
104-
res = fmt.Sprintf("%s\n", core.Name)
105108
}
106-
return
109+
return res
107110
}
108111

109112
func (release *Release) String() string {
110-
res := fmt.Sprintf("Version : %s\n", release.Version)
111-
res += fmt.Sprintf("Boards : \n%s\n", strings.Join(release.Boards, ", "))
112-
res += fmt.Sprintf("Archive File Name : %s\n", release.ArchiveFileName)
113-
res += fmt.Sprintf("Checksum : %s\n", release.Checksum)
114-
res += fmt.Sprintf("File Size : %d\n", release.Size)
113+
res := fmt.Sprintln(" Version : ", release.Version)
114+
res += fmt.Sprintln(" Boards :")
115+
res += fmt.Sprintln(strings.Join(release.Boards, ", "))
116+
res += fmt.Sprintln(" Archive File Name :", release.ArchiveFileName)
117+
res += fmt.Sprintln(" Checksum :", release.Checksum)
118+
res += fmt.Sprintln(" File Size :", release.Size)
115119
return res
116120
}

cores/download.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
30+
package cores

cores/index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (release *indexCoreRelease) extractBoards() []string {
121121

122122
// LoadPackagesIndex reads a package_index.json from a file and returns
123123
// the corresponding Index structure.
124-
func LoadPackagesIndex() (*Index, error) {
124+
func LoadPackagesIndex() (common.Index, error) {
125125
libFile, err := IndexPath()
126126
if err != nil {
127127
return nil, err
@@ -138,5 +138,5 @@ func LoadPackagesIndex() (*Index, error) {
138138
return nil, err
139139
}
140140

141-
return &index, nil
141+
return index, nil
142142
}

cores/package_manager.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* arduino-cli is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*
18+
* As a special exception, you may use this file as part of a free software
19+
* library without restriction. Specifically, if other files instantiate
20+
* templates or use macros or inline functions from this file, or you compile
21+
* this file and link it with other files to produce an executable, this
22+
* file does not by itself cause the resulting executable to be covered by
23+
* the GNU General Public License. This exception does not however
24+
* invalidate any other reasons why the executable file might be covered by
25+
* the GNU General Public License.
26+
*
27+
* Copyright 2017 BCMI LABS SA (http://www.arduino.cc/)
28+
*/
29+
30+
package cores
31+
32+
import "github.com/pmylund/sortutil"
33+
34+
//Package represents a package in the system.
35+
type Package struct {
36+
Name string
37+
Maintainer string
38+
WebsiteURL string
39+
Email string
40+
Cores map[string]*Core // The cores in the system.
41+
// Tools map[string]*Tool // The tools in the system.
42+
}
43+
44+
// AddCore adds a core to the context.
45+
func (pm *Package) AddCore(indexCore *indexCoreRelease) {
46+
name := indexCore.Name
47+
if pm.Cores[name] == nil {
48+
pm.Cores[name] = indexCore.extractCore()
49+
} else {
50+
release := indexCore.extractRelease()
51+
core := pm.Cores[name]
52+
core.Releases[release.Version] = release
53+
}
54+
}
55+
56+
// Names returns an array with all the names of the registered cores.
57+
func (pm Package) Names() []string {
58+
res := make([]string, len(pm.Cores))
59+
i := 0
60+
for n := range pm.Cores {
61+
res[i] = n
62+
i++
63+
}
64+
sortutil.CiAsc(res)
65+
return res
66+
}

cores/status.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,62 +29,61 @@
2929

3030
package cores
3131

32-
import "github.com/pmylund/sortutil"
32+
import (
33+
"github.com/bcmi-labs/arduino-cli/common"
34+
"github.com/pmylund/sortutil"
35+
)
3336

3437
// StatusContext represents the Context of the Cores and Tools in the system.
3538
type StatusContext struct {
3639
Packages map[string]*Package
3740
}
3841

39-
//Package represents a package in the system.
40-
type Package struct {
41-
Name string
42-
Maintainer string
43-
WebsiteURL string
44-
Email string
45-
Cores map[string]*Core // The cores in the system.
46-
// Tools map[string]*Tool // The tools in the system.
47-
}
48-
4942
// AddPackage adds a package to a context from an indexPackage.
5043
//
5144
// NOTE: If the package is already in the context, it is overwritten!
5245
func (sc *StatusContext) AddPackage(indexPackage *indexPackage) {
5346
sc.Packages[indexPackage.Name] = indexPackage.extractPackage()
5447
}
5548

56-
// AddCore adds a core to the context.
57-
func (pm *Package) AddCore(indexCore *indexCoreRelease) {
58-
name := indexCore.Name
59-
if pm.Cores[name] == nil {
60-
pm.Cores[name] = indexCore.extractCore()
61-
} else {
62-
release := indexCore.extractRelease()
63-
core := pm.Cores[name]
64-
core.Releases[release.Version] = release
49+
// Items returns a map matching core name and core struct.
50+
func (pm Package) Items() map[string]interface{} {
51+
ret := make(map[string]interface{}, len(pm.Cores))
52+
for key, val := range pm.Cores {
53+
ret[key] = val
6554
}
55+
return ret
6656
}
6757

68-
// CoreNames returns an array with all the names of the registered cores.
69-
func (pm *Package) CoreNames() []string {
70-
res := make([]string, len(pm.Cores))
58+
// Names returns the array containing the name of the packages.
59+
func (sc StatusContext) Names() []string {
60+
res := make([]string, len(sc.Packages))
7161
i := 0
72-
for n := range pm.Cores {
62+
for n := range sc.Packages {
7363
res[i] = n
7464
i++
7565
}
7666
sortutil.CiAsc(res)
7767
return res
7868
}
7969

80-
// CreateStatusContextFromIndex creates a status context from index data.
81-
func CreateStatusContextFromIndex(index *Index) (*StatusContext, error) {
70+
// Items returns a map matching core name and core struct.
71+
func (sc StatusContext) Items() map[string]interface{} {
72+
ret := make(map[string]interface{}, len(sc.Packages))
73+
for key, val := range sc.Packages {
74+
ret[key] = val
75+
}
76+
return ret
77+
}
78+
79+
// CreateStatusContext creates a status context from index data.
80+
func (index Index) CreateStatusContext() (common.StatusContext, error) {
8281
// Start with an empty status context
8382
packages := StatusContext{
8483
Packages: make(map[string]*Package, len(index.Packages)),
8584
}
8685
for _, packageManager := range index.Packages {
8786
packages.AddPackage(packageManager)
8887
}
89-
return &packages, nil
88+
return packages, nil
9089
}

0 commit comments

Comments
 (0)