From 2dfe06de836b0fd61133c914168ffcf59e66bb82 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Sun, 13 May 2018 13:25:52 +0200 Subject: [PATCH 1/6] Added Test for external dependencys --- ....Roslyn.Tests.Generators.Dependency.csproj | 8 +++ .../NameGenerator.cs | 9 ++++ ...eGeneration.Roslyn.Tests.Generators.csproj | 1 + ...ernalDuplicateWithSuffixByNameAttribute.cs | 24 +++++++++ .../ExternalDuplicateWithSuffixGenerator.cs | 54 +++++++++++++++++++ .../CodeGeneration.Roslyn.Tests.csproj | 1 + .../CodeGenerationTests.cs | 17 +++++- src/CodeGeneration.Roslyn.sln | 9 ++++ 8 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 src/CodeGeneration.Roslyn.Tests.Generators.Dependency/CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj create mode 100644 src/CodeGeneration.Roslyn.Tests.Generators.Dependency/NameGenerator.cs create mode 100644 src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixByNameAttribute.cs create mode 100644 src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixGenerator.cs diff --git a/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj b/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj new file mode 100644 index 000000000..46875e931 --- /dev/null +++ b/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj @@ -0,0 +1,8 @@ + + + + netstandard1.6 + $(PackageTargetFallback);portable-net45+win8+wp8+wpa81; + + + diff --git a/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/NameGenerator.cs b/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/NameGenerator.cs new file mode 100644 index 000000000..da8c5a12a --- /dev/null +++ b/src/CodeGeneration.Roslyn.Tests.Generators.Dependency/NameGenerator.cs @@ -0,0 +1,9 @@ +using System; + +namespace CodeGeneration.Roslyn.Tests.Generators.Dependency +{ + public class NameGenerator + { + public static string Combine(string s1, string s2) => s1 + s2; + } +} diff --git a/src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj b/src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj index 74d38dc05..c47c37262 100644 --- a/src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj +++ b/src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj @@ -7,6 +7,7 @@ + diff --git a/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixByNameAttribute.cs b/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixByNameAttribute.cs new file mode 100644 index 000000000..b7044a0df --- /dev/null +++ b/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixByNameAttribute.cs @@ -0,0 +1,24 @@ +// Copyright (c) Andrew Arnott. All rights reserved. +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. + +namespace CodeGeneration.Roslyn.Tests.Generators +{ + using System; + using System.Diagnostics; + using Validation; + + [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] + [CodeGenerationAttribute("CodeGeneration.Roslyn.Tests.Generators.ExternalDuplicateWithSuffixGenerator, CodeGeneration.Roslyn.Tests.Generators, Version=" + ThisAssembly.AssemblyVersion + ", Culture=neutral, PublicKeyToken=null")] + [Conditional("CodeGeneration")] + public class ExternalDuplicateWithSuffixByNameAttribute : Attribute + { + public ExternalDuplicateWithSuffixByNameAttribute(string suffix) + { + Requires.NotNullOrEmpty(suffix, nameof(suffix)); + + this.Suffix = suffix; + } + + public string Suffix { get; } + } +} diff --git a/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixGenerator.cs b/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixGenerator.cs new file mode 100644 index 000000000..aae5b7a62 --- /dev/null +++ b/src/CodeGeneration.Roslyn.Tests.Generators/ExternalDuplicateWithSuffixGenerator.cs @@ -0,0 +1,54 @@ +// Copyright (c) Andrew Arnott. All rights reserved. +// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information. + +namespace CodeGeneration.Roslyn.Tests.Generators +{ + using System; + using System.Collections.Generic; + using System.Collections.Immutable; + using System.Linq; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using CodeGeneration.Roslyn.Tests.Generators.Dependency; + using Microsoft.CodeAnalysis; + using Microsoft.CodeAnalysis.CSharp; + using Microsoft.CodeAnalysis.CSharp.Syntax; + using Validation; + + public class ExternalDuplicateWithSuffixGenerator : ICodeGenerator + { + private readonly AttributeData attributeData; + private readonly ImmutableDictionary data; + private readonly string suffix; + + public ExternalDuplicateWithSuffixGenerator(AttributeData attributeData) + { + Requires.NotNull(attributeData, nameof(attributeData)); + + this.suffix = (string)attributeData.ConstructorArguments[0].Value; + this.attributeData = attributeData; + this.data = this.attributeData.NamedArguments.ToImmutableDictionary(kv => kv.Key, kv => kv.Value); + } + + public Task> GenerateAsync(TransformationContext context, IProgress progress, CancellationToken cancellationToken) + { + var results = SyntaxFactory.List(); + + MemberDeclarationSyntax copy = null; + var applyToClass = context.ProcessingNode as MethodDeclarationSyntax; + if (applyToClass != null) + { + copy = applyToClass + .WithIdentifier(SyntaxFactory.Identifier(NameGenerator.Combine(applyToClass.Identifier.ValueText, this.suffix))); + } + + if (copy != null) + { + results = results.Add(copy); + } + + return Task.FromResult(results); + } + } +} diff --git a/src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj b/src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj index 4673c247c..d4a42979a 100644 --- a/src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj +++ b/src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj @@ -14,6 +14,7 @@ + diff --git a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs index 68e21bfc1..5a3e644e7 100644 --- a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs +++ b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs @@ -23,12 +23,26 @@ public void SimpleGenerationWorks() Assert.EndsWith(@"src\CodeGeneration.Roslyn.Tests", DirectoryPathTest.Path, StringComparison.OrdinalIgnoreCase); } + [Fact] + public void ExternalDependencyFound() + { + dynamic d = new Wraper(); + d.TestMethdoSuffix(); + } + + public partial class Wraper + { + [ExternalDuplicateWithSuffixByName("Suffix")] + public void TestMethdo() { } + } + + [DuplicateWithSuffixByName("A")] [DuplicateWithSuffixByType("B")] public class Foo { } - + [MultiplySuffix] public partial class MultipliedBar { @@ -36,3 +50,4 @@ public partial class MultipliedBar public string Value { get; set; } } } + diff --git a/src/CodeGeneration.Roslyn.sln b/src/CodeGeneration.Roslyn.sln index 52015eec7..19b8e0ffa 100644 --- a/src/CodeGeneration.Roslyn.sln +++ b/src/CodeGeneration.Roslyn.sln @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeGeneration.Roslyn.Tests EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeGeneration.Roslyn.Tool", "CodeGeneration.Roslyn.Tool\CodeGeneration.Roslyn.Tool.csproj", "{23424A10-0ED4-4906-9CF7-6D34E3F871E2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeGeneration.Roslyn.Tests.Generators.Dependency", "CodeGeneration.Roslyn.Tests.Generators.Dependency\CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj", "{3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -63,8 +65,15 @@ Global {23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Debug|Any CPU.Build.0 = Debug|Any CPU {23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Release|Any CPU.ActiveCfg = Release|Any CPU {23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Release|Any CPU.Build.0 = Release|Any CPU + {3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {4F15606A-A60D-4F68-828C-56A6D217B04C} + EndGlobalSection EndGlobal From b98b69499dc2114fd037bb5cd7b67805cc112ddc Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Sun, 13 May 2018 13:27:00 +0200 Subject: [PATCH 2/6] Fix not filed cache Added AppBaseCompilationAssemblyResolver more then once because of missing add cache --- src/CodeGeneration.Roslyn/CompilationGenerator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/CodeGeneration.Roslyn/CompilationGenerator.cs b/src/CodeGeneration.Roslyn/CompilationGenerator.cs index 3c88f2ab2..9582127e6 100644 --- a/src/CodeGeneration.Roslyn/CompilationGenerator.cs +++ b/src/CodeGeneration.Roslyn/CompilationGenerator.cs @@ -193,6 +193,7 @@ protected virtual Assembly LoadAssembly(string path) new AppBaseCompilationAssemblyResolver(basePath), this.assemblyResolver }); + this.directoriesWithResolver.Add(basePath); } this.assembliesByPath.Add(path, assembly); From 6e9925cf495bc47759b11f5a8da5eb1d4abeca31 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Sun, 13 May 2018 17:30:49 +0200 Subject: [PATCH 3/6] Support dlls that are missing dependencyContext --- src/CodeGeneration.Roslyn/CompilationGenerator.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/CodeGeneration.Roslyn/CompilationGenerator.cs b/src/CodeGeneration.Roslyn/CompilationGenerator.cs index 9582127e6..3874e8670 100644 --- a/src/CodeGeneration.Roslyn/CompilationGenerator.cs +++ b/src/CodeGeneration.Roslyn/CompilationGenerator.cs @@ -184,7 +184,9 @@ protected virtual Assembly LoadAssembly(string path) var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly); var assembly = loadContext.LoadFromAssemblyPath(path); - this.dependencyContext = this.dependencyContext.Merge(DependencyContext.Load(assembly)); + var newDependencyContext = DependencyContext.Load(assembly); + if(newDependencyContext!=null) + this.dependencyContext = this.dependencyContext.Merge(newDependencyContext); var basePath = Path.GetDirectoryName(path); if (!this.directoriesWithResolver.Contains(basePath)) { From 3af5be4b64ce5c1bac51fd7bbbc238766eb16659 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Mon, 14 May 2018 07:38:30 +0200 Subject: [PATCH 4/6] Fix Typo and whitespaces --- src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs | 2 +- src/CodeGeneration.Roslyn/CompilationGenerator.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs index 5a3e644e7..76b429360 100644 --- a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs +++ b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs @@ -33,7 +33,7 @@ public void ExternalDependencyFound() public partial class Wraper { [ExternalDuplicateWithSuffixByName("Suffix")] - public void TestMethdo() { } + public void TestMethod() { } } diff --git a/src/CodeGeneration.Roslyn/CompilationGenerator.cs b/src/CodeGeneration.Roslyn/CompilationGenerator.cs index 3874e8670..2d73b8fc4 100644 --- a/src/CodeGeneration.Roslyn/CompilationGenerator.cs +++ b/src/CodeGeneration.Roslyn/CompilationGenerator.cs @@ -185,7 +185,7 @@ protected virtual Assembly LoadAssembly(string path) var assembly = loadContext.LoadFromAssemblyPath(path); var newDependencyContext = DependencyContext.Load(assembly); - if(newDependencyContext!=null) + if (newDependencyContext != null) this.dependencyContext = this.dependencyContext.Merge(newDependencyContext); var basePath = Path.GetDirectoryName(path); if (!this.directoriesWithResolver.Contains(basePath)) From 73b131bc8915293ce3209d2b3650a3a02f5f7c37 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Mon, 14 May 2018 18:41:21 +0200 Subject: [PATCH 5/6] Fix Typo --- src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs index 76b429360..897eb02a9 100644 --- a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs +++ b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs @@ -30,7 +30,7 @@ public void ExternalDependencyFound() d.TestMethdoSuffix(); } - public partial class Wraper + public partial class Wrapper { [ExternalDuplicateWithSuffixByName("Suffix")] public void TestMethod() { } From 768da6a85a184d283f758d41d715b91a20138410 Mon Sep 17 00:00:00 2001 From: Patrick Kranz Date: Mon, 14 May 2018 19:29:09 +0200 Subject: [PATCH 6/6] (-_ -;) Fixed Build errors --- src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs index 897eb02a9..b4984ceab 100644 --- a/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs +++ b/src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs @@ -26,8 +26,8 @@ public void SimpleGenerationWorks() [Fact] public void ExternalDependencyFound() { - dynamic d = new Wraper(); - d.TestMethdoSuffix(); + dynamic d = new Wrapper(); + d.TestMethodSuffix(); } public partial class Wrapper