Skip to content

Commit 97eacd2

Browse files
feat(dart): Can config dart export (#2641)
This allows packages to export symbols from other packages e.g. ``` extra-exports = "export 'package:google_cloud_gax/gax.dart' show Status;" ``` ```dart ... import 'package:google_cloud_gax/gax.dart'; import 'package:google_cloud_gax/src/encoding.dart'; import 'package:google_cloud_protobuf/protobuf.dart'; export 'package:google_cloud_gax/gax.dart' show Status; part 'src/rpc.p.dart'; ... ``` --------- Signed-off-by: Brian Quinlan <[email protected]> Co-authored-by: Devon Carew <[email protected]>
1 parent b300a4e commit 97eacd2

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

internal/sidekick/internal/dart/annotate.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ type modelAnnotations struct {
5858
ReadMeQuickstartText string
5959
IssueTrackerURL string
6060
ApiKeyEnvironmentVariables []string
61+
// Dart `export` statements e.g.
62+
// ["export 'package:google_cloud_gax/gax.dart' show Any", "export 'package:google_cloud_gax/gax.dart' show Status"]
63+
Exports []string
6164
}
6265

6366
// HasServices returns true if the model has services.
@@ -230,6 +233,7 @@ func (annotate *annotateModel) annotateModel(options map[string]string) error {
230233
readMeQuickstartText string
231234
issueTrackerURL string
232235
apiKeyEnvironmentVariables = []string{}
236+
exports = []string{}
233237
)
234238

235239
for key, definition := range options {
@@ -254,6 +258,13 @@ func (annotate *annotateModel) annotateModel(options map[string]string) error {
254258
packageVersion = definition
255259
case key == "part-file":
256260
partFileReference = definition
261+
case key == "extra-exports":
262+
// extra-export = "export 'package:google_cloud_gax/gax.dart' show Any; export 'package:google_cloud_gax/gax.dart' show Status;"
263+
// Dart `export` statements that should be appended after any imports.
264+
exports = strings.FieldsFunc(definition, func(c rune) bool { return c == ';' })
265+
for i := range exports {
266+
exports[i] = strings.TrimSpace(exports[i])
267+
}
257268
case key == "dev-dependencies":
258269
devDependencies = strings.Split(definition, ",")
259270
case key == "not-for-publication":
@@ -373,6 +384,7 @@ func (annotate *annotateModel) annotateModel(options map[string]string) error {
373384
ReadMeAfterTitleText: readMeAfterTitleText,
374385
ReadMeQuickstartText: readMeQuickstartText,
375386
ApiKeyEnvironmentVariables: apiKeyEnvironmentVariables,
387+
Exports: exports,
376388
}
377389

378390
model.Codec = ann

internal/sidekick/internal/dart/annotate_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ func TestAnnotateModel_Options(t *testing.T) {
8080
}
8181
},
8282
},
83+
{
84+
map[string]string{"extra-exports": "export 'package:google_cloud_gax/gax.dart' show Any; export 'package:google_cloud_gax/gax.dart' show Status;"},
85+
func(t *testing.T, am *annotateModel) {
86+
codec := model.Codec.(*modelAnnotations)
87+
if diff := cmp.Diff([]string{
88+
"export 'package:google_cloud_gax/gax.dart' show Any",
89+
"export 'package:google_cloud_gax/gax.dart' show Status"}, codec.Exports); diff != "" {
90+
t.Errorf("mismatch in Codec.Exports (-want, +got)\n:%s", diff)
91+
}
92+
},
93+
},
8394
{
8495
map[string]string{"version": "1.2.3"},
8596
func(t *testing.T, am *annotateModel) {

internal/sidekick/internal/dart/templates/lib/main.dart.mustache

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ library;
2828
{{#Codec.Imports}}
2929
{{{.}}}
3030
{{/Codec.Imports}}
31+
32+
{{#Codec.Exports}}
33+
{{{.}}};
34+
{{/Codec.Exports}}
35+
3136
{{#Codec.PartFileReference}}
3237

3338
part '{{Codec.PartFileReference}}';

0 commit comments

Comments
 (0)