Skip to content

Commit

Permalink
impl: devtools/cmd/generate,testdata: add command to run protoc-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
julieqiu committed Nov 2, 2024
1 parent 10169c0 commit 4a8e786
Show file tree
Hide file tree
Showing 21 changed files with 3,711 additions and 4 deletions.
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:

```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 @@ -112,10 +112,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
//
// 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

0 comments on commit 4a8e786

Please sign in to comment.