Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lib placeholder files for Visual Studio restores #1251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -330,34 +330,53 @@ public static LockFileTargetLibrary CreateLockFileTargetProject(
// Add files under asset groups
object filesObject;
object msbuildPath;
if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.ProjectRestoreMetadataFiles, out filesObject)
&& localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.MSBuildProjectPath, out msbuildPath))
if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.MSBuildProjectPath, out msbuildPath))
{
var files = (List<ProjectRestoreMetadataFile>)filesObject;
var files = new List<ProjectRestoreMetadataFile>();
var fileLookup = new Dictionary<string, ProjectRestoreMetadataFile>(StringComparer.OrdinalIgnoreCase);

foreach (var file in files)
// Find the project path, this is provided by the resolver
var msbuildFilePathInfo = new FileInfo((string)msbuildPath);

// Ensure a trailing slash for the relative path helper.
var projectDir = msbuildFilePathInfo.Directory.FullName
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a helper method in PathUtility for this..perhaps use that instead for consistency?

.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

// Read files from the project if they were provided.
if (localMatch.LocalLibrary.Items.TryGetValue(KnownLibraryProperties.ProjectRestoreMetadataFiles, out filesObject))
{
files.AddRange((List<ProjectRestoreMetadataFile>)filesObject);
}

var targetFrameworkShortName = targetGraph.Framework.GetShortFolderName();
var libAnyPath = $"lib/{targetFrameworkShortName}/any.dll";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it standard to use forward slashes everywhere in the assets file?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, they must be forward slashes


if (files.Count == 0)
{
var path = file.PackagePath;
// If the project did not provide a list of assets, add in default ones.
// These are used to detect transitive vs non-transitive project references.
var absolutePath = Path.Combine(projectDir, "bin", "placeholder", $"{localMatch.Library.Name}.dll");

files.Add(new ProjectRestoreMetadataFile(libAnyPath, absolutePath));
}

// Process and de-dupe files
for (var i = 0; i < files.Count; i++)
{
var path = files[i].PackagePath;

// LIBANY avoid compatibility checks and will always be used.
if (LIBANY.Equals(path, StringComparison.Ordinal))
{
path = $"lib/{targetGraph.Framework.GetShortFolderName()}/any.dll";
path = libAnyPath;
}

if (!fileLookup.ContainsKey(path))
{
fileLookup.Add(path, file);
fileLookup.Add(path, files[i]);
}
}

var msbuildFilePathInfo = new FileInfo((string)msbuildPath);

// Ensure a trailing slash for the relative path helper.
var projectDir = msbuildFilePathInfo.Directory.FullName
.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;

var contentItems = new ContentItemCollection();
contentItems.Load(fileLookup.Keys);

Expand Down Expand Up @@ -483,7 +502,7 @@ private static IEnumerable<LockFileItem> GetBuildItemsForPackageId(
yield return props;
}

var targets = ordered.FirstOrDefault(c =>
var targets = ordered.FirstOrDefault(c =>
$"{packageId}.targets".Equals(
Path.GetFileName(c.Path),
StringComparison.OrdinalIgnoreCase));
Expand Down Expand Up @@ -572,7 +591,7 @@ private static LockFileItem ToResourceLockFileItem(ContentItem item)
/// Clears a lock file group and replaces the first item with _._ if
/// the group has items. Empty groups are left alone.
/// </summary>
private static void ClearIfExists<T>(IList<T> group) where T: LockFileItem
private static void ClearIfExists<T>(IList<T> group) where T : LockFileItem
{
if (GroupHasNonEmptyItems(group))
{
Expand All @@ -592,7 +611,7 @@ private static void ClearIfExists<T>(IList<T> group) where T: LockFileItem
group.Clear();

// Create a new item with the _._ path
var emptyItem = (T)Activator.CreateInstance(typeof(T), new [] { emptyDir });
var emptyItem = (T)Activator.CreateInstance(typeof(T), new[] { emptyDir });

// Copy over the properties from the first
foreach (var pair in firstItem.Properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,50 +236,12 @@ public static PackageSpec GetPackageSpec(IEnumerable<IMSBuildItem> items)
{
projectDir = Path.GetDirectoryName(result.RestoreMetadata.ProjectPath);
}

AddPlaceHolderFiles(result, projectDir);
}
}

return result;
}

/// <summary>
/// Add placeholder files, these will be used under the compile and runtime sections.
/// </summary>
private static void AddPlaceHolderFiles(PackageSpec spec, string projectDir)
{
var files = new List<ProjectRestoreMetadataFile>();

// Create fake placeholder file names
var assemblyName = $"{spec.RestoreMetadata.ProjectName}.dll";
var absoluteRoot = Path.Combine(projectDir, "bin", "placeholder");

if (spec.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference
|| spec.RestoreMetadata.ProjectStyle == ProjectStyle.ProjectJson)
{
// Add paths based on actual frameworks if they exist
foreach (var framework in spec.TargetFrameworks
.Select(tfi => tfi.FrameworkName)
.Where(f => f.IsSpecificFramework))
{
var packagePath = $"lib/{framework.GetShortFolderName()}/{assemblyName}";
var absolutePath = Path.Combine(absoluteRoot, framework.GetShortFolderName(), assemblyName);

files.Add(new ProjectRestoreMetadataFile(packagePath, absolutePath));
}
}

// If no TFMs exist use LIBANY which to guarantee something is added
if (files.Count == 0)
{
var absolutePath = Path.Combine(absoluteRoot, "any", assemblyName);
files.Add(new ProjectRestoreMetadataFile(LockFileUtils.LIBANY, absolutePath));
}

spec.RestoreMetadata.Files = files;
}

private static void AddPackageTargetFallbacks(PackageSpec spec, IEnumerable<IMSBuildItem> items)
{
foreach (var item in GetItemByType(items, "TargetFrameworkInformation"))
Expand Down