Skip to content

Commit 5af1cfc

Browse files
committed
Copy PackageReferences to generated csproj.
1 parent bed071f commit 5af1cfc

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

src/BenchmarkDotNet/Templates/CsProj.txt

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
<ItemGroup>
3939
<ProjectReference Include="$CSPROJPATH$" />
4040
</ItemGroup>
41+
42+
<ItemGroup>
43+
$PACKAGEREFERENCES$
44+
</ItemGroup>
45+
4146
$RUNTIMESETTINGS$
4247

4348
</Project>

src/BenchmarkDotNet/Templates/MonoAOTLLVMCsProj.txt

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<ItemGroup>
2424
<ProjectReference Include="$CSPROJPATH$" />
2525
</ItemGroup>
26+
27+
<ItemGroup>
28+
$PACKAGEREFERENCES$
29+
</ItemGroup>
2630

2731
<!-- Redirect 'dotnet publish' to in-tree runtime pack -->
2832
<Target Name="TrickRuntimePackLocation" AfterTargets="ProcessFrameworkReferences">

src/BenchmarkDotNet/Templates/WasmCsProj.txt

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
<ItemGroup>
3838
<ProjectReference Include="$(OriginalCSProjPath)" />
3939
</ItemGroup>
40+
41+
<ItemGroup>
42+
$PACKAGEREFERENCES$
43+
</ItemGroup>
4044

4145
<PropertyGroup>
4246
<WasmBuildAppAfterThisTarget>PrepareForWasmBuild</WasmBuildAppAfterThisTarget>

src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs

+11-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
5959

6060
using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
6161
{
62-
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
62+
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);
6363

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

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

105106
var customProperties = new StringBuilder();
107+
var packageReferences = new StringBuilder();
106108
var sdkName = DefaultSdkName;
107109

108110
string line;
@@ -133,9 +135,14 @@ protected virtual string GetRuntimeSettings(GcMode gcMode, IResolver resolver)
133135
if (trimmedLine.StartsWith("<Project Sdk=\"")
134136
|| (TargetFrameworkMoniker.StartsWith("netcoreapp", StringComparison.InvariantCultureIgnoreCase) && trimmedLine.StartsWith("<Import Sdk=\"")))
135137
sdkName = trimmedLine.Split('"')[1]; // its sth like Sdk="name"
138+
139+
if (trimmedLine.StartsWith("<PackageReference Include=\""))
140+
{
141+
packageReferences.AppendLine(trimmedLine);
142+
}
136143
}
137144

138-
return (customProperties.ToString(), sdkName);
145+
return (customProperties.ToString(), sdkName, packageReferences.ToString());
139146
}
140147

141148
/// <summary>

src/BenchmarkDotNet/Toolchains/MonoAotLLVM/MonoAotLLVMGenerator.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected override void GenerateProject(BuildPartition buildPartition, Artifacts
3232

3333
using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
3434
{
35-
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
35+
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);
3636

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

5253
File.WriteAllText(artifactsPaths.ProjectFilePath, content);

src/BenchmarkDotNet/Toolchains/MonoWasm/WasmGenerator.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected void GenerateProjectFile(BuildPartition buildPartition, ArtifactsPaths
4646

4747
using (var file = new StreamReader(File.OpenRead(projectFile.FullName)))
4848
{
49-
var (customProperties, sdkName) = GetSettingsThatNeedsToBeCopied(file, projectFile);
49+
var (customProperties, sdkName, packageReferences) = GetSettingsThatNeedsToBeCopied(file, projectFile);
5050

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

6566
File.WriteAllText(artifactsPaths.ProjectFilePath, content);

tests/BenchmarkDotNet.Tests/CsProjGeneratorTests.cs

+29-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void AssertParsedSdkName(string csProjContent, string targetFrameworkMon
5959

6060
using (var reader = new StringReader(csProjContent))
6161
{
62-
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
62+
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
6363

6464
Assert.Equal(expectedSdkValue, sdkName);
6565
Assert.Empty(customProperties);
@@ -81,13 +81,38 @@ public void UseWpfSettingGetsCopied()
8181

8282
using (var reader = new StringReader(withUseWpfTrue))
8383
{
84-
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
84+
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
8585

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

91+
[Fact]
92+
public void PackageReferenceGetsCopied()
93+
{
94+
const string WithPackageReference = @"
95+
<Project Sdk=""Microsoft.NET.Sdk"">
96+
<PropertyGroup>
97+
<PlatformTarget>AnyCPU</PlatformTarget>
98+
</PropertyGroup>
99+
100+
<ItemGroup>
101+
<PackageReference Include=""TestPackage"" Version=""0.1.0"" />
102+
</ItemGroup>
103+
</Project>
104+
";
105+
var sut = new CsProjGenerator("netcoreapp3.0", null, null, null, true);
106+
107+
using (var reader = new StringReader(WithPackageReference))
108+
{
109+
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
110+
111+
Assert.Equal("<PackageReference Include=\"TestPackage\" Version=\"0.1.0\" />" + Environment.NewLine, packageReferences);
112+
Assert.Equal("Microsoft.NET.Sdk", sdkName);
113+
}
114+
}
115+
91116
[Fact]
92117
public void SettingsFromPropsFileImportedUsingAbsolutePathGetCopies()
93118
{
@@ -110,7 +135,7 @@ public void SettingsFromPropsFileImportedUsingAbsolutePathGetCopies()
110135

111136
using (var reader = new StringReader(importingAbsolutePath))
112137
{
113-
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
138+
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
114139

115140
Assert.Equal("<LangVersion>9.9</LangVersion>" + Environment.NewLine, customProperties);
116141
Assert.Equal("Microsoft.NET.Sdk", sdkName);
@@ -141,7 +166,7 @@ public void SettingsFromPropsFileImportedUsingRelativePathGetCopies()
141166

142167
using (var reader = new StringReader(importingRelativePath))
143168
{
144-
var (customProperties, sdkName) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
169+
var (customProperties, sdkName, packageReferences) = sut.GetSettingsThatNeedsToBeCopied(reader, TestAssemblyFileInfo);
145170

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

0 commit comments

Comments
 (0)