Skip to content

Commit f0a9a62

Browse files
Implement command to execute Rapicgen tool with Refitter settings file
1 parent 2de0045 commit f0a9a62

File tree

1 file changed

+102
-3
lines changed

1 file changed

+102
-3
lines changed

src/VSCode/src/extension.ts

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,73 @@ async function executeRapicgenTypeScript(generator: string, specificationFilePat
545545
}
546546
}
547547

548+
/**
549+
* Executes the Rapicgen tool with Refitter using a settings file
550+
* @param settingsFilePath The path to the .refitter settings file
551+
* @param context The extension context
552+
*/
553+
async function executeRapicgenRefitterSettings(settingsFilePath: string, context: vscode.ExtensionContext): Promise<void> {
554+
// Validate the settings file
555+
if (!validateSpecificationFile(settingsFilePath)) {
556+
return;
557+
}
558+
559+
// Check if .NET SDK is installed
560+
if (!isDotNetSdkInstalled()) {
561+
vscode.window.showErrorMessage(
562+
'.NET SDK not found. Please install .NET SDK 6.0 or higher to use this extension. Visit https://dotnet.microsoft.com/download/dotnet to download and install.'
563+
);
564+
return;
565+
}
566+
567+
// Ensure the Rapicgen tool is installed and up-to-date
568+
const rapicgenAvailable = await ensureRapicgenToolAvailable(context);
569+
if (!rapicgenAvailable) {
570+
return;
571+
}
572+
573+
const command = `rapicgen csharp refitter --settings-file "${settingsFilePath}"`;
574+
575+
try {
576+
// Show progress while generating
577+
await vscode.window.withProgress({
578+
location: vscode.ProgressLocation.Notification,
579+
title: 'Generating Refitter output from settings file...',
580+
cancellable: false
581+
}, async () => {
582+
try {
583+
// Run with higher timeout since code generation can take time
584+
const output = execSync(command, {
585+
encoding: 'utf-8',
586+
timeout: 120000 // 2 minute timeout
587+
});
588+
589+
// Log output for debugging
590+
console.log(`Rapicgen Refitter settings output: ${output}`);
591+
592+
vscode.window.showInformationMessage('Successfully generated Refitter output from settings file');
593+
} catch (error: unknown) {
594+
let errorMessage = 'Error generating Refitter output from settings file';
595+
596+
const err = error as { message?: string; stderr?: string };
597+
if (err.message) {
598+
errorMessage += `: ${err.message}`;
599+
}
600+
601+
if (err.stderr) {
602+
errorMessage += `\n${err.stderr}`;
603+
console.error('Command stderr:', err.stderr);
604+
}
605+
606+
vscode.window.showErrorMessage(errorMessage);
607+
console.error('Command execution error:', error);
608+
}
609+
});
610+
} catch (error) {
611+
console.error('Error during Refitter settings code generation process:', error);
612+
}
613+
}
614+
548615
/**
549616
* Validates that a specification file exists and is readable
550617
* @param specificationFilePath The path to the OpenAPI/Swagger specification file
@@ -641,10 +708,42 @@ export function activate(context: vscode.ExtensionContext) {
641708

642709
executeRapicgenTypeScript(generator.command, fileUri.fsPath, context);
643710
}
644-
);
645-
646-
context.subscriptions.push(disposable);
711+
); context.subscriptions.push(disposable);
647712
}
713+
714+
// Register command for Refitter settings
715+
const refitterSettingsDisposable = vscode.commands.registerCommand(
716+
'restApiClientCodeGenerator.refitterSettings',
717+
async (fileUri: vscode.Uri) => {
718+
if (!fileUri) {
719+
// If command was triggered from command palette, ask for file
720+
const files = await vscode.workspace.findFiles('**/*.refitter');
721+
if (files.length === 0) {
722+
vscode.window.showErrorMessage('No .refitter settings files found in the workspace');
723+
return;
724+
}
725+
726+
const fileItems = files.map(file => ({
727+
label: path.basename(file.fsPath),
728+
description: vscode.workspace.asRelativePath(file),
729+
path: file.fsPath
730+
}));
731+
732+
const selectedFile = await vscode.window.showQuickPick(fileItems, {
733+
placeHolder: 'Select a .refitter settings file'
734+
});
735+
736+
if (!selectedFile) {
737+
return;
738+
}
739+
fileUri = vscode.Uri.file(selectedFile.path);
740+
}
741+
742+
executeRapicgenRefitterSettings(fileUri.fsPath, context);
743+
}
744+
);
745+
746+
context.subscriptions.push(refitterSettingsDisposable);
648747
}
649748

650749
/**

0 commit comments

Comments
 (0)