Skip to content

Commit

Permalink
Fix spacing issue in rsp (#47)
Browse files Browse the repository at this point in the history
closes #44
  • Loading branch information
jaredpar authored Jun 29, 2023
1 parent 98b2b00 commit 5aee6c6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 22 deletions.
27 changes: 27 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,31 @@ public void AllCompilerLogs(bool includeAnalyzers)
TestExport(complogPath, expectedCount: null, includeAnalyzers);
}
}

[Fact]
public void ExportRsp()
{
var args = new[]
{
"blah .cs",
"/r:blah .cs", // only change non-options as options quotes handled specially by command line parser
"a b.cs",
"ab.cs",
};

using var writer = new StringWriter();
ExportUtil.ExportRsp(args, writer);
Assert.Equal("""
"blah .cs"
/r:blah .cs
"a b.cs"
ab.cs

""", writer.ToString());

writer.GetStringBuilder().Length = 0;
ExportUtil.ExportRsp(args, writer, singleLine: true);
Assert.Equal(@"""blah .cs"" /r:blah .cs ""a b.cs"" ab.cs", writer.ToString());
}

}
59 changes: 51 additions & 8 deletions src/Basic.CompilerLog.Util/ExportUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ List<string> ProcessRsp()

foreach (var line in compilerCall.Arguments)
{
// The only non-options are source files and those are rewritten by this
// process
if (!IsOption(line))
// The only non-options are source files and those are rewritten by other
// methods and added to commandLineList
if (!IsOption(line.AsSpan()))
{
continue;
}
Expand Down Expand Up @@ -297,9 +297,6 @@ List<string> ProcessRsp()

lines.AddRange(commandLineList);
return lines;

static bool IsOption(string str) =>
str.Length > 0 && str[0] is '-' or '/';
}

void WriteReferences()
Expand Down Expand Up @@ -364,7 +361,7 @@ void WriteContent()

using var contentStream = Reader.GetContentStream(tuple.ContentHash);
var filePath = builder.WriteContent(tuple.FilePath, contentStream);
commandLineList.Add($@"{prefix}""{PathUtil.RemovePathStart(filePath, builder.DestinationDirectory)}""");
commandLineList.Add($@"{prefix}{FormatPathArgument(filePath)}");
}
}

Expand All @@ -377,7 +374,7 @@ void WriteGeneratedFiles()

if (!IncludeAnalyzers)
{
commandLineList.Add($@"""{PathUtil.RemovePathStart(filePath, builder.DestinationDirectory)}""");
commandLineList.Add(FormatPathArgument(filePath));
}
}
}
Expand All @@ -402,5 +399,51 @@ void WriteResources()
commandLineList.Add(arg);
}
}

string FormatPathArgument(string filePath)
{
filePath = PathUtil.RemovePathStart(filePath, destinationDir);
return MaybeQuoteArgument(filePath);
}
}

public static void ExportRsp(CompilerCall compilerCall, TextWriter writer, bool singleLine = false) =>
ExportRsp(compilerCall.Arguments, writer, singleLine);

public static void ExportRsp(IEnumerable<string> arguments, TextWriter writer, bool singleLine = false)
{
bool isFirst = true;
foreach (var line in arguments)
{
var str = MaybeQuoteArgument(line);
if (singleLine)
{
if (!isFirst)
{
writer.Write(' ');
}
writer.Write(str);
}
else
{
writer.WriteLine(str);
}

isFirst = false;
}
}

private static string MaybeQuoteArgument(string arg)
{
if (arg.Contains(' ') && !IsOption(arg.AsSpan()))
{
var str = $@"""{arg}""";
return str;
}

return arg;
}

private static bool IsOption(ReadOnlySpan<char> str) =>
str.Length > 0 && str[0] is '-' or '/';
}
17 changes: 17 additions & 0 deletions src/Basic.CompilerLog.Util/Polyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal static bool StartsWith(this ReadOnlySpan<char> @this, string value, Str
internal static bool Contains(this string @this, string value, StringComparison comparisonType) =>
@this.IndexOf(value, comparisonType) >= 0;

internal static bool Contains(this ReadOnlySpan<char> @this, char value) =>
@this.IndexOf(value) >= 0;

internal static void ReadExactly(this Stream @this, Span<byte> buffer)
{
var bytes = new byte[1024];
Expand All @@ -62,6 +65,20 @@ internal static void ReadExactly(this Stream @this, Span<byte> buffer)
buffer = buffer.Slice(read);
}
}

internal static void Write(this TextWriter @this, ReadOnlySpan<char> buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
@this.Write(buffer[i]);
}
}

internal static void WriteLine(this TextWriter @this, ReadOnlySpan<char> buffer)
{
Write(@this, buffer);
@this.WriteLine();
}
}

#endif
Expand Down
14 changes: 2 additions & 12 deletions src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,7 @@ int RunResponseFile(IEnumerable<string> args)
Directory.CreateDirectory(rspDirPath);
var rspFilePath = Path.Combine(rspDirPath, "build.rsp");
using var writer = new StreamWriter(rspFilePath, append: false, Encoding.UTF8);
if (singleLine)
{
writer.WriteLine(string.Join(' ', compilerCall.Arguments));
}
else
{
foreach (var arg in compilerCall.Arguments)
{
writer.WriteLine(arg);
}
}
ExportUtil.ExportRsp(compilerCall, writer, singleLine);
}

return ExitSuccess;
Expand Down Expand Up @@ -518,7 +508,7 @@ complog [command] [args]
create Create a compilerlog file
diagnostics Print diagnostics for a compilation
export Export compilation contents, rsp and build files to disk
rsp Generate compiler response file for selected projects
rsp Generate compiler response file projects on this machine
ref Copy all references and analyzers to a single directory
emit Emit all binaries from the log
analyzers Print analyzers used by a compilation
Expand Down
4 changes: 2 additions & 2 deletions src/Basic.CompilerLog/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"profiles": {
"CompilerLogger": {
"commandName": "Project",
"commandLineArgs": "print -satellite",
"workingDirectory": "C:\\Users\\jaredpar\\code\\roslyn\\src\\Compilers\\Core\\Portable"
"commandLineArgs": "rsp C:\\Users\\jaredpar\\Downloads\\7.0.306.zip.binlog",
"workingDirectory": "C:\\users\\jaredpar\\temp"
}
}
}
2 changes: 2 additions & 0 deletions src/Scratch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

// await SolutionScratchAsync(filePath);

using var reader = CompilerLogReader.Create(@"C:\Users\jaredpar\Downloads\7.0.306.zip.binlog");

VerifyAll(filePath);
Console.WriteLine("Done");

Expand Down

0 comments on commit 5aee6c6

Please sign in to comment.