Skip to content

Commit

Permalink
chore: generate go.mod files
Browse files Browse the repository at this point in the history
Closes aws#2090
  • Loading branch information
SoManyHs committed Nov 19, 2020
1 parent 6da0870 commit d1b30b4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 10 deletions.
45 changes: 35 additions & 10 deletions packages/jsii-pacmak/lib/targets/go/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
import { GoClass, GoType, Enum, Interface, Struct } from './types';
import { findTypeInTree, goPackageName, flatMap } from './util';

export const GOMOD_FILENAME = 'go.mod';
export const GO_VERSION = '1.15';

/*
* Package represents a single `.go` source file within a package. This can be the root package file or a submodule
*/
Expand Down Expand Up @@ -66,18 +69,22 @@ export abstract class Package {
}

/*
* The module names of this modules dependencies. Used for import statements
* goModuleName returns the full path to the module name.
* Used for import statements and go.mod generation
*/
public get goModuleName(): string {
const moduleName = this.root.moduleName;
const prefix = moduleName !== '' ? `${moduleName}/` : '';
const rootPackageName = this.root.packageName;
const suffix = this.filePath !== '' ? `/${this.filePath}` : ``;
return `${prefix}${rootPackageName}${suffix}`;
}

/*
* The module names of this module's dependencies. Used for import statements.
*/
public get dependencyImports(): Set<string> {
return new Set(
this.dependencies.map((pack) => {
const moduleName = pack.root.moduleName;
const prefix = moduleName !== '' ? `${moduleName}/` : '';
const rootPackageName = pack.root.packageName;
const suffix = pack.filePath !== '' ? `/${pack.filePath}` : ``;
return `${prefix}${rootPackageName}${suffix}`;
}),
);
return new Set(this.dependencies.map((pkg) => pkg.goModuleName));
}

/*
Expand All @@ -89,6 +96,7 @@ export abstract class Package {

public emit(context: EmitContext): void {
const { code } = context;

code.openFile(this.file);
this.emitHeader(code);
this.emitImports(code);
Expand Down Expand Up @@ -189,8 +197,25 @@ export class RootPackage extends Package {
super.emit(context);
this.emitJsiiPackage(context);
this.readme?.emit(context);

this.emitGomod(context.code);
}

private emitGomod(code: CodeMaker) {
code.openFile(GOMOD_FILENAME);
code.line(`module ${this.goModuleName}`);
code.line();
code.line(`go ${GO_VERSION}`);
code.line();
code.open('require (');
for (const packageName of this.dependencyImports) {
code.line(`${packageName}`);
}
code.close(')');
code.closeFile(GOMOD_FILENAME);
}


/*
* Override package findType for root Package.
*
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1b30b4

Please sign in to comment.