Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Create framework assembly list file in .NET Core targeting pack (#5168)
Browse files Browse the repository at this point in the history
* Create framework assembly list file

* Fix FileUtilities private static style

* Remove package ID prefix from PlatformManifest.txt

* Proactively detect/report missing public key token
  • Loading branch information
dagood authored Feb 14, 2019
1 parent 564452f commit 493c628
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 13 deletions.
7 changes: 5 additions & 2 deletions dir.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@

<PropertyGroup>
<SharedFrameworkName>Microsoft.NETCore.App</SharedFrameworkName>
<NETCoreAppFrameworkMoniker>.NETCoreApp,Version=v$(MajorVersion).$(MinorVersion)</NETCoreAppFrameworkMoniker>
<NETCoreAppFramework>netcoreapp$(MajorVersion).$(MinorVersion)</NETCoreAppFramework>
<NETCoreAppFrameworkIdentifier>.NETCoreApp</NETCoreAppFrameworkIdentifier>
<NETCoreAppFrameworkVersion>$(MajorVersion).$(MinorVersion)</NETCoreAppFrameworkVersion>
<NETCoreAppFrameworkMoniker>$(NETCoreAppFrameworkIdentifier),Version=v$(NETCoreAppFrameworkVersion)</NETCoreAppFrameworkMoniker>
<NETCoreAppFramework>netcoreapp$(NETCoreAppFrameworkVersion)</NETCoreAppFramework>
<NETCoreAppFrameworkBrandName>.NET Core $(NETCoreAppFrameworkVersion)</NETCoreAppFrameworkBrandName>
</PropertyGroup>

<!--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
<SkipValidatePackage>true</SkipValidatePackage>

<!-- Include the platform manifest in the data dir. -->
<PlatformManifestTargetPath>data/</PlatformManifestTargetPath>
<PlatformManifestTargetPath>data/PlatformManifest.txt</PlatformManifestTargetPath>
<FrameworkListTargetPath>data/</FrameworkListTargetPath>

<!-- Exclude runtime.json from the package. -->
<IncludeRuntimeJson>false</IncludeRuntimeJson>
Expand Down
7 changes: 7 additions & 0 deletions src/pkg/projects/dir.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
<PackProjectDependencies>true</PackProjectDependencies>
</PropertyGroup>

<ItemGroup>
<FrameworkListRootAttributes Include="Name" Value="$(NETCoreAppFrameworkBrandName)" />
<FrameworkListRootAttributes Include="TargetFrameworkIdentifier" Value="$(NETCoreAppFrameworkIdentifier)" />
<FrameworkListRootAttributes Include="TargetFrameworkVersion" Value="$(NETCoreAppFrameworkVersion)" />
<FrameworkListRootAttributes Include="FrameworkName" Value="$(SharedFrameworkName)" />
</ItemGroup>

<PropertyGroup Condition="'$(PackageTargetRuntime)' == ''">
<SkipValidatePackage>true</SkipValidatePackage>
<IncludeRuntimeJson>true</IncludeRuntimeJson>
Expand Down
20 changes: 20 additions & 0 deletions src/pkg/projects/dir.targets
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\dir.targets" />

<UsingTask TaskName="CreateFrameworkListFile" AssemblyFile="$(LocalBuildToolsTaskDir)core-setup.tasks.dll"/>
<UsingTask TaskName="GenerateFileVersionProps" AssemblyFile="$(LocalBuildToolsTaskDir)core-setup.tasks.dll"/>

<PropertyGroup>
Expand Down Expand Up @@ -180,4 +181,23 @@
</ItemGroup>
</Target>

<Target Name="IncludeFrameworkListFile"
BeforeTargets="GetFiles"
Condition="'$(PackageTargetRuntime)' == '' AND '$(FrameworkListTargetPath)' != ''">
<PropertyGroup>
<FrameworkListFile>$(IntermediateOutputPath)FrameworkList.xml</FrameworkListFile>
</PropertyGroup>

<CreateFrameworkListFile
Files="@(File)"
TargetFile="$(FrameworkListFile)"
RootAttributes="@(FrameworkListRootAttributes)" />

<ItemGroup>
<File Include="$(FrameworkListFile)">
<TargetPath>$(FrameworkListTargetPath)</TargetPath>
</File>
</ItemGroup>
</Target>

</Project>
82 changes: 82 additions & 0 deletions tools-local/tasks/CreateFrameworkListFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Build.Framework;
using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace Microsoft.DotNet.Build.Tasks
{
public class CreateFrameworkListFile : BuildTask
{
/// <summary>
/// Files to extract basic information from and include in the list.
/// </summary>
[Required]
public ITaskItem[] Files { get; set; }

[Required]
public string TargetFile { get; set; }

/// <summary>
/// Extra attributes to place on the root node.
///
/// %(Identity): Attribute name.
/// %(Value): Attribute value.
/// </summary>
public ITaskItem[] RootAttributes { get; set; }

public override bool Execute()
{
XAttribute[] rootAttributes = RootAttributes
?.Select(item => new XAttribute(item.ItemSpec, item.GetMetadata("Value")))
.ToArray();

var frameworkManifest = new XElement("FileList", rootAttributes);

foreach (var f in Files
.Where(item =>
item.GetMetadata("TargetPath")?.StartsWith("data/") == true &&
item.ItemSpec.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
.Select(item => new
{
Item = item,
AssemblyName = FileUtilities.GetAssemblyName(item.ItemSpec),
FileVersion = FileUtilities.GetFileVersion(item.ItemSpec)
})
.Where(f => f.AssemblyName != null)
.OrderBy(f => f.Item.ItemSpec, StringComparer.OrdinalIgnoreCase))
{
byte[] publicKeyToken = f.AssemblyName.GetPublicKeyToken();
string publicKeyTokenHex;

if (publicKeyToken != null)
{
publicKeyTokenHex = BitConverter.ToString(publicKeyToken)
.ToLowerInvariant()
.Replace("-", "");
}
else
{
Log.LogError($"No public key token found for assembly {f.Item.ItemSpec}");
publicKeyTokenHex = "";
}

frameworkManifest.Add(new XElement(
"File",
new XAttribute("AssemblyName", f.AssemblyName.Name),
new XAttribute("PublicKeyToken", publicKeyTokenHex),
new XAttribute("AssemblyVersion", f.AssemblyName.Version),
new XAttribute("FileVersion", f.FileVersion)));
}

Directory.CreateDirectory(Path.GetDirectoryName(TargetFile));
File.WriteAllText(TargetFile, frameworkManifest.ToString());

return !Log.HasLoggedErrors;
}
}
}
21 changes: 12 additions & 9 deletions tools-local/tasks/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

namespace Microsoft.DotNet.Build.Tasks
{
static partial class FileUtilities
internal static partial class FileUtilities
{
private static readonly HashSet<string> s_assemblyExtensions = new HashSet<string>(
new[] { ".dll", ".exe", ".winmd" },
StringComparer.OrdinalIgnoreCase);

public static Version GetFileVersion(string sourcePath)
{
var fvi = FileVersionInfo.GetVersionInfo(sourcePath);
Expand All @@ -24,21 +28,20 @@ public static Version GetFileVersion(string sourcePath)
return null;
}

static readonly HashSet<string> s_assemblyExtensions = new HashSet<string>(new[] { ".dll", ".exe", ".winmd" }, StringComparer.OrdinalIgnoreCase);
public static Version TryGetAssemblyVersion(string sourcePath)
public static AssemblyName GetAssemblyName(string path)
{
var extension = Path.GetExtension(sourcePath);
if (!s_assemblyExtensions.Contains(Path.GetExtension(path)))
{
return null;
}

return s_assemblyExtensions.Contains(extension) ? GetAssemblyVersion(sourcePath) : null;
}
private static Version GetAssemblyVersion(string sourcePath)
{
try
{
return AssemblyName.GetAssemblyName(sourcePath)?.Version;
return AssemblyName.GetAssemblyName(path);
}
catch (BadImageFormatException)
{
// Not a valid assembly.
return null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tools-local/tasks/GenerateFileVersionProps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ FileVersionData GetFileVersionData(ITaskItem file)
{
return new FileVersionData()
{
AssemblyVersion = FileUtilities.TryGetAssemblyVersion(filePath),
AssemblyVersion = FileUtilities.GetAssemblyName(filePath)?.Version,
FileVersion = FileUtilities.GetFileVersion(filePath)
};
}
Expand Down

0 comments on commit 493c628

Please sign in to comment.