From ad707c6db568aa6f88af483b2892891a8cb3de49 Mon Sep 17 00:00:00 2001 From: Vinayak Kukreja Date: Mon, 8 May 2023 12:54:58 -0700 Subject: [PATCH] address feedback Signed-off-by: Vinayak Kukreja --- packages/codemaker/src/codemaker.ts | 8 ++++ .../jsii-pacmak/lib/targets/go/package.ts | 18 +++++++- .../__snapshots__/examples.test.js.snap | 4 ++ .../__snapshots__/target-go.test.js.snap | 45 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/packages/codemaker/src/codemaker.ts b/packages/codemaker/src/codemaker.ts index 8d0bd703fe..28777bf38e 100644 --- a/packages/codemaker/src/codemaker.ts +++ b/packages/codemaker/src/codemaker.ts @@ -196,6 +196,14 @@ export class CodeMaker { return caseutils.toSnakeCase(s, sep); } + /** + * Gets currently opened file path + * @returns Currently opened file path. + */ + public getCurrentFilePath(): string | undefined { + return this.currentFile?.filePath; + } + private makeIndent(): string { const length = this.currentIndentLength; if (length <= 0) { diff --git a/packages/jsii-pacmak/lib/targets/go/package.ts b/packages/jsii-pacmak/lib/targets/go/package.ts index 1c0a8e6ff4..689b219866 100644 --- a/packages/jsii-pacmak/lib/targets/go/package.ts +++ b/packages/jsii-pacmak/lib/targets/go/package.ts @@ -37,6 +37,7 @@ import { VersionFile } from './version-file'; export const GOMOD_FILENAME = 'go.mod'; export const GO_VERSION = '1.18'; +const MAIN_FILE = 'main.go'; /* * Package represents a single `.go` source file within a package. This can be the root package file or a submodule @@ -203,9 +204,9 @@ export abstract class Package { if (this.types.length > 0) { const { code } = context; - const initFile = join(this.directory, `main.go`); + const initFile = join(this.directory, MAIN_FILE); code.openFile(initFile); - code.line(`package ${this.packageName}`); + this.emitHeader(code); code.line(); importGoModules(code, [GO_REFLECT, JSII_RT_MODULE]); code.line(); @@ -469,6 +470,19 @@ export class RootPackage extends Package { ); } + protected emitHeader(code: CodeMaker) { + const currentFilePath = code.getCurrentFilePath(); + if ( + this.assembly.description !== '' && + currentFilePath !== undefined && + currentFilePath.includes(MAIN_FILE) + ) { + code.line(`// ${this.assembly.description}`); + } + code.line(`package ${this.packageName}`); + code.line(); + } + private emitJsiiPackage({ code }: EmitContext) { const dependencies = this.packageDependencies.sort((l, r) => l.moduleName.localeCompare(r.moduleName), diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap index e536d688c6..1db65777ab 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/examples.test.js.snap @@ -525,8 +525,10 @@ func Initialize() { `; exports[`diamond-struct-parameter.ts: /go/testpkg/main.go 1`] = ` +// testpkg package testpkg + import ( "reflect" @@ -2108,8 +2110,10 @@ func Initialize() { `; exports[`nested-types.ts: /go/testpkg/main.go 1`] = ` +// testpkg package testpkg + import ( "reflect" diff --git a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.js.snap b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.js.snap index b989f5393b..eb0573970d 100644 --- a/packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.js.snap +++ b/packages/jsii-pacmak/test/generated-code/__snapshots__/target-go.test.js.snap @@ -435,8 +435,10 @@ func Initialize() { exports[`Generated code for "@scope/jsii-calc-base": /go/jcb/jsii/scope-jsii-calc-base-0.0.0.tgz 1`] = `go/jcb/jsii/scope-jsii-calc-base-0.0.0.tgz is a tarball`; exports[`Generated code for "@scope/jsii-calc-base": /go/jcb/main.go 1`] = ` +// An example direct dependency for jsii-calc. package jcb + import ( "reflect" @@ -895,8 +897,10 @@ func Initialize() { exports[`Generated code for "@scope/jsii-calc-base-of-base": /go/scopejsiicalcbaseofbase/jsii/scope-jsii-calc-base-of-base-2.1.1.tgz 1`] = `go/scopejsiicalcbaseofbase/jsii/scope-jsii-calc-base-of-base-2.1.1.tgz is a tarball`; exports[`Generated code for "@scope/jsii-calc-base-of-base": /go/scopejsiicalcbaseofbase/main.go 1`] = ` +// An example transitive dependency for jsii-calc. package scopejsiicalcbaseofbase + import ( "reflect" @@ -1947,6 +1951,7 @@ func (r *jsiiProxy_Reflector) AsMap(reflectable IReflectable) *map[string]interf exports[`Generated code for "@scope/jsii-calc-lib": /go/scopejsiicalclib/customsubmodulename/main.go 1`] = ` package customsubmodulename + import ( "reflect" @@ -2072,6 +2077,7 @@ func InterfaceFactory_Create() IInterface { exports[`Generated code for "@scope/jsii-calc-lib": /go/scopejsiicalclib/deprecationremoval/main.go 1`] = ` package deprecationremoval + import ( "reflect" @@ -2159,8 +2165,10 @@ func Initialize() { exports[`Generated code for "@scope/jsii-calc-lib": /go/scopejsiicalclib/jsii/scope-jsii-calc-lib-0.0.0.tgz 1`] = `go/scopejsiicalclib/jsii/scope-jsii-calc-lib-0.0.0.tgz is a tarball`; exports[`Generated code for "@scope/jsii-calc-lib": /go/scopejsiicalclib/main.go 1`] = ` +// A simple calcuator library built on JSII. package scopejsiicalclib + import ( "reflect" @@ -18008,6 +18016,7 @@ func UseOptions_Provide(which *string) interface{} { exports[`Generated code for "jsii-calc": /go/jsiicalc/anonymous/main.go 1`] = ` package anonymous + import ( "reflect" @@ -18186,6 +18195,7 @@ type Type__jsiicalcIRandomNumberGenerator = jsiicalc.IRandomNumberGenerator exports[`Generated code for "jsii-calc": /go/jsiicalc/cdk16625/donotimport/main.go 1`] = ` package donotimport + import ( "reflect" @@ -18212,6 +18222,7 @@ func init() { exports[`Generated code for "jsii-calc": /go/jsiicalc/cdk16625/main.go 1`] = ` package cdk16625 + import ( "reflect" @@ -18292,6 +18303,7 @@ type AcceptsPathProps struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/cdk22369/main.go 1`] = ` package cdk22369 + import ( "reflect" @@ -18498,6 +18510,7 @@ type Type__scopejsiicalclibOperation = scopejsiicalclib.Operation exports[`Generated code for "jsii-calc": /go/jsiicalc/composition/main.go 1`] = ` package composition + import ( "reflect" @@ -18667,6 +18680,7 @@ func (j *jsiiProxy_Derived)SetProp(val *string) { exports[`Generated code for "jsii-calc": /go/jsiicalc/derivedclasshasnoproperties/main.go 1`] = ` package derivedclasshasnoproperties + import ( "reflect" @@ -18781,6 +18795,7 @@ type Homonymous struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/homonymousforwardreferences/bar/main.go 1`] = ` package bar + import ( "reflect" @@ -18867,6 +18882,7 @@ type Homonymous struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/homonymousforwardreferences/foo/main.go 1`] = ` package foo + import ( "reflect" @@ -18972,6 +18988,7 @@ type Hello struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/interfaceinnamespaceincludesclasses/main.go 1`] = ` package interfaceinnamespaceincludesclasses + import ( "reflect" @@ -19011,6 +19028,7 @@ type Hello struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/interfaceinnamespaceonlyinterface/main.go 1`] = ` package interfaceinnamespaceonlyinterface + import ( "reflect" @@ -19147,6 +19165,7 @@ func (o *jsiiProxy_OverrideMe) ImplementMe(opts *ImplementMeOpts) *bool { exports[`Generated code for "jsii-calc": /go/jsiicalc/jsii3656/main.go 1`] = ` package jsii3656 + import ( "reflect" @@ -19173,8 +19192,10 @@ func init() { `; exports[`Generated code for "jsii-calc": /go/jsiicalc/main.go 1`] = ` +// A simple calcuator built on JSII. package jsiicalc + import ( "reflect" @@ -21611,6 +21632,7 @@ func (m *jsiiProxy_MyClass) Foo(_arg *string) { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2530/main.go 1`] = ` package module2530 + import ( "reflect" @@ -21674,6 +21696,7 @@ func OnlyStatics_Foo() { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2617/main.go 1`] = ` package module2617 + import ( "reflect" @@ -21801,6 +21824,7 @@ type Type__scopejsiicalclibIFriendly = scopejsiicalclib.IFriendly exports[`Generated code for "jsii-calc": /go/jsiicalc/module2647/main.go 1`] = ` package module2647 + import ( "reflect" @@ -21894,6 +21918,7 @@ func (m *jsiiProxy_MyClass) Foo(_values *[]scopejsiicalclib.Number) { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2689/methods/main.go 1`] = ` package methods + import ( "reflect" @@ -21988,6 +22013,7 @@ func NewMyClass_Override(m MyClass) { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2689/props/main.go 1`] = ` package props + import ( "reflect" @@ -22087,6 +22113,7 @@ func (m *jsiiProxy_MyClass) Foo() *[]scopejsiicalclib.Number { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2689/retval/main.go 1`] = ` package retval + import ( "reflect" @@ -22128,6 +22155,7 @@ type MyStruct struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2689/structs/main.go 1`] = ` package structs + import ( "reflect" @@ -22157,6 +22185,7 @@ type Bar struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2692/submodule1/main.go 1`] = ` package submodule1 + import ( "reflect" @@ -22199,6 +22228,7 @@ type Foo struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2692/submodule2/main.go 1`] = ` package submodule2 + import ( "reflect" @@ -22420,6 +22450,7 @@ func (j *jsiiProxy_IFoo) Baz() *float64 { exports[`Generated code for "jsii-calc": /go/jsiicalc/module2700/main.go 1`] = ` package module2700 + import ( "reflect" @@ -23107,6 +23138,7 @@ type Type__jcbIBaseInterface = jcb.IBaseInterface exports[`Generated code for "jsii-calc": /go/jsiicalc/module2702/main.go 1`] = ` package module2702 + import ( "reflect" @@ -23340,6 +23372,7 @@ func (t *jsiiProxy_TypeFromSub1) Sub1() *string { exports[`Generated code for "jsii-calc": /go/jsiicalc/nodirect/sub1/main.go 1`] = ` package sub1 + import ( "reflect" @@ -23421,6 +23454,7 @@ func (t *jsiiProxy_TypeFromSub2) Sub2() *string { exports[`Generated code for "jsii-calc": /go/jsiicalc/nodirect/sub2/main.go 1`] = ` package sub2 + import ( "reflect" @@ -23480,6 +23514,7 @@ func OnlyStaticMethods_StaticMethod() *string { exports[`Generated code for "jsii-calc": /go/jsiicalc/onlystatic/main.go 1`] = ` package onlystatic + import ( "reflect" @@ -23669,6 +23704,7 @@ type StructWithSelf struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/pythonself/main.go 1`] = ` package pythonself + import ( "reflect" @@ -23882,6 +23918,7 @@ type MyClassReference struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/backreferences/main.go 1`] = ` package backreferences + import ( "reflect" @@ -24091,6 +24128,7 @@ type Structure struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/child/main.go 1`] = ` package child + import ( "reflect" @@ -24211,6 +24249,7 @@ This is the readme of the \`jsii-calc.submodule.isolated\` module. exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/isolated/main.go 1`] = ` package isolated + import ( "reflect" @@ -24233,6 +24272,7 @@ func init() { exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/main.go 1`] = ` package submodule + import ( "reflect" @@ -24343,6 +24383,7 @@ func (j *jsiiProxy_INamespaced) DefinedAt() *string { exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/nestedsubmodule/deeplynested/main.go 1`] = ` package deeplynested + import ( "reflect" @@ -24376,6 +24417,7 @@ type Type__deeplynestedINamespaced = deeplynested.INamespaced exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/nestedsubmodule/main.go 1`] = ` package nestedsubmodule + import ( "reflect" @@ -24414,6 +24456,7 @@ type SpecialParameter struct { exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/param/main.go 1`] = ` package param + import ( "reflect" @@ -24491,6 +24534,7 @@ func (r *jsiiProxy_ReturnsSpecialParameter) ReturnsSpecialParam() *param.Special exports[`Generated code for "jsii-calc": /go/jsiicalc/submodule/returnsparam/main.go 1`] = ` package returnsparam + import ( "reflect" @@ -24609,6 +24653,7 @@ func (r *jsiiProxy_Resolvable) Resolve() interface{} { exports[`Generated code for "jsii-calc": /go/jsiicalc/union/main.go 1`] = ` package union + import ( "reflect"