This commit is contained in:
Ahmed ElSayed 2017-03-21 14:46:09 -07:00
Родитель 4402f0da0c
Коммит 218fc63935
16 изменённых файлов: 644 добавлений и 0 удалений

36
MakeFunctionJson.sln Normal file
Просмотреть файл

@ -0,0 +1,36 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{14D6456E-2F9D-4483-A378-03701A6EB12D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{9B6D0171-3FFD-4892-B407-B633CA4E6712}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MakeFunctionJson", "src\MakeFunctionJson\MakeFunctionJson.csproj", "{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runner", "Runner\Runner.csproj", "{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}.Release|Any CPU.Build.0 = Release|Any CPU
{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}.Debug|Any CPU.Build.0 = Debug|Any CPU
{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}.Release|Any CPU.ActiveCfg = Release|Any CPU
{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96} = {14D6456E-2F9D-4483-A378-03701A6EB12D}
{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724} = {9B6D0171-3FFD-4892-B407-B633CA4E6712}
EndGlobalSection
EndGlobal

6
Runner/App.config Normal file
Просмотреть файл

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
</configuration>

17
Runner/Program.cs Normal file
Просмотреть файл

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MakeFunctionJson;
namespace Runner
{
class Program
{
static void Main(string[] args)
{
FunctionJsonConvert.Convert(args[0], args[1]);
}
}
}

Просмотреть файл

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Runner")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Runner")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("723db86e-d7c0-4147-b5a7-6a3be1aa9724")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

58
Runner/Runner.csproj Normal file
Просмотреть файл

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{723DB86E-D7C0-4147-B5A7-6A3BE1AA9724}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Runner</RootNamespace>
<AssemblyName>Runner</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\MakeFunctionJson\MakeFunctionJson.csproj">
<Project>{d1a5eeb2-a8d0-4e7a-8fbf-cfc5d2c23b96}</Project>
<Name>MakeFunctionJson</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Просмотреть файл

@ -0,0 +1,21 @@
using System;
using System.Linq;
namespace MakeFunctionJson
{
internal static class AttributeExtensions
{
/// <summary>
/// NameAttribute -> name
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static string ToAttributeFriendlyName(this Attribute attribute)
{
const string suffix = nameof(Attribute);
var name = attribute.GetType().Name;
name = name.Substring(0, name.Length - suffix.Length);
return Char.ToLowerInvariant(name.First()) + name.Substring(1);
}
}
}

Просмотреть файл

@ -0,0 +1,13 @@
using System;
namespace MakeFunctionJson
{
public static class FunctionJsonConvert
{
public static void Convert(string assemblyPath, string outputPath)
{
var converter = new FunctionJsonConverter(assemblyPath, outputPath);
converter.Run();
}
}
}

Просмотреть файл

@ -0,0 +1,47 @@
using System;
using System.IO;
using System.Reflection;
namespace MakeFunctionJson
{
internal class FunctionJsonConverter
{
private string _assemblyPath;
private string _outputPath;
internal FunctionJsonConverter(string assemblyPath, string outputPath)
{
if (string.IsNullOrEmpty(assemblyPath))
{
throw new ArgumentNullException(nameof(assemblyPath));
}
if (string.IsNullOrEmpty(outputPath))
{
throw new ArgumentNullException(nameof(outputPath));
}
_assemblyPath = assemblyPath;
_outputPath = outputPath;
}
internal void Run()
{
var assembly = Assembly.LoadFrom(_assemblyPath);
var relativeAssemblyPath = PathUtility.MakeRelativePath(Path.Combine(_outputPath, "dummyFunctionName"), assembly.Location);
foreach (var type in assembly.GetExportedTypes())
{
foreach (var method in type.GetMethods())
{
if (method.IsWebJobsSdkMethod())
{
var functionJson = method.ToFunctionJson(relativeAssemblyPath);
var functionName = method.GetSdkFunctionName();
var path = Path.Combine(_outputPath, functionName, "function.json");
functionJson.Serialize(path);
}
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,28 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace MakeFunctionJson
{
internal class FunctionJsonSchema
{
[JsonProperty("bindings")]
public IEnumerable<JObject> Bindings { get; set; }
[JsonProperty("disabled")]
public bool Disabled { get; set; }
[JsonProperty("scriptFile")]
public string ScriptFile { get; set; }
[JsonProperty("entryPoint")]
public string EntryPoint { get; set; }
}
internal enum Direction
{
@in,
@out,
@inout
}
}

Просмотреть файл

@ -0,0 +1,16 @@
using System.IO;
using Newtonsoft.Json;
namespace MakeFunctionJson
{
internal static class FunctionJsonSchemaExtension
{
public static void Serialize(this FunctionJsonSchema functionJson, string path)
{
var content = JsonConvert.SerializeObject(functionJson, Formatting.Indented);
var dir = Path.GetDirectoryName(path);
Directory.CreateDirectory(dir);
File.WriteAllText(path, content);
}
}
}

Просмотреть файл

@ -0,0 +1,63 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D1A5EEB2-A8D0-4E7A-8FBF-CFC5D2C23B96}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MakeFunctionJson</RootNamespace>
<AssemblyName>MakeFunctionJson</AssemblyName>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\..\packages\Newtonsoft.Json.10.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ValueTuple, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\..\packages\System.ValueTuple.4.3.0\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AttributeExtensions.cs" />
<Compile Include="FunctionJsonSchema.cs" />
<Compile Include="FunctionJsonConvert.cs" />
<Compile Include="FunctionJsonConverter.cs" />
<Compile Include="FunctionJsonSchemaExtension.cs" />
<Compile Include="MethodInfoExtensions.cs" />
<Compile Include="ParameterInfoExtensions.cs" />
<Compile Include="PathUtility.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Просмотреть файл

@ -0,0 +1,65 @@
using System;
using System.Linq;
using System.Reflection;
namespace MakeFunctionJson
{
internal static class MethodInfoExtensions
{
/// <summary>
/// A method is an SDK method if any of its parameters has an SDK attribute
/// </summary>
/// <param name="method">method to check if an SDK method or not.</param>
/// <returns>true if <paramref name="method"/> is a WebJobs SDK method. False otherwise.</returns>
public static bool IsWebJobsSdkMethod(this MethodInfo method)
{
// TODO: This will have to add && method has FunctionNameAttribute.
return method.GetParameters().Any(p => p.IsWebJobsSdkParameter());
}
/// <summary>
///
/// </summary>
/// <param name="method">method to convert to a <see cref="FunctionJsonSchema"/> object.</param>
/// <param name="assemblyPath">This will be the value of <see cref="FunctionJsonSchema.ScriptFile"/> on the returned value.</param>
/// <returns></returns>
public static FunctionJsonSchema ToFunctionJson(this MethodInfo method, string assemblyPath)
{
return new FunctionJsonSchema
{
// For SDK parameter, convert it to a FunctionJson bindings.
// Every parameter can potentially contain more than 1 attribute that will be converted into a binding object.
Bindings = method.GetParameters().Where(p => p.IsWebJobsSdkParameter()).Select(p => p.ToFunctionJsonBindings()).SelectMany(i => i),
// Entry point is the fully qualified name of the function
EntryPoint = $"{method.DeclaringType.FullName}.{method.Name}",
// scriptFile == assemblyPath.
ScriptFile = assemblyPath
};
}
public static string GetSdkFunctionName(this MethodInfo method)
{
if (!method.IsWebJobsSdkMethod())
{
throw new ArgumentException($"{nameof(method)} has to be a WebJob SDK function");
}
var functionNameAttribute = method.GetCustomAttributes().FirstOrDefault(a => a.GetType().Name == "FunctionNameAttribute");
if (functionNameAttribute != null)
{
return functionNameAttribute.GetType().GetProperty("Name").GetValue(functionNameAttribute).ToString();
}
else
{
var name = method.DeclaringType.Name;
const string suffix = "Function";
if (!name.EndsWith(suffix))
{
throw new InvalidOperationException("By convention, class name must end with '" + suffix + "'");
}
return name.Substring(0, name.Length - suffix.Length);
}
}
}
}

Просмотреть файл

@ -0,0 +1,166 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
namespace MakeFunctionJson
{
internal static class ParameterInfoExtensions
{
private static readonly HashSet<string> _supportedAttributes = new HashSet<string>
{
// SDK
// "StorageAccountAttribute",
"BlobAttribute",
"BlobTriggerAttribute",
"QueueAttribute",
"QueueTriggerAttribute",
"TableAttribute",
"EventHubAttribute",
"EventHubTriggerAttribute",
"TimerTriggerAttribute",
"DocumentDBAttribute",
"ApiHubTableAttribute",
"MobileTableAttribute",
"ServiceBusTriggerAttribute",
"ServiceBusAttribute",
"TwilioSmsAttribute",
"NotificationHubAttribute"
};
public static bool IsWebJobsSdkParameter(this ParameterInfo parameterInfo)
{
return parameterInfo
.GetCustomAttributes()
.Any(a => _supportedAttributes.Contains(a.GetType().Name));
}
public static IEnumerable<JObject> ToFunctionJsonBindings(this ParameterInfo parameterInfo)
{
return parameterInfo
.GetCustomAttributes()
.Where(a => _supportedAttributes.Contains(a.GetType().Name))
.Select(AttributeToJObject)
.Select(o =>
{
o["name"] = parameterInfo.Name;
return o;
});
}
private static JObject AttributeToJObject(Attribute attribute)
{
var obj = new JObject
{
["type"] = attribute.ToAttributeFriendlyName()
};
var direction = Direction.@out;
if (obj["type"].ToString().IndexOf("Trigger") > 0)
{
direction = Direction.@in;
}
foreach (var property in attribute
.GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.CanRead && p.PropertyType != typeof(System.Object)))
{
var propertyValue = property.GetValue(attribute);
if (propertyValue == null || (propertyValue is int && (int)propertyValue == 0))
{
continue;
}
var propertyType = property.PropertyType;
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
propertyType = propertyType.GetGenericArguments().First();
}
if (propertyType == typeof(FileAccess))
{
Direction convert(FileAccess value)
{
if (value == FileAccess.Read)
{
return Direction.@in;
}
else if (value == FileAccess.Write)
{
return Direction.@out;
}
else
{
return Direction.inout;
}
}
direction = convert((FileAccess)propertyValue);
continue;
}
var propertyName = NormalizePropertyName(attribute.GetType().Name, property);
obj[propertyName] = JToken.FromObject(propertyValue);
}
obj["direction"] = direction.ToString();
return obj;
}
private static string NormalizePropertyName(string attrName, PropertyInfo property)
{
var propertyName = property.Name;
if ((attrName == "BlobAttribute") || (attrName == "BlobTriggerAttribute"))
{
if (propertyName == "BlobPath")
{
return "path";
}
}
else if (attrName == "MobileTableAttribute")
{
if (propertyName == "MobileAppUriSetting")
{
return "connection";
}
else if (propertyName == "ApiKeySetting")
{
return "apiKey";
}
}
else if (attrName == "NotificationHubAttribute")
{
if (propertyName == "ConnectionStringSetting")
{
return "connection";
}
}
else if (attrName == "ServiceBusAttribute")
{
if (propertyName == "QueueOrTopicName")
{
return "queue";
}
}
else if (attrName == "TwilioSmsAttribute")
{
if (propertyName == "AccountSidSetting")
{
return "accountSid";
}
else if (propertyName == "AuthTokenSetting")
{
return "authToken";
}
}
return Char.ToLowerInvariant(propertyName.First()) + propertyName.Substring(1);
}
}
}

Просмотреть файл

@ -0,0 +1,31 @@
using System;
using System.IO;
namespace MakeFunctionJson
{
internal class PathUtility
{
internal static string MakeRelativePath(string fromPath, string toPath)
{
if (!fromPath.EndsWith(@"\"))
{
fromPath += @"\";
}
Uri fromUri = new Uri(fromPath);
Uri toUri = new Uri(toPath);
if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative.
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
String relativePath = Uri.UnescapeDataString(relativeUri.ToString());
if (toUri.Scheme.Equals("file", StringComparison.InvariantCultureIgnoreCase))
{
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}
return relativePath;
}
}
}

Просмотреть файл

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MakeFunctionJson")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MakeFunctionJson")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d1a5eeb2-a8d0-4e7a-8fbf-cfc5d2c23b96")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Просмотреть файл

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.1" targetFramework="net46" />
<package id="System.ValueTuple" version="4.3.0" targetFramework="net46" />
</packages>