From db2fa689f1b18cc095ff1b8612e82b09e0275d20 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Thu, 31 Aug 2023 16:34:01 -0700 Subject: [PATCH] more fixup --- .../ProgramTests.cs | 44 +++++++++- src/Basic.CompilerLog/Program.cs | 84 +++++++------------ 2 files changed, 74 insertions(+), 54 deletions(-) diff --git a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs index d052ae6..97a1926 100644 --- a/src/Basic.CompilerLog.UnitTests/ProgramTests.cs +++ b/src/Basic.CompilerLog.UnitTests/ProgramTests.cs @@ -1,4 +1,5 @@ #if NETCOREAPP +using Basic.CompilerLog.Util; using Microsoft.CodeAnalysis.CSharp; using System; using System.Collections.Generic; @@ -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()); + } + + /// + /// Explicit build target overrides the implicit -t:Rebuild + /// + [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() { @@ -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)); } @@ -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"); diff --git a/src/Basic.CompilerLog/Program.cs b/src/Basic.CompilerLog/Program.cs index d295c83..d93e3ad 100644 --- a/src/Basic.CompilerLog/Program.cs +++ b/src/Basic.CompilerLog/Program.cs @@ -63,22 +63,10 @@ int RunCreate(IEnumerable 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; } @@ -87,11 +75,7 @@ int RunCreate(IEnumerable 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, @@ -583,66 +567,63 @@ Stream GetOrCreateCompilerLogStream(List extra) /// string GetLogFilePath(List extra) { - string? path; + string? logFilePath; + IEnumerable args = Array.Empty(); + 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 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); @@ -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");