-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsketch.go
153 lines (125 loc) · 5.04 KB
/
sketch.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
// 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 rulefunction
// The rule functions for sketches.
import (
"strings"
"github.com/arduino/arduino-cli/arduino/globals"
"github.com/arduino/arduino-lint/internal/project/projectdata"
"github.com/arduino/arduino-lint/internal/project/sketch"
"github.com/arduino/arduino-lint/internal/rule/ruleresult"
)
// SketchNameMismatch checks for mismatch between sketch folder name and primary file name.
func SketchNameMismatch() (result ruleresult.Type, output string) {
primarySketchFilePrefix := projectdata.ProjectPath().Base()
directoryListing, err := projectdata.ProjectPath().ReadDir()
if err != nil {
panic(err)
}
directoryListing.FilterOutDirs()
for _, filePath := range directoryListing {
for extension := range globals.MainFileValidExtensions {
if filePath.Base() == primarySketchFilePrefix+extension {
// There was a case-sensitive match (paths package's Exist() is not always case-sensitive, so can't be used here).
return ruleresult.Pass, ""
}
}
}
return ruleresult.Fail, projectdata.ProjectPath().Base() + ".ino"
}
// ProhibitedCharactersInSketchFileName checks for prohibited characters in the sketch file names.
func ProhibitedCharactersInSketchFileName() (result ruleresult.Type, output string) {
directoryListing, _ := projectdata.ProjectPath().ReadDir()
directoryListing.FilterOutDirs()
foundInvalidSketchFileNames := []string{}
for _, potentialSketchFile := range directoryListing {
if sketch.HasSupportedExtension(potentialSketchFile) {
if !validProjectPathBaseName(potentialSketchFile.Base()) {
foundInvalidSketchFileNames = append(foundInvalidSketchFileNames, potentialSketchFile.Base())
}
}
}
if len(foundInvalidSketchFileNames) > 0 {
return ruleresult.Fail, strings.Join(foundInvalidSketchFileNames, ", ")
}
return ruleresult.Pass, ""
}
// SketchFileNameGTMaxLength checks if the sketch file names exceed the maximum length.
func SketchFileNameGTMaxLength() (result ruleresult.Type, output string) {
directoryListing, _ := projectdata.ProjectPath().ReadDir()
directoryListing.FilterOutDirs()
foundTooLongSketchFileNames := []string{}
for _, potentialSketchFile := range directoryListing {
if sketch.HasSupportedExtension(potentialSketchFile) {
if len(potentialSketchFile.Base())-len(potentialSketchFile.Ext()) > 63 {
foundTooLongSketchFileNames = append(foundTooLongSketchFileNames, potentialSketchFile.Base())
}
}
}
if len(foundTooLongSketchFileNames) > 0 {
return ruleresult.Fail, strings.Join(foundTooLongSketchFileNames, ", ")
}
return ruleresult.Pass, ""
}
// PdeSketchExtension checks for use of deprecated .pde sketch file extensions.
func PdeSketchExtension() (result ruleresult.Type, output string) {
directoryListing, _ := projectdata.ProjectPath().ReadDir()
directoryListing.FilterOutDirs()
pdeSketches := []string{}
for _, filePath := range directoryListing {
if filePath.Ext() == ".pde" {
pdeSketches = append(pdeSketches, filePath.Base())
}
}
if len(pdeSketches) > 0 {
return ruleresult.Fail, strings.Join(pdeSketches, ", ")
}
return ruleresult.Pass, ""
}
// IncorrectSketchSrcFolderNameCase checks for incorrect case of src subfolder name in recursive format libraries.
func IncorrectSketchSrcFolderNameCase() (result ruleresult.Type, output string) {
directoryListing, err := projectdata.ProjectPath().ReadDir()
if err != nil {
panic(err)
}
directoryListing.FilterDirs()
path, found := containsIncorrectPathBaseCase(directoryListing, "src")
if found {
return ruleresult.Fail, path.String()
}
return ruleresult.Pass, ""
}
// SketchDotJSONJSONFormat checks whether the sketch.json metadata file is a valid JSON document.
func SketchDotJSONJSONFormat() (result ruleresult.Type, output string) {
metadataPath := sketch.MetadataPath(projectdata.ProjectPath())
if metadataPath == nil {
return ruleresult.Skip, "No metadata file"
}
if isValidJSON(metadataPath) {
return ruleresult.Pass, ""
}
return ruleresult.Fail, ""
}
// SketchDotJSONFormat checks whether the sketch.json metadata file has the required data format.
func SketchDotJSONFormat() (result ruleresult.Type, output string) {
metadataPath := sketch.MetadataPath(projectdata.ProjectPath())
if metadataPath == nil {
return ruleresult.Skip, "No metadata file"
}
if projectdata.MetadataLoadError() == nil {
return ruleresult.Pass, ""
}
return ruleresult.Fail, projectdata.MetadataLoadError().Error()
}