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

Commit

Permalink
Merge pull request #73 from LokiMidgard/ExternalDependency
Browse files Browse the repository at this point in the history
External dependency
  • Loading branch information
AArnott authored May 14, 2018
2 parents d282404 + 768da6a commit c539352
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<ProjectReference Include="..\CodeGeneration.Roslyn.Attributes\CodeGeneration.Roslyn.Attributes.csproj" />
<ProjectReference Include="..\CodeGeneration.Roslyn.Tests.Generators.Dependency\CodeGeneration.Roslyn.Tests.Generators.Dependency.csproj" />
<ProjectReference Include="..\CodeGeneration.Roslyn\CodeGeneration.Roslyn.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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<string, TypedConstant> 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<SyntaxList<MemberDeclarationSyntax>> GenerateAsync(TransformationContext context, IProgress<Diagnostic> progress, CancellationToken cancellationToken)
{
var results = SyntaxFactory.List<MemberDeclarationSyntax>();

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="Xunit" Version="2.2.0" />
<PackageReference Include="Xunit.runner.visualstudio" Version="2.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
Expand Down
17 changes: 16 additions & 1 deletion src/CodeGeneration.Roslyn.Tests/CodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,31 @@ public void SimpleGenerationWorks()
Assert.EndsWith(@"src\CodeGeneration.Roslyn.Tests", DirectoryPathTest.Path, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public void ExternalDependencyFound()
{
dynamic d = new Wrapper();
d.TestMethodSuffix();
}

public partial class Wrapper
{
[ExternalDuplicateWithSuffixByName("Suffix")]
public void TestMethod() { }
}


[DuplicateWithSuffixByName("A")]
[DuplicateWithSuffixByType("B")]
public class Foo
{
}

[MultiplySuffix]
public partial class MultipliedBar
{
[Test(X = 10, Y = 20)]
public string Value { get; set; }
}
}

9 changes: 9 additions & 0 deletions src/CodeGeneration.Roslyn.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion src/CodeGeneration.Roslyn/CompilationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -193,6 +195,7 @@ protected virtual Assembly LoadAssembly(string path)
new AppBaseCompilationAssemblyResolver(basePath),
this.assemblyResolver
});
this.directoriesWithResolver.Add(basePath);
}

this.assembliesByPath.Add(path, assembly);
Expand Down

0 comments on commit c539352

Please sign in to comment.