Skip to content

Commit 395f999

Browse files
Extract ensureRapicgenToolAvailable function for installation and update checks
1 parent 2330a6c commit 395f999

File tree

1 file changed

+51
-64
lines changed

1 file changed

+51
-64
lines changed

src/VSCode/src/extension.ts

Lines changed: 51 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,49 @@ async function installRapicgen(context: vscode.ExtensionContext): Promise<boolea
191191
}
192192
}
193193

194+
/**
195+
* Ensures that the Rapicgen tool is installed and up-to-date
196+
* @param context The extension context
197+
* @returns true if the tool is available and ready to use, false otherwise
198+
*/
199+
async function ensureRapicgenToolAvailable(context: vscode.ExtensionContext): Promise<boolean> {
200+
// Check if the Rapicgen tool is installed
201+
if (!isRapicgenInstalled()) {
202+
const shouldInstall = await vscode.window.showInformationMessage(
203+
'The Rapicgen .NET tool is not installed. Would you like to install it?',
204+
'Yes', 'No'
205+
);
206+
207+
if (shouldInstall === 'Yes') {
208+
const installSuccess = await installRapicgen(context);
209+
if (!installSuccess) {
210+
vscode.window.showErrorMessage('Failed to install the Rapicgen .NET tool. Please install it manually with "dotnet tool install --global rapicgen".');
211+
return false;
212+
}
213+
return true;
214+
} else {
215+
return false;
216+
}
217+
} else {
218+
// Check if update is needed
219+
const versionStatus = checkRapicgenVersionStatus(context);
220+
if (versionStatus.needsUpdate) {
221+
const shouldUpdate = await vscode.window.showInformationMessage(
222+
`A newer version of the Rapicgen tool is available (current: v${versionStatus.currentVersion}, available: v${versionStatus.targetVersion}). Would you like to update?`,
223+
'Yes', 'No'
224+
);
225+
226+
if (shouldUpdate === 'Yes') {
227+
const updateSuccess = await installRapicgen(context);
228+
if (!updateSuccess) {
229+
vscode.window.showWarningMessage(`Failed to update the Rapicgen tool. Continuing with existing version ${versionStatus.currentVersion}.`);
230+
}
231+
}
232+
}
233+
return true;
234+
}
235+
}
236+
194237
/**
195238
* Gets the namespace to use for generated code
196239
* @returns The namespace from configuration or a default value
@@ -295,38 +338,10 @@ async function executeRapicgen(generator: string, specificationFilePath: string,
295338
return;
296339
}
297340

298-
// Check if the Rapicgen tool is installed
299-
if (!isRapicgenInstalled()) {
300-
const shouldInstall = await vscode.window.showInformationMessage(
301-
'The Rapicgen .NET tool is not installed. Would you like to install it?',
302-
'Yes', 'No'
303-
);
304-
305-
if (shouldInstall === 'Yes') {
306-
const installSuccess = await installRapicgen(context);
307-
if (!installSuccess) {
308-
vscode.window.showErrorMessage('Failed to install the Rapicgen .NET tool. Please install it manually with "dotnet tool install --global rapicgen".');
309-
return;
310-
}
311-
} else {
312-
return;
313-
}
314-
} else {
315-
// Check if update is needed
316-
const versionStatus = checkRapicgenVersionStatus(context);
317-
if (versionStatus.needsUpdate) {
318-
const shouldUpdate = await vscode.window.showInformationMessage(
319-
`A newer version of the Rapicgen tool is available (current: v${versionStatus.currentVersion}, available: v${versionStatus.targetVersion}). Would you like to update?`,
320-
'Yes', 'No'
321-
);
322-
323-
if (shouldUpdate === 'Yes') {
324-
const updateSuccess = await installRapicgen(context);
325-
if (!updateSuccess) {
326-
vscode.window.showWarningMessage(`Failed to update the Rapicgen tool. Continuing with existing version ${versionStatus.currentVersion}.`);
327-
}
328-
}
329-
}
341+
// Ensure the Rapicgen tool is installed and up-to-date
342+
const rapicgenAvailable = await ensureRapicgenToolAvailable(context);
343+
if (!rapicgenAvailable) {
344+
return;
330345
}
331346

332347
const namespace = getNamespace();
@@ -415,38 +430,10 @@ async function executeRapicgenTypeScript(generator: string, specificationFilePat
415430
return;
416431
}
417432

418-
// Check if the Rapicgen tool is installed
419-
if (!isRapicgenInstalled()) {
420-
const shouldInstall = await vscode.window.showInformationMessage(
421-
'The Rapicgen .NET tool is not installed. Would you like to install it?',
422-
'Yes', 'No'
423-
);
424-
425-
if (shouldInstall === 'Yes') {
426-
const installSuccess = await installRapicgen(context);
427-
if (!installSuccess) {
428-
vscode.window.showErrorMessage('Failed to install the Rapicgen .NET tool. Please install it manually with "dotnet tool install --global rapicgen".');
429-
return;
430-
}
431-
} else {
432-
return;
433-
}
434-
} else {
435-
// Check if update is needed
436-
const versionStatus = checkRapicgenVersionStatus(context);
437-
if (versionStatus.needsUpdate) {
438-
const shouldUpdate = await vscode.window.showInformationMessage(
439-
`A newer version of the Rapicgen tool is available (current: v${versionStatus.currentVersion}, available: v${versionStatus.targetVersion}). Would you like to update?`,
440-
'Yes', 'No'
441-
);
442-
443-
if (shouldUpdate === 'Yes') {
444-
const updateSuccess = await installRapicgen(context);
445-
if (!updateSuccess) {
446-
vscode.window.showWarningMessage(`Failed to update the Rapicgen tool. Continuing with existing version ${versionStatus.currentVersion}.`);
447-
}
448-
}
449-
}
433+
// Ensure the Rapicgen tool is installed and up-to-date
434+
const rapicgenAvailable = await ensureRapicgenToolAvailable(context);
435+
if (!rapicgenAvailable) {
436+
return;
450437
}
451438

452439
// For TypeScript, we get an output directory rather than a single file

0 commit comments

Comments
 (0)