in src/PublicToInternalGenerator/PublicToInternalGenerator.cs [36:71]
private static (string fileName, string sourceText)? ProcessFile(
AdditionalText file, CancellationToken cancellationToken)
{
var sourceText = file.GetText(cancellationToken);
if (sourceText == null) return null;
var syntaxTree = CSharpSyntaxTree.ParseText(sourceText, cancellationToken: cancellationToken);
var root = syntaxTree.GetCompilationUnitRoot(cancellationToken);
var rewriter = new PublicToInternalRewriter();
var newRoot = rewriter.Visit(root);
if (newRoot == root) return null; // only generate if changes were made
var sourceCode = newRoot.GetText(Encoding.UTF8).ToString();
sourceCode = "// <auto-generated />\r\n" + sourceCode;
const string nullableDisableDirective = "#nullable disable";
if (!sourceCode.Contains(nullableDisableDirective))
throw new Exception("Nullability directive expected!");
sourceCode = sourceCode.Replace(
nullableDisableDirective,
$"#if NETCOREAPP3_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER\r\n{nullableDisableDirective}\r\n#endif");
// when declared as 'partial' it should not clash with the user's Embedded attribute declaration
sourceCode += "\r\nnamespace Microsoft.CodeAnalysis\r\n{\r\n " +
"internal sealed partial class EmbeddedAttribute : global::System.Attribute { }\r\n}";
var fileName = Path.GetFileNameWithoutExtension(file.Path);
var newFileName = $"{fileName}.Internal.cs";
return (newFileName, sourceCode);
}