Skip to content

Commit b637c34

Browse files
committed
Add functions to provide check data for sketches
1 parent 824d963 commit b637c34

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

check/checkdata/checkdata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func Initialize(project project.Type, schemasPath *paths.Path) {
3333
projectPath = project.Path
3434
switch project.ProjectType {
3535
case projecttype.Sketch:
36+
InitializeForSketch(project)
3637
case projecttype.Library:
3738
InitializeForLibrary(project, schemasPath)
3839
case projecttype.Platform:

check/checkdata/sketch.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// This file is part of arduino-check.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-check.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package checkdata
17+
18+
import (
19+
"github.com/arduino/arduino-check/project"
20+
"github.com/arduino/arduino-cli/arduino/sketches"
21+
)
22+
23+
// InitializeForSketch gathers the check data for the specified sketch project.
24+
func InitializeForSketch(project project.Type) {
25+
loadedSketch, sketchLoadError = sketches.NewSketchFromPath(ProjectPath())
26+
27+
metadataSketchObject := &sketches.Sketch{
28+
Name: ProjectPath().Base(),
29+
FullPath: ProjectPath(),
30+
}
31+
metadataLoadError = metadataSketchObject.ImportMetadata()
32+
}
33+
34+
var sketchLoadError error
35+
36+
// SketchLoadError returns the error output from Arduino CLI loading the sketch.
37+
func SketchLoadError() error {
38+
return sketchLoadError
39+
}
40+
41+
var loadedSketch *sketches.Sketch
42+
43+
// Sketch returns the sketch object generated by Arduino CLI.
44+
func Sketch() *sketches.Sketch {
45+
return loadedSketch
46+
}
47+
48+
var metadataLoadError error
49+
50+
// MetadataLoadError returns the error produced during load of the sketch metadata.
51+
func MetadataLoadError() error {
52+
return metadataLoadError
53+
}
54+
55+
var metadataSketchObject *sketches.Sketch
56+
57+
// Metadata returns the metadata object produced by Arduino CLI.
58+
func Metadata() *sketches.Metadata {
59+
return metadataSketchObject.Metadata
60+
}

project/sketch/sketch.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,27 @@ func HasSupportedExtension(filePath *paths.Path) bool {
6262
_, hasAdditionalFileValidExtensions := globals.AdditionalFileValidExtensions[filePath.Ext()]
6363
return hasAdditionalFileValidExtensions || HasMainFileValidExtension(filePath)
6464
}
65+
66+
var empty struct{}
67+
68+
// See: https://arduino.github.io/arduino-cli/latest/sketch-specification/#metadata
69+
var metadataFilenames = map[string]struct{}{
70+
"sketch.json": empty,
71+
}
72+
73+
// MetadataPath returns the path of the sketch's metadata file.
74+
func MetadataPath(sketchPath *paths.Path) *paths.Path {
75+
for metadataFileName := range metadataFilenames {
76+
metadataPath := sketchPath.Join(metadataFileName)
77+
exist, err := metadataPath.ExistCheck()
78+
if err != nil {
79+
panic(err)
80+
}
81+
82+
if exist {
83+
return metadataPath
84+
}
85+
}
86+
87+
return nil
88+
}

project/sketch/sketch_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ func TestHasSupportedExtension(t *testing.T) {
4545
assert.True(t, HasSupportedExtension(paths.New("/foo/bar.h")))
4646
assert.False(t, HasSupportedExtension(paths.New("/foo/bar.baz")))
4747
}
48+
49+
func TestMetadataPath(t *testing.T) {
50+
assert.True(t, MetadataPath(testDataPath.Join("HasMetadataFile")).EquivalentTo(testDataPath.Join("HasMetadataFile", "sketch.json")))
51+
assert.Nil(t, MetadataPath(testDataPath.Join("NoMetadataFile")))
52+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}

project/sketch/testdata/HasMetadataFile/sketch.json

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
void setup() {}
2+
void loop() {}

0 commit comments

Comments
 (0)