forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcores.go
228 lines (201 loc) · 6.88 KB
/
cores.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* 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 cores
import (
"strings"
"github.com/arduino/go-paths-helper"
"github.com/arduino/arduino-cli/arduino/resources"
"github.com/arduino/go-properties-orderedmap"
"go.bug.st/relaxed-semver"
)
// Platform represents a platform package.
type Platform struct {
Architecture string // The name of the architecture of this package.
Name string
Category string
Releases map[string]*PlatformRelease // The Releases of this platform, labeled by version.
Package *Package `json:"-"`
}
// PlatformRelease represents a release of a plaform package.
type PlatformRelease struct {
Resource *resources.DownloadResource
Version *semver.Version
BoardsManifest []*BoardManifest
Dependencies ToolDependencies // The Dependency entries to load tools.
Platform *Platform `json:"-"`
Properties *properties.Map `json:"-"`
Boards map[string]*Board `json:"-"`
Programmers map[string]*properties.Map `json:"-"`
Menus *properties.Map `json:"-"`
InstallDir *paths.Path `json:"-"`
}
// BoardManifest contains information about a board. These metadata are usually
// provided by the package_index.json
type BoardManifest struct {
Name string `json:"-"`
ID []*BoardManifestID `json:"-"`
}
// BoardManifestID contains information on how to identify a board. These metadata
// are usually provided by the package_index.json
type BoardManifestID struct {
USB string `json:"-"`
}
// HasUsbID returns true if the BoardManifes contains the specified USB id as
// identification for this board. usbID should be in the format "0000:0000"
func (bm *BoardManifest) HasUsbID(vid, pid string) bool {
usbID := strings.ToLower(vid + ":" + pid)
for _, id := range bm.ID {
if usbID == strings.ToLower(id.USB) {
return true
}
}
return false
}
// ToolDependencies is a set of tool dependency
type ToolDependencies []*ToolDependency
// ToolDependency is a tuple that uniquely identifies a specific version of a Tool
type ToolDependency struct {
ToolName string
ToolVersion *semver.RelaxedVersion
ToolPackager string
}
func (dep *ToolDependency) String() string {
return dep.ToolPackager + ":" + dep.ToolName + "@" + dep.ToolVersion.String()
}
// GetOrCreateRelease returns the specified release corresponding the provided version,
// or creates a new one if not found.
func (platform *Platform) GetOrCreateRelease(version *semver.Version) (*PlatformRelease, error) {
tag := ""
if version != nil {
tag = version.String()
}
if release, ok := platform.Releases[tag]; ok {
return release, nil
}
release := &PlatformRelease{
Version: version,
Boards: map[string]*Board{},
Properties: properties.NewMap(),
Programmers: map[string]*properties.Map{},
Platform: platform,
}
platform.Releases[tag] = release
return release, nil
}
// FindReleaseWithVersion returns the specified release corresponding the provided version,
// or nil if not found.
func (platform *Platform) FindReleaseWithVersion(version *semver.Version) *PlatformRelease {
// use as an fmt.Stringer
return platform.Releases[version.String()]
}
// GetLatestRelease returns the latest release of this platform, or nil if no releases
// are available
func (platform *Platform) GetLatestRelease() *PlatformRelease {
latestVersion := platform.latestReleaseVersion()
if latestVersion == nil {
return nil
}
return platform.FindReleaseWithVersion(latestVersion)
}
// GetAllReleasesVersions returns all the version numbers in this Platform Package.
func (platform *Platform) GetAllReleasesVersions() []*semver.Version {
versions := []*semver.Version{}
for _, release := range platform.Releases {
versions = append(versions, release.Version)
}
return versions
}
// latestReleaseVersion obtains latest version number, or nil if no release available
func (platform *Platform) latestReleaseVersion() *semver.Version {
// TODO: Cache latest version using a field in Platform
versions := platform.GetAllReleasesVersions()
if len(versions) == 0 {
return nil
}
max := versions[0]
for i := 1; i < len(versions); i++ {
if versions[i].GreaterThan(max) {
max = versions[i]
}
}
return max
}
// GetAllInstalled returns all installed PlatformRelease
func (platform *Platform) GetAllInstalled() []*PlatformRelease {
res := []*PlatformRelease{}
for _, release := range platform.Releases {
if release.InstallDir != nil {
res = append(res, release)
}
}
return res
}
func (platform *Platform) String() string {
return platform.Package.Name + ":" + platform.Architecture
}
// GetOrCreateBoard returns the Board object with the specified boardID
// or creates a new one if not found
func (release *PlatformRelease) GetOrCreateBoard(boardID string) *Board {
if board, ok := release.Boards[boardID]; ok {
return board
}
board := &Board{
BoardID: boardID,
Properties: properties.NewMap(),
PlatformRelease: release,
}
release.Boards[boardID] = board
return board
}
// RequiresToolRelease returns true if the PlatformRelease requires the
// toolReleased passed as parameter
func (release *PlatformRelease) RequiresToolRelease(toolRelease *ToolRelease) bool {
for _, toolDep := range release.Dependencies {
if toolDep.ToolName == toolRelease.Tool.Name &&
toolDep.ToolPackager == toolRelease.Tool.Package.Name &&
toolDep.ToolVersion == toolRelease.Version {
return true
}
}
return false
}
// RuntimeProperties returns the runtime properties for this PlatformRelease
func (release *PlatformRelease) RuntimeProperties() *properties.Map {
res := properties.NewMap()
res.Set("runtime.platform.path", release.InstallDir.String())
return res
}
// GetLibrariesDir returns the path to the core libraries or nil if not
// present
func (release *PlatformRelease) GetLibrariesDir() *paths.Path {
libDir := release.InstallDir.Join("libraries")
if libDir.IsDir() {
return libDir
}
return nil
}
// IsInstalled returns true if the PlatformRelease is installed
func (release *PlatformRelease) IsInstalled() bool {
return release.InstallDir != nil
}
func (release *PlatformRelease) String() string {
version := ""
if release.Version != nil {
version = release.Version.String()
}
return release.Platform.String() + "@" + version
}