Skip to content
Permalink
Browse files

[Assets] Simplified NuGet package restore (only happens during PreLoa…

…dPackageDependencies)
  • Loading branch information
xen2 committed Sep 21, 2018
1 parent 56b6774 commit 2958cf0b1f38489ca2c64885d797e74699d67de6
@@ -937,19 +937,6 @@ public void UpdateAssemblyReferences(ILogger log, PackageLoadParameters loadPara
LoadAssemblyReferencesForPackage(log, loadParameters);
}

/// <summary>
/// Restore NuGet packages of all projects in this package.
/// </summary>
/// <param name="log">The log.</param>
public void RestoreNugetPackages(ILogger log)
{
var projectFullPath = (Container as SolutionProject)?.FullPath;
if (projectFullPath != null)
{
VSProjectHelper.RestoreNugetPackagesNonRecursive(log, projectFullPath.ToWindowsPath()).Wait();
}
}

private static Asset LoadAsset(ILogger log, string packageName, string assetFullPath, string assetPath, byte[] assetContent, out bool assetDirty, out AttachedYamlAssetMetadata yamlMetadata)
{
var loadResult = assetContent != null
@@ -67,15 +67,6 @@ public PackageLoadParameters()
/// <value><c>true</c> if [automatic compile projects]; otherwise, <c>false</c>.</value>
public bool AutoCompileProjects { get; set; }

/// <summary>
/// Specifies if Nuget Restore should be run when loading a project.
/// Only valid when <see cref="AutoCompileProjects"/> is true.
/// </summary>
/// <remarks>
/// This is sometimes necessary when opening a project coming from another computer, since the lock file stores absolute paths.
/// </remarks>
public bool ForceNugetRestore { get; set; }

/// <summary>
/// Gets or sets the build configuration used to <see cref="AutoCompileProjects"/>.
/// </summary>
@@ -87,7 +87,7 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
if (package.State >= PackageState.DependenciesReady)
return;

log.Verbose("Restore NuGet packages...");
log.Verbose($"Process dependencies for {project.Name}...");

var restoreGraph = await GenerateRestoreGraph(log, project.Name, project.FullPath);

@@ -187,6 +187,7 @@ private async Task PreLoadPackageDependencies(ILogger log, SolutionProject proje
}

// Now that our references are upgraded, let's do a real nuget restore (download files)
log.Verbose($"Restore NuGet packages for {project.Name}...");
await VSProjectHelper.RestoreNugetPackages(log, project.FullPath);

project.FlattenedDependencies.Clear();
@@ -625,13 +625,6 @@ public PackageContainer AddExistingProject(UFile projectPath, ILogger logger, Pa

package = project.Package;

if (loadParameters.AutoCompileProjects && loadParameters.ForceNugetRestore)
{
// Note: solution needs to be saved right away so that we can restore nuget packages
Save(logger);
VSProjectHelper.RestoreNugetPackages(logger, SolutionPath).Wait();
}

// Load all missing references/dependencies
LoadMissingDependencies(logger, loadParameters);

@@ -810,21 +803,6 @@ public static void Load(string filePath, PackageSessionResult sessionResult, Pac
return;
}

if (loadParameters.AutoCompileProjects && loadParameters.ForceNugetRestore && PackageSessionHelper.IsPackageFile(filePath))
{
// Restore nuget packages
if (PackageSessionHelper.IsSolutionFile(filePath))
{
VSProjectHelper.RestoreNugetPackages(sessionResult, filePath).Wait();
}
else
{
// No .sln, run NuGet restore for each project
foreach (var package in session.Packages)
package.RestoreNugetPackages(sessionResult);
}
}

// Load all missing references/dependencies
session.LoadMissingReferences(sessionResult, loadParameters);

@@ -159,47 +159,6 @@ public static async Task<DependencyGraphSpec> GenerateRestoreGraphFile(ILogger l
}

public static async Task RestoreNugetPackages(ILogger logger, string projectPath)
{
if (Path.GetExtension(projectPath)?.ToLowerInvariant() == ".csproj")
{
var project = LoadProject(projectPath);

try
{
var addedProjs = new HashSet<string>(); //to avoid worst case circular dependencies.
var allProjs = Core.Utilities.IterateTree(project, project1 =>
{
var projs = new List<Project>();
foreach (var item in project1.AllEvaluatedItems.Where(x => x.ItemType == "ProjectReference"))
{
var path = Path.Combine(project.DirectoryPath, item.EvaluatedInclude);
if (!File.Exists(path)) continue;

if (addedProjs.Add(path))
{
projs.Add(project.ProjectCollection.LoadProject(path));
}
}

return projs;
});

await RestoreNugetPackagesNonRecursive(logger, allProjs.Select(x => x.FullPath));
}
finally
{
project.ProjectCollection.UnloadAllProjects();
project.ProjectCollection.Dispose();
}
}
else
{
// Solution or unknown project file
await RestoreNugetPackagesNonRecursive(logger, projectPath);
}
}

public static async Task RestoreNugetPackagesNonRecursive(ILogger logger, string projectPath)
{
await Task.Run(() =>
{
@@ -225,14 +184,6 @@ public static async Task RestoreNugetPackagesNonRecursive(ILogger logger, string
});
}

public static async Task RestoreNugetPackagesNonRecursive(ILogger logger, IEnumerable<string> projectPaths)
{
foreach (var projectPath in projectPaths)
{
await RestoreNugetPackagesNonRecursive(logger, projectPath);
}
}

public static Microsoft.Build.Evaluation.Project LoadProject(string fullProjectLocation, string configuration = "Debug", string platform = "AnyCPU", Dictionary<string, string> extraProperties = null)
{
configuration = configuration ?? "Debug";
@@ -244,7 +244,6 @@ protected override bool Generate(SessionTemplateGeneratorParameters parameters)

// Add package to session
var loadParams = PackageLoadParameters.Default();
loadParams.ForceNugetRestore = true;
loadParams.GenerateNewAssetIds = true;
loadParams.LoadMissingDependencies = false;
var session = parameters.Session;
@@ -44,9 +44,6 @@ public sealed override bool Run(SessionTemplateGeneratorParameters parameters)

SaveSession(parameters);

parameters.Logger.Verbose("Restore NuGet packages...");
VSProjectHelper.RestoreNugetPackages(parameters.Logger, parameters.Session.SolutionPath).Wait();

// Load missing references (we do this after saving)
// TODO: Better tracking of ProjectReferences (added, removed, etc...)
parameters.Logger.Verbose("Compiling game assemblies...");
@@ -438,7 +438,6 @@ private static PackageLoadParameters CreatePackageLoadParameters(WorkProgressVie
return new PackageLoadParameters
{
CancelToken = cancellationSource.Token,
ForceNugetRestore = true,
PackageUpgradeRequested = (package, pendingUpgrades) =>
{
// Generate message (in markdown, so we need to double line feeds)
@@ -454,21 +454,6 @@ public override bool UpgradeBeforeAssembliesLoaded(PackageLoadParameters loadPar
}
}

// Run NuGet restore
if (loadParameters.AutoCompileProjects)
{
if (session.SolutionPath != null)
{
// .sln needs to be up to date -> save everything
session.Save(new ForwardingLoggerResult(log));
VSProjectHelper.RestoreNugetPackages(log, session.SolutionPath).Wait();
}
else
{
dependentPackage.RestoreNugetPackages(log);
}
}

return true;
}
private bool IsYamlAsset(PackageLoadingAssetFile assetFile)

0 comments on commit 2958cf0

Please sign in to comment.
You can’t perform that action at this time.