|
3 | 3 | * Licensed under the MIT License. See License.txt in the project root for license information.
|
4 | 4 | *--------------------------------------------------------------------------------------------*/
|
5 | 5 |
|
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
6 | 8 | import { types } from 'util';
|
7 | 9 | import { ConfigurationTarget, vscode, WorkspaceConfiguration } from '../vscodeAdapter';
|
| 10 | +import { CSharpExtensionId } from '../constants/csharpExtensionId'; |
| 11 | +import { commonOptions } from './options'; |
| 12 | + |
| 13 | +export interface IDotnetAcquisitionExistingPaths { |
| 14 | + extensionId: string; |
| 15 | + path: string; |
| 16 | +} |
| 17 | + |
| 18 | +export const dotnetPathOption = 'dotnet.dotnetPath'; |
| 19 | +export const dotnetAcquisitionExtensionOption = 'dotnetAcquisitionExtension.existingDotnetPath'; |
8 | 20 |
|
9 | 21 | // Option in the array should be identical to each other, except the name.
|
10 | 22 | export const migrateOptions = [
|
@@ -111,6 +123,96 @@ export async function MigrateOptions(vscode: vscode): Promise<void> {
|
111 | 123 | await MoveOptionsValue(oldName, newName, configuration);
|
112 | 124 | }
|
113 | 125 | }
|
| 126 | + |
| 127 | + await migrateDotnetPathOption(vscode); |
| 128 | +} |
| 129 | + |
| 130 | +async function migrateDotnetPathOption(vscode: vscode): Promise<void> { |
| 131 | + const configuration = vscode.workspace.getConfiguration(); |
| 132 | + |
| 133 | + if (commonOptions.useOmnisharpServer) { |
| 134 | + // Migrate to O# specific option. |
| 135 | + await MoveOptionsValue(dotnetPathOption, 'omnisharp.dotnetPath', configuration); |
| 136 | + } else { |
| 137 | + const oldOptionInspect = configuration.inspect<string>(dotnetPathOption); |
| 138 | + if ( |
| 139 | + !oldOptionInspect || |
| 140 | + (!oldOptionInspect.globalValue && |
| 141 | + !oldOptionInspect.workspaceValue && |
| 142 | + !oldOptionInspect.workspaceFolderValue) |
| 143 | + ) { |
| 144 | + // No value is set, nothing to migrate. |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + const newOptionInspect = configuration.inspect<IDotnetAcquisitionExistingPaths[]>( |
| 149 | + dotnetAcquisitionExtensionOption |
| 150 | + ); |
| 151 | + |
| 152 | + if (oldOptionInspect.globalValue) { |
| 153 | + await migrateSingleDotnetPathValue( |
| 154 | + dotnetPathOption, |
| 155 | + oldOptionInspect.globalValue, |
| 156 | + dotnetAcquisitionExtensionOption, |
| 157 | + newOptionInspect?.globalValue, |
| 158 | + configuration, |
| 159 | + ConfigurationTarget.Global |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + if (oldOptionInspect.workspaceValue) { |
| 164 | + await migrateSingleDotnetPathValue( |
| 165 | + dotnetPathOption, |
| 166 | + oldOptionInspect.workspaceValue, |
| 167 | + dotnetAcquisitionExtensionOption, |
| 168 | + newOptionInspect?.workspaceValue, |
| 169 | + configuration, |
| 170 | + ConfigurationTarget.Workspace |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + if (oldOptionInspect.workspaceFolderValue) { |
| 175 | + await migrateSingleDotnetPathValue( |
| 176 | + dotnetPathOption, |
| 177 | + oldOptionInspect.workspaceFolderValue, |
| 178 | + dotnetAcquisitionExtensionOption, |
| 179 | + newOptionInspect?.workspaceFolderValue, |
| 180 | + configuration, |
| 181 | + ConfigurationTarget.WorkspaceFolder |
| 182 | + ); |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +async function migrateSingleDotnetPathValue( |
| 188 | + oldOptionName: string, |
| 189 | + oldValue: string, |
| 190 | + newOptionName: string, |
| 191 | + currentNewValue: IDotnetAcquisitionExistingPaths[] | undefined, |
| 192 | + configuration: WorkspaceConfiguration, |
| 193 | + configurationTarget: ConfigurationTarget |
| 194 | +): Promise<void> { |
| 195 | + // Migrate to .NET install tool specific option. |
| 196 | + // This requires some adjustments as the .NET install tool expects the full path to the exe and can already have a value set (e.g. a diff extension). |
| 197 | + const extension = process.platform === 'win32' ? '.exe' : ''; |
| 198 | + const newValue = path.join(oldValue, `dotnet${extension}`); |
| 199 | + |
| 200 | + if (!fs.existsSync(newValue)) { |
| 201 | + // If the existing option points to a location that doesn't exist, we'll just remove the old value. |
| 202 | + configuration.update(oldOptionName, undefined, configurationTarget); |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + currentNewValue = currentNewValue ?? []; |
| 207 | + if (currentNewValue && currentNewValue.filter((p) => p.extensionId === CSharpExtensionId).length !== 0) { |
| 208 | + // There's already a dotnet path set for this extension, we don't want to overwrite it. Just delete the old one. |
| 209 | + await configuration.update(oldOptionName, undefined, configurationTarget); |
| 210 | + return; |
| 211 | + } |
| 212 | + |
| 213 | + currentNewValue.push({ extensionId: CSharpExtensionId, path: newValue }); |
| 214 | + await configuration.update(newOptionName, currentNewValue, configurationTarget); |
| 215 | + await configuration.update(oldOptionName, undefined, configurationTarget); |
114 | 216 | }
|
115 | 217 |
|
116 | 218 | function shouldMove(newOptionValue: unknown, defaultInspectValueOfNewOption: unknown): boolean {
|
|
0 commit comments