-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpackageindex.go
160 lines (127 loc) · 5.02 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
// 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 version 3,
// which 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 (
"fmt"
"strings"
clipackageindex "github.com/arduino/arduino-cli/arduino/cores/packageindex"
"github.com/arduino/arduino-lint/internal/project/packageindex"
)
// 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
packageIndexTools = nil
packageIndexSystems = nil
if packageIndexLoadError == nil {
packageIndexPackages = getPackageIndexData(PackageIndex(), "", "packages", "", "name", "")
for _, packageData := range PackageIndexPackages() {
packageIndexPlatforms = append(packageIndexPlatforms, getPackageIndexData(packageData.Object, packageData.JSONPointer, "platforms", packageData.ID+":", "architecture", "version")...)
}
for _, packageData := range PackageIndexPackages() {
packageIndexTools = append(packageIndexTools, getPackageIndexData(packageData.Object, packageData.JSONPointer, "tools", packageData.ID+":", "name", "version")...)
}
for _, toolData := range PackageIndexTools() {
packageIndexSystems = append(packageIndexSystems, getPackageIndexData(toolData.Object, toolData.JSONPointer, "systems", toolData.ID+" - ", "host", "")...)
}
}
}
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 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
}
func getPackageIndexData(interfaceObject map[string]interface{}, pointerPrefix string, dataKey string, iDPrefix string, iDKey string, versionKey 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 {
if iDPrefix != "" && strings.HasPrefix(iDPrefix, pointerPrefix) {
// Parent object uses fallback ID, so this one must even if it was possible to generate a true suffix.
return interfaceElementData.JSONPointer
}
iD := iDPrefix
iDSuffix, ok := object[iDKey].(string)
if !ok {
// Use fallback ID.
return interfaceElementData.JSONPointer
}
iD += iDSuffix
if versionKey != "" {
iDVersion, ok := object[versionKey].(string)
if !ok {
// Use fallback ID.
return interfaceElementData.JSONPointer
}
iD += "@" + iDVersion
}
return iD
}
interfaceElementData.ID = objectID()
data = append(data, interfaceElementData)
}
return data
}