From 282d5fcb1277034c332b258fd128bf5e61f491e2 Mon Sep 17 00:00:00 2001 From: igdmdimitrov Date: Tue, 9 Nov 2021 09:55:02 +0200 Subject: [PATCH 1/4] feat(exporters): added migration comments and test --- .../migrations/common/UpdateChanges.ts | 12 ++++ .../migrations/update-13_0_0/index.spec.ts | 64 +++++++++++++++++++ .../migrations/update-13_0_0/index.ts | 29 +++++++++ 3 files changed, 105 insertions(+) diff --git a/projects/igniteui-angular/migrations/common/UpdateChanges.ts b/projects/igniteui-angular/migrations/common/UpdateChanges.ts index dd44d258f27..791119d4847 100644 --- a/projects/igniteui-angular/migrations/common/UpdateChanges.ts +++ b/projects/igniteui-angular/migrations/common/UpdateChanges.ts @@ -98,6 +98,18 @@ export class UpdateChanges { return this._tsFiles; } + private _ModuletTsFiles: string[] = []; + public get moduleTsFiles(): string[] { + if (!this._ModuletTsFiles.length) { + this.sourceDirsVisitor((fulPath, entry) => { + if (fulPath.endsWith('.module.ts')) { + this._ModuletTsFiles.push(entry.path); + } + }); + } + return this._ModuletTsFiles; + } + private _sassFiles: string[] = []; /** Sass (both .scss and .sass) files in the project being updated. */ public get sassFiles(): string[] { diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts index e36cc3ff4df..66f24968ea6 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts @@ -188,4 +188,68 @@ describe(`Update to ${version}`, () => { `.replace(lineBreaksAndSpaceRegex, '')); }); + + it('should remove exporter services from module.ts files', async () => { + appTree.create('/testSrc/appPrefix/component/app.module.ts', ` +import { NgModule } from "@angular/core"; +import { FormsModule } from "@angular/forms"; +import { BrowserModule } from "@angular/platform-browser"; +import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; +import { AppComponent } from "./app.component"; +import { IgxExcelExporterService } from "igniteui-angular"; +import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; + +@NgModule({ +bootstrap: [AppComponent], +declarations: [ + AppComponent, + ExcelExportComponent +], +imports: [ + BrowserModule, + BrowserAnimationsModule, + FormsModule +], +providers: [IgxExcelExporterService], +entryComponents: [], +schemas: [] +}) +export class AppModule {} +`); + + const tree = await schematicRunner + .runSchematicAsync(migrationName, {}, appTree) + .toPromise(); + + expect( + tree.readContent('/testSrc/appPrefix/component/app.module.ts') + ).toEqual( ` +import { NgModule } from "@angular/core"; +import { FormsModule } from "@angular/forms"; +import { BrowserModule } from "@angular/platform-browser"; +import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; +import { AppComponent } from "./app.component"; +// IgxExcelExporterService has been removed. Exporter services can now be used without providing. +import { /*IgxExcelExporterService*/ } from "igniteui-angular"; +import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; + +@NgModule({ +bootstrap: [AppComponent], +declarations: [ + AppComponent, + ExcelExportComponent +], +imports: [ + BrowserModule, + BrowserAnimationsModule, + FormsModule +], +// IgxExcelExporterService has been removed. Exporter services can now be used without providing. +providers: [/*IgxExcelExporterService*/], +entryComponents: [], +schemas: [] +}) +export class AppModule {} +`); + }); }); diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.ts index d0af55987d4..9adbba58a08 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.ts @@ -16,6 +16,8 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { const GRIDS = ['IgxGridComponent', 'IgxTreeGridComponent', 'IgxHierarchicalGridComponent']; const TAGS = ['igx-grid', 'igx-tree-grid', 'igx-hierarchical-grid']; const tsFiles = update.tsFiles; + const SERVICES = ['IgxCsvExporterService', 'IgxExcelExporterService']; + const moduleTsFiles = update.moduleTsFiles; for (const path of update.templateFiles) { findElementNodes(parseFile(host, path), TAGS) @@ -61,5 +63,32 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { } }); } + + for (const path of moduleTsFiles) { + let content = host.read(path)?.toString(); + SERVICES.forEach(service => { + if (content.indexOf(service) > -1) { + const commentedService = '/*' + service + '*/'; + content = content.replace(new RegExp(service, 'gi'), commentedService); + const indexes = getIndicesOf(commentedService, content); + indexes.reverse().forEach(index => { + const preceedingContent = content.substring(0, index); + const newLineIndex = preceedingContent.lastIndexOf('\n'); + const comment = '// ' + service + ' has been removed. Exporter services can now be used without providing.'; + if (newLineIndex > -1) { + const newPreceedingContent = + [preceedingContent.slice(0, newLineIndex), '\n' + comment, preceedingContent.slice(newLineIndex)].join(''); + content = content.replace(preceedingContent, newPreceedingContent); + } else { + // service is mentioned on the first row + content = comment + '\n' + content; + } + }); + + host.overwrite(path, content); + } + }); + } + update.applyChanges(); }; From c7b4afd9560e703ba9bfb17061f929e48fa2aa9a Mon Sep 17 00:00:00 2001 From: igdmdimitrov Date: Thu, 11 Nov 2021 14:58:17 +0200 Subject: [PATCH 2/4] feat(exporters): added comma to migration comment --- .../migrations/update-13_0_0/index.spec.ts | 8 ++++---- .../igniteui-angular/migrations/update-13_0_0/index.ts | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts index c0bba2903ea..cf05a80fc90 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts @@ -208,7 +208,7 @@ import { FormsModule } from "@angular/forms"; import { BrowserModule } from "@angular/platform-browser"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { AppComponent } from "./app.component"; -import { IgxExcelExporterService } from "igniteui-angular"; +import { IgxExcelExporterService, OtherService } from "igniteui-angular"; import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; @NgModule({ @@ -222,7 +222,7 @@ imports: [ BrowserAnimationsModule, FormsModule ], -providers: [IgxExcelExporterService], +providers: [IgxExcelExporterService , OtherService], entryComponents: [], schemas: [] }) @@ -242,7 +242,7 @@ import { BrowserModule } from "@angular/platform-browser"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { AppComponent } from "./app.component"; // IgxExcelExporterService has been removed. Exporter services can now be used without providing. -import { /*IgxExcelExporterService*/ } from "igniteui-angular"; +import { /*IgxExcelExporterService,*/ OtherService } from "igniteui-angular"; import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; @NgModule({ @@ -257,7 +257,7 @@ imports: [ FormsModule ], // IgxExcelExporterService has been removed. Exporter services can now be used without providing. -providers: [/*IgxExcelExporterService*/], +providers: [/*IgxExcelExporterService,*/ OtherService], entryComponents: [], schemas: [] }) diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.ts index 9adbba58a08..c9f7b29b722 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.ts @@ -69,8 +69,10 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { SERVICES.forEach(service => { if (content.indexOf(service) > -1) { const commentedService = '/*' + service + '*/'; + const commentedServiceWithComa = '/*' + service + ',*/'; content = content.replace(new RegExp(service, 'gi'), commentedService); - const indexes = getIndicesOf(commentedService, content); + content = content.replace(new RegExp('[/][*]' + service + '[*][/]\\s*[,]', 'gi'), commentedServiceWithComa); + const indexes = getIndicesOf('/*' + service, content); indexes.reverse().forEach(index => { const preceedingContent = content.substring(0, index); const newLineIndex = preceedingContent.lastIndexOf('\n'); From be521d06e4ac1f5cf72813add2e0d0365d892ce0 Mon Sep 17 00:00:00 2001 From: igdmdimitrov Date: Fri, 12 Nov 2021 15:42:16 +0200 Subject: [PATCH 3/4] chore(*): commenting only at the start of the file --- .../migrations/update-13_0_0/index.spec.ts | 21 ++++++++------ .../migrations/update-13_0_0/index.ts | 28 ++++++------------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts index cf05a80fc90..2cfc2c906fd 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts @@ -201,14 +201,14 @@ describe(`Update to ${version}`, () => { `.replace(lineBreaksAndSpaceRegex, '')); }); - it('should remove exporter services from module.ts files', async () => { + it('should insert a comment when exporter services are present in module.ts files', async () => { appTree.create('/testSrc/appPrefix/component/app.module.ts', ` import { NgModule } from "@angular/core"; import { FormsModule } from "@angular/forms"; import { BrowserModule } from "@angular/platform-browser"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { AppComponent } from "./app.component"; -import { IgxExcelExporterService, OtherService } from "igniteui-angular"; +import { IgxCsvExporterService, IgxExcelExporterService } from "igniteui-angular"; import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; @NgModule({ @@ -222,7 +222,10 @@ imports: [ BrowserAnimationsModule, FormsModule ], -providers: [IgxExcelExporterService , OtherService], +providers: [ + IgxCsvExporterService, + IgxExcelExporterService +], entryComponents: [], schemas: [] }) @@ -235,14 +238,14 @@ export class AppModule {} expect( tree.readContent('/testSrc/appPrefix/component/app.module.ts') - ).toEqual( ` + ).toEqual( +`// IgxCsvExporterService and IgxExcelExporterService no longer need to be manually provided and can be safely removed. import { NgModule } from "@angular/core"; import { FormsModule } from "@angular/forms"; import { BrowserModule } from "@angular/platform-browser"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; import { AppComponent } from "./app.component"; -// IgxExcelExporterService has been removed. Exporter services can now be used without providing. -import { /*IgxExcelExporterService,*/ OtherService } from "igniteui-angular"; +import { IgxCsvExporterService, IgxExcelExporterService } from "igniteui-angular"; import { ExcelExportComponent } from "./services/export-excel/excel-export.component"; @NgModule({ @@ -256,8 +259,10 @@ imports: [ BrowserAnimationsModule, FormsModule ], -// IgxExcelExporterService has been removed. Exporter services can now be used without providing. -providers: [/*IgxExcelExporterService,*/ OtherService], +providers: [ + IgxCsvExporterService, + IgxExcelExporterService +], entryComponents: [], schemas: [] }) diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.ts index c9f7b29b722..ec1b6980e68 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.ts @@ -66,30 +66,18 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { for (const path of moduleTsFiles) { let content = host.read(path)?.toString(); + const servicesInFile = []; SERVICES.forEach(service => { if (content.indexOf(service) > -1) { - const commentedService = '/*' + service + '*/'; - const commentedServiceWithComa = '/*' + service + ',*/'; - content = content.replace(new RegExp(service, 'gi'), commentedService); - content = content.replace(new RegExp('[/][*]' + service + '[*][/]\\s*[,]', 'gi'), commentedServiceWithComa); - const indexes = getIndicesOf('/*' + service, content); - indexes.reverse().forEach(index => { - const preceedingContent = content.substring(0, index); - const newLineIndex = preceedingContent.lastIndexOf('\n'); - const comment = '// ' + service + ' has been removed. Exporter services can now be used without providing.'; - if (newLineIndex > -1) { - const newPreceedingContent = - [preceedingContent.slice(0, newLineIndex), '\n' + comment, preceedingContent.slice(newLineIndex)].join(''); - content = content.replace(preceedingContent, newPreceedingContent); - } else { - // service is mentioned on the first row - content = comment + '\n' + content; - } - }); - - host.overwrite(path, content); + servicesInFile.push(service); } }); + + if (servicesInFile.length > 0) { + const comment = '// ' + servicesInFile.join(' and ') + ' no longer need to be manually provided and can be safely removed.'; + content = comment + content; + host.overwrite(path, content); + } } update.applyChanges(); From 562ce8caf43e6dcff79a215851064e7eda715df0 Mon Sep 17 00:00:00 2001 From: igdmdimitrov Date: Mon, 15 Nov 2021 12:58:26 +0200 Subject: [PATCH 4/4] feat(exporters): fixed new line at end of comment --- .../migrations/common/UpdateChanges.ts | 12 ------------ .../migrations/update-13_0_0/index.spec.ts | 4 ++-- .../migrations/update-13_0_0/index.ts | 10 ++++++++-- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/projects/igniteui-angular/migrations/common/UpdateChanges.ts b/projects/igniteui-angular/migrations/common/UpdateChanges.ts index 791119d4847..dd44d258f27 100644 --- a/projects/igniteui-angular/migrations/common/UpdateChanges.ts +++ b/projects/igniteui-angular/migrations/common/UpdateChanges.ts @@ -98,18 +98,6 @@ export class UpdateChanges { return this._tsFiles; } - private _ModuletTsFiles: string[] = []; - public get moduleTsFiles(): string[] { - if (!this._ModuletTsFiles.length) { - this.sourceDirsVisitor((fulPath, entry) => { - if (fulPath.endsWith('.module.ts')) { - this._ModuletTsFiles.push(entry.path); - } - }); - } - return this._ModuletTsFiles; - } - private _sassFiles: string[] = []; /** Sass (both .scss and .sass) files in the project being updated. */ public get sassFiles(): string[] { diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts index 2cfc2c906fd..f6aba3d0fc0 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.spec.ts @@ -202,8 +202,8 @@ describe(`Update to ${version}`, () => { }); it('should insert a comment when exporter services are present in module.ts files', async () => { - appTree.create('/testSrc/appPrefix/component/app.module.ts', ` -import { NgModule } from "@angular/core"; + appTree.create('/testSrc/appPrefix/component/app.module.ts', +`import { NgModule } from "@angular/core"; import { FormsModule } from "@angular/forms"; import { BrowserModule } from "@angular/platform-browser"; import { BrowserAnimationsModule } from "@angular/platform-browser/animations"; diff --git a/projects/igniteui-angular/migrations/update-13_0_0/index.ts b/projects/igniteui-angular/migrations/update-13_0_0/index.ts index ec1b6980e68..c58ecdefb4d 100644 --- a/projects/igniteui-angular/migrations/update-13_0_0/index.ts +++ b/projects/igniteui-angular/migrations/update-13_0_0/index.ts @@ -17,7 +17,6 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { const TAGS = ['igx-grid', 'igx-tree-grid', 'igx-hierarchical-grid']; const tsFiles = update.tsFiles; const SERVICES = ['IgxCsvExporterService', 'IgxExcelExporterService']; - const moduleTsFiles = update.moduleTsFiles; for (const path of update.templateFiles) { findElementNodes(parseFile(host, path), TAGS) @@ -64,6 +63,7 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { }); } + const moduleTsFiles = tsFiles.filter(x => x.endsWith('.module.ts')); for (const path of moduleTsFiles) { let content = host.read(path)?.toString(); const servicesInFile = []; @@ -74,7 +74,13 @@ export default (): Rule => (host: Tree, context: SchematicContext) => { }); if (servicesInFile.length > 0) { - const comment = '// ' + servicesInFile.join(' and ') + ' no longer need to be manually provided and can be safely removed.'; + let newLine = '\n'; + if (content.indexOf('\r\n') > -1) { + newLine = '\r\n'; + } + + const comment = + '// ' + servicesInFile.join(' and ') + ' no longer need to be manually provided and can be safely removed.' + newLine; content = comment + content; host.overwrite(path, content); }