forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.go
219 lines (195 loc) · 7.25 KB
/
tools.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
// This file is part of arduino-cli.
//
// Copyright 2020 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 (
"regexp"
"runtime"
"github.com/arduino/arduino-cli/internal/arduino/resources"
"github.com/arduino/go-paths-helper"
properties "github.com/arduino/go-properties-orderedmap"
semver "go.bug.st/relaxed-semver"
)
// Tool represents a single Tool, part of a Package.
type Tool struct {
Name string `json:"name"` // The Name of the Tool.
Releases map[semver.NormalizedString]*ToolRelease `json:"releases"` // Maps Version to Release.
Package *Package `json:"-"`
}
// ToolRelease represents a single release of a tool
type ToolRelease struct {
Version *semver.RelaxedVersion `json:"version"` // The version number of this Release.
Flavors []*Flavor `json:"systems"` // Maps OS to Flavor
Tool *Tool `json:"-"`
InstallDir *paths.Path `json:"-"`
}
// Flavor represents a flavor of a Tool version.
type Flavor struct {
OS string `json:"os"` // The OS Supported by this flavor.
Resource *resources.DownloadResource
}
// GetOrCreateRelease returns the ToolRelease object with the specified version
// or creates a new one if not found
func (tool *Tool) GetOrCreateRelease(version *semver.RelaxedVersion) *ToolRelease {
if release, ok := tool.Releases[version.NormalizedString()]; ok {
return release
}
release := &ToolRelease{
Version: version,
Tool: tool,
}
tool.Releases[version.NormalizedString()] = release
return release
}
// FindReleaseWithRelaxedVersion returns the specified release corresponding the provided version,
// or nil if not found.
func (tool *Tool) FindReleaseWithRelaxedVersion(version *semver.RelaxedVersion) *ToolRelease {
return tool.Releases[version.NormalizedString()]
}
// GetAllReleasesVersions returns all the version numbers in this Core Package.
func (tool *Tool) GetAllReleasesVersions() []*semver.RelaxedVersion {
versions := []*semver.RelaxedVersion{}
for _, release := range tool.Releases {
versions = append(versions, release.Version)
}
return versions
}
// LatestRelease obtains latest version of a core package.
func (tool *Tool) LatestRelease() *ToolRelease {
var latest *ToolRelease
for _, release := range tool.Releases {
if latest == nil || release.Version.GreaterThan(latest.Version) {
latest = release
}
}
return latest
}
// GetLatestInstalled returns the latest installed ToolRelease for the Tool, or nil if no releases are installed.
func (tool *Tool) GetLatestInstalled() *ToolRelease {
var latest *ToolRelease
for _, release := range tool.Releases {
if release.IsInstalled() {
if latest == nil || latest.Version.LessThan(release.Version) {
latest = release
}
}
}
return latest
}
func (tool *Tool) String() string {
return tool.Package.Name + ":" + tool.Name
}
// IsInstalled returns true if the ToolRelease is installed
func (tr *ToolRelease) IsInstalled() bool {
if tr.Tool.Package.Name == "builtin" && tr.InstallDir != nil {
return tr.InstallDir.Join(tr.Tool.Name+".exe").Exist() || tr.InstallDir.Join(tr.Tool.Name).Exist()
}
return tr.InstallDir != nil
}
func (tr *ToolRelease) String() string {
return tr.Tool.String() + "@" + tr.Version.String()
}
// RuntimeProperties returns the runtime properties for this tool
func (tr *ToolRelease) RuntimeProperties() *properties.Map {
res := properties.NewMap()
if tr.IsInstalled() {
res.Set("runtime.tools."+tr.Tool.Name+".path", tr.InstallDir.String())
res.Set("runtime.tools."+tr.Tool.Name+"-"+tr.Version.String()+".path", tr.InstallDir.String())
}
return res
}
var (
regexpLinuxArm = regexp.MustCompile("arm.*-linux-gnueabihf")
regexpLinuxArm64 = regexp.MustCompile("(aarch64|arm64)-linux-gnu")
regexpLinuxRiscv64 = regexp.MustCompile("riscv64-linux-gnu")
regexpLinux64 = regexp.MustCompile("x86_64-.*linux-gnu")
regexpLinux32 = regexp.MustCompile("i[3456]86-.*linux-gnu")
regexpWindows32 = regexp.MustCompile("i[3456]86-.*(mingw32|cygwin)")
regexpWindows64 = regexp.MustCompile("(amd64|x86_64)-.*(mingw32|cygwin)")
regexpMac64 = regexp.MustCompile("x86_64-apple-darwin.*")
regexpMac32 = regexp.MustCompile("i[3456]86-apple-darwin.*")
regexpMacArm64 = regexp.MustCompile("arm64-apple-darwin.*")
regexpFreeBSDArm = regexp.MustCompile("arm.*-freebsd[0-9]*")
regexpFreeBSD32 = regexp.MustCompile("i?[3456]86-freebsd[0-9]*")
regexpFreeBSD64 = regexp.MustCompile("amd64-freebsd[0-9]*")
)
func (f *Flavor) isExactMatchWith(osName, osArch string) bool {
if f.OS == "all" {
return true
}
switch osName + "," + osArch {
case "linux,arm", "linux,armbe":
return regexpLinuxArm.MatchString(f.OS)
case "linux,arm64":
return regexpLinuxArm64.MatchString(f.OS)
case "linux,riscv64":
return regexpLinuxRiscv64.MatchString(f.OS)
case "linux,amd64":
return regexpLinux64.MatchString(f.OS)
case "linux,386":
return regexpLinux32.MatchString(f.OS)
case "windows,386":
return regexpWindows32.MatchString(f.OS)
case "windows,amd64":
return regexpWindows64.MatchString(f.OS)
case "darwin,arm64":
return regexpMacArm64.MatchString(f.OS)
case "darwin,amd64":
return regexpMac64.MatchString(f.OS)
case "darwin,386":
return regexpMac32.MatchString(f.OS)
case "freebsd,arm":
return regexpFreeBSDArm.MatchString(f.OS)
case "freebsd,386":
return regexpFreeBSD32.MatchString(f.OS)
case "freebsd,amd64":
return regexpFreeBSD64.MatchString(f.OS)
}
return false
}
func (f *Flavor) isCompatibleWith(osName, osArch string) (bool, int) {
if f.isExactMatchWith(osName, osArch) {
return true, 1000
}
switch osName + "," + osArch {
case "windows,amd64":
return regexpWindows32.MatchString(f.OS), 10
case "darwin,amd64":
return regexpMac32.MatchString(f.OS), 10
case "darwin,arm64":
// Compatibility guaranteed through Rosetta emulation
if regexpMac64.MatchString(f.OS) {
// Prefer amd64 version if available
return true, 20
}
return regexpMac32.MatchString(f.OS), 10
}
return false, 0
}
// GetCompatibleFlavour returns the downloadable resource compatible with the running O.S.
func (tr *ToolRelease) GetCompatibleFlavour() *resources.DownloadResource {
return tr.GetFlavourCompatibleWith(runtime.GOOS, runtime.GOARCH)
}
// GetFlavourCompatibleWith returns the downloadable resource compatible with the specified O.S.
func (tr *ToolRelease) GetFlavourCompatibleWith(osName, osArch string) *resources.DownloadResource {
var resource *resources.DownloadResource
priority := -1
for _, flavour := range tr.Flavors {
if comp, p := flavour.isCompatibleWith(osName, osArch); comp && p > priority {
resource = flavour.Resource
priority = p
}
}
return resource
}