Skip to content

Commit

Permalink
[wip] persisting and reading dg spec for unloaded or out of current s…
Browse files Browse the repository at this point in the history
…olution projects
  • Loading branch information
jainaashish committed Nov 6, 2018
1 parent 4996f11 commit 7597d58
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,12 @@ public static bool VerifyPackagesOnDisk(RestoreRequest request)
/// </summary>
public static string GetHash(RestoreRequest request)
{
var dgSpec = request.DependencyGraphSpec;

if (request.Project.RestoreMetadata.ProjectStyle == ProjectStyle.DotnetCliTool || request.Project.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference)
{

var uniqueName = request.DependencyGraphSpec.Restore.First();
var dgSpec = request.DependencyGraphSpec.WithProjectClosure(uniqueName);
dgSpec = request.DependencyGraphSpec.WithProjectClosure(uniqueName);

foreach (var projectSpec in dgSpec.Projects){
// The project path where the tool is declared does not affect restore and is only used for logging and transparency.
Expand All @@ -222,15 +223,45 @@ public static string GetHash(RestoreRequest request)
projectSpec.RestoreSettings = null;
}
}
}

PersistHashedDGFileIfDebugging(dgSpec, request.Log);
PersistDGSpecFile(dgSpec, request, request.Log);
return dgSpec.GetHash();
}

private static void PersistDGSpecFile(DependencyGraphSpec spec, RestoreRequest request, ILogger log)
{
var dgPath = GetPersistedDGSpecFilePath(request);

if (dgPath == null)
{
return;
}

PersistHashedDGFileIfDebugging(dgSpec, request.Log);
return dgSpec.GetHash();
if (!Directory.Exists(dgPath))
{
DirectoryUtility.CreateSharedDirectory(dgPath);
}

PersistHashedDGFileIfDebugging(request.DependencyGraphSpec, request.Log);
return request.DependencyGraphSpec.GetHash();
log.LogMinimal($"Persisting no-op dg to {dgPath}");
spec.Save(dgPath);
}

private static string GetPersistedDGSpecFilePath(RestoreRequest request)
{
if (request.ProjectStyle == ProjectStyle.ProjectJson
|| request.ProjectStyle == ProjectStyle.PackageReference
|| request.ProjectStyle == ProjectStyle.Standalone)
{
var outputRoot = request.MSBuildProjectExtensionsPath ?? request.RestoreOutputPath;
var projFileName = Path.GetFileName(request.Project.RestoreMetadata.ProjectPath);
var dgFileName = string.Format(DependencyGraphSpec.DGSpecFileName, projFileName);
return Path.Combine(outputRoot, dgFileName);
}

return null;
}

/// <summary>
/// Write the dg file to a temp location if NUGET_PERSIST_NOOP_DG.
Expand All @@ -256,7 +287,7 @@ private static void PersistHashedDGFileIfDebugging(DependencyGraphSpec spec, ILo
DirectoryUtility.CreateSharedDirectory(Path.GetDirectoryName(path));
}

log.LogMinimal($"Persisting no-op dg to {path}");
log.LogMinimal($"Persisting no-op dg for debugging to {path}");

spec.Save(path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ public static async Task<DependencyGraphSpec> GetSolutionRestoreSpec(
{
var dgSpec = new DependencyGraphSpec();

var uniqueProjectDependencies = new HashSet<string>(PathUtility.GetStringComparerBasedOnOS());

var projects = (await solutionManager.GetNuGetProjectsAsync()).OfType<IDependencyGraphProject>();

foreach (var project in projects)
Expand All @@ -235,6 +237,36 @@ public static async Task<DependencyGraphSpec> GetSolutionRestoreSpec(
{
dgSpec.AddRestore(packageSpec.RestoreMetadata.ProjectUniqueName);
}

var projFileName = Path.GetFileName(packageSpec.RestoreMetadata.ProjectPath);
var dgFileName = string.Format(DependencyGraphSpec.DGSpecFileName, projFileName);
var persistedDGSpecPath = Path.Combine(packageSpec.RestoreMetadata.OutputPath, dgFileName);

if (File.Exists(persistedDGSpecPath))
{
DependencyGraphSpec persistedDGSpec = null;

var projectDependencies = packageSpec.RestoreMetadata.TargetFrameworks.SelectMany(target => target.ProjectReferences);

foreach (var projectReference in projectDependencies)
{
if (!projects.Any(p => PathUtility.GetStringComparerBasedOnOS().Equals(p.MSBuildProjectPath, projectReference.ProjectPath)) &&
uniqueProjectDependencies.Add(projectReference.ProjectUniqueName))
{
if (persistedDGSpec == null)
{
persistedDGSpec = DependencyGraphSpec.Load(persistedDGSpecPath);
}

var dependentPackageSpec = persistedDGSpec.GetProjectSpec(projectReference.ProjectUniqueName);

if (dependentPackageSpec != null)
{
dgSpec.AddProject(dependentPackageSpec);
}
}
}
}
}
}
// Return dg file
Expand Down
2 changes: 2 additions & 0 deletions src/NuGet.Core/NuGet.ProjectModel/DependencyGraphSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace NuGet.ProjectModel
{
public class DependencyGraphSpec
{
public static readonly string DGSpecFileName = "{0}.deps.json";

private readonly SortedSet<string> _restore = new SortedSet<string>(PathUtility.GetStringComparerBasedOnOS());
private readonly SortedDictionary<string, PackageSpec> _projects = new SortedDictionary<string, PackageSpec>(PathUtility.GetStringComparerBasedOnOS());

Expand Down

0 comments on commit 7597d58

Please sign in to comment.