Skip to content

Commit ab8e648

Browse files
authored
feat: support default licenses for all languages in monorepo (#730)
1 parent e9ed3e9 commit ab8e648

File tree

11 files changed

+20497
-1332
lines changed

11 files changed

+20497
-1332
lines changed

packages/infrastructure/test/projects/java/__snapshots__/infrastructure-java-project.test.ts.snap

+1,236
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/infrastructure/test/projects/python/__snapshots__/infrastructure-py-project.test.ts.snap

+1,236
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/monorepo/src/components/nx-configurator.ts

+71-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
22
SPDX-License-Identifier: Apache-2.0 */
33
import * as path from "path";
4-
import { Component, JsonFile, Project, Task, YamlFile } from "projen";
4+
import {
5+
Component,
6+
JsonFile,
7+
License,
8+
Project,
9+
Task,
10+
TextFile,
11+
YamlFile,
12+
} from "projen";
513
import { JavaProject } from "projen/lib/java";
614
import { NodePackageManager, NodeProject } from "projen/lib/javascript";
715
import { Poetry, PythonProject } from "projen/lib/python";
@@ -11,6 +19,7 @@ import { Nx } from "../nx-types";
1119
import { NodePackageUtils, ProjectUtils } from "../utils";
1220

1321
const DEFAULT_PYTHON_VERSION = "3";
22+
const DEFAULT_LICENSE = "Apache-2.0";
1423

1524
/**
1625
* Options for overriding nx build tasks
@@ -87,6 +96,31 @@ export interface INxProjectCore {
8796
): void;
8897
}
8998

99+
/**
100+
* License options.
101+
*
102+
*/
103+
export interface LicenseOptions {
104+
/**
105+
* License type (SPDX).
106+
*
107+
* @see https://github.com/projen/projen/tree/main/license-text for list of supported licenses
108+
*/
109+
readonly spdx?: string;
110+
111+
/**
112+
* Copyright owner.
113+
*
114+
* If the license text for the given spdx has $copyright_owner, this option must be specified.
115+
*/
116+
readonly copyrightOwner?: string;
117+
118+
/**
119+
* Arbitrary license text.
120+
*/
121+
readonly licenseText?: string;
122+
}
123+
90124
/**
91125
* NXConfigurator options.
92126
*/
@@ -95,13 +129,21 @@ export interface NxConfiguratorOptions {
95129
* Branch that NX affected should run against.
96130
*/
97131
readonly defaultReleaseBranch?: string;
132+
133+
/**
134+
* Default package license config.
135+
*
136+
* If nothing is specified, all packages will default to Apache-2.0 (unless they have their own License component).
137+
*/
138+
readonly licenseOptions?: LicenseOptions;
98139
}
99140

100141
/**
101142
* Configues common NX related tasks and methods.
102143
*/
103144
export class NxConfigurator extends Component implements INxProjectCore {
104145
public readonly nx: NxWorkspace;
146+
private readonly licenseOptions?: LicenseOptions;
105147
private nxPlugins: { [dep: string]: string } = {};
106148

107149
constructor(project: Project, options?: NxConfiguratorOptions) {
@@ -134,6 +176,7 @@ export class NxConfigurator extends Component implements INxProjectCore {
134176
description: "Generate dependency graph for monorepo",
135177
});
136178

179+
this.licenseOptions = options?.licenseOptions;
137180
this.nx = NxWorkspace.of(project) || new NxWorkspace(project);
138181
this.nx.affected.defaultBase = options?.defaultReleaseBranch ?? "mainline";
139182
}
@@ -453,12 +496,38 @@ export class NxConfigurator extends Component implements INxProjectCore {
453496
task?.exec(cmd, { receiveArgs: true });
454497
}
455498

499+
/**
500+
* Add licenses to any subprojects which don't already have a license.
501+
*/
502+
private _addLicenses() {
503+
this.project.subprojects
504+
.filter(
505+
(p) => p.components.find((c) => c instanceof License) === undefined
506+
)
507+
.forEach((p) => {
508+
if (!this.licenseOptions || this.licenseOptions.spdx) {
509+
new License(p, {
510+
spdx: this.licenseOptions?.spdx ?? DEFAULT_LICENSE,
511+
copyrightOwner: this.licenseOptions?.copyrightOwner,
512+
});
513+
} else if (!!this.licenseOptions?.licenseText) {
514+
new TextFile(p, "LICENSE", {
515+
marker: false,
516+
committed: true,
517+
lines: this.licenseOptions.licenseText.split("\n"),
518+
});
519+
} else {
520+
throw new Error("Either spdx or licenseText must be specified.");
521+
}
522+
});
523+
}
524+
456525
preSynthesize(): void {
457-
// Calling before super() to ensure proper pre-synth of NxProject component and its nested components
458526
this._ensureNxProjectGraph();
459527
this._emitPackageJson();
460528
this._invokeInstallCITasks();
461529
this.patchPythonProjects([this.project]);
530+
this._addLicenses();
462531
}
463532

464533
/**

packages/monorepo/src/projects/java/monorepo-java.ts

+9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { JavaProjectOptions } from "./java-project-options";
99
import {
1010
NxConfigurator,
1111
INxProjectCore,
12+
LicenseOptions,
1213
} from "../../components/nx-configurator";
1314
import { NxWorkspace } from "../../components/nx-workspace";
1415
import {
@@ -25,6 +26,13 @@ const MVN_PLUGIN_PATH = "./.nx/plugins/nx_plugin.js";
2526
*/
2627
export interface MonorepoJavaOptions extends JavaProjectOptions {
2728
readonly defaultReleaseBranch?: string;
29+
30+
/**
31+
* Default license to apply to all PDK managed packages.
32+
*
33+
* @default Apache-2.0
34+
*/
35+
readonly licenseOptions?: LicenseOptions;
2836
}
2937

3038
/**
@@ -72,6 +80,7 @@ export class MonorepoJavaProject extends JavaProject implements INxProjectCore {
7280

7381
this.nxConfigurator = new NxConfigurator(this, {
7482
defaultReleaseBranch: options.defaultReleaseBranch ?? "main",
83+
licenseOptions: options.licenseOptions,
7584
});
7685

7786
// Setup maven nx plugin

packages/monorepo/src/projects/python/monorepo-py.ts

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Poetry, PythonProject } from "projen/lib/python";
77
import { PythonProjectOptions } from "./python-project-options";
88
import {
99
INxProjectCore,
10+
LicenseOptions,
1011
NxConfigurator,
1112
} from "../../components/nx-configurator";
1213
import { NxProject } from "../../components/nx-project";
@@ -23,6 +24,13 @@ import { NodePackageUtils, ProjectUtils } from "../../utils";
2324
*/
2425
export interface MonorepoPythonProjectOptions extends PythonProjectOptions {
2526
readonly defaultReleaseBranch?: string;
27+
28+
/**
29+
* Default license to apply to all PDK managed packages.
30+
*
31+
* @default Apache-2.0
32+
*/
33+
readonly licenseOptions?: LicenseOptions;
2634
}
2735

2836
/**
@@ -70,6 +78,7 @@ export class MonorepoPythonProject
7078

7179
this.nxConfigurator = new NxConfigurator(this, {
7280
defaultReleaseBranch: options.defaultReleaseBranch ?? "main",
81+
licenseOptions: options.licenseOptions,
7382
});
7483

7584
// Setup python NX plugin

packages/monorepo/src/projects/python/python-project-options.ts

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/monorepo/src/projects/typescript/monorepo-ts.ts

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { TypeScriptProjectOptions } from "./typescript-project-options";
1818
import {
1919
NxConfigurator,
2020
INxProjectCore,
21+
LicenseOptions,
2122
} from "../../components/nx-configurator";
2223
import { NxProject } from "../../components/nx-project";
2324
import { NxWorkspace } from "../../components/nx-workspace";
@@ -127,6 +128,13 @@ export interface MonorepoTsProjectOptions extends TypeScriptProjectOptions {
127128
* @default false
128129
*/
129130
readonly disableNodeWarnings?: boolean;
131+
132+
/**
133+
* Default license to apply to all PDK managed packages.
134+
*
135+
* @default Apache-2.0
136+
*/
137+
readonly licenseOptions?: LicenseOptions;
130138
}
131139

132140
/**
@@ -195,6 +203,7 @@ export class MonorepoTsProject
195203

196204
this.nxConfigurator = new NxConfigurator(this, {
197205
defaultReleaseBranch,
206+
licenseOptions: options.licenseOptions,
198207
});
199208
this._options = options;
200209

packages/monorepo/src/projects/typescript/typescript-project-options.ts

-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)