-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bb0a40
commit 3bca363
Showing
34 changed files
with
1,407 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// WORKAROUND https://github.com/angular/angular/issues/18810 | ||
// This file is required to run ngc on angular libraries, to write files like | ||
// node_modules/@angular/core/core.ngsummary.json | ||
{ | ||
"compilerOptions": { | ||
"lib": [ | ||
"dom", | ||
"es2015" | ||
], | ||
"experimentalDecorators": true, | ||
"types": [] | ||
}, | ||
"include": [ | ||
"node_modules/@angular/**/*" | ||
], | ||
"exclude": [ | ||
"node_modules/@angular/compiler-cli/**", | ||
"node_modules/@angular/**/testing/**" | ||
] | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json", | ||
"schematics": { | ||
"ng-add": { | ||
"description": "Installs the Mosaic CDK", | ||
"factory": "./ng-add/index", | ||
"schema": "./ng-add/schema.json", | ||
"aliases": [ | ||
"install" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './utils'; | ||
export * from './testing'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json", | ||
"schematics": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { getFileContent } from '@schematics/angular/utility/test'; | ||
|
||
import { createTestApp } from '../testing'; | ||
|
||
|
||
describe('CDK ng-add', () => { | ||
let runner: SchematicTestRunner; | ||
let appTree: Tree; | ||
|
||
beforeEach(() => { | ||
runner = new SchematicTestRunner('schematics', require.resolve('../collection.json')); | ||
appTree = createTestApp(runner); | ||
}); | ||
|
||
it('should update the package.json', () => { | ||
const tree = runner.runSchematic('ng-add', {}, appTree); | ||
const packageJson = JSON.parse(getFileContent(tree, '/package.json')); | ||
const dependencies = packageJson.dependencies; | ||
|
||
expect(dependencies['@ptsecurity/cdk']).toBeDefined(); | ||
expect(Object.keys(dependencies)).toEqual(Object.keys(dependencies).sort(), | ||
'Expected the modified "dependencies" to be sorted alphabetically.'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Rule, Tree } from '@angular-devkit/schematics'; | ||
|
||
import { addPackageToPackageJson } from './package-config'; | ||
|
||
|
||
/** Name of the CDK version that is shipped together with the schematics. */ | ||
export const cdkVersion = loadPackageVersionGracefully('@ptsecurity/cdk'); | ||
|
||
/** | ||
* Schematic factory entry-point for the `ng-add` schematic. The ng-add schematic will be | ||
* automatically executed if developers run `ng add @angular/cdk`. | ||
*/ | ||
export default function(): Rule { | ||
return (host: Tree) => { | ||
// By default, the CLI already installs the package that has been installed through `ng add`. | ||
// We just store the version in the `package.json` in case the package manager didn't. | ||
addPackageToPackageJson(host, '@ptsecurity/cdk', `^${cdkVersion}`); | ||
}; | ||
} | ||
|
||
/** Loads the full version from the given Angular package gracefully. */ | ||
function loadPackageVersionGracefully(packageName: string): string | null { | ||
try { | ||
return require(`${packageName}/package.json`).version; | ||
} catch { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
|
||
|
||
/** | ||
* Sorts the keys of the given object. | ||
* @returns A new object instance with sorted keys | ||
*/ | ||
function sortObjectByKeys(obj: object) { | ||
return Object.keys(obj).sort().reduce((result, key) => (result[key] = obj[key]) && result, {}); | ||
} | ||
|
||
/** Adds a package to the package.json in the given host tree. */ | ||
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree { | ||
|
||
if (host.exists('package.json')) { | ||
const sourceText = host.read('package.json')!.toString('utf-8'); | ||
const json = JSON.parse(sourceText); | ||
|
||
if (!json.dependencies) { | ||
json.dependencies = {}; | ||
} | ||
|
||
if (!json.dependencies[pkg]) { | ||
json.dependencies[pkg] = version; | ||
json.dependencies = sortObjectByKeys(json.dependencies); | ||
} | ||
|
||
host.overwrite('package.json', JSON.stringify(json, null, 2)); | ||
} | ||
|
||
return host; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "angular-cdk-ng-add", | ||
"title": "Angular CDK ng-add", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project.", | ||
"$default": { | ||
"$source": "projectName" | ||
} | ||
} | ||
}, | ||
"required": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Schema { | ||
|
||
/** Name of the project to target. */ | ||
project: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './post-scheduled-tasks'; | ||
export * from './test-app'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { EngineHost, TaskExecutor, TaskScheduler } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { from as observableFrom, Observable } from 'rxjs'; | ||
import { concatMap, filter, last } from 'rxjs/operators'; | ||
|
||
|
||
/** | ||
* Due to the fact that the Angular devkit does not support running scheduled tasks from a | ||
* schematic that has been launched through the TestRunner, we need to manually find the task | ||
* executor for the given task name and run all scheduled instances. | ||
* | ||
* Note that this means that there can be multiple tasks with the same name. The observable emits | ||
* only when all tasks finished executing. | ||
*/ | ||
export function runPostScheduledTasks(runner: SchematicTestRunner, taskName: string) | ||
: Observable<any> { | ||
|
||
// Workaround until there is a public API to run scheduled tasks in the @angular-devkit. | ||
// See: https://github.com/angular/angular-cli/issues/11739 | ||
const host = runner.engine['_host'] as EngineHost<{}, {}>; | ||
const tasks = runner.engine['_taskSchedulers'] as TaskScheduler[]; | ||
const createTaskExecutor = (name: string) => | ||
(host.createTaskExecutor(name) as any) as Observable<TaskExecutor<any>>; | ||
|
||
return observableFrom(tasks).pipe( | ||
concatMap((scheduler) => scheduler.finalize()), | ||
filter((task) => task.configuration.name === taskName), | ||
concatMap((task) => { | ||
return createTaskExecutor(task.configuration.name) | ||
.pipe(concatMap((executor) => executor(task.configuration.options, task.context))); | ||
}), | ||
// Only emit the last emitted value because there can be multiple tasks with the same name. | ||
// The observable should only emit a value if all tasks completed. | ||
last() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
|
||
|
||
/** Create a base app used for testing. */ | ||
export function createTestApp(runner: SchematicTestRunner, appOptions = {}): UnitTestTree { | ||
const workspaceTree = runner.runExternalSchematic('@schematics/angular', 'workspace', { | ||
name: 'workspace', | ||
version: '7.0.0', | ||
newProjectRoot: 'projects' | ||
}); | ||
|
||
return runner.runExternalSchematic('@schematics/angular', 'application', | ||
{...appOptions, name: 'material'}, workspaceTree); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"declaration": true, | ||
"lib": [ | ||
"es2017" | ||
], | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"outDir": "../../../dist/packages/cdk/schematics", | ||
"noEmitOnError": false, | ||
"strictNullChecks": true, | ||
"skipDefaultLibCheck": true, | ||
"skipLibCheck": true, | ||
"sourceMap": true, | ||
"target": "es2015", | ||
"types": [ | ||
"jasmine", | ||
"node" | ||
] | ||
}, | ||
"exclude": [ | ||
"**/files/**/*", | ||
"**/*.spec.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @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 {WorkspaceProject} from '@angular-devkit/core/src/workspace'; | ||
import {SchematicsException, Tree} from '@angular-devkit/schematics'; | ||
import {Schema as ComponentOptions} from '@schematics/angular/component/schema'; | ||
import {addImportToModule} from '@schematics/angular/utility/ast-utils'; | ||
import {InsertChange} from '@schematics/angular/utility/change'; | ||
import {getWorkspace} from '@schematics/angular/utility/config'; | ||
import {findModuleFromOptions as internalFindModule} from '@schematics/angular/utility/find-module'; | ||
import {getAppModulePath} from '@schematics/angular/utility/ng-ast-utils'; | ||
import {getProjectMainFile} from './project-main-file'; | ||
import {ts} from './version-agnostic-typescript'; | ||
|
||
|
||
/** Reads file given path and returns TypeScript source file. */ | ||
export function getSourceFile(host: Tree, path: string) { | ||
const buffer = host.read(path); | ||
if (!buffer) { | ||
throw new SchematicsException(`Could not find file for path: ${path}`); | ||
} | ||
const content = buffer.toString(); | ||
return ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); | ||
} | ||
|
||
/** Import and add module to root app module. */ | ||
export function addModuleImportToRootModule(host: Tree, moduleName: string, src: string, | ||
project: WorkspaceProject) { | ||
const modulePath = getAppModulePath(host, getProjectMainFile(project)); | ||
addModuleImportToModule(host, modulePath, moduleName, src); | ||
} | ||
|
||
/** | ||
* Import and add module to specific module path. | ||
* @param host the tree we are updating | ||
* @param modulePath src location of the module to import | ||
* @param moduleName name of module to import | ||
* @param src src location to import | ||
*/ | ||
export function addModuleImportToModule(host: Tree, modulePath: string, moduleName: string, | ||
src: string) { | ||
|
||
const moduleSource = getSourceFile(host, modulePath); | ||
|
||
if (!moduleSource) { | ||
throw new SchematicsException(`Module not found: ${modulePath}`); | ||
} | ||
|
||
const changes = addImportToModule(moduleSource, modulePath, moduleName, src); | ||
const recorder = host.beginUpdate(modulePath); | ||
|
||
changes.forEach((change) => { | ||
if (change instanceof InsertChange) { | ||
recorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
}); | ||
|
||
host.commitUpdate(recorder); | ||
} | ||
|
||
/** Wraps the internal find module from options with undefined path handling */ | ||
export function findModuleFromOptions(host: Tree, options: ComponentOptions): string | undefined { | ||
const workspace = getWorkspace(host); | ||
|
||
if (!options.project) { | ||
options.project = Object.keys(workspace.projects)[0]; | ||
} | ||
|
||
const project = workspace.projects[options.project]; | ||
|
||
if (options.path === undefined) { | ||
options.path = `/${project.root}/src/app`; | ||
} | ||
|
||
return internalFindModule(host, options); | ||
} |
Oops, something went wrong.