Skip to content

[rush-sdk] Expose file change analysis on the ProjectChangeAnalyzer class #4959

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

Merged
merged 15 commits into from
Oct 3, 2024
Merged
18 changes: 9 additions & 9 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,14 @@ export interface ICloudBuildCacheProvider {
updateCachedCredentialInteractiveAsync(terminal: ITerminal): Promise<void>;
}

// @beta (undocumented)
export interface IClusterFileInfoOptions<TItem, TGroup> {
// (undocumented)
infoByPath: ReadonlyMap<string, TItem>;
// (undocumented)
lookup: LookupByPath<TGroup>;
}

// @beta (undocumented)
export interface ICobuildCompletedState {
cacheId: string;
Expand Down Expand Up @@ -517,14 +525,6 @@ export interface IGetChangedProjectsOptions {
variant?: string;
}

// @beta (undocumented)
export interface IGetChangesByProjectOptions {
// (undocumented)
changedFiles: Map<string, IFileDiffStatus>;
// (undocumented)
lookup: LookupByPath<RushConfigurationProject>;
}

// @beta
export interface IGlobalCommand extends IRushCommand {
}
Expand Down Expand Up @@ -1126,7 +1126,7 @@ export class ProjectChangeAnalyzer {
_filterProjectDataAsync<T>(project: RushConfigurationProject, unfilteredProjectData: Map<string, T>, rootDir: string, terminal: ITerminal): Promise<Map<string, T>>;
getChangedProjectsAsync(options: IGetChangedProjectsOptions): Promise<Set<RushConfigurationProject>>;
// (undocumented)
protected getChangesByProject(options: IGetChangesByProjectOptions): Map<RushConfigurationProject, Map<string, IFileDiffStatus>>;
protected getChangesByProject(options: IClusterFileInfoOptions<IFileDiffStatus, RushConfigurationProject>): Map<RushConfigurationProject, Map<string, IFileDiffStatus>>;
// @internal
_tryGetProjectDependenciesAsync(project: RushConfigurationProject, terminal: ITerminal): Promise<Map<string, string> | undefined>;
// @internal
Expand Down
2 changes: 1 addition & 1 deletion libraries/rush-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export {

export {
ProjectChangeAnalyzer,
type IGetChangesByProjectOptions,
type IClusterFileInfoOptions,
type IGetChangedProjectsOptions,
type IRawRepoState as _IRawRepoState
} from './logic/ProjectChangeAnalyzer';
Expand Down
50 changes: 28 additions & 22 deletions libraries/rush-lib/src/logic/ProjectChangeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,31 @@ export interface IGetChangedProjectsOptions {
/**
* @beta
*/
export interface IGetChangesByProjectOptions {
lookup: LookupByPath<RushConfigurationProject>;
changedFiles: Map<string, IFileDiffStatus>;
export interface IClusterFileInfoOptions<TItem, TGroup> {
lookup: LookupByPath<TGroup>;
infoByPath: ReadonlyMap<string, TItem>;
}

function clusterFileInfo<TItem, TGroup>(
options: IClusterFileInfoOptions<TItem, TGroup>
): Map<TGroup, Map<string, TItem>> {
const { lookup, infoByPath } = options;
const clusteredFileInfo: Map<TGroup, Map<string, TItem>> = new Map();

for (const [file, diffStatus] of infoByPath) {
const group: TGroup | undefined = lookup.findChildPath(file);
if (!group) {
continue;
}
let groupInfo: Map<string, TItem> | undefined = clusteredFileInfo.get(group);
if (!groupInfo) {
groupInfo = new Map();
clusteredFileInfo.set(group, groupInfo);
}
groupInfo.set(file, diffStatus);
}

return clusteredFileInfo;
}

interface IGitState {
Expand Down Expand Up @@ -244,7 +266,7 @@ export class ProjectChangeAnalyzer {
const changesByProject: Map<
RushConfigurationProject,
Map<string, IFileDiffStatus>
> = this.getChangesByProject({ changedFiles, lookup });
> = this.getChangesByProject({ infoByPath: changedFiles, lookup });

const changedProjects: Set<RushConfigurationProject> = new Set();
if (enableFiltering) {
Expand Down Expand Up @@ -333,25 +355,9 @@ export class ProjectChangeAnalyzer {
}

protected getChangesByProject(
options: IGetChangesByProjectOptions
options: IClusterFileInfoOptions<IFileDiffStatus, RushConfigurationProject>
): Map<RushConfigurationProject, Map<string, IFileDiffStatus>> {
const { lookup, changedFiles } = options;
const changesByProject: Map<RushConfigurationProject, Map<string, IFileDiffStatus>> = new Map();

for (const [file, diffStatus] of changedFiles) {
const project: RushConfigurationProject | undefined = lookup.findChildPath(file);
if (!project) {
continue;
}
let projectChanges: Map<string, IFileDiffStatus> | undefined = changesByProject.get(project);
if (!projectChanges) {
projectChanges = new Map();
changesByProject.set(project, projectChanges);
}
projectChanges.set(file, diffStatus);
}

return changesByProject;
return clusterFileInfo(options);
}

private async _getDataAsync(terminal: ITerminal): Promise<IRawRepoState> {
Expand Down
Loading