Skip to content

Commit

Permalink
more fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredpar committed Aug 31, 2023
1 parent 2f2f4ef commit db2fa68
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 54 deletions.
44 changes: 42 additions & 2 deletions src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if NETCOREAPP
using Basic.CompilerLog.Util;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -49,6 +50,45 @@ public void Create(string extra, string fileName)
Assert.True(File.Exists(complogPath));
}

[Fact]
public void CreateProjectFile()
{
RunDotNet("new console --name console -o .");
Assert.Equal(0, RunCompLog($"create console.csproj -o msbuild.complog"));
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
Assert.Single(reader.ReadAllCompilerCalls());
}

/// <summary>
/// Explicit build target overrides the implicit -t:Rebuild
/// </summary>
[Fact]
public void CreateNoopBuild()
{
RunDotNet("new console --name console -o .");
RunDotNet("build");
Assert.Equal(0, RunCompLog($"create console.csproj -o msbuild.complog -- -t:Build"));
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
Assert.Empty(reader.ReadAllCompilerCalls());
}

[Theory]
[InlineData("console.sln")]
[InlineData("console.csproj")]
[InlineData("")]
public void CreateSolution(string target)
{
RunDotNet("new console --name console -o .");
RunDotNet("new sln --name console");
RunDotNet("sln add console.csproj");
Assert.Equal(0, RunCompLog($"create {target} -o msbuild.complog"));
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
Assert.Single(reader.ReadAllCompilerCalls());
}

[Fact]
public void CreateFullPath()
{
Expand All @@ -62,7 +102,7 @@ public void CreateFullPath()
[Fact]
public void References()
{
Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {Path.Combine(Fixture.ComplogDirectory, "console.complog")}"));
Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {Fixture.ConsoleComplogPath.Value}"));
Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "refs"), "*.dll"));
Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "analyzers"), "*.dll", SearchOption.AllDirectories));
}
Expand Down Expand Up @@ -90,7 +130,7 @@ public void ExportHelloWorld(string template)
public void EmitConsole(string arg)
{
using var emitDir = new TempDir();
RunCompLog($"emit {arg} -o {emitDir.DirectoryPath} {Fixture.ConsoleComplogPath}");
RunCompLog($"emit {arg} -o {emitDir.DirectoryPath} {Fixture.ConsoleComplogPath.Value}");

AssertOutput(@"console\emit\console.dll");
AssertOutput(@"console\emit\console.pdb");
Expand Down
84 changes: 32 additions & 52 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,10 @@ int RunCreate(IEnumerable<string> args)
}

// todo use the standard get or build code
string? binlogFilePath = null;
if (extra.Count == 1)
string binlogFilePath = GetLogFilePath(extra);
if (PathUtil.Comparer.Equals(".complog", Path.GetExtension(binlogFilePath)))
{
binlogFilePath = extra[0];
}
else if (extra.Count == 0)
{
binlogFilePath = Directory
.EnumerateFiles(CurrentDirectory, "*.binlog")
.OrderBy(x => Path.GetFileName(x), PathUtil.Comparer)
.FirstOrDefault();
}

if (binlogFilePath is null)
{
PrintUsage();
WriteLine($"Already a .complog file: {binlogFilePath}");
return ExitFailure;
}

Expand All @@ -87,11 +75,7 @@ int RunCreate(IEnumerable<string> args)
complogFilePath = Path.ChangeExtension(binlogFilePath, ".complog");
}

if (!Path.IsPathRooted(complogFilePath))
{
complogFilePath = Path.Combine(CurrentDirectory, complogFilePath);
}

complogFilePath = GetResolvedPath(CurrentDirectory, complogFilePath);
var diagnosticList = CompilerLogUtil.ConvertBinaryLog(
binlogFilePath,
complogFilePath,
Expand Down Expand Up @@ -583,66 +567,63 @@ Stream GetOrCreateCompilerLogStream(List<string> extra)
/// </summary>
string GetLogFilePath(List<string> extra)
{
string? path;
string? logFilePath;
IEnumerable<string> args = Array.Empty<string>();
string baseDirectory = CurrentDirectory;
if (extra.Count == 0)
{
path = GetLogFilePathExisting(CurrentDirectory);
if (path is null)
logFilePath = FindLogFilePath(baseDirectory);
}
else
{
logFilePath = extra[0];
args = extra.Skip(1);
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)))
{
throw CreateOptionException();
baseDirectory = logFilePath;
logFilePath = FindLogFilePath(baseDirectory);
}

return path;
}

path = extra[0];
if (path == "--")
if (logFilePath is null)
{
return GetLogFilePathAfterBuild(CurrentDirectory, null, extra.Skip(1));
throw CreateOptionException();
}

switch (Path.GetExtension(path))
switch (Path.GetExtension(logFilePath))
{
case ".complog":
case ".binlog":
if (extra.Count > 1)
if (args.Any())
{
throw new OptionException($"Extra arguments: {string.Join(' ', extra.Skip(1))}", "log");
throw new OptionException($"Extra arguments: {string.Join(' ', args.Skip(1))}", "log");
}

return GetResolvedPath(CurrentDirectory, path);
return GetResolvedPath(CurrentDirectory, logFilePath);
case ".sln":
case ".csproj":
return GetLogFilePathAfterBuild(CurrentDirectory, path, extra.Skip(1));
case ".vbproj":
return GetLogFilePathAfterBuild(baseDirectory, logFilePath, args);
default:
throw new OptionException($"Not a valid log file {path}", "log");
throw new OptionException($"Not a valid log file {logFilePath}", "log");
}

static string GetLogFilePathExisting(string baseDirectory)
{
// Search the directory for valid log files
var path = FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog");
if (path is not null)
{
return path;
}

throw CreateOptionException();
}
static string? FindLogFilePath(string baseDirectory) =>
FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog", "*.sln", "*.csproj", ".vbproj");

static string GetLogFilePathAfterBuild(string baseDirectory, string? buildFileName, IEnumerable<string> buildArgs)
{
var path = buildFileName is not null
? GetResolvedPath(baseDirectory, buildFileName)
: FindFirstFileWithPattern("*.sln", "*.csproj");
: FindFirstFileWithPattern(baseDirectory, "*.sln", "*.csproj", ".vbproj");
if (path is null)
{
throw CreateOptionException();
}

// TODO: use a temp file for the binlog
var args = $"build {path} -bl:msbuild.binlog {string.Join(' ', buildArgs)}";
WriteLine("Building");
var tag = buildArgs.Any() ? "" : "-t:Rebuild";
var args = $"build {path} -bl:complog.binlog {tag} {string.Join(' ', buildArgs)}";
WriteLine($"Building {path}");
WriteLine($"dotnet {args}");
var result = ProcessUtil.Run("dotnet", args, baseDirectory);
WriteLine(result.StandardOut);
Expand All @@ -652,8 +633,7 @@ static string GetLogFilePathAfterBuild(string baseDirectory, string? buildFileNa
throw new Exception("Build failed");
}

// TODO: use a temp file for the binlog
return Path.Combine(baseDirectory, "msbuild.binlog");
return Path.Combine(baseDirectory, "complog.binlog");
}

static OptionException CreateOptionException() => new("Need a file to analyze", "log");
Expand Down

0 comments on commit db2fa68

Please sign in to comment.