DeviceDependentGenerator modified to create all classes in "generated" folder and clean up generated files during dotnet clean process.
This commit is contained in:
1 parent
75a2017385
commit
3342ead2d4
2 files changed
+29
-36
No files matched your search
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
@@ -118,9 +117,8 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
: templateName;
|
||||
|
||||
SourceText fileText = templateSyntax.SyntaxTree.GetText(context.CancellationToken);
|
||||
string filePath = templateSyntax.SyntaxTree.FilePath;
|
||||
CSharpParseOptions baseParseOptions = (CSharpParseOptions)templateSyntax.SyntaxTree.Options;
|
||||
SyntaxTree originalTree = templateSyntax.SyntaxTree;
|
||||
string filePath = templateSyntax.SyntaxTree.FilePath;
|
||||
|
||||
foreach (TemplateVariant variant in template.ExpandVariants())
|
||||
{
|
||||
@@ -159,7 +157,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
generatedRoot = generatedRoot.NormalizeWhitespace();
|
||||
|
||||
string generatedFileName = baseName + "." + variant.FileSuffix + template.NameSuffix + ".generated.cs";
|
||||
string hintName = options.BuildHintName(filePath, generatedFileName);
|
||||
string hintName = options.BuildHintName(template.ContainingNamespace, generatedFileName);
|
||||
context.AddSource(
|
||||
hintName,
|
||||
SourceText.From("// <auto-generated/>\n#nullable enable\n" + generatedRoot.ToFullString(), Encoding.UTF8));
|
||||
@@ -606,6 +604,9 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
|
||||
private readonly struct GeneratorOptions
|
||||
{
|
||||
private const string GeneratedDirectoryName = "generated";
|
||||
private const string GeneratedRootOptionName = "build_property.DeviceDependentGenerator_OutputRoot";
|
||||
|
||||
private readonly AnalyzerConfigOptions globalOptions;
|
||||
|
||||
public GeneratorOptions(AnalyzerConfigOptions globalOptions)
|
||||
@@ -613,51 +614,38 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
||||
this.globalOptions = globalOptions;
|
||||
}
|
||||
|
||||
public string BuildHintName(string templateFilePath, string generatedFileName)
|
||||
public string BuildHintName(string containingNamespace, string generatedFileName)
|
||||
{
|
||||
if (!globalOptions.TryGetValue("build_property.ProjectDir", out string? projectDir) || string.IsNullOrWhiteSpace(projectDir))
|
||||
string root = GetGeneratedRoot();
|
||||
string trimmedRoot = string.IsNullOrWhiteSpace(root)
|
||||
? GeneratedDirectoryName
|
||||
: root.Replace('\\', '/').Trim('/').Trim();
|
||||
if (string.IsNullOrWhiteSpace(trimmedRoot))
|
||||
{
|
||||
return generatedFileName;
|
||||
trimmedRoot = GeneratedDirectoryName;
|
||||
}
|
||||
|
||||
string? templateDirectory = Path.GetDirectoryName(templateFilePath);
|
||||
if (string.IsNullOrWhiteSpace(templateDirectory))
|
||||
string namespacePath = string.IsNullOrWhiteSpace(containingNamespace)
|
||||
? string.Empty
|
||||
: containingNamespace.Replace('.', '/').Trim('/');
|
||||
|
||||
if (namespacePath.Length == 0)
|
||||
{
|
||||
return generatedFileName;
|
||||
return trimmedRoot + "/" + generatedFileName;
|
||||
}
|
||||
|
||||
string relativeDirectory = GetRelativePath(projectDir, templateDirectory);
|
||||
if (relativeDirectory is "." or "")
|
||||
{
|
||||
return generatedFileName;
|
||||
}
|
||||
|
||||
if (relativeDirectory.StartsWith("..", StringComparison.Ordinal))
|
||||
{
|
||||
return generatedFileName;
|
||||
}
|
||||
|
||||
relativeDirectory = relativeDirectory.Replace('\\', '/');
|
||||
return relativeDirectory.TrimEnd('/') + "/" + generatedFileName;
|
||||
return trimmedRoot + "/" + namespacePath + "/" + generatedFileName;
|
||||
}
|
||||
|
||||
private static string GetRelativePath(string baseDirectory, string targetDirectory)
|
||||
private string GetGeneratedRoot()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(baseDirectory) || string.IsNullOrWhiteSpace(targetDirectory))
|
||||
if (globalOptions.TryGetValue(GeneratedRootOptionName, out string? configuredRoot)
|
||||
&& !string.IsNullOrWhiteSpace(configuredRoot))
|
||||
{
|
||||
return targetDirectory;
|
||||
return configuredRoot;
|
||||
}
|
||||
|
||||
if (!baseDirectory.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)
|
||||
&& !baseDirectory.EndsWith(Path.AltDirectorySeparatorChar.ToString(), StringComparison.Ordinal))
|
||||
{
|
||||
baseDirectory += Path.DirectorySeparatorChar;
|
||||
}
|
||||
|
||||
var baseUri = new Uri(baseDirectory, UriKind.Absolute);
|
||||
var targetUri = new Uri(targetDirectory, UriKind.Absolute);
|
||||
string relative = Uri.UnescapeDataString(baseUri.MakeRelativeUri(targetUri).ToString());
|
||||
return relative.Replace('/', Path.DirectorySeparatorChar);
|
||||
return GeneratedDirectoryName;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,4 +46,9 @@
|
||||
SkipUnchangedFiles="true" />
|
||||
</Target>
|
||||
|
||||
<Target Name="CleanDeviceDependentGeneratedFromProjectDir"
|
||||
AfterTargets="Clean">
|
||||
<RemoveDir Directories="$(ProjectDir)generated" />
|
||||
</Target>
|
||||
|
||||
</Project>
|
||||
Reference in new issue
Block a user