Skip to content

fix(ng-add): ng add @angular/material fails in library projects #19164

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

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/cdk/schematics/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
export * from './post-scheduled-tasks';
export * from './test-app';
export * from './test-case-setup';
export * from './test-library';
export * from './file-content';
export * from './resolve-bazel-path';
11 changes: 3 additions & 8 deletions src/cdk/schematics/testing/test-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
import {Tree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';

import {createTestProject} from './test-project';

/** Create a base app used for testing. */
export async function createTestApp(runner: SchematicTestRunner, appOptions = {}, tree?: Tree):
Promise<UnitTestTree> {
const workspaceTree = await runner.runExternalSchematicAsync('@schematics/angular', 'workspace', {
name: 'workspace',
version: '6.0.0',
newProjectRoot: 'projects',
}, tree).toPromise();

return runner.runExternalSchematicAsync('@schematics/angular', 'application',
{name: 'material', ...appOptions}, workspaceTree).toPromise();
return createTestProject(runner, 'application', appOptions, tree);
}
18 changes: 18 additions & 0 deletions src/cdk/schematics/testing/test-library.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Tree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';

import {createTestProject} from './test-project';

/** Create a base library used for testing. */
export async function createTestLibrary(runner: SchematicTestRunner, appOptions = {}, tree?: Tree):
Promise<UnitTestTree> {
return createTestProject(runner, 'library', appOptions, tree);
}
24 changes: 24 additions & 0 deletions src/cdk/schematics/testing/test-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Tree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';

/** Create a base project used for testing. */
export async function createTestProject(
runner: SchematicTestRunner, projectType: 'application'|'library', appOptions = {}, tree?: Tree):
Promise<UnitTestTree> {
const workspaceTree = await runner.runExternalSchematicAsync('@schematics/angular', 'workspace', {
name: 'workspace',
version: '6.0.0',
newProjectRoot: 'projects',
}, tree).toPromise();

return runner.runExternalSchematicAsync('@schematics/angular', projectType,
{name: 'material', ...appOptions}, workspaceTree).toPromise();
}
32 changes: 31 additions & 1 deletion src/material/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getProjectStyleFile,
getProjectTargetOptions,
} from '@angular/cdk/schematics';
import {createTestApp, getFileContent} from '@angular/cdk/schematics/testing';
import {createTestApp, createTestLibrary, getFileContent} from '@angular/cdk/schematics/testing';
import {getWorkspace} from '@schematics/angular/utility/config';
import {COLLECTION_PATH} from '../index.spec';
import {addPackageToPackageJson} from './package-config';
Expand Down Expand Up @@ -419,3 +419,33 @@ describe('ng-add schematic', () => {
});
});
});

describe('ng-add schematic - library project', () => {
let runner: SchematicTestRunner;
let libraryTree: Tree;
let errorOutput: string[];
let warnOutput: string[];

beforeEach(async () => {
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json'));
libraryTree = await createTestLibrary(runner);

errorOutput = [];
warnOutput = [];
runner.logger.subscribe(e => {
if (e.level === 'error') {
errorOutput.push(e.message);
} else if (e.level === 'warn') {
warnOutput.push(e.message);
}
});
});

it('should warn if a library project is targeted', async () => {
await runner.runSchematicAsync('ng-add-setup-project', {}, libraryTree).toPromise();

expect(errorOutput.length).toBe(0);
expect(warnOutput.length).toBe(1);
expect(warnOutput[0]).toMatch(/There is no additional setup required/);
});
});
27 changes: 20 additions & 7 deletions src/material/schematics/ng-add/setup-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@ const noopAnimationsModuleName = 'NoopAnimationsModule';
* - Adds Browser Animation to app.module
*/
export default function(options: Schema): Rule {
return chain([
addAnimationsModule(options),
addThemeToAppStyles(options),
addFontsToIndex(options),
addMaterialAppStyles(options),
addTypographyClass(options),
]);
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);

if (project.projectType === 'application') {
return chain([
addAnimationsModule(options),
addThemeToAppStyles(options),
addFontsToIndex(options),
addMaterialAppStyles(options),
addTypographyClass(options),
]);
}
context.logger.warn(
'Angular Material has been set up in your workspace. There is no additional setup ' +
'required for consuming Angular Material in your library project.\n\n' +
'If you intended to run the schematic on a different project, pass the `--project` ' +
'option.');
return host;
};
}

/**
Expand Down