Skip to content

Commit

Permalink
Extension metadata generation improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Aug 10, 2018
1 parent d87c8b0 commit 045ce79
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package-lock.json
**/Properties/launchSettings.json

/packages
tools/ExtensionsMetadataGenerator/packages
/TestResults

/tools/NuGet.exe
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionsMetadataGenerator
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExtensionsMetadataGenerator.Console", "src\ExtensionsMetadataGenerator.Console\ExtensionsMetadataGenerator.Console.csproj", "{932CEE19-3EB2-4747-BE86-1D2E0AE3A6D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionsMetadataGeneratorTests", "test\ExtensionsMetadataGeneratorTests\ExtensionsMetadataGeneratorTests.csproj", "{1AFA5065-27A2-4AEB-8AF0-E9DFE55CFAAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{932CEE19-3EB2-4747-BE86-1D2E0AE3A6D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{932CEE19-3EB2-4747-BE86-1D2E0AE3A6D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{932CEE19-3EB2-4747-BE86-1D2E0AE3A6D7}.Release|Any CPU.Build.0 = Release|Any CPU
{1AFA5065-27A2-4AEB-8AF0-E9DFE55CFAAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1AFA5065-27A2-4AEB-8AF0-E9DFE55CFAAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1AFA5065-27A2-4AEB-8AF0-E9DFE55CFAAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1AFA5065-27A2-4AEB-8AF0-E9DFE55CFAAD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -14,5 +14,4 @@
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator;
#if !NET46
using System.Runtime.Loader;
#endif
Expand All @@ -16,6 +15,8 @@ namespace ExtensionsMetadataGenerator
{
public class ExtensionsMetadataGenerator
{
private const string WebJobsStartupAttributeType = "Microsoft.Azure.WebJobs.Hosting.WebJobsStartupAttribute";

public static void Generate(string sourcePath, string outputPath, Action<string> logger)
{
if (!Directory.Exists(sourcePath))
Expand All @@ -33,26 +34,46 @@ public static void Generate(string sourcePath, string outputPath, Action<string>
try
{
Assembly assembly = Assembly.LoadFrom(path);

var extensions = assembly.GetExportedTypes()
.Where(t => t.IsExtensionType())
.Select(t => new ExtensionReference
{
Name = t.Name,
TypeName = t.AssemblyQualifiedName
});

extensionReferences.AddRange(extensions);
var currExtensionReferences = GenerateExtensionReferences(assembly);
extensionReferences.AddRange(extensionReferences);
}
catch (Exception exc)
{
logger(exc.Message ?? $"Errot processing {path}");
logger(exc.Message ?? $"Error processing {path}");
}
}

string json = GenerateExtensionsJson(extensionReferences);
File.WriteAllText(outputPath, json);
}

public static string GenerateExtensionsJson(IEnumerable<ExtensionReference> extensionReferences)
{
var referenceObjects = extensionReferences.Select(r => string.Format("{2} {{ \"name\": \"{0}\", \"typeName\":\"{1}\"}}", r.Name, r.TypeName, Environment.NewLine));
string metadataContents = string.Format("{{{1} \"extensions\":[{0}{1} ]{1}}}", string.Join(",", referenceObjects), Environment.NewLine);
File.WriteAllText(outputPath, metadataContents);
string json = string.Format("{{{1} \"extensions\":[{0}{1} ]{1}}}", string.Join(",", referenceObjects), Environment.NewLine);
return json;
}

public static IEnumerable<ExtensionReference> GenerateExtensionReferences(Assembly assembly)
{
var startupAttributes = assembly.GetCustomAttributes().Where(a => string.Equals(a.GetType().FullName, WebJobsStartupAttributeType, StringComparison.OrdinalIgnoreCase));

List<ExtensionReference> extensionReferences = new List<ExtensionReference>();
foreach (var attribute in startupAttributes)
{
var nameProperty = attribute.GetType().GetProperty("Name");
var typeProperty = attribute.GetType().GetProperty("WebJobsStartupType");

var extensionReference = new ExtensionReference
{
Name = (string)nameProperty.GetValue(attribute),
TypeName = ((Type)typeProperty.GetValue(attribute)).Assembly.FullName
};

extensionReferences.Add(extensionReference);
}

return extensionReferences;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Linq;
using Newtonsoft.Json.Linq;
using Xunit;

namespace ExtensionsMetadataGeneratorTests
{
public class ExtensionsMetadataGeneratorTests
{
[Fact]
public void GenerateExtensionReferences_Succeeds()
{
var assembly = GetType().Assembly;
var references = ExtensionsMetadataGenerator.ExtensionsMetadataGenerator.GenerateExtensionReferences(assembly).ToArray();
Assert.Equal(2, references.Length);

Assert.Equal("Foo", references[0].Name);
Assert.Equal(assembly.FullName, references[0].TypeName);

Assert.Equal("BarExtension", references[1].Name);
Assert.Equal(assembly.FullName, references[1].TypeName);
}

[Fact]
public void GenerateExtensionsJson_Succeeds()
{
var assembly = GetType().Assembly;
var references = ExtensionsMetadataGenerator.ExtensionsMetadataGenerator.GenerateExtensionReferences(assembly).ToArray();
string json = ExtensionsMetadataGenerator.ExtensionsMetadataGenerator.GenerateExtensionsJson(references);

var root = JObject.Parse(json);
var extensions = root["extensions"];

Assert.Equal("Foo", extensions[0]["name"]);
Assert.Equal(assembly.FullName, extensions[0]["typeName"]);

Assert.Equal("BarExtension", extensions[1]["name"]);
Assert.Equal(assembly.FullName, extensions[1]["typeName"]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta7-11404" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ExtensionsMetadataGenerator.Console\ExtensionsMetadataGenerator.Console.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ExtensionsMetadataGeneratorTests;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Hosting;

[assembly: WebJobsStartup(typeof(FooWebJobsStartup))]
[assembly: WebJobsStartup(typeof(BarWebJobsStartup), "BarExtension")]

namespace ExtensionsMetadataGeneratorTests
{
public class FooWebJobsStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
}
}

public class BarWebJobsStartup : IWebJobsStartup
{
public void Configure(IWebJobsBuilder builder)
{
}
}
}

0 comments on commit 045ce79

Please sign in to comment.