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 SDK support for generating deps.json files for .NET CLI Tools #1128

Merged
merged 11 commits into from
Apr 27, 2017
Merged
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
4 changes: 4 additions & 0 deletions build/Nuget/Microsoft.NET.Sdk.Nuget.targets
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ vs.localizedResources
<SdkSwrLines Include='$(SdkSwrBlankLine)folder "InstallDir:MSBuild\Sdks\Microsoft.NET.Sdk\build"' />
<SdkSwrLines Include='%20%20file source="%24(OutputPath)PackagesLayout\build\%(LaidOutSdk_BuildFiles.Filename)%(LaidOutSdk_BuildFiles.Extension)"' />

<LaidOutSdk_BuildGenerateDepsFiles Include="$(PackagesLayoutDir)build\GenerateDeps\*" />
<SdkSwrLines Include='$(SdkSwrBlankLine)folder "InstallDir:MSBuild\Sdks\Microsoft.NET.Sdk\build\GenerateDeps"' />
<SdkSwrLines Include='%20%20file source="%24(OutputPath)PackagesLayout\build\GenerateDeps\%(LaidOutSdk_BuildGenerateDepsFiles.Filename)%(LaidOutSdk_BuildGenerateDepsFiles.Extension)"' />

<LaidOutSdk_BuildCrossTargetingFiles Include="$(PackagesLayoutDir)buildCrossTargeting\*" />
<SdkSwrLines Include='$(SdkSwrBlankLine)folder "InstallDir:MSBuild\Sdks\Microsoft.NET.Sdk\buildCrossTargeting"' />
<SdkSwrLines Include='%20%20file source="%24(OutputPath)PackagesLayout\buildCrossTargeting\%(LaidOutSdk_BuildCrossTargetingFiles.Filename)%(LaidOutSdk_BuildCrossTargetingFiles.Extension)"' />
Expand Down
49 changes: 32 additions & 17 deletions src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class DependencyContextBuilder
private CompilationOptions _compilationOptions;
private string _referenceAssembliesPath;
private Dictionary<PackageIdentity, StringBuilder> _filteredPackages;
private bool _includeMainProjectInDepsFile = true;

public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, ProjectContext projectContext)
{
Expand All @@ -37,6 +38,12 @@ public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, ProjectContex
_versionFolderPathResolver = new VersionFolderPathResolver(rootPath: null);
}

public DependencyContextBuilder WithMainProjectInDepsFile(bool includeMainProjectInDepsFile)
{
_includeMainProjectInDepsFile = includeMainProjectInDepsFile;
return this;
}

public DependencyContextBuilder WithFrameworkReferences(IEnumerable<ReferenceInfo> frameworkReferences)
{
// note: Framework libraries only export compile-time stuff
Expand Down Expand Up @@ -101,32 +108,40 @@ public DependencyContext Build()

var runtimeSignature = GenerateRuntimeSignature(runtimeExports);

RuntimeLibrary projectRuntimeLibrary = GetProjectRuntimeLibrary(
_mainProjectInfo,
_projectContext,
dependencyLookup);
IEnumerable<RuntimeLibrary> runtimeLibraries =
new[] { projectRuntimeLibrary }
IEnumerable<RuntimeLibrary> runtimeLibraries = Enumerable.Empty<RuntimeLibrary>();
if (_includeMainProjectInDepsFile)
{
runtimeLibraries = runtimeLibraries.Concat(new[]
{
GetProjectRuntimeLibrary(
_mainProjectInfo,
_projectContext,
dependencyLookup)
});
}
runtimeLibraries = runtimeLibraries
.Concat(GetLibraries(runtimeExports, libraryLookup, dependencyLookup, runtime: true).Cast<RuntimeLibrary>())
.Concat(GetDirectReferenceRuntimeLibraries());

IEnumerable<CompilationLibrary> compilationLibraries;
IEnumerable<CompilationLibrary> compilationLibraries = Enumerable.Empty<CompilationLibrary>();
if (includeCompilationLibraries)
{
CompilationLibrary projectCompilationLibrary = GetProjectCompilationLibrary(
_mainProjectInfo,
_projectContext,
dependencyLookup);
compilationLibraries =
new[] { projectCompilationLibrary }
if (_includeMainProjectInDepsFile)
{
compilationLibraries = compilationLibraries.Concat(new[]
{
GetProjectCompilationLibrary(
_mainProjectInfo,
_projectContext,
dependencyLookup)
});
}

compilationLibraries = compilationLibraries
.Concat(GetFrameworkLibraries())
.Concat(GetLibraries(compilationExports, libraryLookup, dependencyLookup, runtime: false).Cast<CompilationLibrary>())
.Concat(GetDirectReferenceCompilationLibraries());
}
else
{
compilationLibraries = Enumerable.Empty<CompilationLibrary>();
}

var targetInfo = new TargetInfo(
_projectContext.LockFileTarget.TargetFramework.DotNetFrameworkName,
Expand Down
14 changes: 9 additions & 5 deletions src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public class GenerateDepsFile : TaskBase
[Required]
public ITaskItem[] AssemblySatelliteAssemblies { get; set; }

[Required]
public bool IncludeMainProject { get; set; }

[Required]
public ITaskItem[] ReferencePaths { get; set; }

Expand Down Expand Up @@ -117,11 +120,11 @@ protected override void ExecuteCore()
CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions);

SingleProjectInfo mainProject = SingleProjectInfo.Create(
ProjectPath,
AssemblyName,
AssemblyExtension,
AssemblyVersion,
AssemblySatelliteAssemblies);
ProjectPath,
AssemblyName,
AssemblyExtension,
AssemblyVersion,
AssemblySatelliteAssemblies);

IEnumerable<ReferenceInfo> frameworkReferences =
ReferenceInfo.CreateFrameworkReferenceInfos(ReferencePaths);
Expand All @@ -142,6 +145,7 @@ protected override void ExecuteCore()
IsSelfContained);

DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, projectContext)
.WithMainProjectInDepsFile(IncludeMainProject)
.WithFrameworkReferences(frameworkReferences)
.WithDirectReferences(directReferences)
.WithReferenceProjectInfos(referenceProjects)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@
<DependentUpon>Strings.resx</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="build\GenerateDeps\GenerateDeps.proj">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<!-- Remove files from copy local that would not be published as they are provided by the platform package -->
<!-- https://github.com/dotnet/sdk/issues/933 tracks a first class feature for this -->
<Target Name="FilterCopyLocal" DependsOnTargets="RunResolvePublishAssemblies" BeforeTargets="ResolveLockFileCopyLocalProjectDeps">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<Project Sdk="Microsoft.NET.Sdk" DefaultTargets="BuildDepsJson">
<!--
***********************************************************************************************
GenerateDeps.proj

WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.

Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->

<!--
This project is built by the .NET CLI in order to create .deps.json files for .NET CLI tools.
Properties to be passed in by the .NET CLI:
- ProjectAssetsFile: Full path to the project.assets.json file for the tool under the NuGet .tools folder
- ToolName: The simple name of the tool DLL, for example, "dotnet-mytool"
- AdditionalImport: The full path to the .props file from the platform package which will be imported, which
should include the PackageConflictPlatformManifests file.
This is a workaround until NuGet can generate .props and .targets files for imports from packages referenced
by tools, which is tracked by https://github.com/NuGet/Home/issues/5037.
-->

<Import Project="$(AdditionalImport)"
Condition=" '$(AdditionalImport)' != '' And Exists($(AdditionalImport))" />

<PropertyGroup>
<ToolFolder>$([System.IO.Path]::GetDirectoryName($(ProjectAssetsFile)))</ToolFolder>
<ProjectDepsFilePath Condition="'$(ProjectDepsFilePath)' == ''">$(ToolFolder)\$(ToolName).deps.json</ProjectDepsFilePath>

<OutputType>Exe</OutputType>
<IncludeMainProjectInDepsFile>false</IncludeMainProjectInDepsFile>
</PropertyGroup>

<Target Name="BuildDepsJson" DependsOnTargets="$(ResolvePackageDependenciesForBuildDependsOn);GenerateBuildDependencyFile" />

<Target Name="DontRestore" BeforeTargets="Restore">
<Error Text="This project should not be restored" />
</Target>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ Copyright (c) .NET Foundation. All rights reserved.
AssemblySatelliteAssemblies="@(IntermediateSatelliteAssembliesWithTargetPath)"
ReferencePaths="@(ReferencePath)"
ReferenceSatellitePaths="@(ReferenceSatellitePaths)"
IncludeMainProject="$(IncludeMainProjectInDepsFile)"
RuntimeIdentifier="$(RuntimeIdentifier)"
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
FilesToSkip="@(_ConflictPackageFiles);@(_PublishConflictPackageFiles)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ Copyright (c) .NET Foundation. All rights reserved.
<DefaultImplicitPackages>Microsoft.NETCore.App;NETStandard.Library</DefaultImplicitPackages>
</PropertyGroup>

<PropertyGroup Condition="'$(DotnetCliToolTargetFramework)' == '' And '$(BundledNETCoreAppTargetFrameworkVersion)' != ''">
<!-- Set the TFM used to restore .NET CLI tools to match the version of .NET Core bundled in the CLI -->
<DotnetCliToolTargetFramework>netcoreapp$(BundledNETCoreAppTargetFrameworkVersion)</DotnetCliToolTargetFramework>
</PropertyGroup>

<UsingTask TaskName="GetNearestTargetFramework" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
<UsingTask TaskName="NETSdkError" AssemblyFile="$(MicrosoftNETBuildTasksAssembly)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ Copyright (c) .NET Foundation. All rights reserved.
<DebugSymbols Condition="'$(DebugSymbols)'==''">false</DebugSymbols>
<CheckForOverflowUnderflow Condition="'$(CheckForOverflowUnderflow)'==''">false</CheckForOverflowUnderflow>
</PropertyGroup>

<PropertyGroup>
<!-- Path to project that the .NET CLI will build in order to generate deps.json files for .NET CLI tools -->
<ToolDepsJsonGeneratorProject>$(MSBuildThisFileDirectory)GenerateDeps\GenerateDeps.proj</ToolDepsJsonGeneratorProject>
</PropertyGroup>

<!-- Default item includes (globs and implicit references) -->
<Import Project="Microsoft.NET.Sdk.DefaultItems.props" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<ProjectRuntimeConfigFileName Condition="'$(ProjectRuntimeConfigFileName)' == ''">$(AssemblyName).runtimeconfig.json</ProjectRuntimeConfigFileName>
<ProjectRuntimeConfigFilePath Condition="'$(ProjectRuntimeConfigFilePath)' == ''">$(TargetDir)$(ProjectRuntimeConfigFileName)</ProjectRuntimeConfigFilePath>
<ProjectRuntimeConfigDevFilePath Condition="'$(ProjectRuntimeConfigDevFilePath)' == ''">$(TargetDir)$(AssemblyName).runtimeconfig.dev.json</ProjectRuntimeConfigDevFilePath>
<IncludeMainProjectInDepsFile Condition=" '$(IncludeMainProjectInDepsFile)' == '' ">true</IncludeMainProjectInDepsFile>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -62,7 +63,7 @@ Copyright (c) .NET Foundation. All rights reserved.
<_DotNetHostPolicyLibraryName>$(_NativeLibraryPrefix)hostpolicy$(_NativeLibraryExtension)</_DotNetHostPolicyLibraryName>
<_DotNetHostFxrLibraryName>$(_NativeLibraryPrefix)hostfxr$(_NativeLibraryExtension)</_DotNetHostFxrLibraryName>
</PropertyGroup>

<PropertyGroup>
<CoreBuildDependsOn>
$(CoreBuildDependsOn);
Expand Down Expand Up @@ -100,6 +101,7 @@ Copyright (c) .NET Foundation. All rights reserved.
AssemblySatelliteAssemblies="@(IntermediateSatelliteAssembliesWithTargetPath)"
ReferencePaths="@(ReferencePath)"
ReferenceSatellitePaths="@(ReferenceSatellitePaths)"
IncludeMainProject="$(IncludeMainProjectInDepsFile)"
RuntimeIdentifier="$(RuntimeIdentifier)"
PlatformLibraryName="$(MicrosoftNETPlatformLibrary)"
FilesToSkip="@(_ConflictPackageFiles)"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using FluentAssertions;
using Microsoft.NET.TestFramework;
using Microsoft.NET.TestFramework.Assertions;
using Microsoft.NET.TestFramework.Commands;
using Microsoft.NET.TestFramework.ProjectConstruction;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Xunit;

using static Microsoft.NET.TestFramework.Commands.MSBuildTest;

namespace Microsoft.NET.Build.Tests
{
public class GivenThatWeWantToExcludeTheMainProjectFromTheDepsFile : SdkTest
{
[Fact]
public void It_builds_successfully()
{
if (UsingFullFrameworkMSBuild)
{
// Fullframework NuGet versioning on Jenkins infrastructure issue
// https://github.com/dotnet/sdk/issues/1041

// 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
return;
}

TestProject testProject = new TestProject()
{
Name = "ExcludeMainProjectFromDeps",
IsSdkProject = true,
TargetFrameworks = "netcoreapp2.0",
IsExe = true,
};

TestProject referencedProject = new TestProject()
{
Name = "ReferencedProject",
IsSdkProject = true,
TargetFrameworks = "netstandard2.0",
IsExe = false
};

testProject.ReferencedProjects.Add(referencedProject);

var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, testProject.Name)
.WithProjectChanges((path, project) =>
{
if (Path.GetFileNameWithoutExtension(path) == testProject.Name)
{
var ns = project.Root.Name.Namespace;

var propertyGroup = new XElement(ns + "PropertyGroup");
project.Root.Add(propertyGroup);

propertyGroup.Add(new XElement(ns + "IncludeMainProjectInDepsFile", "false"));
}
})
.Restore(testProject.Name);

var buildCommand = new BuildCommand(Stage0MSBuild, testProjectInstance.TestRoot, testProject.Name);

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