Skip to content

Commit db2fa68

Browse files
committed
more fixup
1 parent 2f2f4ef commit db2fa68

File tree

2 files changed

+74
-54
lines changed

2 files changed

+74
-54
lines changed

src/Basic.CompilerLog.UnitTests/ProgramTests.cs

+42-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if NETCOREAPP
2+
using Basic.CompilerLog.Util;
23
using Microsoft.CodeAnalysis.CSharp;
34
using System;
45
using System.Collections.Generic;
@@ -49,6 +50,45 @@ public void Create(string extra, string fileName)
4950
Assert.True(File.Exists(complogPath));
5051
}
5152

53+
[Fact]
54+
public void CreateProjectFile()
55+
{
56+
RunDotNet("new console --name console -o .");
57+
Assert.Equal(0, RunCompLog($"create console.csproj -o msbuild.complog"));
58+
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
59+
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
60+
Assert.Single(reader.ReadAllCompilerCalls());
61+
}
62+
63+
/// <summary>
64+
/// Explicit build target overrides the implicit -t:Rebuild
65+
/// </summary>
66+
[Fact]
67+
public void CreateNoopBuild()
68+
{
69+
RunDotNet("new console --name console -o .");
70+
RunDotNet("build");
71+
Assert.Equal(0, RunCompLog($"create console.csproj -o msbuild.complog -- -t:Build"));
72+
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
73+
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
74+
Assert.Empty(reader.ReadAllCompilerCalls());
75+
}
76+
77+
[Theory]
78+
[InlineData("console.sln")]
79+
[InlineData("console.csproj")]
80+
[InlineData("")]
81+
public void CreateSolution(string target)
82+
{
83+
RunDotNet("new console --name console -o .");
84+
RunDotNet("new sln --name console");
85+
RunDotNet("sln add console.csproj");
86+
Assert.Equal(0, RunCompLog($"create {target} -o msbuild.complog"));
87+
var complogPath = Path.Combine(RootDirectory, "msbuild.complog");
88+
using var reader = CompilerLogReader.Create(complogPath, BasicAnalyzerHostOptions.None);
89+
Assert.Single(reader.ReadAllCompilerCalls());
90+
}
91+
5292
[Fact]
5393
public void CreateFullPath()
5494
{
@@ -62,7 +102,7 @@ public void CreateFullPath()
62102
[Fact]
63103
public void References()
64104
{
65-
Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {Path.Combine(Fixture.ComplogDirectory, "console.complog")}"));
105+
Assert.Equal(0, RunCompLog($"ref -o {RootDirectory} {Fixture.ConsoleComplogPath.Value}"));
66106
Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "refs"), "*.dll"));
67107
Assert.NotEmpty(Directory.EnumerateFiles(Path.Combine(RootDirectory, "console", "analyzers"), "*.dll", SearchOption.AllDirectories));
68108
}
@@ -90,7 +130,7 @@ public void ExportHelloWorld(string template)
90130
public void EmitConsole(string arg)
91131
{
92132
using var emitDir = new TempDir();
93-
RunCompLog($"emit {arg} -o {emitDir.DirectoryPath} {Fixture.ConsoleComplogPath}");
133+
RunCompLog($"emit {arg} -o {emitDir.DirectoryPath} {Fixture.ConsoleComplogPath.Value}");
94134

95135
AssertOutput(@"console\emit\console.dll");
96136
AssertOutput(@"console\emit\console.pdb");

src/Basic.CompilerLog/Program.cs

+32-52
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,10 @@ int RunCreate(IEnumerable<string> args)
6363
}
6464

6565
// todo use the standard get or build code
66-
string? binlogFilePath = null;
67-
if (extra.Count == 1)
66+
string binlogFilePath = GetLogFilePath(extra);
67+
if (PathUtil.Comparer.Equals(".complog", Path.GetExtension(binlogFilePath)))
6868
{
69-
binlogFilePath = extra[0];
70-
}
71-
else if (extra.Count == 0)
72-
{
73-
binlogFilePath = Directory
74-
.EnumerateFiles(CurrentDirectory, "*.binlog")
75-
.OrderBy(x => Path.GetFileName(x), PathUtil.Comparer)
76-
.FirstOrDefault();
77-
}
78-
79-
if (binlogFilePath is null)
80-
{
81-
PrintUsage();
69+
WriteLine($"Already a .complog file: {binlogFilePath}");
8270
return ExitFailure;
8371
}
8472

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

90-
if (!Path.IsPathRooted(complogFilePath))
91-
{
92-
complogFilePath = Path.Combine(CurrentDirectory, complogFilePath);
93-
}
94-
78+
complogFilePath = GetResolvedPath(CurrentDirectory, complogFilePath);
9579
var diagnosticList = CompilerLogUtil.ConvertBinaryLog(
9680
binlogFilePath,
9781
complogFilePath,
@@ -583,66 +567,63 @@ Stream GetOrCreateCompilerLogStream(List<string> extra)
583567
/// </summary>
584568
string GetLogFilePath(List<string> extra)
585569
{
586-
string? path;
570+
string? logFilePath;
571+
IEnumerable<string> args = Array.Empty<string>();
572+
string baseDirectory = CurrentDirectory;
587573
if (extra.Count == 0)
588574
{
589-
path = GetLogFilePathExisting(CurrentDirectory);
590-
if (path is null)
575+
logFilePath = FindLogFilePath(baseDirectory);
576+
}
577+
else
578+
{
579+
logFilePath = extra[0];
580+
args = extra.Skip(1);
581+
if (string.IsNullOrEmpty(Path.GetExtension(logFilePath)))
591582
{
592-
throw CreateOptionException();
583+
baseDirectory = logFilePath;
584+
logFilePath = FindLogFilePath(baseDirectory);
593585
}
594-
595-
return path;
596586
}
597587

598-
path = extra[0];
599-
if (path == "--")
588+
if (logFilePath is null)
600589
{
601-
return GetLogFilePathAfterBuild(CurrentDirectory, null, extra.Skip(1));
590+
throw CreateOptionException();
602591
}
603592

604-
switch (Path.GetExtension(path))
593+
switch (Path.GetExtension(logFilePath))
605594
{
606595
case ".complog":
607596
case ".binlog":
608-
if (extra.Count > 1)
597+
if (args.Any())
609598
{
610-
throw new OptionException($"Extra arguments: {string.Join(' ', extra.Skip(1))}", "log");
599+
throw new OptionException($"Extra arguments: {string.Join(' ', args.Skip(1))}", "log");
611600
}
612601

613-
return GetResolvedPath(CurrentDirectory, path);
602+
return GetResolvedPath(CurrentDirectory, logFilePath);
614603
case ".sln":
615604
case ".csproj":
616-
return GetLogFilePathAfterBuild(CurrentDirectory, path, extra.Skip(1));
605+
case ".vbproj":
606+
return GetLogFilePathAfterBuild(baseDirectory, logFilePath, args);
617607
default:
618-
throw new OptionException($"Not a valid log file {path}", "log");
608+
throw new OptionException($"Not a valid log file {logFilePath}", "log");
619609
}
620610

621-
static string GetLogFilePathExisting(string baseDirectory)
622-
{
623-
// Search the directory for valid log files
624-
var path = FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog");
625-
if (path is not null)
626-
{
627-
return path;
628-
}
629-
630-
throw CreateOptionException();
631-
}
611+
static string? FindLogFilePath(string baseDirectory) =>
612+
FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog", "*.sln", "*.csproj", ".vbproj");
632613

633614
static string GetLogFilePathAfterBuild(string baseDirectory, string? buildFileName, IEnumerable<string> buildArgs)
634615
{
635616
var path = buildFileName is not null
636617
? GetResolvedPath(baseDirectory, buildFileName)
637-
: FindFirstFileWithPattern("*.sln", "*.csproj");
618+
: FindFirstFileWithPattern(baseDirectory, "*.sln", "*.csproj", ".vbproj");
638619
if (path is null)
639620
{
640621
throw CreateOptionException();
641622
}
642623

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

655-
// TODO: use a temp file for the binlog
656-
return Path.Combine(baseDirectory, "msbuild.binlog");
636+
return Path.Combine(baseDirectory, "complog.binlog");
657637
}
658638

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

0 commit comments

Comments
 (0)