-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpackageindex.go
214 lines (169 loc) · 8.25 KB
/
packageindex.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
// This file is part of Arduino Lint.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License, either
// version 3 of the License, or (at your option) any later version.
// This license covers the main part of Arduino Lint.
// 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 projectdata
import (
"bytes"
"fmt"
"text/template"
clipackageindex "github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-lint/internal/project/packageindex"
"github.com/arduino/arduino-lint/internal/rule/schema"
"github.com/arduino/arduino-lint/internal/rule/schema/compliancelevel"
)
// PackageIndexData is the type for package index data.
type PackageIndexData struct {
ID string // Identifier for display to humans
JSONPointer string // Path to the data in the JSON document
Object map[string]interface{} // The data of the object
}
// InitializeForPackageIndex gathers the package index rule data for the specified project.
func InitializeForPackageIndex() {
packageIndex, packageIndexLoadError = packageindex.Properties(ProjectPath())
if ProjectPath() != nil {
_, packageIndexCLILoadError = clipackageindex.LoadIndex(ProjectPath())
}
packageIndexPackages = nil
packageIndexPlatforms = nil
packageIndexBoards = nil
packageIndexToolsDependencies = nil
packageIndexDiscoveryDependencies = nil
packageIndexMonitorDependencies = nil
packageIndexTools = nil
packageIndexSystems = nil
packageIndexSchemaValidationResult = nil
if packageIndexLoadError == nil {
packageIndexPackages = getPackageIndexData(PackageIndex(), "", "packages", "", "{{index . 0}}", []string{"name"})
for _, packageData := range PackageIndexPackages() {
packageIndexPlatforms = append(packageIndexPlatforms, getPackageIndexData(packageData.Object, packageData.JSONPointer, "platforms", packageData.ID, ":{{index . 0}}@{{index . 1}}", []string{"architecture", "version"})...)
packageIndexTools = append(packageIndexTools, getPackageIndexData(packageData.Object, packageData.JSONPointer, "tools", packageData.ID, ":{{index . 0}}@{{index . 1}}", []string{"name", "version"})...)
}
for _, platformData := range PackageIndexPlatforms() {
packageIndexBoards = append(packageIndexBoards, getPackageIndexData(platformData.Object, platformData.JSONPointer, "boards", platformData.ID, " >> {{index . 0}}", []string{"name"})...)
packageIndexToolsDependencies = append(packageIndexToolsDependencies, getPackageIndexData(platformData.Object, platformData.JSONPointer, "toolsDependencies", platformData.ID, " >> {{index . 0}}:{{index . 1}}@{{index . 2}}", []string{"packager", "name", "version"})...)
packageIndexDiscoveryDependencies = append(packageIndexDiscoveryDependencies, getPackageIndexData(platformData.Object, platformData.JSONPointer, "discoveryDependencies", platformData.ID, " >> {{index . 0}}:{{index . 1}}", []string{"packager", "name"})...)
packageIndexMonitorDependencies = append(packageIndexMonitorDependencies, getPackageIndexData(platformData.Object, platformData.JSONPointer, "monitorDependencies", platformData.ID, " >> {{index . 0}}:{{index . 1}}", []string{"packager", "name"})...)
}
for _, toolData := range PackageIndexTools() {
packageIndexSystems = append(packageIndexSystems, getPackageIndexData(toolData.Object, toolData.JSONPointer, "systems", toolData.ID, " >> {{index . 0}}", []string{"host"})...)
}
packageIndexSchemaValidationResult = packageindex.Validate(PackageIndex())
}
}
var packageIndex map[string]interface{}
// PackageIndex returns the package index data.
func PackageIndex() map[string]interface{} {
return packageIndex
}
var packageIndexLoadError error
// PackageIndexLoadError returns the error from loading the package index.
func PackageIndexLoadError() error {
return packageIndexLoadError
}
var packageIndexCLILoadError error
// PackageIndexCLILoadError returns the error return of Arduino CLI's packageindex.LoadIndex().
func PackageIndexCLILoadError() error {
return packageIndexCLILoadError
}
var packageIndexPackages []PackageIndexData
// PackageIndexPackages returns the slice of package data for the package index.
func PackageIndexPackages() []PackageIndexData {
return packageIndexPackages
}
var packageIndexPlatforms []PackageIndexData
// PackageIndexPlatforms returns the slice of platform data for the package index.
func PackageIndexPlatforms() []PackageIndexData {
return packageIndexPlatforms
}
var packageIndexBoards []PackageIndexData
// PackageIndexBoards returns the slice of board data for the package index.
func PackageIndexBoards() []PackageIndexData {
return packageIndexBoards
}
var packageIndexToolsDependencies []PackageIndexData
// PackageIndexToolsDependencies returns the slice of tool dependency data for the package index.
func PackageIndexToolsDependencies() []PackageIndexData {
return packageIndexToolsDependencies
}
var packageIndexDiscoveryDependencies []PackageIndexData
// PackageIndexDiscoveryDependencies returns the slice of pluggable discovery tool dependency data for the package index.
func PackageIndexDiscoveryDependencies() []PackageIndexData {
return packageIndexDiscoveryDependencies
}
var packageIndexMonitorDependencies []PackageIndexData
// PackageIndexMonitorDependencies returns the slice of pluggable monitor tool dependency data for the package index.
func PackageIndexMonitorDependencies() []PackageIndexData {
return packageIndexMonitorDependencies
}
var packageIndexTools []PackageIndexData
// PackageIndexTools returns the slice of tool data for the package index.
func PackageIndexTools() []PackageIndexData {
return packageIndexTools
}
var packageIndexSystems []PackageIndexData
// PackageIndexSystems returns the slice of system data for the package index.
func PackageIndexSystems() []PackageIndexData {
return packageIndexSystems
}
var packageIndexSchemaValidationResult map[compliancelevel.Type]schema.ValidationResult
// PackageIndexSchemaValidationResult returns the result of validating the package index against the JSON schema.
func PackageIndexSchemaValidationResult() map[compliancelevel.Type]schema.ValidationResult {
return packageIndexSchemaValidationResult
}
func getPackageIndexData(interfaceObject map[string]interface{}, pointerPrefix string, dataKey string, iDPrefix string, iDSuffixTemplateString string, iDSuffixKeys []string) []PackageIndexData {
var data []PackageIndexData
interfaceSlice, ok := interfaceObject[dataKey].([]interface{})
if !ok {
return data
}
for index, interfaceElement := range interfaceSlice {
interfaceElementData := PackageIndexData{
JSONPointer: fmt.Sprintf("%s/%s/%v", pointerPrefix, dataKey, index),
Object: nil,
}
object, ok := interfaceElement.(map[string]interface{})
if ok {
interfaceElementData.Object = object
}
objectID := func() string {
// In the event missing data prevents creating a standard reference ID for the data, use the JSON pointer.
fallbackID := interfaceElementData.JSONPointer
if iDPrefix != "" && iDPrefix == pointerPrefix {
// Parent object uses fallback ID, so this one must even if it was possible to generate a true suffix.
return fallbackID
}
// Gather the ID suffix components.
iDSuffixComponents := []string{}
for _, key := range iDSuffixKeys {
component, ok := object[key].(string)
if !ok {
return fallbackID
}
if component == "" {
return fallbackID
}
iDSuffixComponents = append(iDSuffixComponents, component)
}
// Fill the ID suffix components into the template.
iDSuffixTemplate := template.Must(template.New("iDSuffixTemplate").Parse(iDSuffixTemplateString))
iDSuffix := new(bytes.Buffer)
iDSuffixTemplate.Execute(iDSuffix, iDSuffixComponents)
return iDPrefix + iDSuffix.String()
}
interfaceElementData.ID = objectID()
data = append(data, interfaceElementData)
}
return data
}