Skip to content

Instantly share code, notes, and snippets.

@benmccallum
Created July 16, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benmccallum/b170426f05ad88fa8973c69c756102fd to your computer and use it in GitHub Desktop.
Save benmccallum/b170426f05ad88fa8973c69c756102fd to your computer and use it in GitHub Desktop.
Disable nullable reference types in all files
const string sourcePath = @"C:\src\autoguru\src\Microservices";
const string enabledHeader = "#nullable enable";
const string disabledHeader = "#nullable disable";
var files = Directory.GetFiles(sourcePath, "*.cs", SearchOption.AllDirectories);
var projFiles = Directory.GetFiles(sourcePath, "*.csproj", SearchOption.AllDirectories);
foreach (var file in files)
{
var contents = File.ReadAllText(file);
if (contents.StartsWith(enabledHeader))
{
// remove as we'll be setting enabled at project level
File.WriteAllText(file, contents.Substring(enabledHeader.Length).TrimStart(), System.Text.Encoding.UTF8);
//Console.WriteLine($"Enable header removed from: {file}.");
}
else if (contents.StartsWith(disabledHeader))
{
// skip
}
else
{
// add disabled header on files we'll migrate over as time allows
File.WriteAllText(file, $"{disabledHeader}{Environment.NewLine}{Environment.NewLine}{contents}", System.Text.Encoding.UTF8);
//Console.WriteLine($"Disable header added to: {file}.");
}
}
const string projEnable = "<Nullable>enable</Nullable>";
foreach (var projFile in projFiles)
{
var contents = File.ReadAllText(projFile);
if (!contents.Contains(projEnable))
{
Console.WriteLine($"Please add {projEnable} into: {projFile}.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment