Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "CodeGeneratorRequest" access in BuildContext #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ type AST interface {
// (FQN). The FQN uses dot notation of the form ".{package}.{entity}", or the
// input path for Files.
Lookup(name string) (Entity, bool)

// The original CodeGeneratorRequest from protoc
CodeGeneratorRequest() *plugin_go.CodeGeneratorRequest
}

type graph struct {
d Debugger
req *plugin_go.CodeGeneratorRequest

targets map[string]File
packages map[string]Package
Expand All @@ -41,6 +45,8 @@ func (g *graph) Lookup(name string) (Entity, bool) {
return e, ok
}

func (g *graph) CodeGeneratorRequest() *plugin_go.CodeGeneratorRequest { return g.req }

// ProcessDescriptors is deprecated; use ProcessCodeGeneratorRequest instead
func ProcessDescriptors(debug Debugger, req *plugin_go.CodeGeneratorRequest) AST {
return ProcessCodeGeneratorRequest(debug, req)
Expand All @@ -51,6 +57,7 @@ func ProcessDescriptors(debug Debugger, req *plugin_go.CodeGeneratorRequest) AST
func ProcessCodeGeneratorRequest(debug Debugger, req *plugin_go.CodeGeneratorRequest) AST {
g := &graph{
d: debug,
req: req,
targets: make(map[string]File, len(req.GetFileToGenerate())),
packages: make(map[string]Package),
entities: make(map[string]Entity),
Expand Down
16 changes: 14 additions & 2 deletions build_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package pgs

import "path/filepath"
import (
plugin_go "github.com/golang/protobuf/protoc-gen-go/plugin"
"path/filepath"
)

// BuildContext tracks code generation relative to an output path. By default,
// BuildContext's path is relative to the output location specified when
Expand Down Expand Up @@ -40,17 +43,21 @@ type BuildContext interface {
// Parameters returns the command line parameters passed in from protoc,
// mutated with any provided ParamMutators via InitOptions.
Parameters() Parameters

// The original CodeGeneratorRequest from protoc
CodeGeneratorRequest() *plugin_go.CodeGeneratorRequest
}

// Context creates a new BuildContext with the provided debugger and initial
// output path. For protoc-gen-go plugins, output is typically ".", while
// Module's may use a custom path.
func Context(d Debugger, params Parameters, output string) BuildContext {
func Context(d Debugger, params Parameters, output string, req *plugin_go.CodeGeneratorRequest) BuildContext {
return rootContext{
dirContext: dirContext{
prefixContext: prefixContext{parent: nil, d: d},
p: filepath.Clean(output),
},
req: req,
params: params,
}
}
Expand Down Expand Up @@ -80,6 +87,8 @@ func (c prefixContext) Push(prefix string) BuildContext { return initPrefixConte
func (c prefixContext) Pop() BuildContext { return c.parent }
func (c prefixContext) PopDir() BuildContext { return c.parent.PopDir() }

func (c prefixContext) CodeGeneratorRequest() *plugin_go.CodeGeneratorRequest { return c.parent.CodeGeneratorRequest() }

type dirContext struct {
prefixContext
p string
Expand Down Expand Up @@ -117,6 +126,7 @@ type prefixContext struct {
type rootContext struct {
dirContext
params Parameters
req *plugin_go.CodeGeneratorRequest
}

func (c rootContext) OutputPath() string { return c.p }
Expand All @@ -129,6 +139,8 @@ func (c rootContext) Pop() BuildContext {
return nil
}

func (c rootContext) CodeGeneratorRequest() *plugin_go.CodeGeneratorRequest { return c.req }

func (c rootContext) JoinPath(name ...string) string {
return filepath.Join(append([]string{c.OutputPath()}, name...)...)
}
2 changes: 1 addition & 1 deletion workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (wf *standardWorkflow) Init(g *Generator) AST {
}

func (wf *standardWorkflow) Run(ast AST) (arts []Artifact) {
ctx := Context(wf.Debugger, wf.params, wf.params.OutputPath())
ctx := Context(wf.Debugger, wf.params, wf.params.OutputPath(), ast.CodeGeneratorRequest())

wf.Debug("initializing modules")
for _, m := range wf.mods {
Expand Down