Skip to content

Commit

Permalink
feat(go): version file in the generated module directory (#2492)
Browse files Browse the repository at this point in the history
Since golang version isn't available anywhere inside the module, we embed a version file so that consumers can access the version. 

This is needed for publishing [go modules](cdklabs/publib#23) using `jsii-release` without the user having to supply the version externally.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
iliapolo authored Feb 2, 2021
1 parent 32c0add commit da3ea25
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 32 deletions.
4 changes: 4 additions & 0 deletions packages/jsii-pacmak/lib/targets/go/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from './runtime';
import { GoClass, GoType, Enum, Interface, Struct } from './types';
import { findTypeInTree, goPackageName, flatMap } from './util';
import { VersionFile } from './version-file';

export const GOMOD_FILENAME = 'go.mod';
export const GO_VERSION = '1.15';
Expand Down Expand Up @@ -184,6 +185,7 @@ export class RootPackage extends Package {
public readonly assembly: Assembly;
public readonly version: string;
private readonly readme?: ReadmeFile;
private readonly versionFile: VersionFile;

public constructor(assembly: Assembly) {
const packageName = goPackageName(assembly.name);
Expand All @@ -201,6 +203,7 @@ export class RootPackage extends Package {

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

if (this.assembly.readme?.markdown) {
this.readme = new ReadmeFile(
Expand All @@ -216,6 +219,7 @@ export class RootPackage extends Package {
this.readme?.emit(context);

this.emitGomod(context.code);
this.versionFile.emit(context.code);
}

private emitGomod(code: CodeMaker) {
Expand Down
17 changes: 17 additions & 0 deletions packages/jsii-pacmak/lib/targets/go/version-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CodeMaker } from 'codemaker';

/**
* Represents the version of the go module. This is needed because the version is not
* available in standard go module files.
*/
export class VersionFile {
private static readonly NAME = 'version';

public constructor(private readonly version: string) {}

public emit(code: CodeMaker) {
code.openFile(VersionFile.NAME);
code.line(this.version);
code.closeFile(VersionFile.NAME);
}
}

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

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

0 comments on commit da3ea25

Please sign in to comment.