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

chore: add proprieraty-icons migration #7969

Merged
merged 1 commit into from
Jun 28, 2024
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
16 changes: 8 additions & 8 deletions projects/cdk/schematics/ng-update/v4/migrate-icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ import {
} from '../../../utils/colored-log';
import {getFileSystem} from '../../utils/get-file-system';
import {renameIcons} from './rename-icons';
import {renameProprietaryIcons} from './rename-proprietary-icons';

export function migrateIcons(options: TuiSchema): Rule {
return chain([
(tree: Tree, _: SchematicContext) => {
if (hasProprietaryIcons(tree)) {
// TODO: add proprietary-icons migration

return;
}

const fileSystem = getFileSystem(tree);

!options['skip-logs'] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing strings...`);
renameIcons();
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing icons...`);

if (hasProprietaryIcons(tree)) {
renameProprietaryIcons();
} else {
renameIcons();
}

fileSystem.commitEdits();
saveActiveProject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// <reference lib="es2021" />
import {getSourceFiles} from 'ng-morph';

import {ALL_FILES} from '../../../constants';

export function renameProprietaryIcons(pattern = ALL_FILES): void {
const sourceFiles = getSourceFiles(pattern);

sourceFiles.forEach(file => {
let text = file.getFullText();

const regex = /\btuiIcon(?!Button\b)[A-Z][a-zA-Z0-9]*\b/g;

text = text.replaceAll(regex, match => convertString(match));

file.replaceWithText(text);
});
}

function convertString(input: string): string {
const result = input
.replace(/^tuiIconTds/, '')
.replace(/SmallPragmatic$/, '')
.replace(/MediumPragmatic$/, '')
.replace(/Small$/, '')
.replace(/Medium$/, '')
.replaceAll(/([A-Z0-9])/g, '-$1')
.toLowerCase();

const pack = input.includes('Pragmatic') ? 'pragmatic' : 'fancy';
const size = input.includes('Medium') ? 'medium' : 'small';

return `@tui.${pack}.${size}.${result.startsWith('-') ? result.slice(1) : result}`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,10 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
from: {name: 'TuiPortalModule', moduleSpecifier: '@taiga-ui/cdk'},
to: {name: 'TuiDropdownPortal', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TuiDropdownModule', moduleSpecifier: '@taiga-ui/core'},
to: {name: 'TuiDropdown', moduleSpecifier: '@taiga-ui/core'},
},
{
from: {name: 'TuiDroppableModule', moduleSpecifier: '@taiga-ui/cdk'},
to: {name: 'TuiDroppable', moduleSpecifier: '@taiga-ui/cdk'},
Expand Down Expand Up @@ -696,6 +700,20 @@ export const IDENTIFIERS_TO_REPLACE: ReplacementIdentifierMulti[] = [
moduleSpecifier: '@taiga-ui/core',
},
},
{
from: {name: 'TUI_ARROW', moduleSpecifier: '@taiga-ui/kit'},
to: {
name: 'TUI_ARROW',
moduleSpecifier: '@taiga-ui/legacy',
},
},
{
from: {name: 'TuiArrowComponent', moduleSpecifier: '@taiga-ui/kit'},
to: {
name: 'TuiArrowComponent',
moduleSpecifier: '@taiga-ui/legacy',
},
},
{
from: {name: 'TuiDialogModule', moduleSpecifier: '@taiga-ui/core'},
to: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,10 @@ export const MIGRATION_WARNINGS: MigrationWarning[] = [
message:
'TUI_INPUT_NUMBER_OPTIONS "precision" and "decimal" have been moved to TUI_FORMAT_NUMBER_OPTIONS. See https://taiga-ui.dev/components/input-number#options ',
},
{
name: 'TUI_ARROW',
moduleSpecifier: '@taiga-ui/kit',
message:
'TUI_ARROW is deprecated, Use tuiChevron directive instead. See example https://taiga-ui.dev/components/data-list#links',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ const TEMPLATE_AFTER = `
<button tuiIconButton icon="@tui.x">Button</button>
`;

const PROPRIETARY_TEMPLATE_BEFORE = `
<tui-avatar
avatarUrl="tuiIconTdsLockSmall"
text="alex inkin"
[rounded]="true"
></tui-avatar>
<tui-avatar
avatarUrl="tuiIconTdsTransportAutoRubleMedium"
></tui-avatar>
<button tuiIconButton icon="tuiIconTdsMoreMediumPragmatic">Button</button>
<button tuiIconButton icon="tuiIconTdsCheckMedium">Button</button>
`;

const PROPRIETARY_TEMPLATE_AFTER = `
<tui-avatar
avatarUrl="@tui.fancy.small.lock"
text="alex inkin"
[rounded]="true"
></tui-avatar>
<tui-avatar
avatarUrl="@tui.fancy.medium.transport-auto-ruble"
></tui-avatar>
<button tuiIconButton icon="@tui.pragmatic.medium.more">Button</button>
<button tuiIconButton icon="@tui.fancy.medium.check">Button</button>
`;

describe('ng-update', () => {
let host: UnitTestTree;
let runner: SchematicTestRunner;
Expand Down Expand Up @@ -94,6 +120,26 @@ describe('ng-update', () => {
expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER);
});

it('should migrate proprietary icons in ts files', async () => {
createSourceFile(
'package.json',
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/proprietary-icons": "~3.42.0"}}',
{overwrite: true},
);

saveActiveProject();

const tree = await runner.runSchematic(
'migrateIconsV4',
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>,
host,
);

expect(tree.readContent('test/app/proprietary-test.template.html')).toEqual(
PROPRIETARY_TEMPLATE_AFTER,
);
});

afterEach(() => {
resetActiveProject();
});
Expand All @@ -104,6 +150,11 @@ function createMainFiles(): void {

createSourceFile('test/app/test.template.html', TEMPLATE_BEFORE);

createSourceFile(
'test/app/proprietary-test.template.html',
PROPRIETARY_TEMPLATE_BEFORE,
);

createSourceFile(
'package.json',
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/addon-commerce": "~3.42.0"}}',
Expand Down
Loading