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
+30
-37
No files matched your search
@@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -118,9 +117,8 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
|||||||
: templateName;
|
: templateName;
|
||||||
|
|
||||||
SourceText fileText = templateSyntax.SyntaxTree.GetText(context.CancellationToken);
|
SourceText fileText = templateSyntax.SyntaxTree.GetText(context.CancellationToken);
|
||||||
string filePath = templateSyntax.SyntaxTree.FilePath;
|
|
||||||
CSharpParseOptions baseParseOptions = (CSharpParseOptions)templateSyntax.SyntaxTree.Options;
|
CSharpParseOptions baseParseOptions = (CSharpParseOptions)templateSyntax.SyntaxTree.Options;
|
||||||
SyntaxTree originalTree = templateSyntax.SyntaxTree;
|
string filePath = templateSyntax.SyntaxTree.FilePath;
|
||||||
|
|
||||||
foreach (TemplateVariant variant in template.ExpandVariants())
|
foreach (TemplateVariant variant in template.ExpandVariants())
|
||||||
{
|
{
|
||||||
@@ -159,7 +157,7 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
|||||||
generatedRoot = generatedRoot.NormalizeWhitespace();
|
generatedRoot = generatedRoot.NormalizeWhitespace();
|
||||||
|
|
||||||
string generatedFileName = baseName + "." + variant.FileSuffix + template.NameSuffix + ".generated.cs";
|
string generatedFileName = baseName + "." + variant.FileSuffix + template.NameSuffix + ".generated.cs";
|
||||||
string hintName = options.BuildHintName(filePath, generatedFileName);
|
string hintName = options.BuildHintName(template.ContainingNamespace, generatedFileName);
|
||||||
context.AddSource(
|
context.AddSource(
|
||||||
hintName,
|
hintName,
|
||||||
SourceText.From("// <auto-generated/>\n#nullable enable\n" + generatedRoot.ToFullString(), Encoding.UTF8));
|
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 readonly struct GeneratorOptions
|
||||||
{
|
{
|
||||||
|
private const string GeneratedDirectoryName = "generated";
|
||||||
|
private const string GeneratedRootOptionName = "build_property.DeviceDependentGenerator_OutputRoot";
|
||||||
|
|
||||||
private readonly AnalyzerConfigOptions globalOptions;
|
private readonly AnalyzerConfigOptions globalOptions;
|
||||||
|
|
||||||
public GeneratorOptions(AnalyzerConfigOptions globalOptions)
|
public GeneratorOptions(AnalyzerConfigOptions globalOptions)
|
||||||
@@ -613,51 +614,38 @@ public sealed class DeviceDependentGenerator : IIncrementalGenerator
|
|||||||
this.globalOptions = globalOptions;
|
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);
|
string namespacePath = string.IsNullOrWhiteSpace(containingNamespace)
|
||||||
if (string.IsNullOrWhiteSpace(templateDirectory))
|
? string.Empty
|
||||||
|
: containingNamespace.Replace('.', '/').Trim('/');
|
||||||
|
|
||||||
|
if (namespacePath.Length == 0)
|
||||||
{
|
{
|
||||||
return generatedFileName;
|
return trimmedRoot + "/" + generatedFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
string relativeDirectory = GetRelativePath(projectDir, templateDirectory);
|
return trimmedRoot + "/" + namespacePath + "/" + generatedFileName;
|
||||||
if (relativeDirectory is "." or "")
|
|
||||||
{
|
|
||||||
return generatedFileName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (relativeDirectory.StartsWith("..", StringComparison.Ordinal))
|
private string GetGeneratedRoot()
|
||||||
{
|
{
|
||||||
return generatedFileName;
|
if (globalOptions.TryGetValue(GeneratedRootOptionName, out string? configuredRoot)
|
||||||
|
&& !string.IsNullOrWhiteSpace(configuredRoot))
|
||||||
|
{
|
||||||
|
return configuredRoot;
|
||||||
}
|
}
|
||||||
|
|
||||||
relativeDirectory = relativeDirectory.Replace('\\', '/');
|
return GeneratedDirectoryName;
|
||||||
return relativeDirectory.TrimEnd('/') + "/" + generatedFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetRelativePath(string baseDirectory, string targetDirectory)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(baseDirectory) || string.IsNullOrWhiteSpace(targetDirectory))
|
|
||||||
{
|
|
||||||
return targetDirectory;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,4 +46,9 @@
|
|||||||
SkipUnchangedFiles="true" />
|
SkipUnchangedFiles="true" />
|
||||||
</Target>
|
</Target>
|
||||||
|
|
||||||
|
<Target Name="CleanDeviceDependentGeneratedFromProjectDir"
|
||||||
|
AfterTargets="Clean">
|
||||||
|
<RemoveDir Directories="$(ProjectDir)generated" />
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
Reference in new issue
Block a user