Skip to content

Commit f397a05

Browse files
authored
Merge pull request #6 from moonstream-to/file-headers
solface can now add SPDX header and pragma to codegen output
2 parents f400c2a + cc8e202 commit f397a05

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

interface.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type InterfaceSpecification struct {
3535
IncludeAnnotations bool
3636
CompoundTypes []CompoundType
3737
SolfaceVersion string
38+
License string
39+
Pragma string
3840
}
3941

4042
func GenerateName(nameCounter *int) string {
@@ -213,8 +215,14 @@ func ResolveCompounds(abi DecodedABI) DecodedABIWithCompundTypes {
213215
return result
214216
}
215217

216-
func GenerateInterface(interfaceName string, abi DecodedABI, annotations Annotations, includeAnnotations bool, writer io.Writer) error {
218+
func GenerateInterface(interfaceName, license, pragma string, abi DecodedABI, annotations Annotations, includeAnnotations bool, writer io.Writer) error {
217219
const interfaceTemplate = `
220+
{{ if .License }}
221+
// SPDX-License-Identifier: {{.License}}
222+
{{ end }}
223+
{{- if .Pragma }}
224+
pragma solidity {{.Pragma}};
225+
{{ end }}
218226
// Interface generated by solface: https://github.com/moonstream-to/solface
219227
// solface version: {{.SolfaceVersion}}
220228
{{- $includeAnnotations := .IncludeAnnotations}}
@@ -253,7 +261,7 @@ interface {{.Name}} {
253261
`
254262

255263
resolved := ResolveCompounds(abi)
256-
spec := InterfaceSpecification{Name: interfaceName, ABI: resolved.EnrichedABI, Annotations: annotations, IncludeAnnotations: includeAnnotations, CompoundTypes: resolved.CompoundTypes, SolfaceVersion: VERSION}
264+
spec := InterfaceSpecification{Name: interfaceName, ABI: resolved.EnrichedABI, Annotations: annotations, IncludeAnnotations: includeAnnotations, CompoundTypes: resolved.CompoundTypes, SolfaceVersion: VERSION, License: license, Pragma: pragma}
257265

258266
templateFuncs := map[string]any{
259267
"needsMemory": SolidityTypeRequiresLocation,

interface_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ func TestGenerateInterfaceDiamondCutFacet(t *testing.T) {
151151
includeAnnotations := false
152152

153153
// Replace io.Discard with os.Stdout to inspect output:
154-
// err := GenerateInterface("IDiamondCutFacet", abi, os.Stdout)
155-
err := GenerateInterface("IDiamondCutFacet", abi, annotations, includeAnnotations, io.Discard)
154+
// err := GenerateInterface("IDiamondCutFacet", "", "", abi, annotations, includeAnnotations, os.Stdout)
155+
err := GenerateInterface("IDiamondCutFacet", "", "", abi, annotations, includeAnnotations, io.Discard)
156156

157157
if err != nil {
158158
t.Fatalf("Error generating interface: %s", err.Error())
@@ -174,8 +174,8 @@ func TestGenerateInterfaceOwnableERC20(t *testing.T) {
174174
includeAnnotations := false
175175

176176
// Replace io.Discard with os.Stdout to inspect output:
177-
// err := GenerateInterface("IOwnableERC20", abi, os.Stdout)
178-
err := GenerateInterface("IOwnableERC20", abi, annotations, includeAnnotations, io.Discard)
177+
// err := GenerateInterface("IOwnableERC20", "Apache-2.0", "^8.20.0", abi, annotations, includeAnnotations, os.Stdout)
178+
err := GenerateInterface("IOwnableERC20", "Apache-2.0", "^8.20.0", abi, annotations, includeAnnotations, io.Discard)
179179

180180
if err != nil {
181181
t.Fatalf("Error generating interface: %s", err.Error())

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"os"
99
)
1010

11-
var VERSION string = "0.1.0"
11+
var VERSION string = "0.1.1"
1212

1313
func main() {
14-
var interfaceName string
14+
var interfaceName, license, pragma string
1515
var addAnnotations bool
1616
flag.StringVar(&interfaceName, "name", "", "Name for Solidity interface you would like to generate")
1717
flag.BoolVar(&addAnnotations, "annotations", false, "If present, adds annotations to generated interface. Annotations include: interface ID, method selectors, event signatures.")
18+
flag.StringVar(&license, "license", "", "License to include in generated interface - adds a comment at the top of the output with this as the SPDX identifier")
19+
flag.StringVar(&pragma, "pragma", "", "Solidity pragma to include in generated interface - adds this parameter as the pragma constraint at the top of the output")
1820

1921
flag.Usage = func() {
2022
fmt.Fprintf(flag.CommandLine.Output(), "%s -name <interface name> [-annotations] {<path to ABI file> | stdin}\n\n", os.Args[0])
@@ -55,7 +57,7 @@ func main() {
5557
log.Fatalf("Error generating annotations: %s", annotationErr.Error())
5658
}
5759

58-
generateErr := GenerateInterface(interfaceName, abi, annotations, addAnnotations, os.Stdout)
60+
generateErr := GenerateInterface(interfaceName, license, pragma, abi, annotations, addAnnotations, os.Stdout)
5961
if generateErr != nil {
6062
log.Fatalf("Error generating interface (%s): %s", interfaceName, generateErr.Error())
6163
}

0 commit comments

Comments
 (0)