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

impl: devtools/cmd/generate,testdata: add command to run protoc-gen #72

Merged
merged 2 commits into from
Nov 3, 2024
Merged
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
6 changes: 6 additions & 0 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ A tool for generating client libraries.

Run the following command from the generator directory:

```bash
go run ./devtools/cmd/generate -language=rust
```

Alternatively, you can run the protoc command directly:
julieqiu marked this conversation as resolved.
Show resolved Hide resolved

```bash
go install github.com/googleapis/google-cloud-rust/generator/cmd/protoc-gen-gclient
protoc -I cmd/protoc-gen-gclient/testdata/smprotos \
Expand Down
72 changes: 72 additions & 0 deletions generator/devtools/cmd/generate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2024 Google LLC
//
// 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
//
// https://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 (
"flag"
"fmt"
"log"
"log/slog"
"os"
"os/exec"
"path/filepath"
)

var (
input = flag.String("input", "testdata/google/cloud/secretmanager/v1/", "path to protos to generate from")
output = flag.String("out", "output", "the path to the output directory")
language = flag.String("language", "", "the generated language")
testdata = flag.String("testdata", "testdata/", "path to testdata directory")
)

func main() {
flag.Parse()
if *language == "" {
log.Fatalf("language must be provided")
}
if err := run(*language, *testdata, *input, *output); err != nil {
log.Fatal(err)
}
}

func run(language, testdata, input, output string) error {
var files []string
err := filepath.Walk(input, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".proto" {
files = append(files, path)
}
return nil
})
if err != nil {
return err
}

args := []string{
"-I", testdata,
fmt.Sprintf("--gclient_out=%s", output),
fmt.Sprintf("--gclient_opt=language=%s", language),
}
args = append(args, files...)

cmd := exec.Command("protoc", args...)
slog.Info(cmd.String())

cmd.Stdout = os.Stdout // or any other io.Writer
cmd.Stderr = os.Stderr // or any other io.Writer
return cmd.Run()
}
5 changes: 1 addition & 4 deletions generator/internal/genclient/genclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,7 @@ func Generate(req *GenerateRequest) (*Output, error) {
return err
}
fn := filepath.Join(req.outDir(), filepath.Dir(strings.TrimPrefix(path, root)), strings.TrimSuffix(d.Name(), ".mustache"))
if err := os.WriteFile(fn, []byte(s), os.ModePerm); err != nil {
return err
}
return nil
return os.WriteFile(fn, []byte(s), os.ModePerm)
})
if err != nil {
slog.Error("error walking templates", "err", err.Error())
Expand Down
31 changes: 31 additions & 0 deletions generator/testdata/google/api/annotations.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2015 Google LLC
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is the metaphysical discussion around what is the copyright year for a copy of a file. LOL.

Alternative: we could use a git submodule and reference github.com/googleapis/googleapis instead of copying a few files

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this option but I thought that it might be nice to just limit to the set of files we need so the input is clear. Happy to change this later on though

//
// 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.

syntax = "proto3";

package google.api;

import "google/api/http.proto";
import "google/protobuf/descriptor.proto";

option go_package = "google.golang.org/genproto/googleapis/api/annotations;annotations";
option java_multiple_files = true;
option java_outer_classname = "AnnotationsProto";
option java_package = "com.google.api";
option objc_class_prefix = "GAPI";

extend google.protobuf.MethodOptions {
// See `HttpRule`.
HttpRule http = 72295728;
}
Loading
Loading