dotnet-installer/src/Microsoft.DotNet.Configurer/NuGetCachePrimer.cs

142 lines
4.6 KiB
C#
Raw Normal View History

// 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.Collections.Generic;
using System.Linq;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.Extensions.EnvironmentAbstractions;
namespace Microsoft.DotNet.Configurer
{
public class NuGetCachePrimer : INuGetCachePrimer
{
private static IReadOnlyList<IReadOnlyList<string>> _templatesUsedToPrimeCache = new List<IReadOnlyList<string>>()
{
2017-02-15 20:38:25 -08:00
new List<string>() { "mvc", "-f", "netcoreapp1.0", "-au", "Individual", "--debug:ephemeral-hive", "--no-tools" },
new List<string>() { "mvc", "-f", "netcoreapp1.1", "-au", "Individual", "--debug:ephemeral-hive", "--no-tools" }
};
private readonly ICommandFactory _commandFactory;
private readonly IDirectory _directory;
private readonly IFile _file;
private readonly INuGetPackagesArchiver _nugetPackagesArchiver;
private readonly INuGetCacheSentinel _nuGetCacheSentinel;
public NuGetCachePrimer(
ICommandFactory commandFactory,
INuGetPackagesArchiver nugetPackagesArchiver,
INuGetCacheSentinel nuGetCacheSentinel)
: this(commandFactory,
nugetPackagesArchiver,
nuGetCacheSentinel,
FileSystemWrapper.Default.Directory,
FileSystemWrapper.Default.File)
{
}
internal NuGetCachePrimer(
ICommandFactory commandFactory,
INuGetPackagesArchiver nugetPackagesArchiver,
INuGetCacheSentinel nuGetCacheSentinel,
IDirectory directory,
IFile file)
{
_commandFactory = commandFactory;
_directory = directory;
_nugetPackagesArchiver = nugetPackagesArchiver;
_nuGetCacheSentinel = nuGetCacheSentinel;
_file = file;
}
public void PrimeCache()
{
if (SkipPrimingTheCache())
{
return;
}
var extractedPackagesArchiveDirectory = _nugetPackagesArchiver.ExtractArchive();
PrimeCacheUsingArchive(extractedPackagesArchiveDirectory);
}
private bool SkipPrimingTheCache()
{
return !_file.Exists(_nugetPackagesArchiver.NuGetPackagesArchive);
}
private void PrimeCacheUsingArchive(string extractedPackagesArchiveDirectory)
{
bool succeeded = true;
foreach (IReadOnlyList<string> templateInfo in _templatesUsedToPrimeCache)
{
if (succeeded)
{
using (var temporaryDotnetNewDirectory = _directory.CreateTemporaryDirectory())
{
var workingDirectory = temporaryDotnetNewDirectory.DirectoryPath;
succeeded &= CreateTemporaryProject(workingDirectory, templateInfo);
if (succeeded)
{
succeeded &= RestoreTemporaryProject(extractedPackagesArchiveDirectory, workingDirectory);
}
}
}
}
if (succeeded)
{
_nuGetCacheSentinel.CreateIfNotExists();
}
}
private bool CreateTemporaryProject(string workingDirectory, IReadOnlyList<string> templateInfo)
{
return RunCommand(
"new",
templateInfo,
workingDirectory);
}
private bool RestoreTemporaryProject(string extractedPackagesArchiveDirectory, string workingDirectory)
{
return RunCommand(
"restore",
new[] { "-s", extractedPackagesArchiveDirectory },
workingDirectory);
}
private bool RunCommand(string commandToExecute, IEnumerable<string> args, string workingDirectory)
{
var command = _commandFactory
.Create(commandToExecute, args)
.WorkingDirectory(workingDirectory)
.CaptureStdOut()
.CaptureStdErr();
var commandResult = command.Execute();
if (commandResult.ExitCode != 0)
{
Reporter.Verbose.WriteLine(commandResult.StdErr);
Reporter.Error.WriteLine(
2016-12-16 19:06:40 -08:00
string.Format(LocalizableStrings.FailedToPrimeCacheError, commandToExecute, commandResult.ExitCode));
}
return commandResult.ExitCode == 0;
}
}
}