Skip to content

Commit

Permalink
Changes (#154)
Browse files Browse the repository at this point in the history
closes #152
  • Loading branch information
jaredpar authored Sep 8, 2024
1 parent acc6edb commit edf024a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/Basic.CompilerLog.UnitTests/ProgramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,32 @@ private void AssertCorrectReader(ICompilerCallReader reader, string logFilePath)

public (int ExitCode, string Output) RunCompLogEx(string args, string? currentDirectory = null)
{
var savedCurrentDirectory = Constants.CurrentDirectory;
var savedLocalAppDataDirectory = Constants.LocalAppDataDirectory;

try
{
var writer = new System.IO.StringWriter();
currentDirectory ??= RootDirectory;
Constants.CurrentDirectory = currentDirectory;
Constants.LocalAppDataDirectory = Path.Combine(currentDirectory, "localappdata");
Constants.Out = writer;
Constants.OnCompilerCallReader = OnCompilerCallReader;
var assembly = typeof(FilterOptionSet).Assembly;
var program = assembly.GetType("Program", throwOnError: true);
var main = program!.GetMethod("<Main>$", BindingFlags.Static | BindingFlags.NonPublic);
Assert.NotNull(main);
var ret = main!.Invoke(null, new[] { args.Split(' ', StringSplitOptions.RemoveEmptyEntries) });
if (Directory.Exists(Constants.LocalAppDataDirectory))
{
Assert.Empty(Directory.EnumerateFileSystemEntries(Constants.LocalAppDataDirectory));
}
return ((int)ret!, writer.ToString());
}
finally
{
Constants.CurrentDirectory = savedCurrentDirectory;
Constants.LocalAppDataDirectory = savedLocalAppDataDirectory;
Constants.Out = Console.Out;
Constants.OnCompilerCallReader = _ => { };
}
Expand Down Expand Up @@ -362,7 +372,8 @@ public void ResponseMultiTarget()
[Fact]
public void ResponseNoLogArgument()
{
var (exitCode, output) = RunCompLogEx($"rsp -o {RootDirectory}", Path.GetDirectoryName(Fixture.ConsoleProjectPath)!);
var consoleDir = Path.GetDirectoryName(Fixture.ConsoleProjectPath)!;
var (exitCode, output) = RunCompLogEx($"rsp -o {RootDirectory}", consoleDir);
TestOutputHelper.WriteLine(output);
Assert.Equal(Constants.ExitSuccess, exitCode);
Assert.True(File.Exists(Path.Combine(RootDirectory, "console", "build.rsp")));
Expand Down
3 changes: 3 additions & 0 deletions src/Basic.CompilerLog/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ internal static class Constants
internal const int ExitSuccess = 0;

internal static string CurrentDirectory { get; set; } = Environment.CurrentDirectory;
internal static string LocalAppDataDirectory { get; set; } = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"Basic.CompilerLog");
internal static TextWriter Out { get; set; } = Console.Out;

internal static Action<ICompilerCallReader> OnCompilerCallReader = _ => { };
Expand Down
25 changes: 18 additions & 7 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
? ("help", Enumerable.Empty<string>())
: (args[0], args.Skip(1));

var appDataDirectory = Path.Combine(LocalAppDataDirectory, Guid.NewGuid().ToString());

try
{
return command.ToLower() switch
Expand Down Expand Up @@ -41,6 +43,13 @@
RunHelp(null);
return ExitFailure;
}
finally
{
if (Directory.Exists(appDataDirectory))
{
Directory.Delete(appDataDirectory, recursive: true);
}
}

int RunCreate(IEnumerable<string> args)
{
Expand Down Expand Up @@ -730,7 +739,7 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
case ".sln":
case ".csproj":
case ".vbproj":
return GetLogFilePathAfterBuild(baseDirectory, logFilePath, args);
return GetLogFilePathAfterBuild(appDataDirectory, baseDirectory, logFilePath, args);
default:
throw new OptionException($"Not a valid log file {logFilePath}", "log");
}
Expand All @@ -740,12 +749,14 @@ string GetLogFilePath(List<string> extra, bool includeCompilerLogs = true)
? FindFirstFileWithPattern(baseDirectory, "*.complog", "*.binlog", "*.sln", "*.csproj", ".vbproj")
: FindFirstFileWithPattern(baseDirectory, "*.binlog", "*.sln", "*.csproj", ".vbproj");

static string GetLogFilePathAfterBuild(string baseDirectory, string buildFileName, IEnumerable<string> buildArgs)
static string GetLogFilePathAfterBuild(string appDataDirectory, string baseDirectory, string buildFileName, IEnumerable<string> buildArgs)
{
var path = GetResolvedPath(baseDirectory, buildFileName);
Directory.CreateDirectory(appDataDirectory);
var binlogFilePath = Path.Combine(appDataDirectory, "build.binlog");
var buildFilePath = GetResolvedPath(baseDirectory, buildFileName);
var tag = buildArgs.Any() ? "" : "-t:Rebuild";
var args = $"build {path} -bl:build.binlog -nr:false {tag} {string.Join(' ', buildArgs)}";
WriteLine($"Building {path}");
var args = $"build {buildFilePath} -bl:\"{binlogFilePath}\" -nr:false {tag} {string.Join(' ', buildArgs)}";
WriteLine($"Building {buildFilePath}");
WriteLine($"dotnet {args}");
var result = DotnetUtil.Command(args, baseDirectory);
WriteLine(result.StandardOut);
Expand All @@ -755,7 +766,7 @@ static string GetLogFilePathAfterBuild(string baseDirectory, string buildFileNam
WriteLine("Build Failed!");
}

return Path.Combine(baseDirectory, "build.binlog");
return binlogFilePath;
}

static OptionException CreateOptionException() => new("Need a file to analyze", "log");
Expand Down Expand Up @@ -830,4 +841,4 @@ static string GetResolvedPath(string baseDirectory, string path)
}

static void Write(string str) => Constants.Out.Write(str);
static void WriteLine(string line) => Constants.Out.WriteLine(line);
static void WriteLine(string line) => Constants.Out.WriteLine(line);

0 comments on commit edf024a

Please sign in to comment.