Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit c539352

Browse files
authored
Merge pull request #73 from LokiMidgard/ExternalDependency
External dependency
2 parents d282404 + 768da6a commit c539352

File tree

9 files changed

+126
-2
lines changed

9 files changed

+126
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard1.6</TargetFramework>
5+
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace CodeGeneration.Roslyn.Tests.Generators.Dependency
4+
{
5+
public class NameGenerator
6+
{
7+
public static string Combine(string s1, string s2) => s1 + s2;
8+
}
9+
}

src/CodeGeneration.Roslyn.Tests.Generators/CodeGeneration.Roslyn.Tests.Generators.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<ItemGroup>
99
<ProjectReference Include="..\CodeGeneration.Roslyn.Attributes\CodeGeneration.Roslyn.Attributes.csproj" />
10+
<ProjectReference Include="..\CodeGeneration.Roslyn.Tests.Generators.Dependency\CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj" />
1011
<ProjectReference Include="..\CodeGeneration.Roslyn\CodeGeneration.Roslyn.csproj" />
1112
</ItemGroup>
1213
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Diagnostics;
8+
using Validation;
9+
10+
[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
11+
[CodeGenerationAttribute("CodeGeneration.Roslyn.Tests.Generators.ExternalDuplicateWithSuffixGenerator, CodeGeneration.Roslyn.Tests.Generators, Version=" + ThisAssembly.AssemblyVersion + ", Culture=neutral, PublicKeyToken=null")]
12+
[Conditional("CodeGeneration")]
13+
public class ExternalDuplicateWithSuffixByNameAttribute : Attribute
14+
{
15+
public ExternalDuplicateWithSuffixByNameAttribute(string suffix)
16+
{
17+
Requires.NotNullOrEmpty(suffix, nameof(suffix));
18+
19+
this.Suffix = suffix;
20+
}
21+
22+
public string Suffix { get; }
23+
}
24+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) Andrew Arnott. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
3+
4+
namespace CodeGeneration.Roslyn.Tests.Generators
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Collections.Immutable;
9+
using System.Linq;
10+
using System.Text;
11+
using System.Threading;
12+
using System.Threading.Tasks;
13+
using CodeGeneration.Roslyn.Tests.Generators.Dependency;
14+
using Microsoft.CodeAnalysis;
15+
using Microsoft.CodeAnalysis.CSharp;
16+
using Microsoft.CodeAnalysis.CSharp.Syntax;
17+
using Validation;
18+
19+
public class ExternalDuplicateWithSuffixGenerator : ICodeGenerator
20+
{
21+
private readonly AttributeData attributeData;
22+
private readonly ImmutableDictionary<string, TypedConstant> data;
23+
private readonly string suffix;
24+
25+
public ExternalDuplicateWithSuffixGenerator(AttributeData attributeData)
26+
{
27+
Requires.NotNull(attributeData, nameof(attributeData));
28+
29+
this.suffix = (string)attributeData.ConstructorArguments[0].Value;
30+
this.attributeData = attributeData;
31+
this.data = this.attributeData.NamedArguments.ToImmutableDictionary(kv => kv.Key, kv => kv.Value);
32+
}
33+
34+
public Task<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
35+
{
36+
var results = SyntaxFactory.List<MemberDeclarationSyntax>();
37+
38+
MemberDeclarationSyntax copy = null;
39+
var applyToClass = context.ProcessingNode as MethodDeclarationSyntax;
40+
if (applyToClass != null)
41+
{
42+
copy = applyToClass
43+
.WithIdentifier(SyntaxFactory.Identifier(NameGenerator.Combine(applyToClass.Identifier.ValueText, this.suffix)));
44+
}
45+
46+
if (copy != null)
47+
{
48+
results = results.Add(copy);
49+
}
50+
51+
return Task.FromResult(results);
52+
}
53+
}
54+
}

src/CodeGeneration.Roslyn.Tests/CodeGeneration.Roslyn.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17+
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
1718
<PackageReference Include="Xunit" Version="2.2.0" />
1819
<PackageReference Include="Xunit.runner.visualstudio" Version="2.2.0" />
1920
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />

src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,31 @@ public void SimpleGenerationWorks()
2323
Assert.EndsWith(@"src\CodeGeneration.Roslyn.Tests", DirectoryPathTest.Path, StringComparison.OrdinalIgnoreCase);
2424
}
2525

26+
[Fact]
27+
public void ExternalDependencyFound()
28+
{
29+
dynamic d = new Wrapper();
30+
d.TestMethodSuffix();
31+
}
32+
33+
public partial class Wrapper
34+
{
35+
[ExternalDuplicateWithSuffixByName("Suffix")]
36+
public void TestMethod() { }
37+
}
38+
39+
2640
[DuplicateWithSuffixByName("A")]
2741
[DuplicateWithSuffixByType("B")]
2842
public class Foo
2943
{
3044
}
31-
45+
3246
[MultiplySuffix]
3347
public partial class MultipliedBar
3448
{
3549
[Test(X = 10, Y = 20)]
3650
public string Value { get; set; }
3751
}
3852
}
53+

src/CodeGeneration.Roslyn.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeGeneration.Roslyn.Tests
2929
EndProject
3030
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeGeneration.Roslyn.Tool", "CodeGeneration.Roslyn.Tool\CodeGeneration.Roslyn.Tool.csproj", "{23424A10-0ED4-4906-9CF7-6D34E3F871E2}"
3131
EndProject
32+
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}"
33+
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3436
Debug|Any CPU = Debug|Any CPU
@@ -63,8 +65,15 @@ Global
6365
{23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
6466
{23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
6567
{23424A10-0ED4-4906-9CF7-6D34E3F871E2}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{3F88FB1C-AAAE-4D3E-B816-220CDF096E1A}.Release|Any CPU.Build.0 = Release|Any CPU
6672
EndGlobalSection
6773
GlobalSection(SolutionProperties) = preSolution
6874
HideSolutionNode = FALSE
6975
EndGlobalSection
76+
GlobalSection(ExtensibilityGlobals) = postSolution
77+
SolutionGuid = {4F15606A-A60D-4F68-828C-56A6D217B04C}
78+
EndGlobalSection
7079
EndGlobal

src/CodeGeneration.Roslyn/CompilationGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,9 @@ protected virtual Assembly LoadAssembly(string path)
184184
var loadContext = AssemblyLoadContext.GetLoadContext(this.GetType().GetTypeInfo().Assembly);
185185
var assembly = loadContext.LoadFromAssemblyPath(path);
186186

187-
this.dependencyContext = this.dependencyContext.Merge(DependencyContext.Load(assembly));
187+
var newDependencyContext = DependencyContext.Load(assembly);
188+
if (newDependencyContext != null)
189+
this.dependencyContext = this.dependencyContext.Merge(newDependencyContext);
188190
var basePath = Path.GetDirectoryName(path);
189191
if (!this.directoriesWithResolver.Contains(basePath))
190192
{
@@ -193,6 +195,7 @@ protected virtual Assembly LoadAssembly(string path)
193195
new AppBaseCompilationAssemblyResolver(basePath),
194196
this.assemblyResolver
195197
});
198+
this.directoriesWithResolver.Add(basePath);
196199
}
197200

198201
this.assembliesByPath.Add(path, assembly);

0 commit comments

Comments
 (0)