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

Automatically set Link metadata on items outside the project folder #1246

Merged
merged 4 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions TestAssets/TestProjects/LinkTest/LinkTest/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace LinkTest
{
public class Class1
{
}
}
63 changes: 63 additions & 0 deletions TestAssets/TestProjects/LinkTest/LinkTest/LinkTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup Condition="'$(IncludeWithGlob)' == 'true' And '$(UseLinkBase)' == 'true'">
<Compile Include="..\Linked\**\*.cs" LinkBase="Linked\Files" />
<AdditionalFiles Include="..\Linked\**\*.Additional.txt" LinkBase="Linked\Files" />
<None Include="..\Linked\**\*.None.txt" LinkBase="Linked\Files" />
<Content Include="..\Linked\**\*.Content.txt" LinkBase="Linked\Files" />
<EmbeddedResource Include="..\Linked\**\*.Embedded.txt" LinkBase="Linked\Files" />
<CustomItem Include="..\Linked\**\*.Custom.txt" LinkBase="Linked\Files" />
</ItemGroup>

<ItemGroup Condition="'$(IncludeWithGlob)' == 'true' And '$(UseLinkBase)' != 'true'">
<Compile Include="..\Linked\**\*.cs" />
<AdditionalFiles Include="..\Linked\**\*.Additional.txt" />
<None Include="..\Linked\**\*.None.txt" />
<Content Include="..\Linked\**\*.Content.txt" />
<EmbeddedResource Include="..\Linked\**\*.Embedded.txt" />
<CustomItem Include="..\Linked\**\*.Custom.txt" />
</ItemGroup>

<ItemGroup Condition="'$(IncludeWithGlob)' != 'true' And '$(UseLinkBase)' == 'true'">
<Compile Include="..\Linked\Linked.Class.cs" LinkBase="Linked\Files"/>
<AdditionalFiles Include="..\Linked\Linked.Additional.txt" LinkBase="Linked\Files"/>
<None Include="..\Linked\Linked.None.txt" LinkBase="Linked\Files"/>
<Content Include="..\Linked\Linked.Content.txt" LinkBase="Linked\Files"/>
<EmbeddedResource Include="..\Linked\Linked.Embedded.txt" LinkBase="Linked\Files"/>
<CustomItem Include="..\Linked\*Linked.Custom.txt" LinkBase="Linked\Files"/>
</ItemGroup>

<ItemGroup Condition="'$(IncludeWithGlob)' != 'true' And '$(UseLinkBase)' != 'true'">
<Compile Include="..\Linked\Linked.Class.cs"/>
<AdditionalFiles Include="..\Linked\Linked.Additional.txt" />
<None Include="..\Linked\Linked.None.txt" />
<Content Include="..\Linked\Linked.Content.txt" />
<EmbeddedResource Include="..\Linked\Linked.Embedded.txt" />
<CustomItem Include="..\Linked\*Linked.Custom.txt" />
</ItemGroup>


<Target Name="WriteItems" DependsOnTargets="Compile">
<ItemGroup>
<LinesToWrite Include="Compile%09%(Compile.FullPath)%09%(Compile.Link)" />
<LinesToWrite Include="AdditionalFiles%09%(AdditionalFiles.FullPath)%09%(AdditionalFiles.Link)" />
<LinesToWrite Include="None%09%(None.FullPath)%09%(None.Link)" />
<LinesToWrite Include="Content%09%(Content.FullPath)%09%(Content.Link)" />
<LinesToWrite Include="EmbeddedResource%09%(EmbeddedResource.FullPath)%09%(EmbeddedResource.Link)" />
<LinesToWrite Include="CustomItem%09%(CustomItem.FullPath)%09%(CustomItem.Link)" />
</ItemGroup>

<WriteLinesToFile
File="$(IntermediateOutputPath)\Items.txt"
Lines="@(LinesToWrite)"
Overwrite="true"
Encoding="Unicode"
/>
</Target>


</Project>
Empty file.
8 changes: 8 additions & 0 deletions TestAssets/TestProjects/LinkTest/Linked/A/B C/Linked.Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace LinkTest
{
public class LinkedSubClass
{
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions TestAssets/TestProjects/LinkTest/Linked/Linked.Class.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;

namespace LinkTest
{
public class LinkedClass
{
}
}
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,52 @@ Copyright (c) .NET Foundation. All rights reserved.
<!-- Default to use the version of the framework runtime matching the target framework version-->
<RuntimeFrameworkVersion Condition="'$(RuntimeFrameworkVersion)' == ''">$(_TargetFrameworkVersionWithoutV)</RuntimeFrameworkVersion>
</PropertyGroup>

<!--
Automatically add Link metadata to items of specific types if they are outside of the project folder and don't already have the Link metadata set.
This will cause them to be shown in the Solution Explorer. If an item has LinkBase metadata, the automatic Link will start with that value, and
the items will appear in the Solution Explorer under the folder specified by LinkBase.
-->
<ItemGroup Condition="'$(SetLinkMetadataAutomatically)' != 'false'">
<Compile Update="@(Compile)">
<!-- First, add a trailing slash to the LinkBase metadata if necessary. This allows us to use the same value
for the Link metadata whether or not LinkBase metadata is set: %(LinkBase)%(RecursiveDir)%(Filename)%(Extension)

Note that RecursiveDir already includes the trailing slash.
-->
<LinkBase Condition="'%(LinkBase)' != ''">$([MSBuild]::EnsureTrailingSlash(%(LinkBase)))</LinkBase>

<!-- Set the Link metadata if it's not already set and the item is outside of the project directory.
Check whether an item is inside the project directory by seeing if the FullPath starts with EnuserTrailingSlash(MSBuildProjectDirectory)
The FullPath and the MSBuildProjectDirectory will both already be normalized full paths.
The call to [MSBuild]::ValueOrDefault() is there in order to allow calling StartsWith on the FullPath value, since it's
not possible to call a string method on a metadata item directly. The intrinsic ValueOrDefault() will be more
performant than calling String.Copy(), which has been used for this in other contexts, but actually makes a copy
of the string data.
-->
<Link Condition="'%(Link)' == '' And !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">%(LinkBase)%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

<AdditionalFiles Update="@(AdditionalFiles)">
<LinkBase Condition="'%(LinkBase)' != ''">$([MSBuild]::EnsureTrailingSlash(%(LinkBase)))</LinkBase>
<Link Condition="'%(Link)' == '' And !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">%(LinkBase)%(RecursiveDir)%(Filename)%(Extension)</Link>
</AdditionalFiles>

<None Update="@(None)">
<LinkBase Condition="'%(LinkBase)' != ''">$([MSBuild]::EnsureTrailingSlash(%(LinkBase)))</LinkBase>
<Link Condition="'%(Link)' == '' And !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">%(LinkBase)%(RecursiveDir)%(Filename)%(Extension)</Link>
</None>

<Content Update="@(Content)">
<LinkBase Condition="'%(LinkBase)' != ''">$([MSBuild]::EnsureTrailingSlash(%(LinkBase)))</LinkBase>
<Link Condition="'%(Link)' == '' And !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">%(LinkBase)%(RecursiveDir)%(Filename)%(Extension)</Link>
</Content>

<EmbeddedResource Update="@(EmbeddedResource )">
Copy link
Contributor

@nguerrera nguerrera May 24, 2017

Choose a reason for hiding this comment

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

whitespace: @(EmbeddedResource ) -> @(EmbeddedResource)

<LinkBase Condition="'%(LinkBase)' != ''">$([MSBuild]::EnsureTrailingSlash(%(LinkBase)))</LinkBase>
<Link Condition="'%(Link)' == '' And !$([MSBuild]::ValueOrDefault('%(FullPath)', '').StartsWith($([MSBuild]::EnsureTrailingSlash($(MSBuildProjectDirectory)))))">%(LinkBase)%(RecursiveDir)%(Filename)%(Extension)</Link>
</EmbeddedResource >
Copy link
Contributor

@nguerrera nguerrera May 24, 2017

Choose a reason for hiding this comment

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

whitespace:

@(EmbeddedResource ) -> @(EmbeddedResource)
</EmbeddedResource /> -> </EmbeddedResource>

</ItemGroup>

Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't LinkBase work inside the project cone as well? I might want to display/copy in/to a different relative path.

<UsingTask TaskName="CheckForImplicitPackageReferenceOverrides" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.Text;
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Xunit;

using Xunit.Abstractions;
using System.Xml.Linq;
using System.IO;
using System.Linq;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToIncludeItemsOutsideTheProjectFolder : SdkTest
{
public GivenThatWeWantToIncludeItemsOutsideTheProjectFolder(ITestOutputHelper log) : base(log)
{
}

// Disabled on full framework MSBuild until CI machines have VS with bundled .NET Core / .NET Standard versions
// See https://github.com/dotnet/sdk/issues/1077
[CoreMSBuildOnlyTheory]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void Link_metadata_is_added_to_items_outside_the_project_folder(bool includeWithGlob, bool useLinkBase)
{
string identifier = (includeWithGlob ? "Globbed" : "Direct") + (useLinkBase ? "_LinkBase" : "");
var testAsset = _testAssetsManager
.CopyTestAsset("LinkTest", "LinkTest_", identifier)
.WithSource()
.WithProjectChanges(project =>
{
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Element(ns + "PropertyGroup");
propertyGroup.Add(new XElement(ns + "IncludeWithGlob", includeWithGlob));
propertyGroup.Add(new XElement(ns + "UseLinkBase", useLinkBase));
})
.Restore(Log, relativePath: "LinkTest");

var command = new MSBuildCommand(Log, "WriteItems", testAsset.TestRoot, "LinkTest");

command.Execute()
.Should()
.Pass();

string intermediateOutputPath = Path.Combine(command.GetBaseIntermediateDirectory().FullName, "Debug", "netstandard2.0");
string itemsFile = Path.Combine(intermediateOutputPath, "Items.txt");

var items = File.ReadAllLines(itemsFile)
.Select(l => l.Split('\t'))
.Select(f => (itemType: f[0], fullPath: f[1], link: f[2]))
.ToList();

var itemDict = items.GroupBy(i => i.itemType)
.ToDictionary(g => g.Key, g => g.Select(i => (fullPath: i.fullPath, link: i.link)).ToList());

// Remove generated source files
itemDict["Compile"].RemoveAll(i =>
{
string filename = Path.GetFileName(i.fullPath);
return filename.Contains("AssemblyInfo") ||
filename.Contains("AssemblyAttributes");
});

var expectedItems = new Dictionary<string, List<string>>()
{
["Compile"] = new List<string>() { "Class1.cs", @"..\Linked\Linked.Class.cs" },
["AdditionalFiles"] = new List<string>() { @"..\Linked\Linked.Additional.txt" },
["None"] = new List<string>() { @"..\Linked\Linked.None.txt" },
["Content"] = new List<string>() { @"..\Linked\Linked.Content.txt" },
["EmbeddedResource"] = new List<string>() { @"..\Linked\Linked.Embedded.txt" },
["CustomItem"] = new List<string>() { @"..\Linked\Linked.Custom.txt" },
};

if (includeWithGlob)
{
expectedItems["Compile"].Add(@"..\Linked\A\B C\Linked.Class.cs");
expectedItems["AdditionalFiles"].Add(@"..\Linked\A\B C\Linked.Additional.txt");
expectedItems["None"].Add(@"..\Linked\A\B C\Linked.None.txt");
expectedItems["Content"].Add(@"..\Linked\A\B C\Linked.Content.txt");
expectedItems["EmbeddedResource"].Add(@"..\Linked\A\B C\Linked.Embedded.txt");
expectedItems["CustomItem"].Add(@"..\Linked\A\B C\Linked.Custom.txt");
}

var projectFolder = Path.Combine(testAsset.TestRoot, "LinkTest");

var expectedItemMetadata = expectedItems.ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Select(item =>
{
string fullPath = Path.GetFullPath(Path.Combine(projectFolder, item.Replace('\\', Path.DirectorySeparatorChar)));
string link = "";
string linkedPrefix = @"..\Linked\";
if (item.StartsWith(linkedPrefix) && kvp.Key != "CustomItem")
{
link = item.Substring(linkedPrefix.Length);
if (useLinkBase)
{
link = @"Linked\Files\" + link;
}
}

link = link.Replace('\\', Path.DirectorySeparatorChar);

return (fullPath: fullPath, link: link);
}));

foreach (var itemType in expectedItemMetadata.Keys)
{
itemDict[itemType].Should().BeEquivalentTo(expectedItemMetadata[itemType]);
}
}
}
}