Skip to content

Commit

Permalink
chore(igx-ts,ng-update): attempt to migrate summaries operand change …
Browse files Browse the repository at this point in the history
…from #425
  • Loading branch information
damyanpetev committed Dec 13, 2018
1 parent dc136d2 commit ba84705
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
5 changes: 5 additions & 0 deletions migrations/migration-collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"version": "3.0.0",
"description": "Updates Ignite UI for Angular project from 2.1.x",
"factory": "./update-3"
},
"migration-03": {
"version": "3.2.0",
"description": "Updates Ignite UI for Angular project from 3.1.x",
"factory": "./update-3_2"
}
}
}
74 changes: 74 additions & 0 deletions migrations/update-3_2/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as path from "path";

// tslint:disable:no-implicit-dependencies
import { EmptyTree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { SchematicTestRunner, UnitTestTree } from "@angular-devkit/schematics/testing";

describe("Update 3.2.0", () => {
let appTree: UnitTestTree;
const schematicRunner = new SchematicTestRunner("ig-migrate", path.join(__dirname, "../migration-collection.json"));
const configJson = {
defaultProject: "testProj",
projects: {
testProj: {
sourceRoot: "/src"
}
}
};

beforeEach(() => {
appTree = new UnitTestTree(new EmptyTree());
appTree.create("/angular.json", JSON.stringify(configJson));
});

it("should update CustomDateSummary summaryResult assignment", done => {
const summaryFile = "/src/app/grid-summaries/grid-summaries.component.ts";
appTree.create(summaryFile,
`class CustomDateSummary extends IgxDateSummaryOperand {
constructor() {
super();
}
public operate(data?: any[]): IgxSummaryResult[] {
const result = [];
result.push({
key: 'earliest',
label: 'Earliest Date',
summaryResult: (IgxDateSummaryOperand.earliest(data)).toLocaleDateString()
});
result.push({
key: 'latest',
label: 'Latest Date',
summaryResult: (IgxDateSummaryOperand.latest(data)).toLocaleDateString()
});
return result;
}
}`
);

schematicRunner.runSchematic("migration-03", {}, appTree);
expect(appTree.readContent(summaryFile))
.toEqual(
`class CustomDateSummary extends IgxDateSummaryOperand {
constructor() {
super();
}
public operate(data?: any[]): IgxSummaryResult[] {
const result = [];
result.push({
key: 'earliest',
label: 'Earliest Date',
summaryResult: data.length ? (IgxDateSummaryOperand.earliest(data)).toLocaleDateString() : null
});
result.push({
key: 'latest',
label: 'Latest Date',
summaryResult: data.length ? (IgxDateSummaryOperand.latest(data)).toLocaleDateString() : null
});
return result;
}
}`
);
done();
});
});
43 changes: 43 additions & 0 deletions migrations/update-3_2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// tslint:disable:no-implicit-dependencies
import { Rule, SchematicContext, Tree } from "@angular-devkit/schematics";
// tslint:disable-next-line:no-submodule-imports
import { getWorkspace } from "@schematics/angular/utility/config";

const summaryEarliestAssignment =
`summaryResult: (IgxDateSummaryOperand.earliest(data)).toLocaleDateString()`;
const summaryEarliestReplacement =
`summaryResult: data.length ? (IgxDateSummaryOperand.earliest(data)).toLocaleDateString() : null`;

const summaryLatestAssignment =
`summaryResult: (IgxDateSummaryOperand.latest(data)).toLocaleDateString()`;
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];

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

context.logger.info(`Applying migration for Ignite UI CLI 3.2.0 related to custom IgxSummaryOperand.`);
context.logger.info(`See https://github.com/IgniteUI/igniteui-angular/issues/3414`);

sourceDir.visit((path, entry) => {
if (path.endsWith(ext)) {
let content = entry.content.toString();
if (content.indexOf(summaryEarliestAssignment) !== -1) {
content = content.replace(summaryEarliestAssignment, summaryEarliestReplacement);
host.overwrite(path, content);
}
if (content.indexOf(summaryLatestAssignment) !== -1) {
content = content.replace(summaryLatestAssignment, summaryLatestReplacement);
host.overwrite(path, content);
}
}
});
};
}

0 comments on commit ba84705

Please sign in to comment.