Skip to content

Copy PackageReferences to generated csproj #2347

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

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions src/BenchmarkDotNet/Templates/CsProj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<AppendTargetFrameworkToOutputPath>true</AppendTargetFrameworkToOutputPath>
<!-- fix for NETSDK1150: https://docs.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/referencing-executable-generates-error -->
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
<StartupObject>BenchmarkDotNet.Autogenerated.UniqueProgramName</StartupObject>
</PropertyGroup>

<ItemGroup>
Expand All @@ -38,6 +39,11 @@
<ItemGroup>
<ProjectReference Include="$CSPROJPATH$" />
</ItemGroup>

<ItemGroup>
$PACKAGEREFERENCES$
</ItemGroup>

$RUNTIMESETTINGS$

</Project>
5 changes: 5 additions & 0 deletions src/BenchmarkDotNet/Templates/MonoAOTLLVMCsProj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<AssemblyName>$PROGRAMNAME$</AssemblyName>
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<StartupObject>BenchmarkDotNet.Autogenerated.UniqueProgramName</StartupObject>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,6 +24,10 @@
<ItemGroup>
<ProjectReference Include="$CSPROJPATH$" />
</ItemGroup>

<ItemGroup>
$PACKAGEREFERENCES$
</ItemGroup>

<!-- Redirect 'dotnet publish' to in-tree runtime pack -->
<Target Name="TrickRuntimePackLocation" AfterTargets="ProcessFrameworkReferences">
Expand Down
5 changes: 5 additions & 0 deletions src/BenchmarkDotNet/Templates/WasmCsProj.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<WasmGenerateRunV8Script>true</WasmGenerateRunV8Script>
<ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>
<EnableDefaultWasmAssembliesToBundle>false</EnableDefaultWasmAssembliesToBundle>
<StartupObject>BenchmarkDotNet.Autogenerated.UniqueProgramName</StartupObject>
$COPIEDSETTINGS$
</PropertyGroup>

Expand All @@ -37,6 +38,10 @@
<ItemGroup>
<ProjectReference Include="$(OriginalCSProjPath)" />
</ItemGroup>

<ItemGroup>
$PACKAGEREFERENCES$
</ItemGroup>

<PropertyGroup>
<WasmBuildAppAfterThisTarget>PrepareForWasmBuild</WasmBuildAppAfterThisTarget>
Expand Down
24 changes: 20 additions & 4 deletions src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts

using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
{
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);

var content = new StringBuilder(ResourceHelper.LoadTemplate("CsProj.txt"))
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
Expand All @@ -71,6 +71,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
.Replace("$COPIEDSETTINGS$", customProperties)
.Replace("$CONFIGURATIONNAME$", buildPartition.BuildConfiguration)
.Replace("$SDKNAME$", sdkName)
.Replace("$PACKAGEREFERENCES$", packageReferences)
.ToString();

File.WriteAllText(artifactsPaths.ProjectFilePath, content);
Expand All @@ -97,12 +98,13 @@ protected virtual string GetRuntimeSettings(GcMode gcMode, IResolver resolver)
// the host project or one of the .props file that it imports might contain some custom settings that needs to be copied, sth like
// <NetCoreAppImplicitPackageVersion>2.0.0-beta-001607-00</NetCoreAppImplicitPackageVersion>
// <RuntimeFrameworkVersion>2.0.0-beta-001607-00</RuntimeFrameworkVersion>
internal (string customProperties, string sdkName) GetSettingsThatNeedsToBeCopied(TextReader streamReader, FileInfo projectFile)
internal (string customProperties, string sdkName, string packageReferences) GetSettingsThatNeedsToBeCopied(TextReader streamReader, FileInfo projectFile)
{
if (!string.IsNullOrEmpty(RuntimeFrameworkVersion)) // some power users knows what to configure, just do it and copy nothing more
return ($"<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>", DefaultSdkName);
return ($"<RuntimeFrameworkVersion>{RuntimeFrameworkVersion}</RuntimeFrameworkVersion>", DefaultSdkName, string.Empty);

var customProperties = new StringBuilder();
var packageReferences = new StringBuilder();
var sdkName = DefaultSdkName;

string line;
Expand Down Expand Up @@ -133,9 +135,23 @@ protected virtual string GetRuntimeSettings(GcMode gcMode, IResolver resolver)
if (trimmedLine.StartsWith("<Project Sdk=\"")
|| (TargetFrameworkMoniker.StartsWith("netcoreapp", StringComparison.InvariantCultureIgnoreCase) && trimmedLine.StartsWith("<Import Sdk=\"")))
sdkName = trimmedLine.Split('"')[1]; // its sth like Sdk="name"

if (trimmedLine.StartsWith("<PackageReference Include=\""))
{
AppendPackageReference(trimmedLine, packageReferences, streamReader);
}
}

return (customProperties.ToString(), sdkName);
return (customProperties.ToString(), sdkName, packageReferences.ToString());
}

private static void AppendPackageReference(string line, StringBuilder stringBuilder, TextReader streamReader)
{
do
{
stringBuilder.AppendLine(line);
}
while (!line.EndsWith("\" />") && !line.EndsWith("</PackageReference>") && (line = streamReader.ReadLine()) != null);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts

using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
{
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);

string content = new StringBuilder(ResourceHelper.LoadTemplate("MonoAOTLLVMCsProj.txt"))
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
Expand All @@ -47,6 +47,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
.Replace("$COMPILERBINARYPATH$", AotCompilerPath)
.Replace("$RUNTIMEIDENTIFIER$", CustomDotNetCliToolchainBuilder.GetPortableRuntimeIdentifier())
.Replace("$USELLVM$", useLLVM)
.Replace("$PACKAGEREFERENCES$", packageReferences)
.ToString();

File.WriteAllText(artifactsPaths.ProjectFilePath, content);
Expand Down
3 changes: 2 additions & 1 deletion src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void GenerateProjectFile(BuildPartition buildPartition, ArtifactsPaths

using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
{
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);

string content = new StringBuilder(ResourceHelper.LoadTemplate("WasmCsProj.txt"))
.Replace("$PLATFORM$", buildPartition.Platform.ToConfig())
Expand All @@ -60,6 +60,7 @@ protected void GenerateProjectFile(BuildPartition buildPartition, ArtifactsPaths
.Replace("$SDKNAME$", sdkName)
.Replace("$WASMDATADIR$", runtime.WasmDataDir)
.Replace("$TARGET$", CustomRuntimePack != null ? "PublishWithCustomRuntimePack" : "Publish")
.Replace("$PACKAGEREFERENCES$", packageReferences)
.ToString();

File.WriteAllText(artifactsPaths.ProjectFilePath, content);
Expand Down
64 changes: 60 additions & 4 deletions tests/BenchmarkDotNet.Tests/CsProjGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void AssertParsedSdkName(string csProjContent, string targetFrameworkMon

using (var reader = new StringReader(csProjContent))
{
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal(expectedSdkValue, sdkName);
Assert.Empty(customProperties);
Expand All @@ -81,13 +81,69 @@ public void UseWpfSettingGetsCopied()

using (var reader = new StringReader(withUseWpfTrue))
{
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal("<UseWpf>true</UseWpf>" + Environment.NewLine, customProperties);
Assert.Equal("Microsoft.NET.Sdk", sdkName);
}
}

[Fact]
public void PackageReferenceSingleLineGetsCopied()
{
const string WithPackageReference = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<PackageReference Include=""xunit"" Version=""2.4.2"" />
</ItemGroup>
</Project>
";
var sut = new CsProjGenerator("netcoreapp3.0", null, null, null, true);

using (var reader = new StringReader(WithPackageReference))
{
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal("<PackageReference Include=\"xunit\" Version=\"2.4.2\" />" + Environment.NewLine, packageReferences);
Assert.Equal("Microsoft.NET.Sdk", sdkName);
}
}

[Fact]
public void PackageReferenceMultiLineGetsCopied()
{
const string WithPackageReference = @"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<PackageReference Include=""xunit.runner.visualstudio"" Version=""2.4.5"">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
";
var sut = new CsProjGenerator("netcoreapp3.0", null, null, null, true);

using (var reader = new StringReader(WithPackageReference))
{
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal(@"<PackageReference Include=""xunit.runner.visualstudio"" Version=""2.4.5"">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>".Replace(Environment.NewLine, "\n").Replace("\n", Environment.NewLine) + Environment.NewLine, packageReferences);
Assert.Equal("Microsoft.NET.Sdk", sdkName);
}
}

[Fact]
public void SettingsFromPropsFileImportedUsingAbsolutePathGetCopies()
{
Expand All @@ -110,7 +166,7 @@ public void SettingsFromPropsFileImportedUsingAbsolutePathGetCopies()

using (var reader = new StringReader(importingAbsolutePath))
{
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal("<LangVersion>9.9</LangVersion>" + Environment.NewLine, customProperties);
Assert.Equal("Microsoft.NET.Sdk", sdkName);
Expand Down Expand Up @@ -141,7 +197,7 @@ public void SettingsFromPropsFileImportedUsingRelativePathGetCopies()

using (var reader = new StringReader(importingRelativePath))
{
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);

Assert.Equal("<LangVersion>9.9</LangVersion>" + Environment.NewLine, customProperties);
Assert.Equal("Microsoft.NET.Sdk", sdkName);
Expand Down