Created
July 16, 2020 09:36
-
-
Save benmccallum/b170426f05ad88fa8973c69c756102fd to your computer and use it in GitHub Desktop.
Disable nullable reference types in all files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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