Skip to content

Commit 455001d

Browse files
committed
Use Arduino CLI infrastructure for querying library index
In the past, I have found that using Arduino CLI infrastructure for handling projects is not always the right choice because Arduino CLI is intended to be resilient to sub-ideal project configurations when feasible, whereas Arduino Lint is intended to enforce or encourage ideal project configurations. However, Arduino Lint is not intended to lint the Library Manager index, so using Arduino CLI's code for querying the index should be safe and avoid the problems associated with the code duplication that would be the alternative.
1 parent 45b32ff commit 455001d

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

internal/project/projectdata/library.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,20 @@
1616
package projectdata
1717

1818
import (
19-
"encoding/json"
20-
"io/ioutil"
19+
"io"
2120
"net/http"
2221
"os"
2322

2423
"github.com/arduino/arduino-cli/arduino/libraries"
24+
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
2525
"github.com/arduino/arduino-lint/internal/configuration"
2626
"github.com/arduino/arduino-lint/internal/configuration/rulemode"
2727
"github.com/arduino/arduino-lint/internal/project"
2828
"github.com/arduino/arduino-lint/internal/project/library/libraryproperties"
2929
"github.com/arduino/arduino-lint/internal/result/feedback"
3030
"github.com/arduino/arduino-lint/internal/rule/schema"
3131
"github.com/arduino/arduino-lint/internal/rule/schema/compliancelevel"
32+
"github.com/arduino/go-paths-helper"
3233
"github.com/arduino/go-properties-orderedmap"
3334
"github.com/client9/misspell"
3435
"github.com/sirupsen/logrus"
@@ -60,23 +61,34 @@ func InitializeForLibrary(project project.Type) {
6061

6162
// Download the Library Manager index if needed.
6263
if !configuration.RuleModes(project.SuperprojectType)[rulemode.LibraryManagerIndexing] && libraryManagerIndex == nil {
63-
url := "http://downloads.arduino.cc/libraries/library_index.json"
64-
httpResponse, err := http.Get(url)
64+
// Set up the temporary folder for the index
65+
libraryIndexFolderPath, err := paths.TempDir().MkTempDir("arduino-lint-library-index-folder")
66+
defer libraryIndexFolderPath.RemoveAll()
6567
if err != nil {
66-
feedback.Errorf("Unable to download Library Manager index from %s: %s", err, url)
68+
panic(err)
69+
}
70+
libraryIndexPath := libraryIndexFolderPath.Join("library_index.json")
71+
72+
// Download the index data
73+
httpResponse, err := http.Get(librariesmanager.LibraryIndexURL.String())
74+
if err != nil {
75+
feedback.Errorf("Unable to download Library Manager index from %s: %s", err, librariesmanager.LibraryIndexURL)
6776
os.Exit(1)
6877
}
6978
defer httpResponse.Body.Close()
7079

71-
bytes, err := ioutil.ReadAll(httpResponse.Body)
80+
// Write the index data to file
81+
libraryIndexFile, err := libraryIndexPath.Create()
82+
defer libraryIndexFile.Close()
7283
if err != nil {
7384
panic(err)
7485
}
75-
76-
err = json.Unmarshal(bytes, &libraryManagerIndex)
77-
if err != nil {
86+
if _, err := io.Copy(libraryIndexFile, httpResponse.Body); err != nil {
7887
panic(err)
7988
}
89+
90+
libraryManagerIndex = librariesmanager.NewLibraryManager(libraryIndexFolderPath, nil)
91+
libraryManagerIndex.LoadIndex()
8092
}
8193

8294
if misspelledWordsReplacer == nil { // The replacer only needs to be compiled once per run.
@@ -120,10 +132,10 @@ func SourceHeaders() []string {
120132
return sourceHeaders
121133
}
122134

123-
var libraryManagerIndex map[string]interface{}
135+
var libraryManagerIndex *librariesmanager.LibrariesManager
124136

125137
// LibraryManagerIndex returns the Library Manager index data.
126-
func LibraryManagerIndex() map[string]interface{} {
138+
func LibraryManagerIndex() *librariesmanager.LibrariesManager {
127139
return libraryManagerIndex
128140
}
129141

internal/rule/rulefunction/library.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -1461,15 +1461,8 @@ func IncorrectExamplesFolderNameCase() (result ruleresult.Type, output string) {
14611461

14621462
// nameInLibraryManagerIndex returns whether there is a library in Library Manager index using the given name.
14631463
func nameInLibraryManagerIndex(name string) bool {
1464-
libraries := projectdata.LibraryManagerIndex()["libraries"].([]interface{})
1465-
for _, libraryInterface := range libraries {
1466-
library := libraryInterface.(map[string]interface{})
1467-
if library["name"].(string) == name {
1468-
return true
1469-
}
1470-
}
1471-
1472-
return false
1464+
library := projectdata.LibraryManagerIndex().Index.FindIndexedLibrary(&libraries.Library{Name: name})
1465+
return library != nil
14731466
}
14741467

14751468
// spellCheckLibraryPropertiesFieldValue returns the value of the provided library.properties field with commonly misspelled words corrected.

0 commit comments

Comments
 (0)