Skip to content

Commit

Permalink
refactor(Schematics): Move schematics utilities to modules/schematics…
Browse files Browse the repository at this point in the history
…-core (#1044)
brandonroberts authored May 10, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c0893f1 commit 6d4ec0d
Showing 46 changed files with 200 additions and 101 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -54,6 +54,9 @@ jobs:
# Install the dependencies from NPM, using the node and yarn binaries managed by Bazel
- run: bazel run @yarn//:yarn

# Copy shared schematics-core into schematics/src directory
- run: yarn copy:schematics

# Build and Test
# Use bazel query so that we explicitly ask for all buildable targets to
# be built even though we run `bazel test`
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -69,4 +69,8 @@ output
*.ngfactory.ts
tmp

example-dist/
example-dist/

schematics-core
!modules/schematics-core
*.tgz
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ filegroup(
ext,
]) for pkg in [
"@angular",
"@angular-devkit",
"jasmine",
"jasmine-marbles",
"typescript",
5 changes: 0 additions & 5 deletions build/config.ts
Original file line number Diff line number Diff line change
@@ -35,9 +35,4 @@ export const packages: PackageDescription[] = [
hasTestingModule: false,
bundle: true,
},
{
name: 'schematics',
hasTestingModule: false,
bundle: false,
},
];
5 changes: 4 additions & 1 deletion example-app/app/shared/utils.ts
Original file line number Diff line number Diff line change
@@ -25,7 +25,10 @@ export class CustomRouterStateSerializer
route = route.firstChild;
}

const { url, root: { queryParams } } = routerState;
const {
url,
root: { queryParams },
} = routerState;
const { params } = route;

// Only return an object including the URL, params and query params
70 changes: 70 additions & 0 deletions modules/schematics-core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
dasherize,
decamelize,
camelize,
classify,
underscore,
group,
capitalize,
featurePath,
} from './utility/strings';

export {
findNodes,
getSourceNodes,
getDecoratorMetadata,
getContentOfKeyLiteral,
insertAfterLastOccurrence,
addBootstrapToModule,
addDeclarationToModule,
addExportToModule,
addImportToModule,
addProviderToModule,
} from './utility/ast-utils';

export {
Host,
Change,
NoopChange,
InsertChange,
RemoveChange,
ReplaceChange,
} from './utility/change';

export {
AppConfig,
CliConfig,
getAppFromConfig,
getConfig,
getWorkspace,
getWorkspacePath,
} from './utility/config';

export {
findModule,
findModuleFromOptions,
buildRelativePath,
ModuleOptions,
} from './utility/find-module';

export {
addReducerToState,
addReducerToStateInferface,
addReducerImportToNgModule,
addReducerToActionReducerMap,
omit,
} from './utility/ngrx-utils';

export { getProjectPath } from './utility/project';
export { insertImport } from './utility/route-utils';

export const stringUtils = {
dasherize,
decamelize,
camelize,
classify,
underscore,
group,
capitalize,
featurePath,
};
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ const defaultModuleOptions = {
flat: false,
};

export function getProjectPath(
export function getTestProjectPath(
workspaceOptions: any = defaultWorkspaceOptions,
appOptions: any = defaultAppOptions
) {
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './create-app-module';
export * from './create-reducers';
export * from './create-workspace';
export * from './get-file-content';
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -8,8 +8,8 @@
import { tags } from '@angular-devkit/core';
import { VirtualTree } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { Change, InsertChange } from '../utility/change';
import { getFileContent } from '../utility/test';
import { Change, InsertChange } from './change';
import { getFileContent } from '../testing';
import { addExportToModule } from './ast-utils';

function getTsSource(path: string, content: string): ts.SourceFile {
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as ts from 'typescript';
import * as stringUtils from '../strings';
import * as stringUtils from './strings';
import { InsertChange, Change, NoopChange } from './change';
import { Tree, SchematicsException, Rule } from '@angular-devkit/schematics';
import { normalize } from '@angular-devkit/core';
import { buildRelativePath } from './find-module';
import { insertImport } from './route-utils';
import { Schema as ReducerOptions } from '../reducer/schema';
import { addImportToModule } from './ast-utils';

export function addReducerToState(options: ReducerOptions): Rule {
export function addReducerToState(options: any): Rule {
return (host: Tree) => {
if (!options.reducers) {
return host;
@@ -185,7 +184,7 @@ export function addReducerToActionReducerMap(
/**
* Add reducer feature to NgModule
*/
export function addReducerImportToNgModule(options: ReducerOptions): Rule {
export function addReducerImportToNgModule(options: any): Rule {
return (host: Tree) => {
if (!options.module) {
return host;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getWorkspace } from '../utility/config';
import { getWorkspace } from './config';
import { Tree } from '@angular-devkit/schematics';

export function getProjectPath(
File renamed without changes.
35 changes: 35 additions & 0 deletions modules/schematics/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package(default_visibility = ["//visibility:public"])

load("@build_bazel_rules_typescript//:defs.bzl", "ts_library")
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")

ts_library(
name = "schematics",
srcs = glob(
[
"**/src/**/*.ts",
],
exclude = [
"**/src/**/*.spec.ts",
"**/src/**/files/**/*",
],
),
module_name = "@ngrx/schematics",
tsconfig = ":tsconfig-build.json",
)

npm_package(
name = "npm_package",
srcs = [
":collection.json",
":package.json",
":README.md",
] + glob([
"**/src/*/files/**/*",
"**/schema.json",
"**/migration.json",
]),
deps = [
":schematics",
],
)
5 changes: 4 additions & 1 deletion modules/schematics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngrx/schematics",
"version": "5.2.0",
"version": "0.0.0-PLACEHOLDER",
"description": "NgRx Schematics for Angular",
"repository": {
"type": "git",
@@ -21,6 +21,9 @@
},
"homepage": "https://github.com/ngrx/platform#readme",
"schematics": "./collection.json",
"ng-update": {
"packageGroup": "NG_UPDATE_PACKAGE_GROUP"
},
"peerDependencies": {
"@angular-devkit/core": "^0.5.0",
"@angular-devkit/schematics": "^0.5.0"
6 changes: 3 additions & 3 deletions modules/schematics/src/action/index.spec.ts
Original file line number Diff line number Diff line change
@@ -5,9 +5,9 @@ import {
import * as path from 'path';
import { Schema as ActionOptions } from './schema';
import {
getProjectPath,
getTestProjectPath,
createWorkspace,
} from '../utility/test/create-workspace';
} from '../../../schematics-core/testing';

describe('Action Schematic', () => {
const schematicRunner = new SchematicTestRunner(
@@ -23,7 +23,7 @@ describe('Action Schematic', () => {
flat: true,
};

const projectPath = getProjectPath();
const projectPath = getTestProjectPath();

let appTree: UnitTestTree;

4 changes: 1 addition & 3 deletions modules/schematics/src/action/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { normalize } from '@angular-devkit/core';
import {
Rule,
SchematicsException,
@@ -14,9 +13,8 @@ import {
Tree,
SchematicContext,
} from '@angular-devkit/schematics';
import * as stringUtils from '../strings';
import { Schema as ActionOptions } from './schema';
import { getProjectPath } from '../utility/project';
import { getProjectPath, stringUtils } from '../schematics-core';

export default function(options: ActionOptions): Rule {
return (host: Tree, context: SchematicContext) => {
File renamed without changes.
6 changes: 3 additions & 3 deletions modules/schematics/src/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -5,9 +5,9 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import {
getTestProjectPath,
createWorkspace,
getProjectPath,
} from './utility/test/create-workspace';
} from '../../schematics-core/testing';

describe('CLI Schematic', () => {
const schematicRunner = new SchematicTestRunner(
@@ -20,7 +20,7 @@ describe('CLI Schematic', () => {
project: 'bar',
};

const projectPath = getProjectPath();
const projectPath = getTestProjectPath();

let appTree: UnitTestTree;

7 changes: 4 additions & 3 deletions modules/schematics/src/container/index.spec.ts
Original file line number Diff line number Diff line change
@@ -4,10 +4,11 @@ import {
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Schema as ContainerOptions } from './schema';
import {} from '../../schematics-core';
import {
getTestProjectPath,
createWorkspace,
getProjectPath,
} from '../utility/test/create-workspace';
} from '../../../schematics-core/testing';

describe('Container Schematic', () => {
const schematicRunner = new SchematicTestRunner(
@@ -29,7 +30,7 @@ describe('Container Schematic', () => {
prefix: 'app',
};

const projectPath = getProjectPath();
const projectPath = getTestProjectPath();

let appTree: UnitTestTree;

16 changes: 10 additions & 6 deletions modules/schematics/src/container/index.ts
Original file line number Diff line number Diff line change
@@ -14,13 +14,17 @@ import {
mergeWith,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';
import * as stringUtils from '../strings';
import {
stringUtils,
buildRelativePath,
insertImport,
NoopChange,
ReplaceChange,
InsertChange,
getProjectPath,
omit,
} from '../schematics-core';
import { Schema as ContainerOptions } from './schema';
import { buildRelativePath } from '../utility/find-module';
import { NoopChange, InsertChange, ReplaceChange } from '../utility/change';
import { insertImport } from '../utility/route-utils';
import { omit } from '../utility/ngrx-utils';
import { getProjectPath } from '../utility/project';

function addStateToComponent(options: ContainerOptions) {
return (host: Tree) => {
File renamed without changes.
13 changes: 5 additions & 8 deletions modules/schematics/src/effect/index.spec.ts
Original file line number Diff line number Diff line change
@@ -3,16 +3,13 @@ import {
UnitTestTree,
} from '@angular-devkit/schematics/testing';
import * as path from 'path';
import {
createAppModule,
getFileContent,
createAppModuleWithEffects,
} from '../utility/test';
import {} from '../../schematics-core';
import { Schema as EffectOptions } from './schema';
import {
getProjectPath,
getTestProjectPath,
createWorkspace,
} from '../utility/test/create-workspace';
createAppModuleWithEffects,
} from '../../../schematics-core/testing';

describe('Effect Schematic', () => {
const schematicRunner = new SchematicTestRunner(
@@ -32,7 +29,7 @@ describe('Effect Schematic', () => {
group: false,
};

const projectPath = getProjectPath();
const projectPath = getTestProjectPath();

let appTree: UnitTestTree;

13 changes: 6 additions & 7 deletions modules/schematics/src/effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { normalize } from '@angular-devkit/core';
import {
Rule,
SchematicContext,
@@ -15,16 +14,16 @@ import {
url,
} from '@angular-devkit/schematics';
import * as ts from 'typescript';
import * as stringUtils from '../strings';
import { addImportToModule } from '../utility/ast-utils';
import { InsertChange } from '../utility/change';
import {
stringUtils,
insertImport,
buildRelativePath,
addImportToModule,
InsertChange,
getProjectPath,
findModuleFromOptions,
} from '../utility/find-module';
} from '../schematics-core';
import { Schema as EffectOptions } from './schema';
import { insertImport } from '../utility/route-utils';
import { getProjectPath } from '../utility/project';

function addImportToNgModule(options: EffectOptions): Rule {
return (host: Tree) => {
File renamed without changes.
Loading

0 comments on commit 6d4ec0d

Please sign in to comment.