Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pacmak): emit LICENSE file with SPDX license text, NOTICE file #2604

Merged
merged 5 commits into from Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 25 additions & 1 deletion packages/jsii-pacmak/lib/generator.ts
Expand Up @@ -46,11 +46,35 @@ export interface IGenerator {

/**
* Determine if the generated artifacts for this generator are already up-to-date.
*
* @param outDir the directory where generated artifacts would be placed.
* @param tarball the tarball of the bundled node library
* @param legalese the license and notice file contents (if any)
*
* @return ``true`` if no generation is necessary
*/
upToDate(outDir: string): Promise<boolean>;
save(outdir: string, tarball: string): Promise<any>;

/**
* Saves the generated code in the provided output directory.
*
* @param outdir the directory in which to place generated code.
* @param tarball the bundled npm library backing the generated code.
* @param legalese the LICENSE & NOTICE contents for this package.
*/
save(outdir: string, tarball: string, legalese: Legalese): Promise<any>;
}

export interface Legalese {
/**
* The text of the SPDX license associated with this package, if any.
*/
readonly license?: string;

/**
* The contents of the NOTICE file for this package, if any.
*/
readonly notice?: string;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion packages/jsii-pacmak/lib/target.ts
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs-extra';
import * as reflect from 'jsii-reflect';
import { Rosetta } from 'jsii-rosetta';
import * as path from 'path';
import * as spdx from 'spdx-license-list/full';

import { traverseDependencyGraph } from './dependency-graph';
import { IGenerator } from './generator';
Expand Down Expand Up @@ -39,7 +40,18 @@ export abstract class Target {

if (this.force || !(await this.generator.upToDate(outDir))) {
this.generator.generate(this.fingerprint);
await this.generator.save(outDir, tarball);

const licenseFile = path.join(this.packageDir, 'LICENSE');
const license = (await fs.pathExists(licenseFile))
? await fs.readFile(licenseFile, 'utf8')
: spdx[this.assembly.license]?.licenseText;

const noticeFile = path.join(this.packageDir, 'NOTICE');
const notice = (await fs.pathExists(noticeFile))
? await fs.readFile(noticeFile, 'utf8')
: undefined;

await this.generator.save(outDir, tarball, { license, notice });
} else {
logging.info(
`Generated code for ${this.targetName} was already up-to-date in ${outDir} (use --force to re-generate)`,
Expand Down
20 changes: 18 additions & 2 deletions packages/jsii-pacmak/lib/targets/go.ts
Expand Up @@ -4,7 +4,7 @@ import { Assembly } from 'jsii-reflect';
import { Rosetta } from 'jsii-rosetta';
import * as path from 'path';

import { IGenerator } from '../generator';
import { IGenerator, Legalese } from '../generator';
import * as logging from '../logging';
import { findLocalBuildDirs, Target, TargetOptions } from '../target';
import { shell } from '../util';
Expand Down Expand Up @@ -147,11 +147,27 @@ class GoGenerator implements IGenerator {
});
}

public async save(outDir: string, tarball: string): Promise<any> {
public async save(
outDir: string,
tarball: string,
{ license, notice }: Legalese,
): Promise<any> {
await this.embedTarball(tarball);

const output = path.join(outDir, goPackageName(this.assembly.name));
await this.code.save(output);

if (license) {
await fs.writeFile(path.join(output, 'LICENSE'), license, {
encoding: 'utf8',
});
}

if (notice) {
await fs.writeFile(path.join(output, 'NOTICE'), notice, {
encoding: 'utf8',
});
}
}

private async embedTarball(source: string) {
Expand Down