Skip to content

Commit

Permalink
feat(schematics): add experimental migration (#7399)
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored May 15, 2024
1 parent e18d991 commit 474af06
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"Wolfflegelstainhausenbergedorf",
"Arkhangelsky",
"Zoya",
"Kosmodemyanskaya"
"Kosmodemyanskaya",
"Siteheader"
],
"ignoreRegExpList": ["\\(https?://.*?\\)", "\\/{1}.+\\/{1}", "\\%2F.+", "\\%2C.+", "\\ɵ.+", "\\ыва.+"],
"overrides": [
Expand Down
5 changes: 5 additions & 0 deletions projects/cdk/schematics/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"version": "3.40.0",
"factory": "./ng-update/v3-40/index#updateToV3_40"
},
"updateToV3_78": {
"description": "Add experimental proprietary logos",
"version": "3.78.0",
"factory": "./ng-update/v3-78/index#updateToV3_78"
},
"updateToV4": {
"description": "Updates Taiga UI packages to v4",
"version": "4.0.0",
Expand Down
8 changes: 8 additions & 0 deletions projects/cdk/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ export interface TuiSchema {
* ```
*/
'skip-logs'?: boolean;

/**
* @example
* ```console
* schematics ./dist/cdk:updateToV3 --experimental
* ```
*/
experimental?: boolean;
}
38 changes: 38 additions & 0 deletions projects/cdk/schematics/ng-update/v3-78/constants/icons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const ICONS: Array<{from: string; to: string}> = [
{
from: 'tuiIconTdsTinkoffShieldLogoInverseEnLogoSiteheader',
to: 'tuiIconTdsTBankEngInverseLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffBusinessShieldLogoLogoSiteheader',
to: 'tuiIconTdsTBankBusinessLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffBusinessShieldLogoInverseLogoSiteheader',
to: 'tuiIconTdsTBankBusinessInverseLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffEngLogo',
to: 'tuiIconTdsTBankTextEngLogo',
},
{
from: 'tuiIconTdsTinkoffRuLogo',
to: 'tuiIconTdsTBankTextLogo',
},
{
from: 'tuiIconTdsTinkoffShieldLogoInverseEnLogoSiteheader',
to: 'tuiIconTdsTBankEngInverseLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffShieldLogoEnLogoSiteheader',
to: 'tuiIconTdsTBankEngLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffShieldLogoLogoSiteheader',
to: 'tuiIconTdsTBankLogoSiteheader',
},
{
from: 'tuiIconTdsTinkoffShieldLogoInverseLogoSiteheader',
to: 'tuiIconTdsTBankInverseLogoSiteheader',
},
].sort((prev, next) => next.from.length - prev.from.length);
45 changes: 45 additions & 0 deletions projects/cdk/schematics/ng-update/v3-78/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {Rule, SchematicContext, Tree} from '@angular-devkit/schematics';
import {getPackageJsonDependency, saveActiveProject} from 'ng-morph';

import {TuiSchema} from '../../ng-add/schema';
import {
FINISH_SYMBOL,
infoLog,
REPLACE_SYMBOL,
SMALL_TAB_SYMBOL,
titleLog,
} from '../../utils/colored-log';
import {getFileSystem} from '../utils/get-file-system';
import {replaceText} from '../utils/replace-text';
import {ICONS} from './constants/icons';

function hasProprietaryIcons(tree: Tree): boolean {
return !!getPackageJsonDependency(tree, '@taiga-ui/proprietary-icons');
}

export function updateToV3_78(options: TuiSchema): Rule {
return (tree: Tree, _: SchematicContext) => {
if (!hasProprietaryIcons(tree)) {
!options['skip-logs'] &&
titleLog(`${FINISH_SYMBOL} No migrations required\n`);

return;
}

const fileSystem = getFileSystem(tree);

if (options.experimental) {
!options['skip-logs'] &&
infoLog(
`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing deprecated logos...`,
);
replaceText(ICONS);
}

fileSystem.commitEdits();
saveActiveProject();

!options['skip-logs'] &&
titleLog(`${FINISH_SYMBOL} Icons successfully migrated \n`);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import {HostTree} from '@angular-devkit/schematics';
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
import {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema';
import {
createProject,
createSourceFile,
resetActiveProject,
saveActiveProject,
setActiveProject,
} from 'ng-morph';
import {join} from 'path';

import {createAngularJson} from '../../../utils/create-angular-json';

const collectionPath = join(__dirname, '../../../migration.json');

const COMPONENT_BEFORE = `
@Component({templateUrl: './test.template.html'})
export class TestComponent {
icon1 = tuiIconTdsTinkoffShieldLogoInverseEnLogoSiteheader;
icon2 = tuiIconTdsTinkoffBusinessShieldLogoLogoSiteheader;
icon3 = tuiIconTdsTinkoffBusinessShieldLogoInverseLogoSiteheader;
icon4 = tuiIconTdsTinkoffEngLogo;
icon5 = tuiIconTdsTinkoffRuLogo;
icon6 = tuiIconTdsTinkoffShieldLogoInverseEnLogoSiteheader;
icon7 = tuiIconTdsTinkoffShieldLogoEnLogoSiteheader;
icon8 = tuiIconTdsTinkoffShieldLogoLogoSiteheader;
icon9 = tuiIconTdsTinkoffShieldLogoInverseLogoSiteheader;
}`;

const COMPONENT_AFTER = `
@Component({templateUrl: './test.template.html'})
export class TestComponent {
icon1 = tuiIconTdsTBankEngInverseLogoSiteheader;
icon2 = tuiIconTdsTBankBusinessLogoSiteheader;
icon3 = tuiIconTdsTBankBusinessInverseLogoSiteheader;
icon4 = tuiIconTdsTBankTextEngLogo;
icon5 = tuiIconTdsTBankTextLogo;
icon6 = tuiIconTdsTBankEngInverseLogoSiteheader;
icon7 = tuiIconTdsTBankEngLogoSiteheader;
icon8 = tuiIconTdsTBankLogoSiteheader;
icon9 = tuiIconTdsTBankInverseLogoSiteheader;
}`;

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;

beforeEach(() => {
host = new UnitTestTree(new HostTree());
runner = new SchematicTestRunner('schematics', collectionPath);

setActiveProject(createProject(host));

createMainFiles();

saveActiveProject();
});

it('should migrate old deprecated logos', async () => {
const tree = await runner
.runSchematicAsync(
'updateToV3_78',
{
experimental: true,
'skip-logs': process.env['TUI_CI'] === 'true',
} as Partial<TuiSchema>,
host,
)
.toPromise();

expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER);
});

afterEach(() => {
resetActiveProject();
});
});

function createMainFiles(): void {
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE);

createAngularJson();
createSourceFile(
'package.json',
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/proprietary-icons": "~3.78.0"}}',
);
}

0 comments on commit 474af06

Please sign in to comment.