Skip to content

Instantly share code, notes, and snippets.

@jaredpar
Created September 24, 2020 22:06
Show Gist options
  • Select an option

  • Save jaredpar/25007091752c130ceeaf595b2d5e9c0b to your computer and use it in GitHub Desktop.

Select an option

Save jaredpar/25007091752c130ceeaf595b2d5e9c0b to your computer and use it in GitHub Desktop.
namespace rewrite_code
{
class Program
{
static void Main(string[] args)
{
foreach (var filePath in File.ReadAllLines(@"p:\temp\files.txt"))
{
if (filePath.Contains(@"artifacts\obj"))
{
continue;
}
if (filePath.Contains(@"Generated.cs") ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Program.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\ResultSetTracking\SymbolHoldingResultSetTracker.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Graph\LsifProject.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Graph\Item.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Graph\Moniker.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Generator.cs" ||
filePath == @"P:\roslyn3\src\Features\Lsif\Generator\Graph\Id.cs" ||
filePath == @"P:\roslyn3\src\Tools\Source\CompilerGeneratorTools\Source\IOperationGenerator\IOperationClassWriter.cs" ||
filePath == @"P:\roslyn3\src\Tools\Source\CompilerGeneratorTools\Source\IOperationGenerator\Model.cs" ||
filePath == @"P:\roslyn3\src\Tools\ExternalAccess\Razor\RazorDocumentServiceProviderWrapper.cs" ||
filePath == @"P:\roslyn3\src\Tools\ExternalAccess\Razor\IRazorDocumentPropertiesService.cs" ||
filePath == @"P:\roslyn3\src\Tools\ExternalAccess\Razor\RazorRemoteHostClient.cs" )
{
continue;
}
if (!File.Exists(filePath))
{
Console.WriteLine($"Missing {filePath}");
continue;
}
using var reader = new StreamReader(filePath, detectEncodingFromByteOrderMarks: true);
var foundNullable = false;
var lines = new List<string>();
var isLastLineBlank = false;
while (reader.ReadLine() is string line)
{
if (line.StartsWith("#nullable enable"))
{
foundNullable = true;
if (reader.ReadLine() is string nextLine)
{
// Avoid double blank line after rewrite
if (!isLastLineBlank || !string.IsNullOrWhiteSpace(nextLine))
{
lines.Add(nextLine);
}
}
break;
}
lines.Add(line);
if (line.StartsWith("namespace"))
{
break;
}
isLastLineBlank = string.IsNullOrWhiteSpace(line);
}
while (reader.ReadLine() is string line)
{
lines.Add(line);
}
reader.Close();
using var writer = new StreamWriter(filePath, append:false, reader.CurrentEncoding);
var handledNullable = false;
foreach (var line in lines)
{
if (!handledNullable && !line.StartsWith("//"))
{
if (!foundNullable)
{
writer.WriteLine("#nullable disable");
}
handledNullable = true;
}
if (line.StartsWith("#nullable restore"))
{
writer.WriteLine("#nullable disable");
}
else
{
writer.WriteLine(line);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment