diff --git a/src/cdk/schematics/testing/index.ts b/src/cdk/schematics/testing/index.ts index b8387646f1f1..bf95b5d4c4b6 100644 --- a/src/cdk/schematics/testing/index.ts +++ b/src/cdk/schematics/testing/index.ts @@ -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'; diff --git a/src/cdk/schematics/testing/test-app.ts b/src/cdk/schematics/testing/test-app.ts index f70f80ca7034..98274d39d94a 100644 --- a/src/cdk/schematics/testing/test-app.ts +++ b/src/cdk/schematics/testing/test-app.ts @@ -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 { - 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); } diff --git a/src/cdk/schematics/testing/test-library.ts b/src/cdk/schematics/testing/test-library.ts new file mode 100644 index 000000000000..3306a8a5a3cd --- /dev/null +++ b/src/cdk/schematics/testing/test-library.ts @@ -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 { + return createTestProject(runner, 'library', appOptions, tree); +} diff --git a/src/cdk/schematics/testing/test-project.ts b/src/cdk/schematics/testing/test-project.ts new file mode 100644 index 000000000000..4f09fb6a94ff --- /dev/null +++ b/src/cdk/schematics/testing/test-project.ts @@ -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 { + 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(); +} diff --git a/src/material/schematics/ng-add/index.spec.ts b/src/material/schematics/ng-add/index.spec.ts index 3875bae0cf93..d54132ba0cc2 100644 --- a/src/material/schematics/ng-add/index.spec.ts +++ b/src/material/schematics/ng-add/index.spec.ts @@ -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'; @@ -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/); + }); +}); diff --git a/src/material/schematics/ng-add/setup-project.ts b/src/material/schematics/ng-add/setup-project.ts index 1ce7a29ab957..448f1e230947 100644 --- a/src/material/schematics/ng-add/setup-project.ts +++ b/src/material/schematics/ng-add/setup-project.ts @@ -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; + }; } /**