|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { RoslynLanguageServer } from './roslynLanguageServer'; |
| 8 | +import { RestorableProjects, RestoreParams, RestorePartialResult, RestoreRequest } from './roslynProtocol'; |
| 9 | +import path = require('path'); |
| 10 | + |
| 11 | +let _restoreInProgress = false; |
| 12 | + |
| 13 | +export function registerRestoreCommands( |
| 14 | + context: vscode.ExtensionContext, |
| 15 | + languageServer: RoslynLanguageServer, |
| 16 | + restoreChannel: vscode.OutputChannel |
| 17 | +) { |
| 18 | + context.subscriptions.push( |
| 19 | + vscode.commands.registerCommand('dotnet.restore.project', async (_request): Promise<void> => { |
| 20 | + return chooseProjectAndRestore(languageServer, restoreChannel); |
| 21 | + }) |
| 22 | + ); |
| 23 | + context.subscriptions.push( |
| 24 | + vscode.commands.registerCommand('dotnet.restore.all', async (): Promise<void> => { |
| 25 | + return restore(languageServer, restoreChannel); |
| 26 | + }) |
| 27 | + ); |
| 28 | +} |
| 29 | +async function chooseProjectAndRestore( |
| 30 | + languageServer: RoslynLanguageServer, |
| 31 | + restoreChannel: vscode.OutputChannel |
| 32 | +): Promise<void> { |
| 33 | + const projects = await languageServer.sendRequest0( |
| 34 | + RestorableProjects.type, |
| 35 | + new vscode.CancellationTokenSource().token |
| 36 | + ); |
| 37 | + |
| 38 | + const items = projects.map((p) => { |
| 39 | + const projectName = path.basename(p); |
| 40 | + const item: vscode.QuickPickItem = { |
| 41 | + label: vscode.l10n.t(`Restore {0}`, projectName), |
| 42 | + description: p, |
| 43 | + }; |
| 44 | + return item; |
| 45 | + }); |
| 46 | + |
| 47 | + const pickedItem = await vscode.window.showQuickPick(items); |
| 48 | + if (!pickedItem) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + await restore(languageServer, restoreChannel, pickedItem.description); |
| 53 | +} |
| 54 | + |
| 55 | +async function restore( |
| 56 | + languageServer: RoslynLanguageServer, |
| 57 | + restoreChannel: vscode.OutputChannel, |
| 58 | + projectFile?: string |
| 59 | +): Promise<void> { |
| 60 | + if (_restoreInProgress) { |
| 61 | + vscode.window.showErrorMessage(vscode.l10n.t('Restore already in progress')); |
| 62 | + return; |
| 63 | + } |
| 64 | + _restoreInProgress = true; |
| 65 | + restoreChannel.show(true); |
| 66 | + |
| 67 | + const request: RestoreParams = { projectFilePath: projectFile }; |
| 68 | + await vscode.window |
| 69 | + .withProgress( |
| 70 | + { |
| 71 | + location: vscode.ProgressLocation.Notification, |
| 72 | + title: vscode.l10n.t('Restore'), |
| 73 | + cancellable: true, |
| 74 | + }, |
| 75 | + async (progress, token) => { |
| 76 | + const writeOutput = (output: RestorePartialResult) => { |
| 77 | + if (output.message) { |
| 78 | + restoreChannel.appendLine(output.message); |
| 79 | + } |
| 80 | + |
| 81 | + progress.report({ message: output.stage }); |
| 82 | + }; |
| 83 | + |
| 84 | + progress.report({ message: vscode.l10n.t('Sending request') }); |
| 85 | + const responsePromise = languageServer.sendRequestWithProgress( |
| 86 | + RestoreRequest.type, |
| 87 | + request, |
| 88 | + async (p) => writeOutput(p), |
| 89 | + token |
| 90 | + ); |
| 91 | + |
| 92 | + await responsePromise.then( |
| 93 | + (result) => result.forEach((r) => writeOutput(r)), |
| 94 | + (err) => restoreChannel.appendLine(err) |
| 95 | + ); |
| 96 | + } |
| 97 | + ) |
| 98 | + .then( |
| 99 | + () => { |
| 100 | + _restoreInProgress = false; |
| 101 | + }, |
| 102 | + () => { |
| 103 | + _restoreInProgress = false; |
| 104 | + } |
| 105 | + ); |
| 106 | + |
| 107 | + return; |
| 108 | +} |
0 commit comments