From f31ed73bdac1ef639331e232b1e7a8c8d7463391 Mon Sep 17 00:00:00 2001 From: Greg Pabian <35925521+grzpab@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:57:30 +0100 Subject: [PATCH] feat: INT-2196 read the dry-run while requested with direct-link --- src/components/messageBus.ts | 5 ++ src/data/readHomeDirectoryCases.ts | 88 +++++++++++++++++++++++++++++- src/extension.ts | 16 ++---- 3 files changed, 95 insertions(+), 14 deletions(-) diff --git a/src/components/messageBus.ts b/src/components/messageBus.ts index 50ebba12..9625a850 100644 --- a/src/components/messageBus.ts +++ b/src/components/messageBus.ts @@ -48,6 +48,7 @@ export const enum MessageKind { executionQueueChange = 39, loadHomeDirectoryData = 40, + loadHomeDirectoryCase = 41, } export type Command = @@ -171,6 +172,10 @@ export type Message = }> | Readonly<{ kind: MessageKind.loadHomeDirectoryData; + }> + | Readonly<{ + kind: MessageKind.loadHomeDirectoryCase; + caseHashDigest: CaseHash; }>; type EmitterMap = { diff --git a/src/data/readHomeDirectoryCases.ts b/src/data/readHomeDirectoryCases.ts index 3ba7b6f5..c48fa98c 100644 --- a/src/data/readHomeDirectoryCases.ts +++ b/src/data/readHomeDirectoryCases.ts @@ -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 { @@ -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; @@ -174,6 +175,39 @@ const readHomeDirectoryCase = async ( }); }; +export const readSingleHomeDirectoryCase = async ( + rootUri: Uri, + codemodEntities: Record, + 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, @@ -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) => { + 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'); } } diff --git a/src/extension.ts b/src/extension.ts index 8e2da11c..37628aaf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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