Skip to content

Commit 685ef93

Browse files
authored
feat(sidekick/swift): track path to monorepo root (#5190)
When generating the `Package.swift` files we need to reference local packages by their path. It is easier to express all these paths as relative to the project monorepo root, and the generator can inject enough `../` paths to find the root. I expect in most cases `../../` will be enough.
1 parent 399e2cc commit 685ef93

5 files changed

Lines changed: 37 additions & 5 deletions

File tree

internal/sidekick/swift/annotate_model.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ type modelAnnotations struct {
2222
CopyrightYear string
2323
BoilerPlate []string
2424
PackageName string
25+
MonorepoRoot string
2526
}
2627

2728
func (codec *codec) annotateModel() error {
2829
annotations := &modelAnnotations{
2930
CopyrightYear: codec.GenerationYear,
3031
BoilerPlate: license.HeaderBulk(),
3132
PackageName: codec.PackageName,
33+
MonorepoRoot: codec.MonorepoRoot,
3234
}
3335
codec.Model.Codec = annotations
3436
for _, message := range codec.Model.Messages {

internal/sidekick/swift/annotate_model_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestModelAnnotations(t *testing.T) {
3333
want := &modelAnnotations{
3434
PackageName: "GoogleCloudWorkflowsV1",
3535
CopyrightYear: "2038",
36+
MonorepoRoot: ".",
3637
}
3738
if diff := cmp.Diff(want, model.Codec, cmpopts.IgnoreFields(modelAnnotations{}, "BoilerPlate")); diff != "" {
3839
t.Errorf("mismatch (-want +got):\n%s", diff)

internal/sidekick/swift/codec.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package swift
1616

1717
import (
1818
"fmt"
19+
"path/filepath"
1920
"time"
2021

2122
"github.com/googleapis/librarian/internal/config"
@@ -33,18 +34,35 @@ import (
3334
type codec struct {
3435
GenerationYear string
3536
PackageName string
37+
MonorepoRoot string
3638
// Most libraries are generated from `googleapis`. Rarely, we use protobuf,
3739
// gapic-showcase, or a different root.
3840
RootName string
3941
Model *api.API
4042
Dependencies []config.SwiftDependency
4143
}
4244

43-
func newCodec(model *api.API, cfg *parser.ModelConfig, swiftCfg *config.SwiftPackage) *codec {
45+
func newCodec(model *api.API, cfg *parser.ModelConfig, swiftCfg *config.SwiftPackage, outdir string) (*codec, error) {
4446
year, _, _ := time.Now().Date()
47+
absOutdir, err := filepath.Abs(outdir)
48+
if err != nil {
49+
return nil, err
50+
}
51+
// The generator must run at the root of the monorepo, because that is where we keep the `librarian.yaml` file and
52+
// because all the `outdir` directories are computed relative to that location. So effectively this gets the root
53+
// of the monorepo.
54+
absRoot, err := filepath.Abs(".")
55+
if err != nil {
56+
return nil, err
57+
}
58+
rel, err := filepath.Rel(absOutdir, absRoot)
59+
if err != nil {
60+
return nil, err
61+
}
4562
result := &codec{
4663
GenerationYear: fmt.Sprintf("%04d", year),
4764
PackageName: PackageName(model),
65+
MonorepoRoot: rel,
4866
RootName: "googleapis",
4967
Model: model,
5068
}
@@ -63,5 +81,5 @@ func newCodec(model *api.API, cfg *parser.ModelConfig, swiftCfg *config.SwiftPac
6381
// Ignore other options.
6482
}
6583
}
66-
return result
84+
return result, nil
6785
}

internal/sidekick/swift/codec_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ func TestParseOptions(t *testing.T) {
3131
},
3232
}
3333
model := api.NewTestAPI([]*api.Message{}, []*api.Enum{}, []*api.Service{})
34-
got := newCodec(model, cfg, nil)
34+
got, err := newCodec(model, cfg, nil, ".")
35+
if err != nil {
36+
t.Fatal(err)
37+
}
3538
want := &codec{
3639
GenerationYear: "2038",
3740
PackageName: "GoogleCloudBigtable",
41+
MonorepoRoot: ".",
3842
RootName: "test-root",
3943
Model: model,
4044
}
@@ -49,5 +53,9 @@ func newTestCodec(t *testing.T, model *api.API, options map[string]string) *code
4953
cfg := &parser.ModelConfig{
5054
Codec: options,
5155
}
52-
return newCodec(model, cfg, nil)
56+
codec, err := newCodec(model, cfg, nil, ".")
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
return codec
5361
}

internal/sidekick/swift/generate.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ var templates embed.FS
3131

3232
// Generate generates code from the model.
3333
func Generate(ctx context.Context, model *api.API, outdir string, cfg *parser.ModelConfig, swiftCfg *config.SwiftPackage) error {
34-
codec := newCodec(model, cfg, swiftCfg)
34+
codec, err := newCodec(model, cfg, swiftCfg, outdir)
35+
if err != nil {
36+
return err
37+
}
3538
if err := codec.annotateModel(); err != nil {
3639
return err
3740
}

0 commit comments

Comments
 (0)