Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
feat: INT-2196 read the dry-run while requested with direct-link
Browse files Browse the repository at this point in the history
  • Loading branch information
grzpab committed Dec 1, 2023
1 parent 3908240 commit f31ed73
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/components/messageBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const enum MessageKind {
executionQueueChange = 39,

loadHomeDirectoryData = 40,
loadHomeDirectoryCase = 41,
}

export type Command =
Expand Down Expand Up @@ -171,6 +172,10 @@ export type Message =
}>
| Readonly<{
kind: MessageKind.loadHomeDirectoryData;
}>
| Readonly<{
kind: MessageKind.loadHomeDirectoryCase;
caseHashDigest: CaseHash;
}>;

type EmitterMap<K extends MessageKind> = {
Expand Down
88 changes: 86 additions & 2 deletions src/data/readHomeDirectoryCases.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createReadStream } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import { FileType, Uri, workspace } from 'vscode';
import { FileType, Uri, window, workspace } from 'vscode';
import { readSurfaceAgnosticCase } from './readSurfaceAgnosticCase';
import { Case, caseHashCodec } from '../cases/types';
import { Case, CaseHash, caseHashCodec } from '../cases/types';
import { Job, JobKind, jobHashCodec } from '../jobs/types';
import { parseSurfaceAgnosticCase } from './schemata/surfaceAgnosticCaseSchema';
import {
Expand All @@ -14,6 +14,7 @@ import { CodemodEntry } from '../codemods/types';
import EventEmitter from 'events';
import { MessageBus, MessageKind } from '../components/messageBus';
import { Store } from '.';
import { actions } from './slice';

interface HomeDirectoryEventEmitter extends EventEmitter {
emit(event: 'start'): boolean;
Expand Down Expand Up @@ -174,6 +175,39 @@ const readHomeDirectoryCase = async (
});
};

export const readSingleHomeDirectoryCase = async (
rootUri: Uri,
codemodEntities: Record<string, CodemodEntry | undefined>,
caseHashDigest: CaseHash,
) => {
const path = join(
homedir(),
'.intuita',
'cases',
caseHashDigest,
'case.data',
);

const eventEmitter: HomeDirectoryEventEmitter = new EventEmitter();

eventEmitter.once('start', async () => {
try {
await readHomeDirectoryCase(
eventEmitter,
rootUri,
codemodEntities,
path,
);
} catch (error) {
console.error(error);
}

eventEmitter.emit('end');
});

return eventEmitter;
};

export const readHomeDirectoryCases = async (
rootUri: Uri,
codemodEntities: Record<string, CodemodEntry | undefined>,
Expand Down Expand Up @@ -248,5 +282,55 @@ export class HomeDirectoryService {

eventEmitter?.emit('start');
});

__messageBus.subscribe(
MessageKind.loadHomeDirectoryCase,
async ({ caseHashDigest }) => {
await this.__handleLoadHomeDirectoryCase(caseHashDigest);
},
);
}

private async __handleLoadHomeDirectoryCase(caseHashDigest: CaseHash) {
if (!this.__rootUri) {
return;
}

const eventEmitter = await readSingleHomeDirectoryCase(
this.__rootUri,
this.__store.getState().codemod.entities,
caseHashDigest,
);

let caseExists = false;

const jobHandler = (kase: Case, jobs: ReadonlyArray<Job>) => {
this.__messageBus.publish({
kind: MessageKind.upsertCase,
kase,
jobs,
});

if (!caseExists) {
caseExists = true;

this.__store.dispatch(actions.setActiveTabId('codemodRuns'));
this.__store.dispatch(
actions.setSelectedCaseHash(caseHashDigest),
);
}
};

eventEmitter?.once('end', () => {
if (!caseExists) {
window.showErrorMessage('The requested dry-run does not exist');
}

eventEmitter.off('job', jobHandler);
});

eventEmitter?.on('job', jobHandler);

eventEmitter?.emit('start');
}
}
16 changes: 4 additions & 12 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1014,19 +1014,11 @@ export async function activate(context: vscode.ExtensionContext) {
prettyReporter.report(validation).join('\n'),
);
}
const hash = validation.right;

// if there is no such hash in runs
if (!state.case.ids.includes(hash)) {
vscode.window.showErrorMessage(
'Requested hash does not exist',
);
return;
}

// otherwise open tab and set selected run
store.dispatch(actions.setActiveTabId('codemodRuns'));
store.dispatch(actions.setSelectedCaseHash(hash));
messageBus.publish({
kind: MessageKind.loadHomeDirectoryCase,
caseHashDigest: validation.right,
});
}

// user is exporting codemod from studio into extension
Expand Down

0 comments on commit f31ed73

Please sign in to comment.