Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(go): packageName and versionSuffix #2687

Merged
merged 7 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,31 @@
!!! danger
The **go** target is currently unstable and not suitable for production use.

To enable go package generation, add the **go** key with an empty object to the jsii targets configuration.
To enable go package generation, add the `go` key to the jsii targets configuration:

This will add generated go package code to your specified `outDir` for testing and experimentation.
- `packageName` (optional) - The name of the Go package name. If this is not
eladb marked this conversation as resolved.
Show resolved Hide resolved
specified, the package name will be derived from the npm module name by
filtering non-alphanumeric characters (e.g. `aws-cdk-lib` will be
`awscdklib`). You can use this field to set a specific package name.
- `moduleName` (required) - The name of the **target repository** in which this
iliapolo marked this conversation as resolved.
Show resolved Hide resolved
module will be published (e.g. `github.com/foo/bar`). The module itself will
*always* be published under a subdirectory named according to the Go package
name of the module (e.g. `github.com/foo/bar/awscdk`).
- `versionSuffix` (optional) - Can be provided that will be appended at the end
of the module version.

```json
This will add generated go package code to your specified `outDir` under
`go/PACKAGE_NAME` (e.g. `dist/go/awscdklib`).

```js
{
"jsii": {
"targets": {
"go": {},
"go": {
"moduleName": "github.com/foo/bar", // REQUIRED
"packageName": "hello", // OPTIONAL
"versionSuffix": "-devprefix" // OPTIONAL
},
// ...
},
// ...
Expand Down
1 change: 1 addition & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ defineTest(
},
go: {
moduleName: 'github.com/aws/jsii/jsii-calc/go',
versionSuffix: '-devpreview',
},
java: {
package: 'software.amazon.jsii.tests.calculator.lib',
Expand Down
3 changes: 2 additions & 1 deletion packages/@scope/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"outdir": "dist",
"targets": {
"go": {
"moduleName": "github.com/aws/jsii/jsii-calc/go"
"moduleName": "github.com/aws/jsii/jsii-calc/go",
"packageName": "jcb"
},
"java": {
"package": "software.amazon.jsii.tests.calculator.base",
Expand Down
3 changes: 2 additions & 1 deletion packages/@scope/jsii-calc-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"outdir": "dist",
"targets": {
"go": {
"moduleName": "github.com/aws/jsii/jsii-calc/go"
"moduleName": "github.com/aws/jsii/jsii-calc/go",
"versionSuffix": "-devpreview"
},
"java": {
"package": "software.amazon.jsii.tests.calculator.lib",
Expand Down
6 changes: 3 additions & 3 deletions packages/jsii-pacmak/lib/targets/go.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { shell } from '../util';
import { Documentation } from './go/documentation';
import { GOMOD_FILENAME, RootPackage } from './go/package';
import { JSII_INIT_PACKAGE } from './go/runtime';
import { goPackageName, tarballName } from './go/util';
import { tarballName } from './go/util';

export class Golang extends Target {
private readonly goGenerator: GoGenerator;
Expand All @@ -35,7 +35,7 @@ export class Golang extends Target {
// copy generated sources to the output directory
await this.copyFiles(sourceDir, outDir);

const pkgDir = path.join(outDir, goPackageName(this.assembly.name));
const pkgDir = path.join(outDir, this.goGenerator.rootPackage.packageName);

// write `local.go.mod` with "replace" directives for local modules
const localGoMod = await this.writeLocalGoMod(pkgDir);
Expand Down Expand Up @@ -154,7 +154,7 @@ class GoGenerator implements IGenerator {
tarball: string,
{ license, notice }: Legalese,
): Promise<any> {
const output = path.join(outDir, goPackageName(this.assembly.name));
const output = path.join(outDir, this.rootPackage.packageName);
await this.code.save(output);
await fs.copyFile(
tarball,
Expand Down
22 changes: 16 additions & 6 deletions packages/jsii-pacmak/lib/targets/go/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import {
JSII_INIT_ALIAS,
} from './runtime';
import { GoClass, GoType, Enum, GoInterface, Struct } from './types';
import { findTypeInTree, goPackageName, flatMap, tarballName } from './util';
import {
findTypeInTree,
goPackageNameForAssembly,
flatMap,
tarballName,
} from './util';
import { VersionFile } from './version-file';

export const GOMOD_FILENAME = 'go.mod';
Expand Down Expand Up @@ -206,21 +211,26 @@ export class RootPackage extends Package {
private readonly versionFile: VersionFile;

public constructor(assembly: Assembly) {
const packageName = goPackageName(assembly.name);
const goConfig = assembly.targets?.go ?? {};
const packageName = goPackageNameForAssembly(assembly);
const filePath = '';
const moduleName = assembly.targets?.go?.moduleName ?? '';
const moduleName = goConfig.moduleName ?? '';
let version = assembly.version;
if (goConfig.versionSuffix) {
version += goConfig.versionSuffix;
}
eladb marked this conversation as resolved.
Show resolved Hide resolved

super(
assembly.types,
assembly.submodules,
packageName,
filePath,
moduleName,
assembly.version,
version,
);

this.assembly = assembly;
this.version = assembly.version;
this.version = version;
this.versionFile = new VersionFile(this.version);

if (this.assembly.readme?.markdown) {
Expand Down Expand Up @@ -384,7 +394,7 @@ export class InternalPackage extends Package {
public readonly parent: Package;

public constructor(root: Package, parent: Package, assembly: JsiiSubmodule) {
const packageName = goPackageName(assembly.name);
const packageName = goPackageNameForAssembly(assembly);
const filePath =
parent === root ? packageName : `${parent.filePath}/${packageName}`;

Expand Down
13 changes: 10 additions & 3 deletions packages/jsii-pacmak/lib/targets/go/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Assembly } from 'jsii-reflect';
import { Assembly, Submodule } from 'jsii-reflect';

import { Package } from './package';
import { GoMethod, GoTypeMember, GoType } from './types';
Expand All @@ -24,8 +24,15 @@ export function findTypeInTree(
/*
* Format NPM package names as idiomatic Go module name
*/
export function goPackageName(name: string): string {
return name.replace(/[^a-z0-9.]/gi, '').toLowerCase();
export function goPackageNameForAssembly(
assembly: Assembly | Submodule,
): string {
const config = assembly.targets?.go ?? {};
if (config.packageName) {
return config.packageName;
}

return assembly.name.replace(/[^a-z0-9.]/gi, '').toLowerCase();
}

export function flatMap<T, R>(
Expand Down
Loading