-
Notifications
You must be signed in to change notification settings - Fork 4
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 sourceinfo plugin #22
Closed
Closed
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
b79a31e
Add sourceinfo plugin
Alfus c00a0cd
use compact
Alfus 14d621d
lint
Alfus a87ee39
nit
Alfus f0d0ea7
fix windows test
Alfus 7cd6b87
try to fix windows again
Alfus 0790ed3
add comments
Alfus eb3a151
nit
Alfus f26b23c
Add cmd
Alfus ca777e3
switch to .binpb
Alfus 75dc2f1
Add support for embedding SourceInfo
Alfus 72fc2b8
lint
Alfus fc431ec
nit
Alfus 9c31ed9
Switch to fs.FS
Alfus d943794
re
Alfus 2031d73
re
Alfus 911c540
re
Alfus 2c40417
re
Alfus dc81810
Update readme
Alfus e8511c2
re
Alfus eb5b1d4
nit
Alfus 0792f84
update readme
Alfus e19188d
re
Alfus 1b01549
nit
Alfus e67d687
nit
Alfus e0f253b
nit
Alfus a116ab6
Merge branch 'main' into alfus/sourceinfo
Alfus 2aa6f75
nit
Alfus c3976c8
re
Alfus 15f80d5
re
Alfus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2024 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/bufbuild/protoplugin" | ||
"github.com/bufbuild/protoschema-plugins/internal" | ||
"github.com/bufbuild/protoschema-plugins/internal/protoschema/plugin/pluginsourceinfo" | ||
) | ||
|
||
func main() { | ||
protoplugin.Main(protoplugin.HandlerFunc(pluginsourceinfo.Handle), protoplugin.WithVersion(internal.Version())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2024 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
_ "github.com/bufbuild/protoschema-plugins/internal/gen/proto/bufext/cel/expr/conformance/proto3" | ||
"github.com/bufbuild/protoschema-plugins/internal/protoschema/golden" | ||
"github.com/bufbuild/protoschema-plugins/internal/protoschema/plugin/pluginsourceinfo" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
if errString := err.Error(); errString != "" { | ||
_, _ = fmt.Fprintln(os.Stderr, errString) | ||
} | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func run() error { | ||
if len(os.Args) != 2 { | ||
return fmt.Errorf("usage: %s [output dir]", os.Args[0]) | ||
} | ||
outputDir := os.Args[1] | ||
// Make sure the directory exists | ||
if err := os.MkdirAll(outputDir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
testFiles, err := golden.GetTestFiles(filepath.FromSlash("./internal/testdata")) | ||
if err != nil { | ||
return err | ||
} | ||
// TODO: Use the normal plugin to generate golden files | ||
includePrefixes := []string{ | ||
filepath.FromSlash("buf/protoschema/test/"), | ||
filepath.FromSlash("bufext/cel/expr/conformance/proto3/"), | ||
} | ||
testFiles.RangeFiles(func(testDesc protoreflect.FileDescriptor) bool { | ||
if !shouldIncludeFile(testDesc, includePrefixes) { | ||
return true | ||
} | ||
fileName := pluginsourceinfo.GetSourceInfoPath(testDesc) | ||
filePath := filepath.Join(outputDir, fileName) | ||
// Create any missing directories | ||
if innerErr := os.MkdirAll(filepath.Dir(filePath), 0755); innerErr != nil { | ||
err = fmt.Errorf("failed to create directory for %s: %w", filePath, innerErr) | ||
return false | ||
} | ||
var data []byte | ||
data, innerErr := pluginsourceinfo.GenFileContents(testDesc) | ||
if innerErr != nil { | ||
err = fmt.Errorf("failed to generate source info for %s: %w", testDesc.FullName(), innerErr) | ||
return false | ||
} | ||
if innerErr := os.WriteFile(filePath, data, 0600); innerErr != nil { | ||
err = fmt.Errorf("failed to write source info to %s: %w", filePath, innerErr) | ||
return false | ||
} | ||
return true | ||
}) | ||
return err | ||
} | ||
|
||
func shouldIncludeFile(fileDesc protoreflect.FileDescriptor, includePrefixes []string) bool { | ||
for _, prefix := range includePrefixes { | ||
if strings.HasPrefix(fileDesc.Path(), prefix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
internal/protoschema/plugin/pluginsourceinfo/pluginsourceinfo.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2024 Buf Technologies, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package pluginsourceinfo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/bufbuild/protoplugin" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protodesc" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
// FileExtension is the file extension for the source info files. | ||
const FileExtension = ".sourceinfo.binpb" | ||
Alfus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Handle implements protoplugin.Handler and is the main entry point for the plugin. | ||
func Handle( | ||
_ context.Context, | ||
_ protoplugin.PluginEnv, | ||
responseWriter protoplugin.ResponseWriter, | ||
request protoplugin.Request, | ||
) error { | ||
fileDescriptors, err := request.FileDescriptorsToGenerate() | ||
if err != nil { | ||
return err | ||
} | ||
for _, fileDescriptor := range fileDescriptors { | ||
// Write the YAML string to the response. | ||
data, err := GenFileContents(fileDescriptor) | ||
if err != nil { | ||
return err | ||
} | ||
name := GetSourceInfoPath(fileDescriptor) | ||
responseWriter.AddFile(name, string(data)) | ||
} | ||
|
||
responseWriter.SetFeatureProto3Optional() | ||
return nil | ||
} | ||
|
||
// GetSourceInfoPath returns the path to the source info file for the given file descriptor. | ||
func GetSourceInfoPath(fileDescriptor protoreflect.FileDescriptor) string { | ||
path := fileDescriptor.Path() | ||
path = strings.TrimSuffix(path, ".proto") | ||
return fmt.Sprintf("%s%s", path, FileExtension) | ||
} | ||
|
||
// GenFileContents marshals the `SourceCodeInfo` field of the given file descriptor to protobuf bytes. | ||
func GenFileContents(fileDescriptor protoreflect.FileDescriptor) ([]byte, error) { | ||
// Convert the file descriptor to a descriptorpb.FileDescriptorProto. | ||
fileDescProto := protodesc.ToFileDescriptorProto(fileDescriptor) | ||
return proto.Marshal(fileDescProto.SourceCodeInfo) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can this be a drop-in if you just are serializing the
google.protobuf.SourceCodeInfo
file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is hooking into @jhump's library to provide a drop in replacement, backed by normal protoregistry values.