Skip to content

Commit

Permalink
genai: improve code generation command (#144)
Browse files Browse the repository at this point in the history
Use the cached directory for the right version of the pb.go files.
This ensures that the generated code matches the version of
cloud.google.com/go/ai in the go.mod file, rather than whatever
happens to be checked out in the google-cloud-go repo.
  • Loading branch information
jba authored Jun 25, 2024
1 parent 5220ad2 commit bf8c52d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
15 changes: 2 additions & 13 deletions genai/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// For the following go:generate line to work, do the following:
// Install the protoveener tool:
// For the following go:generate line to work, install the protoveener tool:
// git clone https://github.com/googleapis/google-cloud-go
// cd google-cloud-go
// go install ./internal/protoveneer/cmd/protoveneer
//
// Set the environment variable GOOGLE_CLOUD_GO to the path to the above repo on your machine.
// For example:
// export GOOGLE_CLOUD_GO=$HOME/repos/google-cloud-go
//
// Ensure that the working directory of that repo is at the version of cloud.google.com/go/ai in
// this repo's go.mod file. For example, if the go.mod contains
// cloud.google.com/go/ai v0.6.0
// then from the root of the google-cloud-go repo, run
// git switch -d ai/v0.6.0

//go:generate protoveneer -license license.txt config.yaml $GOOGLE_CLOUD_GO/ai/generativelanguage/apiv1beta/generativelanguagepb
//go:generate ./generate.sh

package genai

Expand Down
32 changes: 32 additions & 0 deletions genai/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh
# Copyright 2023 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.

version=$(awk '$1 == "cloud.google.com/go/ai" {print $2}' ../go.mod)

if [[ $version = '' ]]; then
echo >&2 "could not get version of cloud.google.com/go/ai from ../go.mod"
exit 1
fi

dir=~/go/pkg/mod/cloud.google.com/go/ai@$version/generativelanguage/apiv1beta/generativelanguagepb

if [[ ! -d $dir ]]; then
echo >&2 "$dir does not exist or is not a directory"
exit 1
fi

echo "generating from $dir"
protoveneer -license license.txt config.yaml $dir

0 comments on commit bf8c52d

Please sign in to comment.