Skip to content

Commit

Permalink
chore: schematics add badge migration, refactor (#5809)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin authored Oct 31, 2023
1 parent 0467739 commit 9ca6e20
Show file tree
Hide file tree
Showing 33 changed files with 462 additions and 209 deletions.
12 changes: 12 additions & 0 deletions projects/cdk/schematics/ng-update/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from './asset';
export * from './migration-warning';
export * from './removable-input';
export * from './removed-module';
export * from './replacement-attribute';
export * from './replacement-attribute-value';
export * from './replacement-enum';
export * from './replacement-identifier';
export * from './replacement-service';
export * from './replacement-tag';
export * from './replacement-type';
export * from './template-resource';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ReplaceableAttributeValue {
export interface ReplacementAttributeValue {
readonly attrNames: string[];
readonly values: Array<{readonly from: string; readonly to: string}>;
readonly withTagNames?: string[];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Element} from 'parse5';

export interface ReplaceableAttribute {
export interface ReplacementAttribute {
readonly from: {
readonly attrName: string;
readonly filterFn?: (element: Element) => boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ReplacementConst {
export interface ReplacementIdentifier {
readonly from: {
readonly moduleSpecifier?: string[] | string;
readonly name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ReplacementConst} from './replacement-const';
import {ReplacementIdentifier} from './replacement-identifier';

export interface ReplacementService extends ReplacementConst {
export interface ReplacementService extends ReplacementIdentifier {
readonly replaceMethods?: Array<{
from: string;
to: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface ReplaceableTag {
export interface ReplacementTag {
readonly addAttributes?: string[];
readonly from: string;
readonly to: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface TypeToRename {
export interface ReplacementType {
readonly from: string;
readonly moduleSpecifier?: string[] | string;
readonly preserveGenerics?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {addUniqueImport} from '../../../utils/add-unique-import';
import {SMALL_TAB_SYMBOL, SUCCESS_SYMBOL, successLog} from '../../../utils/colored-log';
import {removeImport} from '../../../utils/import-manipulations';
import {setupProgressLogger} from '../../../utils/progress';
import {ReplacementConst} from '../../interfaces/replacement-const';
import {ReplacementIdentifier} from '../../interfaces/replacement-identifier';

export function replaceImports(
replaceable: ReplacementConst[],
replaceable: ReplacementIdentifier[],
options: TuiSchema,
): void {
const allImports = getImports(ALL_TS_FILES);
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/schematics/ng-update/steps/rename-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {
} from '../../utils/colored-log';
import {getNamedImportReferences} from '../../utils/get-named-import-references';
import {removeImport, renameImport} from '../../utils/import-manipulations';
import {TypeToRename} from '../interfaces/type-to-rename';
import {ReplacementType} from '../interfaces/replacement-type';

export function renameTypes(options: TuiSchema, types: readonly TypeToRename[]): void {
export function renameTypes(options: TuiSchema, types: readonly ReplacementType[]): void {
!options[`skip-logs`] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} renaming types...`);

Expand Down
12 changes: 6 additions & 6 deletions projects/cdk/schematics/ng-update/steps/replace-identifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import {
} from '../../utils/colored-log';
import {getNamedImportReferences} from '../../utils/get-named-import-references';
import {removeImport} from '../../utils/import-manipulations';
import {ReplacementConst} from '../interfaces/replacement-const';
import {ReplacementIdentifier} from '../interfaces/replacement-identifier';

export function replaceConstants(
export function replaceIdentifiers(
options: TuiSchema,
constants: readonly ReplacementConst[],
constants: readonly ReplacementIdentifier[],
): void {
!options[`skip-logs`] &&
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing constants...`);
infoLog(`${SMALL_TAB_SYMBOL}${REPLACE_SYMBOL} replacing identifiers...`);

constants.forEach(constToReplace => replaceIdentifier(constToReplace));

!options[`skip-logs`] &&
successLog(`${SMALL_TAB_SYMBOL}${SUCCESS_SYMBOL} constants replaced \n`);
successLog(`${SMALL_TAB_SYMBOL}${SUCCESS_SYMBOL} identifiers replaced \n`);
}

export function replaceIdentifier({from, to}: ReplacementConst): void {
export function replaceIdentifier({from, to}: ReplacementIdentifier): void {
const references = getNamedImportReferences(from.name, from.moduleSpecifier);

references.forEach(ref => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ export function removeInputs({
resource,
recorder,
fileSystem,
replaceableItems,
data,
}: {
fileSystem: DevkitFileSystem;
recorder: UpdateRecorder;
replaceableItems: RemovableInput[];
data: readonly RemovableInput[];
resource: TemplateResource;
}): void {
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

replaceableItems.forEach(({inputName, tags}) => {
data.forEach(({inputName, tags}) => {
const offsets = [
...getInputPropertyOffsets(template, inputName, tags),
...getInputPropertyOffsets(template, `[${inputName}]`, tags),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ import {
getTemplateFromTemplateResource,
getTemplateOffset,
} from '../../../utils/templates/template-resource';
import {ReplaceableAttributeValue} from '../../interfaces/replaceable-attribute-value';
import {ReplacementAttributeValue} from '../../interfaces/replacement-attribute-value';
import {TemplateResource} from '../../interfaces/template-resource';

export function replaceAttrValues({
resource,
recorder,
fileSystem,
replaceableItems,
data,
}: {
fileSystem: DevkitFileSystem;
recorder: UpdateRecorder;
replaceableItems: ReplaceableAttributeValue[];
data: ReplacementAttributeValue[];
resource: TemplateResource;
}): void {
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

replaceableItems.forEach(({attrNames, values, withTagNames}) => {
data.forEach(({attrNames, values, withTagNames}) => {
const elements = [
...findElementsWithAttributeOnTag(template, attrNames, withTagNames),
];
Expand Down
50 changes: 50 additions & 0 deletions projects/cdk/schematics/ng-update/utils/templates/replace-attrs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {UpdateRecorder} from '@angular-devkit/schematics';
import {DevkitFileSystem} from 'ng-morph';

import {
findAttributeOnElementWithAttrs,
findAttributeOnElementWithTag,
} from '../../../utils/templates/elements';
import {
getTemplateFromTemplateResource,
getTemplateOffset,
} from '../../../utils/templates/template-resource';
import {ReplacementAttribute} from '../../interfaces/replacement-attribute';
import {TemplateResource} from '../../interfaces/template-resource';

export function replaceAttrs({
resource,
recorder,
fileSystem,
data,
}: {
fileSystem: DevkitFileSystem;
recorder: UpdateRecorder;
resource: TemplateResource;
data: readonly ReplacementAttribute[];
}): void {
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

data.forEach(({from, to}) => {
const offsets = [
...findAttributeOnElementWithTag(
template,
from.attrName,
from.withTagNames || [],
from.filterFn,
),
...findAttributeOnElementWithAttrs(
template,
from.attrName,
from.withAttrsNames || [],
from.filterFn,
),
];

offsets.forEach(offset => {
recorder.remove(offset + templateOffset, from.attrName.length);
recorder.insertRight(offset + templateOffset, to.attrName);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import {
getTemplateFromTemplateResource,
getTemplateOffset,
} from '../../../utils/templates/template-resource';
import {ReplaceableTag} from '../../interfaces/replaceable-tag';
import {ReplacementTag} from '../../interfaces/replacement-tag';
import {TemplateResource} from '../../interfaces/template-resource';
import {replaceTag} from './replace-tag';

export function replaceTags({
resource,
recorder,
fileSystem,
replaceableItems,
data,
}: {
fileSystem: DevkitFileSystem;
recorder: UpdateRecorder;
replaceableItems: ReplaceableTag[];
data: readonly ReplacementTag[];
resource: TemplateResource;
}): void {
const template = getTemplateFromTemplateResource(resource, fileSystem);
const templateOffset = getTemplateOffset(resource);

replaceableItems.forEach(({from, to, addAttributes}) => {
data.forEach(({from, to, addAttributes}) => {
const elements = findElementsByTagName(template, from);

elements.forEach(({sourceCodeLocation}) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ReplacementConst} from '../../interfaces/replacement-const';
import {ReplacementIdentifier} from '../../interfaces/replacement-identifier';
import {ICONS} from './icons';

export const ICONS_TS: ReplacementConst[] = ICONS.map(({from, to}) => ({
export const ICONS_TS: ReplacementIdentifier[] = ICONS.map(({from, to}) => ({
from: {name: from, moduleSpecifier: `@taiga-ui/proprietary-icons`},
to: {name: to, moduleSpecifier: `@taiga-ui/proprietary-tds-icons`},
}));
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ReplacementConst} from '../../interfaces/replacement-const';
import {ReplacementIdentifier} from '../../interfaces/replacement-identifier';
import {ICONS} from './icons';

export const ICONS_TS: ReplacementConst[] = ICONS.map(({from, to}) => ({
export const ICONS_TS: ReplacementIdentifier[] = ICONS.map(({from, to}) => ({
from: {name: from, moduleSpecifier: `@taiga-ui/proprietary-icons`},
to: {name: to, moduleSpecifier: `@taiga-ui/proprietary-tds-icons`},
}));
4 changes: 2 additions & 2 deletions projects/cdk/schematics/ng-update/v3/constants/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ReplacementConst} from '../../interfaces/replacement-const';
import {ReplacementIdentifier} from '../../interfaces/replacement-identifier';

export const CONSTANTS_TO_REPLACE: ReplacementConst[] = [
export const CONSTANTS_TO_REPLACE: ReplacementIdentifier[] = [
{
from: {
name: `EMPTY_VALIDATOR`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TypeToRename} from '../../interfaces/type-to-rename';
import {ReplacementType} from '../../interfaces/replacement-type';

export const DEPRECATED_FUNCTIONS: readonly TypeToRename[] = [
export const DEPRECATED_FUNCTIONS: readonly ReplacementType[] = [
{
from: `tuiReplayedValueChangesFrom`,
to: `tuiControlValue`,
Expand Down
12 changes: 6 additions & 6 deletions projects/cdk/schematics/ng-update/v3/constants/templates.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {hasElementAttribute} from '../../../utils/templates/elements';
import {RemovableInput} from '../../interfaces/removable-input';
import {ReplaceableAttribute} from '../../interfaces/replaceable-attribute';
import {ReplaceableAttributeValue} from '../../interfaces/replaceable-attribute-value';
import {ReplaceableTag} from '../../interfaces/replaceable-tag';
import {ReplacementAttribute} from '../../interfaces/replacement-attribute';
import {ReplacementAttributeValue} from '../../interfaces/replacement-attribute-value';
import {ReplacementTag} from '../../interfaces/replacement-tag';
import {AttributeToDirective} from '../interfaces/attribute-to-directive';
import {TUI_INTERACTIVE_SELECTORS} from './tui-interactive-selectors';

export const ATTRS_TO_REPLACE: ReplaceableAttribute[] = [
export const ATTRS_TO_REPLACE: ReplacementAttribute[] = [
{
from: {attrName: `tuiResizableColumn`, withAttrsNames: [`tuiResizableColumn`]},
to: {attrName: `tuiTh [resizable]="true"`},
Expand Down Expand Up @@ -504,7 +504,7 @@ export const INPUTS_TO_REMOVE: RemovableInput[] = [
},
];

export const TAGS_TO_REPLACE: ReplaceableTag[] = [
export const TAGS_TO_REPLACE: ReplacementTag[] = [
{
from: `tui-group`,
to: `div`,
Expand Down Expand Up @@ -731,7 +731,7 @@ export const TEMPLATE_COMMENTS = [
},
] as const;

export const REPLACE_ATTR_VALUE: ReplaceableAttributeValue[] = [
export const REPLACE_ATTR_VALUE: ReplacementAttributeValue[] = [
{
attrNames: [`tuiHintDirection`],
values: [
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/schematics/ng-update/v3/constants/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TypeToRename} from '../../interfaces/type-to-rename';
import {ReplacementType} from '../../interfaces/replacement-type';

export const TYPES_TO_RENAME: readonly TypeToRename[] = [
export const TYPES_TO_RENAME: readonly ReplacementType[] = [
{
from: `ButtonOptions`,
to: `TuiButtonOptions`,
Expand Down
4 changes: 2 additions & 2 deletions projects/cdk/schematics/ng-update/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {removeModules} from '../steps/remove-module';
import {renameTypes} from '../steps/rename-types';
import {replaceDeepImports} from '../steps/replace-deep-import';
import {replaceEnums} from '../steps/replace-enums';
import {replaceConstants} from '../steps/replace-identifier';
import {replaceIdentifiers} from '../steps/replace-identifier';
import {replaceServices} from '../steps/replace-services';
import {showWarnings} from '../steps/show-warnings';
import {getFileSystem} from '../utils/get-file-system';
Expand Down Expand Up @@ -69,7 +69,7 @@ function main(options: TuiSchema): Rule {
replaceDeepImports(options);
replaceEnums(options, ENUMS_TO_REPLACE);
renameTypes(options, TYPES_TO_RENAME);
replaceConstants(options, CONSTANTS_TO_REPLACE);
replaceIdentifiers(options, CONSTANTS_TO_REPLACE);
replaceServices(options, SERVICES_TO_REPLACE);
replaceStyles();
showWarnings(context, MIGRATION_WARNINGS);
Expand Down
Loading

0 comments on commit 9ca6e20

Please sign in to comment.