Skip to content

Commit

Permalink
Merge pull request #489 from natidea/resolveFrameworkAssemblies
Browse files Browse the repository at this point in the history
Add Framework Assemblies from project assets file to compiler references
  • Loading branch information
natidea authored Dec 20, 2016
2 parents 30fb880 + c4cd28a commit dceed2b
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net451</TargetFramework>
<RuntimeIdentifier>win7-x86</RuntimeIdentifier>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="1.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.ComponentModel.DataAnnotations;

class Program
{
static void Main(string[] args)
{
var b = new VerifyCodeViewModel
{
Provider = "Required Test Provider"
};
Console.WriteLine(b.Provider);
}

public class VerifyCodeViewModel
{
[Required]
public string Provider { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.Diagnostics;

namespace StopwatchLib
{
public static class Helper
{
public static Stopwatch StartWatch()
{
return Stopwatch.StartNew();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net45</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NETStandard.Library" Version="1.6" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ private void GetFileDependencies(LockFileTargetLibrary package, string targetNam
item.SetMetadata(MetadataKeys.ParentTarget, targetName); // Foreign Key
item.SetMetadata(MetadataKeys.ParentPackage, packageId); // Foreign Key

if (fileGroup == FileGroup.FrameworkAssembly)
{
// NOTE: the path provided for framework assemblies is the name of the framework reference
item.SetMetadata("FrameworkAssembly", filePath);
}

foreach (var property in properties)
{
item.SetMetadata(property.Key, property.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,34 @@ Copyright (c) .NET Foundation. All rights reserved.
</CreateItem>
</Target>

<!--
============================================================
HELPER: Get File Dependencies matching active TFM and RID
============================================================
-->

<Target Name="_ComputeActiveTFMFileDependencies"
DependsOnTargets="RunResolvePackageDependencies"
Returns="_ActiveTFMFileDependencies">
<ItemGroup>
<_ActiveTFMFileDependencies Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />
</ItemGroup>
</Target>

<!--
============================================================
Reference Targets: For populating References based on lock file
- _ComputeLockFileReferences
- _ComputeLockFileFrameworks
- ResolveLockFileReferences
============================================================
-->

<Target Name="_ComputeLockFileReferences"
DependsOnTargets="RunResolvePackageDependencies"
DependsOnTargets="_ComputeActiveTFMFileDependencies"
Returns="ResolvedCompileFileDefinitions">
<ItemGroup>
<TFMFileItems Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />
<_CompileFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'CompileTimeAssembly'))" />
<_CompileFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'CompileTimeAssembly'))" />

<!-- Get corresponding file definitions -->
<__CompileFileDefinitions Include="@(FileDefinitions)" Exclude="@(_CompileFileItems)" />
Expand All @@ -251,11 +265,28 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
</Target>

<Target Name="_ComputeLockFileFrameworks"
Condition="'$(DisableLockFileFrameworks)' != 'true'"
DependsOnTargets="_ComputeActiveTFMFileDependencies"
Returns="ResolvedFrameworkAssemblies">
<ItemGroup>
<_FrameworkAssemblies Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'FrameworkAssembly'))" />

<ResolvedFrameworkAssemblies Include="%(_FrameworkAssemblies.FrameworkAssembly)">
<Private>false</Private>
<NuGetIsFrameworkReference>true</NuGetIsFrameworkReference>
<NuGetSourceType>Package</NuGetSourceType>
</ResolvedFrameworkAssemblies>

</ItemGroup>
</Target>

<Target Name="ResolveLockFileReferences"
DependsOnTargets="_ComputeLockFileReferences">
DependsOnTargets="_ComputeLockFileReferences;_ComputeLockFileFrameworks">
<ItemGroup>
<!-- Add the references we computed -->
<Reference Include="@(ResolvedCompileFileDefinitions)" />
<Reference Include="@(ResolvedFrameworkAssemblies)" />
</ItemGroup>
</Target>

Expand Down Expand Up @@ -295,25 +326,23 @@ Copyright (c) .NET Foundation. All rights reserved.
-->

<Target Name="_ComputeLockFileCopyLocal"
DependsOnTargets="RunResolvePackageDependencies;RunProduceContentAssets"
DependsOnTargets="_ComputeActiveTFMFileDependencies;RunProduceContentAssets"
Returns="NativeCopyLocalItems;RuntimeCopyLocalItems;ResourceCopyLocalItems;AllCopyLocalItems">
<ItemGroup>
<TFMFileItems Include="@(FileDependencies->WithMetadataValue('ParentTarget', '$(_NugetTargetMonikerAndRID)'))" />

<!--NativeLibrary-->
<_NativeFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'NativeLibrary'))" />
<_NativeFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'NativeLibrary'))" />
<__NativeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_NativeFileItems)" />
<_NativeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__NativeCopyLocalItems)" />
<NativeCopyLocalItems Include="%(_NativeCopyLocalItems.ResolvedPath)" />

<!--RuntimeAssembly-->
<_RuntimeFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'RuntimeAssembly'))" />
<_RuntimeFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'RuntimeAssembly'))" />
<__RuntimeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_RuntimeFileItems)" />
<_RuntimeCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__RuntimeCopyLocalItems)" />
<RuntimeCopyLocalItems Include="%(_RuntimeCopyLocalItems.ResolvedPath)" />

<!--ResourceAssembly-->
<_ResourceFileItems Include="@(TFMFileItems->WithMetadataValue('FileGroup', 'ResourceAssembly'))" />
<_ResourceFileItems Include="@(_ActiveTFMFileDependencies->WithMetadataValue('FileGroup', 'ResourceAssembly'))" />
<__ResourceCopyLocalItems Include="@(FileDefinitions)" Exclude="@(_ResourceFileItems)" />
<_ResourceCopyLocalItems Include="@(FileDefinitions)" Exclude="@(__ResourceCopyLocalItems)" />
<ResourceCopyLocalItems Include="%(_ResourceCopyLocalItems.ResolvedPath)" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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 FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using System;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Xunit;
using static Microsoft.NET.TestFramework.Commands.MSBuildTest;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToBuildAppsWithFrameworkRefs
{
private TestAssetsManager _testAssetsManager = TestAssetsManager.TestProjectsAssetsManager;

[Fact]
public void It_builds_the_projects_successfully()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyProjectsBuild(testAsset);
}

[Fact]
public void It_builds_with_disable_implicit_frameworkRefs()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyProjectsBuild(testAsset, "/p:DisableImplicitFrameworkReferences=true");
}

void VerifyProjectsBuild(TestAsset testAsset, params string[] buildArgs)
{
VerifyBuild(testAsset, "StopwatchLib", "net45", buildArgs,
"StopwatchLib.dll",
"StopwatchLib.pdb");

VerifyBuild(testAsset, "EntityFrameworkApp", "net451", buildArgs,
"EntityFrameworkApp.exe",
"EntityFrameworkApp.pdb",
"EntityFrameworkApp.runtimeconfig.dev.json",
"EntityFrameworkApp.runtimeconfig.json");

// Try running EntityFrameworkApp.exe
var appProjectDirectory = Path.Combine(testAsset.TestRoot, "EntityFrameworkApp");
var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory("net451");

Command.Create(Path.Combine(outputDirectory.FullName, "EntityFrameworkApp.exe"), Enumerable.Empty<string>())
.CaptureStdOut()
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining("Required Test Provider");
}

private void VerifyBuild(TestAsset testAsset, string project, string targetFramework,
string [] buildArgs,
params string [] expectedFiles)
{
var appProjectDirectory = Path.Combine(testAsset.TestRoot, project);

var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

buildCommand
.Execute(buildArgs)
.Should()
.Pass();

outputDirectory.Should().HaveFiles(expectedFiles);
}

[Fact]
public void The_clean_target_removes_all_files_from_the_output_folder()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return;
}

var testAsset = _testAssetsManager
.CopyTestAsset("AppsWithFrameworkReferences")
.WithSource();

testAsset.Restore("EntityFrameworkApp");
testAsset.Restore("StopwatchLib");

VerifyClean(testAsset, "StopwatchLib", "net45",
"StopwatchLib.dll",
"StopwatchLib.pdb");

VerifyClean(testAsset, "EntityFrameworkApp", "net451",
"EntityFrameworkApp.exe",
"EntityFrameworkApp.pdb",
"EntityFrameworkApp.runtimeconfig.dev.json",
"EntityFrameworkApp.runtimeconfig.json");
}

private void VerifyClean(TestAsset testAsset, string project, string targetFramework,
params string[] expectedFiles)
{
var appProjectDirectory = Path.Combine(testAsset.TestRoot, project);

var buildCommand = new BuildCommand(Stage0MSBuild, appProjectDirectory);
var outputDirectory = buildCommand.GetOutputDirectory(targetFramework);

buildCommand
.Execute()
.Should()
.Pass();

outputDirectory.Should().HaveFiles(expectedFiles);

var cleanCommand = Stage0MSBuild.CreateCommandForTarget("Clean", buildCommand.FullPathProjectFile);

cleanCommand
.Execute()
.Should()
.Pass();

outputDirectory.Should().OnlyHaveFiles(Array.Empty<string>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file()
{
var testAsset = _testAssetsManager
.CopyTestAsset("CompilationContext", "PreserveCompilationContext")
.WithSource()
.WithProjectChanges(project =>
{
// Workaround for https://github.com/dotnet/sdk/issues/367
var ns = project.Root.Name.Namespace;
var propertyGroup = project.Root.Elements(ns + "PropertyGroup").FirstOrDefault();
propertyGroup.Should().NotBeNull();
propertyGroup.Add(new XElement(ns + "DisableImplicitFrameworkReferences", "true"));
});
.WithSource();

testAsset.Restore("TestApp");
testAsset.Restore("TestLibrary");
Expand All @@ -52,7 +42,12 @@ public void It_publishes_the_project_with_a_refs_folder_and_correct_deps_file()
}

publishCommand
.Execute($"/p:TargetFramework={targetFramework}")
.Execute(
$"/p:TargetFramework={targetFramework}",

// Additional properties to workaround https://github.com/Microsoft/msbuild/issues/1345
"/p:DisableImplicitFrameworkReferences=true",
"/p:DisableLockFileFrameworks=true")
.Should()
.Pass();

Expand Down

0 comments on commit dceed2b

Please sign in to comment.