Skip to content

Commit

Permalink
Merge pull request #3 from lazyledger/adlerjohn-license_option
Browse files Browse the repository at this point in the history
Add option for license from command-line.
  • Loading branch information
adlerjohn authored Sep 6, 2020
2 parents 72a3673 + a389420 commit cd33cbb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
10 changes: 10 additions & 0 deletions cmd/protoc-gen-sol/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ func main() {
// Initialize generator with request
g := generator.New(request)

// Parse any command-line parameters
err = g.ParseParameters()
if err != nil {
err = responseError(err)
if err != nil {
panic(err)
}
os.Exit(0)
}

// Generate response
response, err := g.Generate()
if err != nil {
Expand Down
32 changes: 30 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"path/filepath"
"strings"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
Expand All @@ -20,16 +21,44 @@ const SolidityABIString = "pragma experimental ABIEncoderV2;"
type Generator struct {
request *pluginpb.CodeGeneratorRequest
enumMaxes map[string]int

licenseString string
}

// New initializes a new Generator.
func New(request *pluginpb.CodeGeneratorRequest) *Generator {
g := new(Generator)

g.request = request
g.enumMaxes = make(map[string]int)

g.licenseString = "CC0"

return g
}

// ParseParameters parses command-line parameters
func (g *Generator) ParseParameters() error {
parameterString := g.request.GetParameter()
if len(parameterString) == 0 {
return nil
}

for _, parameter := range strings.Split(parameterString, ",") {
keyvalue := strings.Split(parameter, "=")
key, value := keyvalue[0], keyvalue[1]

switch key {
case "license":
g.licenseString = value
default:
return errors.New("unrecognized option " + key)
}
}

return nil
}

// Generate generates Solidity code from the requested .proto files.
func (g *Generator) Generate() (*pluginpb.CodeGeneratorResponse, error) {
response := &pluginpb.CodeGeneratorResponse{}
Expand Down Expand Up @@ -63,9 +92,8 @@ func (g *Generator) generateFile(protoFile *descriptorpb.FileDescriptorProto) (*
// Buffer to hold the generate file's text
b := &WriteableBuffer{}

// TODO option for license
// Generate heading
b.P(fmt.Sprintf("// SPDX-License-Identifier: %s", "CC0"))
b.P(fmt.Sprintf("// SPDX-License-Identifier: %s", g.licenseString))
b.P("pragma solidity " + SolidityVersionString + ";")
b.P(SolidityABIString)
b.P()
Expand Down

0 comments on commit cd33cbb

Please sign in to comment.