Skip to content

Commit

Permalink
feat: support default licenses for all languages in monorepo (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
agdimech committed Mar 21, 2024
1 parent e9ed3e9 commit ab8e648
Show file tree
Hide file tree
Showing 11 changed files with 20,497 additions and 1,332 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

73 changes: 71 additions & 2 deletions packages/monorepo/src/components/nx-configurator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/*! Copyright [Amazon.com](http://amazon.com/), Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
import * as path from "path";
import { Component, JsonFile, Project, Task, YamlFile } from "projen";
import {
Component,
JsonFile,
License,
Project,
Task,
TextFile,
YamlFile,
} from "projen";
import { JavaProject } from "projen/lib/java";
import { NodePackageManager, NodeProject } from "projen/lib/javascript";
import { Poetry, PythonProject } from "projen/lib/python";
Expand All @@ -11,6 +19,7 @@ import { Nx } from "../nx-types";
import { NodePackageUtils, ProjectUtils } from "../utils";

const DEFAULT_PYTHON_VERSION = "3";
const DEFAULT_LICENSE = "Apache-2.0";

/**
* Options for overriding nx build tasks
Expand Down Expand Up @@ -87,6 +96,31 @@ export interface INxProjectCore {
): void;
}

/**
* License options.
*
*/
export interface LicenseOptions {
/**
* License type (SPDX).
*
* @see https://github.com/projen/projen/tree/main/license-text for list of supported licenses
*/
readonly spdx?: string;

/**
* Copyright owner.
*
* If the license text for the given spdx has $copyright_owner, this option must be specified.
*/
readonly copyrightOwner?: string;

/**
* Arbitrary license text.
*/
readonly licenseText?: string;
}

/**
* NXConfigurator options.
*/
Expand All @@ -95,13 +129,21 @@ export interface NxConfiguratorOptions {
* Branch that NX affected should run against.
*/
readonly defaultReleaseBranch?: string;

/**
* Default package license config.
*
* If nothing is specified, all packages will default to Apache-2.0 (unless they have their own License component).
*/
readonly licenseOptions?: LicenseOptions;
}

/**
* Configues common NX related tasks and methods.
*/
export class NxConfigurator extends Component implements INxProjectCore {
public readonly nx: NxWorkspace;
private readonly licenseOptions?: LicenseOptions;
private nxPlugins: { [dep: string]: string } = {};

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

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

/**
* Add licenses to any subprojects which don't already have a license.
*/
private _addLicenses() {
this.project.subprojects
.filter(
(p) => p.components.find((c) => c instanceof License) === undefined
)
.forEach((p) => {
if (!this.licenseOptions || this.licenseOptions.spdx) {
new License(p, {
spdx: this.licenseOptions?.spdx ?? DEFAULT_LICENSE,
copyrightOwner: this.licenseOptions?.copyrightOwner,
});
} else if (!!this.licenseOptions?.licenseText) {
new TextFile(p, "LICENSE", {
marker: false,
committed: true,
lines: this.licenseOptions.licenseText.split("\n"),
});
} else {
throw new Error("Either spdx or licenseText must be specified.");
}
});
}

preSynthesize(): void {
// Calling before super() to ensure proper pre-synth of NxProject component and its nested components
this._ensureNxProjectGraph();
this._emitPackageJson();
this._invokeInstallCITasks();
this.patchPythonProjects([this.project]);
this._addLicenses();
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/monorepo/src/projects/java/monorepo-java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { JavaProjectOptions } from "./java-project-options";
import {
NxConfigurator,
INxProjectCore,
LicenseOptions,
} from "../../components/nx-configurator";
import { NxWorkspace } from "../../components/nx-workspace";
import {
Expand All @@ -25,6 +26,13 @@ const MVN_PLUGIN_PATH = "./.nx/plugins/nx_plugin.js";
*/
export interface MonorepoJavaOptions extends JavaProjectOptions {
readonly defaultReleaseBranch?: string;

/**
* Default license to apply to all PDK managed packages.
*
* @default Apache-2.0
*/
readonly licenseOptions?: LicenseOptions;
}

/**
Expand Down Expand Up @@ -72,6 +80,7 @@ export class MonorepoJavaProject extends JavaProject implements INxProjectCore {

this.nxConfigurator = new NxConfigurator(this, {
defaultReleaseBranch: options.defaultReleaseBranch ?? "main",
licenseOptions: options.licenseOptions,
});

// Setup maven nx plugin
Expand Down
9 changes: 9 additions & 0 deletions packages/monorepo/src/projects/python/monorepo-py.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Poetry, PythonProject } from "projen/lib/python";
import { PythonProjectOptions } from "./python-project-options";
import {
INxProjectCore,
LicenseOptions,
NxConfigurator,
} from "../../components/nx-configurator";
import { NxProject } from "../../components/nx-project";
Expand All @@ -23,6 +24,13 @@ import { NodePackageUtils, ProjectUtils } from "../../utils";
*/
export interface MonorepoPythonProjectOptions extends PythonProjectOptions {
readonly defaultReleaseBranch?: string;

/**
* Default license to apply to all PDK managed packages.
*
* @default Apache-2.0
*/
readonly licenseOptions?: LicenseOptions;
}

/**
Expand Down Expand Up @@ -70,6 +78,7 @@ export class MonorepoPythonProject

this.nxConfigurator = new NxConfigurator(this, {
defaultReleaseBranch: options.defaultReleaseBranch ?? "main",
licenseOptions: options.licenseOptions,
});

// Setup python NX plugin
Expand Down

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

9 changes: 9 additions & 0 deletions packages/monorepo/src/projects/typescript/monorepo-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { TypeScriptProjectOptions } from "./typescript-project-options";
import {
NxConfigurator,
INxProjectCore,
LicenseOptions,
} from "../../components/nx-configurator";
import { NxProject } from "../../components/nx-project";
import { NxWorkspace } from "../../components/nx-workspace";
Expand Down Expand Up @@ -127,6 +128,13 @@ export interface MonorepoTsProjectOptions extends TypeScriptProjectOptions {
* @default false
*/
readonly disableNodeWarnings?: boolean;

/**
* Default license to apply to all PDK managed packages.
*
* @default Apache-2.0
*/
readonly licenseOptions?: LicenseOptions;
}

/**
Expand Down Expand Up @@ -195,6 +203,7 @@ export class MonorepoTsProject

this.nxConfigurator = new NxConfigurator(this, {
defaultReleaseBranch,
licenseOptions: options.licenseOptions,
});
this._options = options;

Expand Down

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

Loading

0 comments on commit ab8e648

Please sign in to comment.