aab9af71b8
This commit implements the `uninstall tool` command. The `uninstall tool` command is responsible for uninstalling global tools that are installed with the `install tool` command. This commit heavily refactors the ToolPackage and ShellShim namespaces to better support the operations required for the uninstall command. Several string resources have been updated to be more informative or to correct oddly structured sentences. This commit also fixes `--version` on the install command not supporting ranges and wildcards. Fixes #8549. Issue #8485 is partially fixed by this commit (`--prerelease` is not yet implemented).
104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.DotNet.Cli;
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
using Microsoft.DotNet.ShellShim;
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
|
|
{
|
|
internal class ShellShimRepositoryMock : IShellShimRepository
|
|
{
|
|
private static IFileSystem _fileSystem;
|
|
private readonly DirectoryPath _pathToPlaceShim;
|
|
|
|
public ShellShimRepositoryMock(DirectoryPath pathToPlaceShim, IFileSystem fileSystem = null)
|
|
{
|
|
_pathToPlaceShim = pathToPlaceShim;
|
|
_fileSystem = fileSystem ?? new FileSystemWrapper();
|
|
}
|
|
|
|
public void CreateShim(FilePath targetExecutablePath, string commandName)
|
|
{
|
|
if (ShimExists(commandName))
|
|
{
|
|
throw new ShellShimException(
|
|
string.Format(CommonLocalizableStrings.ShellShimConflict,
|
|
commandName));
|
|
}
|
|
|
|
TransactionalAction.Run(
|
|
action: () => {
|
|
var shim = new FakeShim
|
|
{
|
|
Runner = "dotnet",
|
|
ExecutablePath = targetExecutablePath.Value
|
|
};
|
|
|
|
_fileSystem.File.WriteAllText(
|
|
GetShimPath(commandName).Value,
|
|
JsonConvert.SerializeObject(shim));
|
|
},
|
|
rollback: () => {
|
|
_fileSystem.File.Delete(GetShimPath(commandName).Value);
|
|
});
|
|
}
|
|
|
|
public void RemoveShim(string commandName)
|
|
{
|
|
var originalShimPath = GetShimPath(commandName);
|
|
if (!_fileSystem.File.Exists(originalShimPath.Value))
|
|
{
|
|
return;
|
|
}
|
|
|
|
string tempShimPath = null;
|
|
TransactionalAction.Run(
|
|
action: () => {
|
|
var tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
|
_fileSystem.File.Move(originalShimPath.Value, tempFile);
|
|
tempShimPath = tempFile;
|
|
},
|
|
commit: () => {
|
|
if (tempShimPath != null)
|
|
{
|
|
_fileSystem.File.Delete(tempShimPath);
|
|
}
|
|
},
|
|
rollback: () => {
|
|
if (tempShimPath != null)
|
|
{
|
|
_fileSystem.File.Move(tempShimPath, originalShimPath.Value);
|
|
}
|
|
});
|
|
}
|
|
|
|
private bool ShimExists(string commandName)
|
|
{
|
|
return _fileSystem.File.Exists(GetShimPath(commandName).Value);
|
|
}
|
|
|
|
private FilePath GetShimPath(string shellCommandName)
|
|
{
|
|
var shimPath = Path.Combine(_pathToPlaceShim.Value, shellCommandName);
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
{
|
|
shimPath += ".exe";
|
|
}
|
|
|
|
return new FilePath(shimPath);
|
|
}
|
|
|
|
public class FakeShim
|
|
{
|
|
public string Runner { get; set; }
|
|
public string ExecutablePath { get; set; }
|
|
}
|
|
}
|
|
}
|