Skip to content

Commit

Permalink
feat: added apex and lwc in migration report
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhinavKumar-sf committed Nov 14, 2024
1 parent 17c21af commit f57178f
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 99 deletions.
11 changes: 7 additions & 4 deletions src/commands/omnistudio/migration/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,15 @@ export default class Migrate extends OmniStudioBaseCommand {
allVersions,
this.org
);
const relatedObjectMigrationResult = omnistudioRelatedObjectsMigration.migrateAll(objectMigrationResults, []);
const relatedObjectMigrationResult = omnistudioRelatedObjectsMigration.migrateAll(objectMigrationResults, [
'lwc',
'apex',
]);
generatePackageXml.createChangeList(
relatedObjectMigrationResult.apexClasses,
relatedObjectMigrationResult.lwcComponents
relatedObjectMigrationResult.apexAssessmentInfos,
relatedObjectMigrationResult.lwcAssessmentInfos
);
await ResultsBuilder.generate(objectMigrationResults, conn.instanceUrl);
await ResultsBuilder.generate(objectMigrationResults, relatedObjectMigrationResult, conn.instanceUrl);

// save timer to debug logger
this.logger.debug(timer);
Expand Down
22 changes: 0 additions & 22 deletions src/migration/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,6 @@ export interface NameTransformData {
nameWithSepcialCharactorSet: Set<string>;
}

export interface RelatedObjectsMigrate {
/**
* Identifies migration candidates based on the provided migration results and namespace.
*
* @param migrationResults List of migration results to identify objects from.
* @param namespace The namespace used to identify objects.
* @returns List of identified migration candidates as strings.
*/
identifyObjects(migrationResults: MigrationResult[]): Promise<JSON[]>;

/**
* Private method to perform the migration of related objects based on the provided candidates.
*
* @param migrationResults List of migration results to use for migration.
* @param namespace The namespace used to perform the migration.
* @param migrationCandidates List of candidates to migrate.
*/
migrateRelatedObjects(migrationResults: MigrationResult[], migrationCandidates: JSON[]): string[];

processObjectType(): string;
}

export type LWCComponentMigrationTool = MigrationTool;

export type CustomLabelMigrationTool = MigrationTool;
Expand Down
43 changes: 28 additions & 15 deletions src/migration/related/ApexMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import {
SingleTokenUpdate,
TokenUpdater,
} from '../../utils/apex/parser/apexparser';
import { MigrationResult, RelatedObjectsMigrate } from '../interfaces';
import { MigrationResult } from '../interfaces';
import { sfProject } from '../../utils/sfcli/project/sfProject';
import { fileutil, File } from '../../utils/file/fileutil';
import { Logger } from '../../utils/logger';
import { ApexAssessmentInfo } from '../../utils';
import { FileDiffUtil } from '../../utils/lwcparser/fileutils/FileDiffUtil';
import { Stringutil } from '../../utils/StringValue/stringutil';
import { BaseRelatedObjectMigration } from './BaseRealtedObjectMigration';

const APEXCLASS = 'Apexclass';
Expand All @@ -26,7 +27,7 @@ const CALLABLE = 'Callable';
const VLOCITY_OPEN_INTERFACE2 = 'VlocityOpenInterface2';
const VLOCITY_OPEN_INTERFACE = 'VlocityOpenInterface';

export class ApexMigration extends BaseRelatedObjectMigration implements RelatedObjectsMigrate {
export class ApexMigration extends BaseRelatedObjectMigration {
private readonly callableInterface: InterfaceImplements;
private readonly vlocityOpenInterface2: InterfaceImplements;
private readonly vlocityOpenInterface: InterfaceImplements;
Expand All @@ -43,25 +44,24 @@ export class ApexMigration extends BaseRelatedObjectMigration implements Related
public identifyObjects(migrationResults: MigrationResult[]): Promise<JSON[]> {
throw new Error('Method not implemented.');
}
public migrateRelatedObjects(migrationResults: MigrationResult[], migrationCandidates: JSON[]): string[] {
public migrateRelatedObjects(migrationResults: MigrationResult[], migrationCandidates: JSON[]): ApexAssessmentInfo[] {
return this.migrate();
}
public migrate(): string[] {
public migrate(): ApexAssessmentInfo[] {
const pwd = shell.pwd();
shell.cd(this.projectPath);
const targetOrg: Org = this.org;
// sfProject.retrieve(APEXCLASS, targetOrg.getUsername());
sfProject.retrieve(APEXCLASS, targetOrg.getUsername());
const apexAssessmentInfos = this.processApexFiles(this.projectPath);
sfProject.deploy(APEXCLASS, targetOrg.getUsername());
// sfProject.deploy(APEXCLASS, targetOrg.getUsername());
shell.cd(pwd);
return this.mapTOName(apexAssessmentInfos);
return apexAssessmentInfos;
}

public assess(): ApexAssessmentInfo[] {
const pwd = shell.pwd();
shell.cd(this.projectPath);
// const targetOrg: Org = this.org;
// sfProject.retrieve(APEXCLASS, this.org.getUsername());
sfProject.retrieve(APEXCLASS, this.org.getUsername());
const apexAssessmentInfos = this.processApexFiles(this.projectPath);
shell.cd(pwd);
return apexAssessmentInfos;
Expand Down Expand Up @@ -158,6 +158,19 @@ export class ApexMigration extends BaseRelatedObjectMigration implements Related
for (const tokenChange of namespaceChanges.get(this.namespace))
tokenUpdates.push(new SingleTokenUpdate(this.updatedNamespace, tokenChange));
}

const methodParameters = parser.methodParameters;
if (methodParameters.size === 0) return tokenUpdates;
const drParameters = methodParameters.get(ParameterType.DR_NAME);
if (drParameters) {
for (const token of drParameters) {
const newName = `'${Stringutil.cleanName(token.text)}'`;
if (token.text === newName) continue;
Logger.logger.info(`In Apex ${file.name} DR name ${token.text} will be updated to ${newName} `);
Logger.ux.log(`In Apex ${file.name} DR name ${token.text} will be updated to ${newName}`);
tokenUpdates.push(new SingleTokenUpdate(newName, token));
}
}
return tokenUpdates;
}

Expand All @@ -184,10 +197,10 @@ export class ApexMigration extends BaseRelatedObjectMigration implements Related
}
`;
}

private mapTOName(apexAssessmentInfos: ApexAssessmentInfo[]): string[] {
return apexAssessmentInfos.map((apexAssessmentInfo) => {
return apexAssessmentInfo.name;
});
}
/*
private mapTOName(apexAssessmentInfos: ApexAssessmentInfo[]): string[] {
return apexAssessmentInfos.map((apexAssessmentInfo) => {
return apexAssessmentInfo.name;
});
} */
}
8 changes: 4 additions & 4 deletions src/migration/related/LwcMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as shell from 'shelljs';
import { Org } from '@salesforce/core';
import { fileutil, File } from '../../utils/file/fileutil';
import { MigrationResult, RelatedObjectsMigrate } from '../interfaces';
import { MigrationResult } from '../interfaces';
import { sfProject } from '../../utils/sfcli/project/sfProject';
import { Logger } from '../../utils/logger';
import { FileProcessorFactory } from '../../utils/lwcparser/fileutils/FileProcessorFactory';
Expand All @@ -12,7 +12,7 @@ import { BaseRelatedObjectMigration } from './BaseRealtedObjectMigration';
const LWC_DIR_PATH = '/force-app/main/default/lwc';
const LWCTYPE = 'LightningComponentBundle';

export class LwcMigration extends BaseRelatedObjectMigration implements RelatedObjectsMigrate {
export class LwcMigration extends BaseRelatedObjectMigration {
public processObjectType(): string {
return 'lwc';
}
Expand All @@ -27,7 +27,7 @@ export class LwcMigration extends BaseRelatedObjectMigration implements RelatedO
const type = 'assessment';
const pwd = shell.pwd();
shell.cd(this.projectPath);
// sfProject.retrieve(LWCTYPE, this.org.getUsername());
sfProject.retrieve(LWCTYPE, this.org.getUsername());
const filesMap = this.processLwcFiles(this.projectPath);
shell.cd(pwd);
return this.processFiles(filesMap, type);
Expand All @@ -40,7 +40,7 @@ export class LwcMigration extends BaseRelatedObjectMigration implements RelatedO
sfProject.retrieve(LWCTYPE, targetOrg.getUsername());
const filesMap = this.processLwcFiles(this.projectPath);
const LWCAssessmentInfos = this.processFiles(filesMap, 'migration');
sfProject.deploy(LWCTYPE, targetOrg.getUsername());
// sfProject.deploy(LWCTYPE, targetOrg.getUsername());
shell.cd(pwd);
return LWCAssessmentInfos;
}
Expand Down
52 changes: 25 additions & 27 deletions src/migration/related/OmnistudioRelatedObjectMigrationFacade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Org } from '@salesforce/core';
import '../../utils/prototypes';
import { DebugTimer, MigratedObject } from '../../utils';
import { RelatedObjectMigrationResult, RelatedObjectsMigrate } from '../interfaces';
import {
ApexAssessmentInfo,
DebugTimer,
LWCAssessmentInfo,
MigratedObject,
RelatedObjectAssesmentInfo,
} from '../../utils';
import { sfProject } from '../../utils/sfcli/project/sfProject';
import { Logger } from '../../utils/logger';
import { ApexMigration } from './ApexMigration';
Expand Down Expand Up @@ -44,35 +49,33 @@ export default class OmnistudioRelatedObjectMigrationFacade {
return process.cwd() + '/' + defaultProjectName;
}
}
public migrateAll(migrationResult: MigratedObject[], relatedObjects: string[]): RelatedObjectMigrationResult {
public migrateAll(migrationResult: MigratedObject[], relatedObjects: string[]): RelatedObjectAssesmentInfo {
// Start the debug timer
DebugTimer.getInstance().start();

// Declare an array of MigrationTool
const migrationTools: RelatedObjectsMigrate[] = [];
const projectDirectory: string = OmnistudioRelatedObjectMigrationFacade.intializeProject();
const debugTimer = DebugTimer.getInstance();
debugTimer.start();
// Initialize migration tools based on the relatedObjects parameter
if (relatedObjects.includes('lwc')) {
migrationTools.push(this.createLWCComponentMigrationTool(this.namespace, projectDirectory));
}
if (relatedObjects.includes('labels')) {
migrationTools.push(this.createCustomLabelMigrationTool(this.namespace, this.org));
}
if (relatedObjects.includes('apex')) {
migrationTools.push(this.createApexClassMigrationTool(projectDirectory));
}
const results = new Map<string, string[]>();
const apexMigrator = this.createApexClassMigrationTool(projectDirectory);
const lwcMigrator = this.createLWCComponentMigrationTool(this.namespace, projectDirectory);
let apexAssessmentInfos: ApexAssessmentInfo[] = [];
let lwcAssessmentInfos: LWCAssessmentInfo[] = [];
// Proceed with migration logic
for (const migrationTool of migrationTools) {
try {
results.set(migrationTool.processObjectType(), migrationTool.migrateRelatedObjects(null, null));
} catch (Error) {
// Log the error
Logger.logger.error(Error.message);
}
try {
apexAssessmentInfos = apexMigrator.migrate();
} catch (Error) {
// Log the error
Logger.logger.error(Error.message);
}
try {
lwcAssessmentInfos = lwcMigrator.migrate();
} catch (Error) {
// Log the error
Logger.logger.error(Error.message);
}

// Truncate existing objects if necessary
// Stop the debug timer
const timer = debugTimer.stop();
Expand All @@ -81,7 +84,7 @@ export default class OmnistudioRelatedObjectMigrationFacade {
Logger.logger.debug(timer);

// Return results needed for --json flag
return { apexClasses: results.get('apex'), lwcComponents: results.get('lwc') };
return { apexAssessmentInfos, lwcAssessmentInfos };
}

// Factory methods to create instances of specific tools
Expand All @@ -90,11 +93,6 @@ export default class OmnistudioRelatedObjectMigrationFacade {
return new LwcMigration(projectPath, this.namespace, this.org);
}

private createCustomLabelMigrationTool(namespace: string, org: Org): RelatedObjectsMigrate {
// Return an instance of CustomLabelMigrationTool when implemented
throw new Error('CustomLabelMigrationTool implementation is not provided yet.');
}

private createApexClassMigrationTool(projectPath: string): ApexMigration {
// Return an instance of ApexClassMigrationTool when implemented
return new ApexMigration(projectPath, this.namespace, this.org);
Expand Down
27 changes: 24 additions & 3 deletions src/utils/generatePackageXml.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';
import { ApexAssessmentInfo, LWCAssessmentInfo } from './interfaces';

export class generatePackageXml {
// Method to generate package.xml with additional types
public static createChangeList(apexClasses: string[], lwcComponents: string[]): void {
const apexXml = generatePackageXml.getXmlElementforMembers(apexClasses, 'ApexClass');
const lwcXml = generatePackageXml.getXmlElementforMembers(lwcComponents, 'LightningComponentBundle');
public static createChangeList(
apexAssementInfos: ApexAssessmentInfo[],
lwcAssessmentInfos: LWCAssessmentInfo[]
): void {
const apexXml = generatePackageXml.getXmlElementforMembers(this.getApexclasses(apexAssementInfos), 'ApexClass');
const lwcXml = generatePackageXml.getXmlElementforMembers(
this.getLwcs(lwcAssessmentInfos),
'LightningComponentBundle'
);

const additionalTypes = `
<types>
Expand Down Expand Up @@ -67,4 +74,18 @@ export class generatePackageXml {
<name>${type}</name>
</types> `;
}

private static getApexclasses(apexAssessmentInfos: ApexAssessmentInfo[]): string[] {
if (!apexAssessmentInfos || apexAssessmentInfos.length === 0) return [];
return apexAssessmentInfos.map((apexAssessmentInfo) => {
return apexAssessmentInfo.name;
});
}

private static getLwcs(lwcAssessmentInfos: LWCAssessmentInfo[]): string[] {
if (!lwcAssessmentInfos || lwcAssessmentInfos.length === 0) return [];
return lwcAssessmentInfos.map((lwcAssessmentInfo) => {
return lwcAssessmentInfo.name;
});
}
}
4 changes: 4 additions & 0 deletions src/utils/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ export interface AssessmentInfo {
dataRaptorAssessmentInfos: DataRaptorAssessmentInfo[];
}

export interface RelatedObjectAssesmentInfo {
apexAssessmentInfos: ApexAssessmentInfo[];
lwcAssessmentInfos: LWCAssessmentInfo[];
}
export interface FlexCardAssessmentInfo {
name: string;
id: string;
Expand Down
13 changes: 6 additions & 7 deletions src/utils/lwcparser/fileutils/HtmlFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ export class HtmlFileProcessor implements FileProcessor {
const fileDiffUtil = new FileDiffUtil();
const fileContent: Map<string, string> = parse.replaceTags(namespace);
if (fileContent) {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
if (type != null && type === 'migration') {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
} else {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
return diff;
}
return diff;
}
}
}
13 changes: 6 additions & 7 deletions src/utils/lwcparser/fileutils/JavascriptFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ export class JavascriptFileProcessor implements FileProcessor {
const filePath = file.location;
const fileContent: Map<string, string> = jsParser.replaceImportSource(filePath, namespace);
if (fileContent) {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
if (type != null && type === 'migration') {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
} else {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
return diff;
}
return diff;
}
}
}
13 changes: 6 additions & 7 deletions src/utils/lwcparser/fileutils/XmlFileProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ export class XmlFileProcessor implements FileProcessor {
const fileDiffUtil = new FileDiffUtil();
const fileContent: Map<string, string> = parser.removeNode(XML_TAG_TO_REPLACE);
if (fileContent) {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
if (type != null && type === 'migration') {
fileutil.saveToFile(filePath, fileContent.get(FileConstant.MODIFIED_CONTENT));
} else {
const diff = fileDiffUtil.getFileDiff(
file.name + file.ext,
fileContent.get(FileConstant.BASE_CONTENT),
fileContent.get(FileConstant.MODIFIED_CONTENT)
);
return diff;
}
return diff;
}
}
}
Loading

0 comments on commit f57178f

Please sign in to comment.