2018-01-28 21:35:04 +00:00
|
|
|
// 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.Linq;
|
|
|
|
using Microsoft.DotNet.Cli;
|
|
|
|
using Microsoft.DotNet.ToolPackage;
|
|
|
|
using Microsoft.Extensions.EnvironmentAbstractions;
|
2018-02-23 03:13:36 +00:00
|
|
|
using NuGet.Versioning;
|
2018-01-28 21:35:04 +00:00
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Tools.Tests.ComponentMocks
|
|
|
|
{
|
|
|
|
internal class ToolPackageMock : IToolPackage
|
|
|
|
{
|
|
|
|
private IFileSystem _fileSystem;
|
|
|
|
private Lazy<IReadOnlyList<CommandSettings>> _commands;
|
|
|
|
private Action _uninstallCallback;
|
2018-03-26 19:50:09 +00:00
|
|
|
private IEnumerable<string> _warnings;
|
2018-01-28 21:35:04 +00:00
|
|
|
|
|
|
|
public ToolPackageMock(
|
|
|
|
IFileSystem fileSystem,
|
2018-02-23 03:13:36 +00:00
|
|
|
PackageId id,
|
|
|
|
NuGetVersion version,
|
2018-01-28 21:35:04 +00:00
|
|
|
DirectoryPath packageDirectory,
|
2018-03-26 19:50:09 +00:00
|
|
|
Action uninstallCallback = null,
|
|
|
|
IEnumerable<string> warnings = null)
|
2018-01-28 21:35:04 +00:00
|
|
|
{
|
|
|
|
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
|
2018-02-23 03:13:36 +00:00
|
|
|
Id = id;
|
|
|
|
Version = version ?? throw new ArgumentNullException(nameof(version));
|
2018-01-28 21:35:04 +00:00
|
|
|
PackageDirectory = packageDirectory;
|
|
|
|
_commands = new Lazy<IReadOnlyList<CommandSettings>>(GetCommands);
|
|
|
|
_uninstallCallback = uninstallCallback;
|
2018-03-26 19:50:09 +00:00
|
|
|
_warnings = warnings ?? new List<string>();
|
2018-01-28 21:35:04 +00:00
|
|
|
}
|
|
|
|
|
2018-02-23 03:13:36 +00:00
|
|
|
public PackageId Id { get; private set; }
|
2018-01-28 21:35:04 +00:00
|
|
|
|
2018-02-23 03:13:36 +00:00
|
|
|
public NuGetVersion Version { get; private set; }
|
2018-01-28 21:35:04 +00:00
|
|
|
|
|
|
|
public DirectoryPath PackageDirectory { get; private set; }
|
|
|
|
|
|
|
|
public IReadOnlyList<CommandSettings> Commands
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _commands.Value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-26 19:50:09 +00:00
|
|
|
public IEnumerable<string> Warnings => _warnings;
|
|
|
|
|
2018-01-28 21:35:04 +00:00
|
|
|
public void Uninstall()
|
|
|
|
{
|
|
|
|
var rootDirectory = PackageDirectory.GetParentPath();
|
|
|
|
string tempPackageDirectory = null;
|
|
|
|
|
|
|
|
TransactionalAction.Run(
|
|
|
|
action: () => {
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_fileSystem.Directory.Exists(PackageDirectory.Value))
|
|
|
|
{
|
|
|
|
var tempPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
|
|
|
|
_fileSystem.Directory.Move(PackageDirectory.Value, tempPath);
|
|
|
|
tempPackageDirectory = tempPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fileSystem.Directory.Exists(rootDirectory.Value) &&
|
|
|
|
!_fileSystem.Directory.EnumerateFileSystemEntries(rootDirectory.Value).Any())
|
|
|
|
{
|
|
|
|
_fileSystem.Directory.Delete(rootDirectory.Value, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_uninstallCallback != null)
|
|
|
|
{
|
|
|
|
_uninstallCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex) when (ex is UnauthorizedAccessException || ex is IOException)
|
|
|
|
{
|
|
|
|
throw new ToolPackageException(
|
|
|
|
string.Format(
|
|
|
|
CommonLocalizableStrings.FailedToUninstallToolPackage,
|
2018-02-23 03:13:36 +00:00
|
|
|
Id,
|
2018-01-28 21:35:04 +00:00
|
|
|
ex.Message),
|
|
|
|
ex);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
commit: () => {
|
|
|
|
if (tempPackageDirectory != null)
|
|
|
|
{
|
|
|
|
_fileSystem.Directory.Delete(tempPackageDirectory, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
rollback: () => {
|
|
|
|
if (tempPackageDirectory != null)
|
|
|
|
{
|
|
|
|
_fileSystem.Directory.CreateDirectory(rootDirectory.Value);
|
|
|
|
_fileSystem.Directory.Move(tempPackageDirectory, PackageDirectory.Value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private IReadOnlyList<CommandSettings> GetCommands()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// The mock restorer wrote the path to the executable into project.assets.json (not a real assets file)
|
|
|
|
// Currently only "dotnet" commands are supported
|
|
|
|
var executablePath = _fileSystem.File.ReadAllText(Path.Combine(PackageDirectory.Value, "project.assets.json"));
|
|
|
|
return new CommandSettings[]
|
|
|
|
{
|
|
|
|
new CommandSettings(
|
|
|
|
ProjectRestorerMock.FakeCommandName,
|
|
|
|
"dotnet",
|
|
|
|
PackageDirectory.WithFile(executablePath))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
|
|
|
throw new ToolPackageException(
|
|
|
|
string.Format(
|
|
|
|
CommonLocalizableStrings.FailedToRetrieveToolConfiguration,
|
2018-02-23 03:13:36 +00:00
|
|
|
Id,
|
2018-01-28 21:35:04 +00:00
|
|
|
ex.Message),
|
|
|
|
ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|