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

refator schematics workspace api #829

Merged
merged 3 commits into from
Mar 12, 2021
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
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
"name": "Launch Test",
"program": "${workspaceFolder}/spec/jasmine-runner.js",
"preLaunchTask": "build",
"outFiles": [ "${workspaceRoot}/lib/**/*.js",
"outFiles": [ "${workspaceRoot}/packages/**/*.js",
"${workspaceRoot}/spec/**/*.js" ]
},
{
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/migrations/update-3_2/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe("Update 3.2.0", () => {
testProj: {
sourceRoot: "/src"
}
}
},
version: 1
};

beforeEach(() => {
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/migrations/update-3_2/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:no-implicit-dependencies
import { workspaces } from "@angular-devkit/core";
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { getWorkspace } from "@schematics/angular/utility/config";
import { createWorkspaceHost } from "@igniteui/cli-core";

const summaryEarliestAssignment =
`summaryResult: (IgxDateSummaryOperand.earliest(data)).toLocaleDateString()`;
Expand All @@ -14,13 +14,13 @@ const summaryLatestReplacement =
`summaryResult: data.length ? (IgxDateSummaryOperand.latest(data)).toLocaleDateString() : null`;

export default function(): Rule {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = workspace.defaultProject ?
workspace.projects[workspace.defaultProject] :
workspace.projects[0];
return async (tree: Tree, context: SchematicContext) => {
const { workspace } = await workspaces.readWorkspace("/", createWorkspaceHost(tree));
const project = workspace.extensions.defaultProject ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use getDefaultProject here.

workspace.projects.get(workspace.extensions.defaultProject as string) :
workspace.projects.values().next().value as workspaces.ProjectDefinition;

const sourceDir = host.getDir(project.sourceRoot || "./src");
const sourceDir = tree.getDir(project.sourceRoot || "./src");
const ext = ".ts";

context.logger.info(`Applying migration for Ignite UI CLI 3.2.0 related to custom IgxSummaryOperand.`);
Expand All @@ -31,11 +31,11 @@ export default function(): Rule {
let content = entry.content.toString();
if (content.indexOf(summaryEarliestAssignment) !== -1) {
content = content.replace(summaryEarliestAssignment, summaryEarliestReplacement);
host.overwrite(path, content);
tree.overwrite(path, content);
}
if (content.indexOf(summaryLatestAssignment) !== -1) {
content = content.replace(summaryLatestAssignment, summaryLatestReplacement);
host.overwrite(path, content);
tree.overwrite(path, content);
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/migrations/update-4_2_3/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ describe("Update 4.2.3", () => {
testProj: {
sourceRoot: "/src"
}
}
},
version: 1
};

beforeEach(() => {
Expand Down
18 changes: 9 additions & 9 deletions packages/cli/migrations/update-4_2_3/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tslint:disable:no-implicit-dependencies
import { workspaces } from "@angular-devkit/core";
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { getWorkspace } from "@schematics/angular/utility/config";
import { createWorkspaceHost } from "@igniteui/cli-core";

const awesomeGridPaginatorStyle =
` .igx-paginator>* {`;
Expand All @@ -16,13 +16,13 @@ const paginatorStyleReplacement =
.igx-paginator>* {`;

export default function(): Rule {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = workspace.defaultProject ?
workspace.projects[workspace.defaultProject] :
workspace.projects[0];
return async (tree: Tree, context: SchematicContext) => {
const { workspace } = await workspaces.readWorkspace("/", createWorkspaceHost(tree));
const project = workspace.extensions.defaultProject ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use getDefaultProject here.

workspace.projects.get(workspace.extensions.defaultProject as string) :
workspace.projects.values().next().value as workspaces.ProjectDefinition;

const sourceDir = host.getDir(project.sourceRoot || "./src");
const sourceDir = tree.getDir(project.sourceRoot || "./src");
const ext = ".scss";

context.logger.info(`Applying migration for Ignite UI CLI 4.2.3.`);
Expand All @@ -32,7 +32,7 @@ export default function(): Rule {
let content = entry.content.toString();
if (content.indexOf(awesomeGridPaginatorStyle) !== -1) {
content = content.replace(awesomeGridPaginatorStyle, paginatorStyleReplacement);
host.overwrite(path, content);
tree.overwrite(path, content);
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/migrations/update-5_0_0/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe("Update 5.0.0", () => {
prefix: "app",
sourceRoot: "src"
}
}
},
version: 1
};

beforeEach(() => {
Expand Down
19 changes: 9 additions & 10 deletions packages/cli/migrations/update-5_0_0/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// tslint:disable:no-implicit-dependencies
import { workspaces } from "@angular-devkit/core";
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
import { App, FS_TOKEN, FS_TYPE_TOKEN, FsTypes, IFileSystem, TypeScriptFileUpdate } from "@igniteui/cli-core";
// tslint:disable-next-line:no-submodule-imports
import { getWorkspace } from "@schematics/angular/utility/config";
import { App, createWorkspaceHost, FS_TOKEN, FS_TYPE_TOKEN, FsTypes, IFileSystem, TypeScriptFileUpdate } from "@igniteui/cli-core";

//#region Temp duplicate of schematics pack fs
export class NgTreeFileSystem implements IFileSystem {
Expand Down Expand Up @@ -36,18 +35,18 @@ export function setVirtual(tree: Tree) {
//#endregion

export default function(): Rule {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = workspace.defaultProject ?
workspace.projects[workspace.defaultProject] :
workspace.projects[0];
return async (tree: Tree, context: SchematicContext) => {
const { workspace } = await workspaces.readWorkspace("/", createWorkspaceHost(tree));
const project = workspace.extensions.defaultProject ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use getDefaultProject here.

workspace.projects.get(workspace.extensions.defaultProject as string) :
workspace.projects.values().next().value as workspaces.ProjectDefinition;

const moduleFile = `${project.sourceRoot}/${project.prefix}/app.module.ts`;

context.logger.info(`Applying migration for Ignite UI CLI 5.0.0`);

if (host.exists(moduleFile)) {
setVirtual(host);
if (tree.exists(moduleFile)) {
setVirtual(tree);
const mainModule = new TypeScriptFileUpdate(moduleFile);
mainModule.addNgModuleMeta({ import: "HammerModule", from: "@angular/platform-browser" });
mainModule.finalize();
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/migrations/update-5_0_3/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ describe("Update 5.0.0", () => {
prefix: "app",
sourceRoot: "src"
}
}
},
version: 1
};

beforeEach(() => {
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/migrations/update-5_0_3/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// tslint:disable:no-implicit-dependencies
import { workspaces } from "@angular-devkit/core";
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { getWorkspace } from "@schematics/angular/utility/config";
import { createWorkspaceHost } from "@igniteui/cli-core";

export default function(): Rule {
return (host: Tree, context: SchematicContext) => {
const workspace = getWorkspace(host);
const project = workspace.defaultProject ?
workspace.projects[workspace.defaultProject] :
workspace.projects[0];
return async (tree: Tree, context: SchematicContext) => {
const { workspace } = await workspaces.readWorkspace("/", createWorkspaceHost(tree));
const project = workspace.extensions.defaultProject ?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You caн use getDefaultProject here.

workspace.projects.get(workspace.extensions.defaultProject as string) :
workspace.projects.values().next().value as workspaces.ProjectDefinition;

const errorService = `${project.sourceRoot}/${project.prefix}/error-routing/error/global-error-handler.service.ts`;

context.logger.info(`Applying migration for Ignite UI CLI 5.0.3`);

if (host.exists(errorService)) {
let fileContents = host.read(errorService).toString();
if (tree.exists(errorService)) {
let fileContents = tree.read(errorService).toString();
fileContents = fileContents.replace("throw error;", "console.error(error);");
host.overwrite(errorService, fileContents);
tree.overwrite(errorService, fileContents);
}
};
}
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"jasmine-spec-reporter": "~5.0.0",
"lite-server": "^2.4.0",
"nyc": "^13.1.0",
"rxjs": "6.5.4",
"rxjs": "6.6.3",
"source-map-support": "^0.5.4",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
"author": "Infragistics",
"license": "MIT",
"dependencies": {
"@angular-devkit/schematics": "^9.0.1",
"chalk": "^2.3.2",
"glob": "^7.1.2",
"inquirer": "^6.2.2",
"through2": "^2.0.3",
"typescript": "^3.7.5"
},
"devDependencies": {
"@angular-devkit/schematics": "^11.0.0"
}
}
23 changes: 23 additions & 0 deletions packages/core/util/Schematics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// tslint:disable: no-implicit-dependencies
import { workspaces } from "@angular-devkit/core";
import { Tree } from "@angular-devkit/schematics";

// TODO: Better place for these?
// current code transpiles to clean functions without imports from packages
// that core doesn't depend on, but still not the best option.

export function addTypography(host: Tree) {
const indexHtml = "src/index.html";
const bodyTagRegex = /<body[^>]*?>/;
Expand Down Expand Up @@ -32,3 +38,20 @@ export function addTypography(host: Tree) {
host.overwrite(indexHtml, content);
}
}

/** Creates a schematics tree workspace host. */
export const createWorkspaceHost = (tree: Tree): workspaces.WorkspaceHost => ({
readFile: async (path: string): Promise<string> => {
const data = tree.read(path);
// can use fileBufferToString
return data?.toString();
},
writeFile: async (path: string, data: string): Promise<void> => {
tree.overwrite(path, data);
},

isDirectory: async (path: string): Promise<boolean> =>
!tree.exists(path) && tree.getDir(path).subfiles.length > 0,

isFile: async (path: string): Promise<boolean> => tree.exists(path)
});
8 changes: 4 additions & 4 deletions packages/ng-schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
"license": "MIT",
"schematics": "./src/collection.json",
"dependencies": {
"@angular-devkit/core": "^9.0.1",
"@angular-devkit/schematics": "^9.0.1",
"@angular-devkit/core": "^11.0.0",
"@angular-devkit/schematics": "^11.0.0",
"@igniteui/angular-templates": "~11.1.710",
"@igniteui/cli-core": "~7.1.0",
"@schematics/angular": "^9.0.1",
"rxjs": "6.5.3"
"@schematics/angular": "^11.0.0",
"rxjs": "6.6.3"
},
"devDependencies": {
"@types/jasmine": "3.3.9",
Expand Down
4 changes: 2 additions & 2 deletions packages/ng-schematics/src/app-projects/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from "path";
const collectionPath = path.join(__dirname, "../collection.json");

describe("app-projects", () => {
it("works", () => {
it("works", async () => {
const runner = new SchematicTestRunner("schematics", collectionPath);
const tree = Tree.empty();
const mockOptions = {
Expand All @@ -19,7 +19,7 @@ describe("app-projects", () => {
};
spyOn(tree, "read").and.returnValue(`Mock package content "igniteui-cli":`);
spyOn(tree, "overwrite");
runner.runSchematic("app-projects", mockOptions, tree);
await runner.runSchematicAsync("app-projects", mockOptions, tree).toPromise();

expect(mockOptions.projTemplate.generateConfig).toHaveBeenCalled();
expect(tree.read).toHaveBeenCalledWith("./package.json");
Expand Down
21 changes: 10 additions & 11 deletions packages/ng-schematics/src/cli-config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DependencyNotFoundException } from "@angular-devkit/core";
import { yellow } from "@angular-devkit/core/src/terminal";
import { chain, FileDoesNotExistException, Rule, Tree } from "@angular-devkit/schematics";
import { chain, FileDoesNotExistException, Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
import { NPM_PACKAGE, resolveIgxPackage } from "@igniteui/angular-templates";
import { addTypography, TypeScriptFileUpdate } from "@igniteui/cli-core";
import { createCliConfig } from "../utils/cli-config";
Expand Down Expand Up @@ -32,7 +31,7 @@ function getDependencyVersion(pkg: string, tree: Tree): string {
}

function displayVersionMismatch(): Rule {
return (tree: Tree) => {
return (tree: Tree, context: SchematicContext) => {
const igxPackage = resolveIgxPackage(NPM_PACKAGE);
const pkgJson = JSON.parse(tree.read(`/node_modules/${igxPackage}/package.json`)!.toString());
const ngKey = "@angular/core";
Expand All @@ -43,10 +42,9 @@ function displayVersionMismatch(): Rule {
const ngCommonVer = pkgJson.peerDependencies[ngCommonKey];

if (ngCoreProjVer < ngCoreVer || ngCommonProjVer < ngCommonVer) {
// tslint:disable-next-line:no-console
console.warn(yellow(`
context.logger.warn(`
WARNING Version mismatch detected - ${igxPackage} is built against a newer version of @angular/core (${ngCoreVer}).
Running 'ng update' will prevent potential version conflicts.\n`));
Running 'ng update' will prevent potential version conflicts.\n`);
}
};
}
Expand All @@ -58,8 +56,9 @@ function addTypographyToProj(): Rule {
}

function importBrowserAnimations(): Rule {
return (tree: Tree) => {
const moduleFile = `${getDefaultProject(tree).sourceRoot}/app/app.module.ts`;
return async (tree: Tree) => {
const project = await getDefaultProject(tree);
const moduleFile = `${project.sourceRoot}/app/app.module.ts`;
if (tree.exists(moduleFile)) {
const mainModule = new TypeScriptFileUpdate(moduleFile);
mainModule.addNgModuleMeta({ import: "BrowserAnimationsModule", from: "@angular/platform-browser/animations" });
Expand All @@ -69,9 +68,9 @@ function importBrowserAnimations(): Rule {
}

function importStyles(): Rule {
return (tree: Tree) => {
addFontsToIndexHtml(tree);
importDefaultTheme(tree);
return async (tree: Tree) => {
await addFontsToIndexHtml(tree);
await importDefaultTheme(tree);
};
}

Expand Down
Loading