Skip to content

Commit

Permalink
feat!: allow publishing to subset of jsii target languages for experi…
Browse files Browse the repository at this point in the history
…mental modules (#12)

* add missing test

* add jsii target language option and remove from cdklabs ts project

* fix import order

* add test suite
  • Loading branch information
kaizencc authored Jan 20, 2023
1 parent c6e85f8 commit 7153a20
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 67 deletions.
75 changes: 53 additions & 22 deletions API.md

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

99 changes: 61 additions & 38 deletions src/cdklabs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { UpdateSnapshot } from 'projen/lib/javascript';
import { deepMerge } from 'projen/lib/util';

export enum JsiiLanguage {
PYTHON,
JAVA,
DOTNET,
GO,
};

import {
CdkConstructLibrary,
CdkConstructLibraryOptions,
Expand Down Expand Up @@ -30,27 +37,46 @@ const cdklabsDefaultProps = {
defaultReleaseBranch: 'main',
};

function createCdklabsPublishingDefaults(npmPackageName: string) {
function createCdklabsPublishingDefaults(npmPackageName: string, langs?: JsiiLanguage[]) {
return {
publishToPypi: {
distName: npmPackageName,
module: changeDelimiter(npmPackageName, '_'),
},
publishToMaven: {
javaPackage: `io.github.cdklabs.${changeDelimiter(npmPackageName, '.')}`,
mavenGroupId: 'io.github.cdklabs',
mavenArtifactId: npmPackageName,
mavenEndpoint: 'https://s01.oss.sonatype.org',
},
publishToNuget: {
dotNetNamespace: `Cdklabs${upperCaseName(npmPackageName)}`,
packageId: `Cdklabs${upperCaseName(npmPackageName)}`,
},
publishToGo: {
moduleName: `github.com/cdklabs/${npmPackageName}-go`,
},
...publishLanguageWrapper(JsiiLanguage.PYTHON, {
publishToPypi: {
distName: npmPackageName,
module: changeDelimiter(npmPackageName, '_'),
},
}),
...publishLanguageWrapper(JsiiLanguage.JAVA, {
publishToMaven: {
javaPackage: `io.github.cdklabs.${changeDelimiter(npmPackageName, '.')}`,
mavenGroupId: 'io.github.cdklabs',
mavenArtifactId: npmPackageName,
mavenEndpoint: 'https://s01.oss.sonatype.org',
},
}),
...publishLanguageWrapper(JsiiLanguage.DOTNET, {
publishToNuget: {
dotNetNamespace: `Cdklabs${upperCaseName(npmPackageName)}`,
packageId: `Cdklabs${upperCaseName(npmPackageName)}`,
},
}),
...publishLanguageWrapper(JsiiLanguage.GO, {
publishToGo: {
moduleName: `github.com/cdklabs/${npmPackageName}-go`,
},
}),
};

function publishLanguageWrapper(lang: JsiiLanguage, obj: Record<string, any>) {
return publishLanguage(lang) ? obj : {};
}

function publishLanguage(lang: JsiiLanguage): boolean {
// langs not specified === all languages published
if (!langs) { return true; }
if (langs.includes(lang)) { return true; }
return false;
}

function upperCaseName(str: string) {
let words = str.split('-');
words = words.map((w) => w[0].toUpperCase() + w.substring(1));
Expand All @@ -75,6 +101,16 @@ export interface CdklabsConstructLibraryOptions extends CdkConstructLibraryOptio
* @default true
*/
readonly cdklabsPublishingDefaults?: boolean;

/**
* Specify specific languages to publish to. This can be used when the library
* is experimental only, because stable libraries must publish to all jsii languages.
* This should be used in conjunction with `cdklabsPublishingDefaults: true`; otherwise
* it is a no-op.
*
* @default - all jsii target languages
*/
readonly jsiiTargetLanguages?: JsiiLanguage[];
}

/**
Expand All @@ -84,10 +120,12 @@ export interface CdklabsConstructLibraryOptions extends CdkConstructLibraryOptio
*/
export class CdklabsConstructLibrary extends CdkConstructLibrary {
constructor(options: CdklabsConstructLibraryOptions) {
const cdklabsPublishingDefaultProps = (options.cdklabsPublishingDefaults ?? true) ?
createCdklabsPublishingDefaults(options.name) : {};
const cdklabsPublishingDefaultProps: Record<string, any> = (options.cdklabsPublishingDefaults ?? true) ?
createCdklabsPublishingDefaults(options.name, options.jsiiTargetLanguages) : {};

// the leftmost object is mutated and returned by deepMerge
const mergedOptions = deepMerge([
{},
cdklabsDefaultProps,
cdklabsPublishingDefaultProps,
options,
Expand All @@ -98,20 +136,7 @@ export class CdklabsConstructLibrary extends CdkConstructLibrary {
}
}

export interface CdklabsTypeScriptProjectOptions extends CdkTypeScriptProjectOptions {
/**
* Set default publishing properties. Setting this property guarantees
* that your project will have reasonable publishing names. You can choose
* to modify them however you wish with the traditional `publishToPypi`,
* `publishToMaven`, `publishToNuget`, and `publishToGo` properties, and
* your configuration will be respected.
*
* This should be set to false only if you do not plan on releasing the package.
*
* @default true
*/
readonly cdklabsPublishingDefaults?: boolean;
}
export interface CdklabsTypeScriptProjectOptions extends CdkTypeScriptProjectOptions { }

/**
* Create a Cdklabs TypeScript Project
Expand All @@ -120,12 +145,10 @@ export interface CdklabsTypeScriptProjectOptions extends CdkTypeScriptProjectOpt
*/
export class CdklabsTypeScriptProject extends CdkTypeScriptProject {
constructor(options: CdklabsTypeScriptProjectOptions) {
const cdklabsPublishingDefaultProps = (options.cdklabsPublishingDefaults ?? true) ?
createCdklabsPublishingDefaults(options.name) : {};

// the leftmost object is mutated and returned by deepMerge
const mergedOptions = deepMerge([
{},
cdklabsDefaultProps,
cdklabsPublishingDefaultProps,
options,
cdklabsForcedProps,
]) as CdkConstructLibraryOptions;
Expand Down
25 changes: 22 additions & 3 deletions test/cdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { CdkConstructLibrary, CdkConstructLibraryOptions, CdkTypeScriptProject,
describe('CdkConstructLibrary', () => {
test('synthesizes with default settings', () => {
const project = new TestCdkConstructLibrary();

const outdir = Testing.synth(project);

// defaults to private
Expand Down Expand Up @@ -39,8 +38,28 @@ describe('CdkConstructLibrary', () => {
].join('\n'));
});

test('thorws when a publishing to a subset of languages', () => {

test('throws when a publishing to a subset of languages', () => {
expect(() => {
new TestCdkConstructLibrary({
stability: Stability.STABLE,
publishToPypi: {
distName: 'distName',
module: 'module',
},
publishToMaven: {
javaPackage: 'javaPackage',
mavenArtifactId: 'mavenArtifactId',
mavenGroupId: 'mavenGroupId',
},
publishToNuget: {
dotNetNamespace: 'dotNetNamespace',
packageId: 'packageId',
},
});
}).toThrowError([
'The project does not pass stability requirements due to the following errors:',
' Publishing Error: project not configured to publish to Go',
].join('\n'));
});
});
});
Expand Down
Loading

0 comments on commit 7153a20

Please sign in to comment.