forked from arduino/arduino-cli
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.go
104 lines (92 loc) · 3.35 KB
/
index.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
/*
* 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 librariesindex
import (
"sort"
"github.com/arduino/arduino-cli/arduino/libraries"
"github.com/arduino/arduino-cli/arduino/resources"
semver "go.bug.st/relaxed-semver"
)
// Index represents the list of libraries available for download
type Index struct {
Libraries map[string]*Library `json:"libraries"`
}
// Library is a library available for download
type Library struct {
Name string `json:"name"`
Releases map[string]*Release `json:"releases"`
Latest *Release `json:"-"`
Index *Index `json:"-"`
}
// Release is a release of a library available for download
type Release struct {
Author string `json:"author"`
Version *semver.Version `json:"version"`
Maintainer string `json:"maintainer"`
Sentence string `json:"sentence"`
Paragraph string `json:"paragraph"`
Website string `json:"website"`
Category string `json:"category"`
Architectures []string `json:"architectures"`
Types []string `json:"types"`
Resource *resources.DownloadResource `json:"resource"`
Library *Library `json:"-"`
}
func (r *Release) String() string {
return r.Library.Name + "@" + r.Version.String()
}
// FindRelease search a library Release in the index. Returns nil if the
// release is not found
func (idx *Index) FindRelease(ref *Reference) *Release {
if library, exists := idx.Libraries[ref.Name]; exists {
if ref.Version == nil {
return library.Latest
}
return library.Releases[ref.Version.String()]
}
return nil
}
// FindIndexedLibrary search an indexed library that matches the provided
// installed library or nil if not found
func (idx *Index) FindIndexedLibrary(lib *libraries.Library) *Library {
return idx.Libraries[lib.Name]
}
// FindLibraryUpdate check if an installed library may be updated using
// one of the indexed libraries. This function returns the Release to install
// to update the library if found, otherwise nil is returned.
func (idx *Index) FindLibraryUpdate(lib *libraries.Library) *Release {
indexLib := idx.FindIndexedLibrary(lib)
if indexLib == nil {
return nil
}
if indexLib.Latest.Version.GreaterThan(lib.Version) {
return indexLib.Latest
}
return nil
}
// Versions returns an array of all versions available of the library
func (library *Library) Versions() []*semver.Version {
res := []*semver.Version{}
for version := range library.Releases {
v, err := semver.Parse(version)
if err == nil {
res = append(res, v)
}
}
sort.Sort(semver.List(res))
return res
}