Skip to content

Commit

Permalink
feat(generator/rust): simpler library generation (#920)
Browse files Browse the repository at this point in the history
Another pass simplifying the library generation. This time we
automatically run the tests after the library is generated.
  • Loading branch information
coryan authored Jan 31, 2025
1 parent 96d0719 commit c32356b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
11 changes: 5 additions & 6 deletions doc/contributor/howto-guide-generated-code-maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ new branch in your fork:
git checkout -b feat-websecurityscanner-generate-library
```

Generate the library:
This command will generate the library, add the library to Cargo and git, and
run the necessary tests:

```bash
go -C generator/ run ./cmd/sidekick rust-generate \
-project-root .. \
-service-config google/cloud/websecurityscanner/v1/websecurityscanner_v1.yaml
```

Compile and run the tests:

```bash
typos && cargo build && cargo test && cargo doc
```
Often we identify typos in the Protobuf comments. Add the typos to the ignore
list on `.typos.toml` and fix the problem upstream. Do not treat this as a
blocker.

Commit all these changes and send a PR to merge them:

Expand Down
57 changes: 54 additions & 3 deletions generator/internal/sidekick/rust_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ package sidekick
import (
"errors"
"fmt"
"log/slog"
"os"
"os/exec"
"path"
"strings"

"github.com/googleapis/google-cloud-rust/generator/internal/config"
toml "github.com/pelletier/go-toml/v2"
)

func init() {
Expand Down Expand Up @@ -53,21 +56,26 @@ func rust_generate(rootConfig *config.Config, cmdLine *CommandLine) error {
}

if err := runExternalCommand("cargo", "--version"); err != nil {
return fmt.Errorf("got an error trying to run `cargo --version`, please verify it is installed: %w", err)
return fmt.Errorf("got an error trying to run `cargo --version`, the instructions on https://www.rust-lang.org/learn/get-started may solve this problem: %w", err)
}
if err := runExternalCommand("taplo", "--version"); err != nil {
return fmt.Errorf("got an error trying to run `taplo --version`, please verify it is installed: %w", err)
return fmt.Errorf("got an error trying to run `taplo --version`, please install using `cargo install taplo-cli`: %w", err)
}
if err := runExternalCommand("typos", "--version"); err != nil {
return fmt.Errorf("got an error trying to run `typos --version`, please install using `cargo install typos-cli`: %w", err)
}
if err := runExternalCommand("git", "--version"); err != nil {
return fmt.Errorf("got an error trying to run `git --version`, please verify it is installed: %w", err)
return fmt.Errorf("got an error trying to run `git --version`, the instructions on https://github.com/git-guides/install-git may solve this problem: %w", err)
}

slog.Info("Preparing cargo workspace to get new package")
if err := runExternalCommand("cargo", "new", "--vcs", "none", "--lib", cmdLine.Output); err != nil {
return err
}
if err := runExternalCommand("taplo", "fmt", "Cargo.toml"); err != nil {
return err
}
slog.Info("Generating new library code and adding it to git")
if err := generate(rootConfig, cmdLine); err != nil {
return err
}
Expand All @@ -77,6 +85,28 @@ func rust_generate(rootConfig *config.Config, cmdLine *CommandLine) error {
if err := runExternalCommand("git", "add", cmdLine.Output); err != nil {
return err
}
packagez, err := getPackageName(cmdLine.Output)
if err != nil {
return err
}
slog.Info("Generated new client library", "package", packagez)
slog.Info("Running `cargo test` on new client library")
if err := runExternalCommand("cargo", "test", "--package", packagez); err != nil {
return err
}
slog.Info("Running `cargo doc` on new client library")
if err := runExternalCommand("cargo", "doc", "--package", packagez); err != nil {
return err
}
slog.Info("Running `cargo clippy` on new client library")
if err := runExternalCommand("cargo", "clippy", "--package", packagez, "--", "--deny", "warnings"); err != nil {
return err
}
slog.Info("Running `typos` on new client library")
if err := runExternalCommand("cargo", "clippy", "--package", packagez, "--", "--deny", "warnings"); err != nil {
slog.Info("please manually add the typos to `.typos.toml` and fix the problem upstream")
return err
}

return nil
}
Expand All @@ -92,3 +122,24 @@ func runExternalCommand(c string, arg ...string) error {
}
return nil
}

func getPackageName(output string) (string, error) {
cargo := CargoConfig{}
filename := path.Join(output, "Cargo.toml")
if contents, err := os.ReadFile(filename); err == nil {
err = toml.Unmarshal(contents, &cargo)
if err != nil {
return "", fmt.Errorf("error reading %s: %w", filename, err)
}
}
// Ignore errors reading the top-level file.
return cargo.Package.Name, nil
}

type CargoConfig struct {
Package CargoPackage // `toml:"package"`
}

type CargoPackage struct {
Name string // `toml:"name"`
}

0 comments on commit c32356b

Please sign in to comment.