Skip to content

Commit 4a4cae4

Browse files
committed
Added support for specifying arguments, added a .NET Core 3.1 integration test project, added a failing integration test for version 9 binlogs, and updated deps as far as I can before MSBuild start multi-targeting
1 parent 164c70d commit 4a4cae4

File tree

8 files changed

+98
-3
lines changed

8 files changed

+98
-3
lines changed

src/Buildalyzer/Buildalyzer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="2.1.0" />
2222
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
2323
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.1.1" />
24-
<PackageReference Include="MSBuild.StructuredLogger" Version="2.0.11" />
24+
<PackageReference Include="MSBuild.StructuredLogger" Version="2.0.174" />
2525
<PackageReference Include="MsBuildPipeLogger.Server" Version="1.1.2" />
2626
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.6.0" />
2727
</ItemGroup>

src/Buildalyzer/Environment/BuildEnvironment.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public sealed class BuildEnvironment
4242

4343
public string DotnetExePath { get; }
4444

45+
public IEnumerable<string> Arguments { get; }
46+
4547
public IReadOnlyDictionary<string, string> GlobalProperties => _globalProperties;
4648

4749
public IReadOnlyDictionary<string, string> EnvironmentVariables => _environmentVariables;
@@ -52,12 +54,14 @@ public BuildEnvironment(
5254
string[] targetsToBuild,
5355
string msBuildExePath,
5456
string dotnetExePath,
57+
IEnumerable<string> arguments,
5558
IDictionary<string, string> additionalGlobalProperties = null,
5659
IDictionary<string, string> additionalEnvironmentVariables = null)
5760
{
5861
DesignTime = designTime;
5962
Restore = restore;
6063
TargetsToBuild = targetsToBuild ?? throw new ArgumentNullException(nameof(targetsToBuild));
64+
Arguments = arguments ?? throw new ArgumentNullException(nameof(arguments));
6165

6266
// Check if we've already specified a path to MSBuild
6367
string envMsBuildExePath = System.Environment.GetEnvironmentVariable(Environment.EnvironmentVariables.MSBUILD_EXE_PATH);
@@ -133,6 +137,7 @@ public BuildEnvironment WithTargetsToBuild(params string[] targets) =>
133137
targets,
134138
MsBuildExePath,
135139
DotnetExePath,
140+
Arguments,
136141
_additionalGlobalProperties,
137142
_additionalEnvironmentVariables);
138143
}

src/Buildalyzer/Environment/EnvironmentFactory.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Xml.Linq;
77
using Buildalyzer.Construction;
88
using Microsoft.Build.Utilities;
9+
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Logging;
1011

1112
namespace Buildalyzer.Environment
@@ -114,6 +115,7 @@ private BuildEnvironment CreateCoreEnvironment(EnvironmentOptions options)
114115
options.TargetsToBuild.ToArray(),
115116
msBuildExePath,
116117
options.DotnetExePath,
118+
options.Arguments,
117119
additionalGlobalProperties,
118120
additionalEnvironmentVariables);
119121
}
@@ -150,6 +152,7 @@ private BuildEnvironment CreateFrameworkEnvironment(EnvironmentOptions options)
150152
options.TargetsToBuild.ToArray(),
151153
msBuildExePath,
152154
options.DotnetExePath,
155+
options.Arguments,
153156
additionalGlobalProperties,
154157
options.EnvironmentVariables);
155158
}

src/Buildalyzer/Environment/EnvironmentOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,19 @@ public class EnvironmentOptions
4242
/// </remarks>
4343
public string DotnetExePath { get; set; } = "dotnet";
4444

45+
/// <summary>
46+
/// The global MSBuild properties to set.
47+
/// </summary>
4548
public IDictionary<string, string> GlobalProperties { get; } = new Dictionary<string, string>();
4649

50+
/// <summary>
51+
/// Environment variables to set.
52+
/// </summary>
4753
public IDictionary<string, string> EnvironmentVariables { get; } = new Dictionary<string, string>();
54+
55+
/// <summary>
56+
/// Additional MSBuild command-line arguments to use.
57+
/// </summary>
58+
public IList<string> Arguments { get; } = new List<string>();
4859
}
4960
}

src/Buildalyzer/ProjectAnalyzer.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public IAnalyzerResults Build(string[] targetFrameworks, EnvironmentOptions envi
113113
targetFrameworks = new string[] { null };
114114
}
115115

116-
// Create a new build envionment for each target
116+
// Create a new build environment for each target
117117
AnalyzerResults results = new AnalyzerResults();
118118
foreach (string targetFramework in targetFrameworks)
119119
{
@@ -247,6 +247,11 @@ private string GetCommand(BuildEnvironment buildEnvironment, string targetFramew
247247
initialArguments = $"\"{buildEnvironment.MsBuildExePath}\"";
248248
}
249249

250+
// Environment arguments
251+
string environmentArguments = buildEnvironment.Arguments.Any()
252+
? string.Join(" ", buildEnvironment.Arguments)
253+
: string.Empty;
254+
250255
// Get the logger arguments (/l)
251256
string loggerPath = typeof(BuildalyzerLogger).Assembly.Location;
252257
bool logEverything = _buildLoggers.Count > 0;
@@ -273,7 +278,7 @@ private string GetCommand(BuildEnvironment buildEnvironment, string targetFramew
273278
// Get the restore argument (/restore)
274279
string restoreArgument = buildEnvironment.Restore ? "/restore" : string.Empty;
275280

276-
arguments = $"{initialArguments} /noconsolelogger {restoreArgument} {targetArgument} {propertyArgument} {loggerArgument} {FormatArgument(ProjectFile.Path)}";
281+
arguments = $"{initialArguments} /noconsolelogger {environmentArguments} {restoreArgument} {targetArgument} {propertyArgument} {loggerArgument} {FormatArgument(ProjectFile.Path)}";
277282
return fileName;
278283
}
279284

tests/Buildalyzer.Tests/Integration/SimpleProjectsFixture.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Linq;
56
using System.Text;
67
using System.Xml.Linq;
@@ -35,6 +36,7 @@ public class SimpleProjectsFixture
3536
@"SdkMultiTargetingProject\SdkMultiTargetingProject.csproj",
3637
#endif
3738
@"SdkNetCoreProject\SdkNetCoreProject.csproj",
39+
@"SdkNetCore31Project\SdkNetCore31Project.csproj",
3840
@"SdkNetCoreProjectImport\SdkNetCoreProjectImport.csproj",
3941
@"SdkNetCoreProjectWithReference\SdkNetCoreProjectWithReference.csproj",
4042
@"SdkNetCoreProjectWithImportedProps\SdkNetCoreProjectWithImportedProps.csproj",
@@ -500,6 +502,56 @@ public void BuildsFSharpProject()
500502
results.ShouldAllBe(x => x.Succeeded, log.ToString());
501503
}
502504

505+
[Test]
506+
public void GetsSourceFilesFromVersion9BinLog()
507+
{
508+
// Given
509+
StringWriter log = new StringWriter();
510+
IProjectAnalyzer analyzer = GetProjectAnalyzer(
511+
@"SdkNetCore31Project\SdkNetCore31Project.csproj",
512+
log);
513+
string binLogPath = Path.ChangeExtension(Path.GetTempFileName(), ".binlog");
514+
EnvironmentOptions options = new EnvironmentOptions();
515+
options.Arguments.Add("/bl:" + binLogPath); // Tell MSBuild to produce the binlog so we use the latest internal logger
516+
517+
try
518+
{
519+
// When
520+
analyzer.Build(options);
521+
using (Stream stream = File.OpenRead(binLogPath))
522+
{
523+
using (GZipStream gzip = new GZipStream(stream, CompressionMode.Decompress))
524+
{
525+
using (BinaryReader reader = new BinaryReader(gzip))
526+
{
527+
// Verify this produced a version 9 binlog
528+
reader.ReadInt32().ShouldBe(9);
529+
}
530+
}
531+
}
532+
IReadOnlyList<string> sourceFiles = analyzer.Manager.Analyze(binLogPath).First().SourceFiles;
533+
534+
// Then
535+
sourceFiles.ShouldNotBeNull(log.ToString());
536+
new[]
537+
{
538+
#if Is_Windows
539+
// Linux and Mac builds appear to omit the AssemblyAttributes.cs file
540+
"AssemblyAttributes",
541+
#endif
542+
"Class1",
543+
"AssemblyInfo"
544+
}.ShouldBeSubsetOf(sourceFiles.Select(x => Path.GetFileName(x).Split('.').TakeLast(2).First()), log.ToString());
545+
}
546+
finally
547+
{
548+
if (File.Exists(binLogPath))
549+
{
550+
File.Delete(binLogPath);
551+
}
552+
}
553+
}
554+
503555
private static IProjectAnalyzer GetProjectAnalyzer(string projectFile, StringWriter log)
504556
{
505557
IProjectAnalyzer analyzer = new AnalyzerManager(
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace SdkNetCoreProject
4+
{
5+
public class Class1
6+
{
7+
public void Foo()
8+
{
9+
Console.WriteLine("Bar");
10+
}
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
</Project>

0 commit comments

Comments
 (0)