aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Tango.NET
diff options
context:
space:
mode:
authorRoy Ben-Shabat <Roy@Twine-s.com>2017-10-30 19:38:10 +0200
committerRoy Ben-Shabat <Roy@Twine-s.com>2017-10-30 19:38:10 +0200
commit4fbe47ccac2bdcae52aafa07d6a80176e9606bd9 (patch)
tree333dd813958bcb74a7d21a81f6cee28c47aba53b /Software/Tango.NET
parentc5bb832c1880d0a0d55c3ba11cf97cc6dff6a78d (diff)
downloadTango-4fbe47ccac2bdcae52aafa07d6a80176e9606bd9.tar.gz
Tango-4fbe47ccac2bdcae52aafa07d6a80176e9606bd9.zip
Some more work on protobuf compilers.
Diffstat (limited to 'Software/Tango.NET')
-rw-r--r--Software/Tango.NET/Tango.Core/AssemblyHelper.cs21
-rw-r--r--Software/Tango.NET/Tango.Core/PathHelper.cs32
-rw-r--r--Software/Tango.NET/Tango.Core/Properties/AssemblyInfo.cs6
-rw-r--r--Software/Tango.NET/Tango.Core/Tango.Core.csproj48
-rw-r--r--Software/Tango.NET/Tango.Protobuf/Compilers/CSharpCompiler.cs18
-rw-r--r--Software/Tango.NET/Tango.Protobuf/IProtoCompiler.cs22
-rw-r--r--Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs123
-rw-r--r--Software/Tango.NET/Tango.Protobuf/ProtoLanguage.cs36
-rw-r--r--Software/Tango.NET/Tango.Protobuf/ProtoResult.cs51
-rw-r--r--Software/Tango.NET/Tango.Protobuf/Tango.Protobuf.csproj9
-rw-r--r--Software/Tango.NET/Tango.sln6
11 files changed, 344 insertions, 28 deletions
diff --git a/Software/Tango.NET/Tango.Core/AssemblyHelper.cs b/Software/Tango.NET/Tango.Core/AssemblyHelper.cs
new file mode 100644
index 000000000..5a78f9fe3
--- /dev/null
+++ b/Software/Tango.NET/Tango.Core/AssemblyHelper.cs
@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Core
+{
+ public static class AssemblyHelper
+ {
+ public static String GetCurrentAssemblyFolder()
+ {
+ string codeBase = Assembly.GetExecutingAssembly().CodeBase;
+ UriBuilder uri = new UriBuilder(codeBase);
+ string path = Uri.UnescapeDataString(uri.Path);
+ return Path.GetDirectoryName(path);
+ }
+ }
+}
diff --git a/Software/Tango.NET/Tango.Core/PathHelper.cs b/Software/Tango.NET/Tango.Core/PathHelper.cs
new file mode 100644
index 000000000..08dbf1c16
--- /dev/null
+++ b/Software/Tango.NET/Tango.Core/PathHelper.cs
@@ -0,0 +1,32 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Core
+{
+ public static class PathHelper
+ {
+ public static String GetTempFolderPath()
+ {
+ String tempDirectory = Path.Combine(Path.GetTempPath(), "Twine", Path.GetRandomFileName());
+ Directory.CreateDirectory(tempDirectory);
+ return tempDirectory;
+ }
+
+ public static bool TryDeleteFolder(String path)
+ {
+ try
+ {
+ Directory.Delete(path, true);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+ }
+}
diff --git a/Software/Tango.NET/Tango.Core/Properties/AssemblyInfo.cs b/Software/Tango.NET/Tango.Core/Properties/AssemblyInfo.cs
new file mode 100644
index 000000000..f76af4bb8
--- /dev/null
+++ b/Software/Tango.NET/Tango.Core/Properties/AssemblyInfo.cs
@@ -0,0 +1,6 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+[assembly: AssemblyTitle("Twine - Core Components")]
+[assembly: ComVisible(false)] \ No newline at end of file
diff --git a/Software/Tango.NET/Tango.Core/Tango.Core.csproj b/Software/Tango.NET/Tango.Core/Tango.Core.csproj
new file mode 100644
index 000000000..803d9e3e6
--- /dev/null
+++ b/Software/Tango.NET/Tango.Core/Tango.Core.csproj
@@ -0,0 +1,48 @@
+<?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>{A34EE0F0-649D-41C8-8489-B6F1CC6924EE}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Tango.Core</RootNamespace>
+ <AssemblyName>Tango.Core</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>..\Build\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="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="AssemblyHelper.cs" />
+ <Compile Include="PathHelper.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+</Project> \ No newline at end of file
diff --git a/Software/Tango.NET/Tango.Protobuf/Compilers/CSharpCompiler.cs b/Software/Tango.NET/Tango.Protobuf/Compilers/CSharpCompiler.cs
new file mode 100644
index 000000000..4f9e52a74
--- /dev/null
+++ b/Software/Tango.NET/Tango.Protobuf/Compilers/CSharpCompiler.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Protobuf.Compilers
+{
+ public class CSharpCompiler : ProtoCompiler
+ {
+ public override ProtoLanguage Language => ProtoLanguage.CSharp;
+
+ protected override string GetProtoArguments()
+ {
+ return "--csharp_out";
+ }
+ }
+}
diff --git a/Software/Tango.NET/Tango.Protobuf/IProtoCompiler.cs b/Software/Tango.NET/Tango.Protobuf/IProtoCompiler.cs
index bd2923ede..3d7e05411 100644
--- a/Software/Tango.NET/Tango.Protobuf/IProtoCompiler.cs
+++ b/Software/Tango.NET/Tango.Protobuf/IProtoCompiler.cs
@@ -15,8 +15,24 @@ namespace Tango.Protobuf
/// Compiles the specified .proto message file and saves the result at the specified output folder.
/// </summary>
/// <param name="inputFile">.proto file to compile</param>
- /// <param name="outputFolder">Output folder to save the resulted source code.</param>
- /// <returns>A list of output source code files.</returns>
- List<String> Compile(String inputFile, String outputFolder);
+ /// <returns>A list of compiled results.</returns>
+ IEnumerable<ProtoResult> Compile(String inputFile);
+
+ /// <summary>
+ /// Compiles the specified .proto message file asynchronously and saves the result at the specified output folder.
+ /// </summary>
+ /// <param name="inputFile">.proto file to compile</param>
+ /// <returns>A list of compiled results.</returns>
+ Task<IEnumerable<ProtoResult>> CompileAsync(String inputFile);
+
+ /// <summary>
+ /// Gets the compiler language.
+ /// </summary>
+ ProtoLanguage Language { get; }
+
+ /// <summary>
+ /// Gets the proto imports folders.
+ /// </summary>
+ List<String> ImportsFolders { get; }
}
}
diff --git a/Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs b/Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs
index 0095046ce..eedebf63b 100644
--- a/Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs
+++ b/Software/Tango.NET/Tango.Protobuf/ProtoCompiler.cs
@@ -1,51 +1,124 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
using System.Linq;
+using System.Reflection;
using System.Text;
using System.Threading.Tasks;
+using Tango.Core;
namespace Tango.Protobuf
{
+ /// <summary>
+ /// Represents a protobuf compiler base class.
+ /// </summary>
+ /// <seealso cref="Tango.Protobuf.IProtoCompiler" />
public abstract class ProtoCompiler : IProtoCompiler
{
- public List<string> Compile(string inputFile, string outputFolder)
+ private const String COMPILERS_FOLDER_NAME = "ProtoCompilers"; //Compilers folder name.
+ protected String _compilersPath; //Compilers folder path.
+
+ /// <summary>
+ /// Gets the compiler language.
+ /// </summary>
+ public abstract ProtoLanguage Language { get; }
+
+ /// <summary>
+ /// Gets the proto imports folders.
+ /// </summary>
+ public List<String> ImportsFolders { get; private set; }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtoCompiler"/> class.
+ /// </summary>
+ public ProtoCompiler()
{
- throw new NotImplementedException();
+ _compilersPath = Path.Combine(AssemblyHelper.GetCurrentAssemblyFolder(), COMPILERS_FOLDER_NAME);
}
- #region IDisposable Support
- private bool disposedValue = false; // To detect redundant calls
-
- protected virtual void Dispose(bool disposing)
+ /// <summary>
+ /// Compiles the specified .proto message file and saves the result at the specified output folder.
+ /// </summary>
+ /// <param name="inputFile">.proto file to compile</param>
+ /// <returns>
+ /// A list of compiled results.
+ /// </returns>
+ public virtual IEnumerable<ProtoResult> Compile(string inputFile)
{
- if (!disposedValue)
+ String tmpPath = PathHelper.GetTempFolderPath();
+
+ String importsString = String.Empty;
+
+ foreach (var path in ImportsFolders)
{
- if (disposing)
- {
- // TODO: dispose managed state (managed objects).
- }
+ importsString = "--proto_path \"" + path + "\" ";
+ }
+
+ Process p = new Process();
+ p.StartInfo.FileName = Path.Combine(_compilersPath, GetProtoCompilerName());
+ p.StartInfo.Arguments = String.Format(
+ "{0} {1}=\"{2}\" \"{3}\"",
+ importsString,
+ GetProtoArguments(),
+ tmpPath,
+ inputFile);
+
+ p.StartInfo.CreateNoWindow = true;
+ p.StartInfo.UseShellExecute = false;
+ p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
+
+ p.Start();
+ p.WaitForExit(5000);
- // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
- // TODO: set large fields to null.
+ List<ProtoResult> results = new List<ProtoResult>();
- disposedValue = true;
+ foreach (var file in Directory.GetFiles(tmpPath))
+ {
+ ProtoResult result = new ProtoResult(Path.GetFileName(file), File.ReadAllText(file), Language);
}
+
+ PathHelper.TryDeleteFolder(tmpPath);
+
+ return results;
}
- // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
- // ~ProtoCompiler() {
- // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- // Dispose(false);
- // }
+ /// <summary>
+ /// Compiles the specified .proto message file asynchronously and saves the result at the specified output folder.
+ /// </summary>
+ /// <param name="inputFile">.proto file to compile</param>
+ /// <returns>
+ /// A list of compiled results.
+ /// </returns>
+ public async Task<IEnumerable<ProtoResult>> CompileAsync(string inputFile)
+ {
+ return await new Task<IEnumerable<ProtoResult>>(() => { return Compile(inputFile); });
+ }
+
+ /// <summary>
+ /// Gets the protobuf compiler CLI arguments (without input/output files!).
+ /// </summary>
+ /// <returns></returns>
+ protected abstract String GetProtoArguments();
+
+ /// <summary>
+ /// Gets the protobuf compiler CLI file name (override when using a compiler other than the default 'protoc.exe').
+ /// </summary>
+ /// <remarks>
+ /// The compiler program must be located in the compilers folder.
+ /// </remarks>
+ /// <returns></returns>
+ protected virtual String GetProtoCompilerName()
+ {
+ return "protoc.exe";
+ }
- // This code added to correctly implement the disposable pattern.
+ /// <summary>
+ /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+ /// </summary>
public void Dispose()
{
- // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
- Dispose(true);
- // TODO: uncomment the following line if the finalizer is overridden above.
- // GC.SuppressFinalize(this);
+ //TODO: Dispose...
}
- #endregion
}
}
diff --git a/Software/Tango.NET/Tango.Protobuf/ProtoLanguage.cs b/Software/Tango.NET/Tango.Protobuf/ProtoLanguage.cs
new file mode 100644
index 000000000..0a53a3c01
--- /dev/null
+++ b/Software/Tango.NET/Tango.Protobuf/ProtoLanguage.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Protobuf
+{
+ /// <summary>
+ /// Represents an enumeration for available protobuf compiler languages.
+ /// </summary>
+ public enum ProtoLanguage
+ {
+ [Description("C#")]
+ CSharp,
+ [Description("Java")]
+ Java,
+ [Description("Java Nano")]
+ JavaNano,
+ [Description("C++")]
+ CPP,
+ [Description("C")]
+ C,
+ [Description("Embedded C")]
+ EmbeddedC,
+ [Description("JavaScript")]
+ JS,
+ [Description("Python")]
+ Python,
+ [Description("PHP")]
+ PHP,
+ [Description("Ruby")]
+ Ruby,
+ }
+}
diff --git a/Software/Tango.NET/Tango.Protobuf/ProtoResult.cs b/Software/Tango.NET/Tango.Protobuf/ProtoResult.cs
new file mode 100644
index 000000000..5b35f9b5a
--- /dev/null
+++ b/Software/Tango.NET/Tango.Protobuf/ProtoResult.cs
@@ -0,0 +1,51 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Tango.Protobuf
+{
+ /// <summary>
+ /// Represents a single protobuf compile result.
+ /// </summary>
+ public class ProtoResult
+ {
+ /// <summary>
+ /// Gets the compiled language.
+ /// </summary>
+ public ProtoLanguage Language { get; private set; }
+
+ /// <summary>
+ /// Gets the name of the file.
+ /// </summary
+ public String FileName { get; private set; }
+
+ /// <summary>
+ /// Gets the file content.
+ /// </summary>
+ public String Content { get; private set; }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ProtoResult"/> class.
+ /// </summary>
+ /// <param name="fileName">Name of the file.</param>
+ /// <param name="content">The content.</param>
+ /// <param name="language">The language.</param>
+ public ProtoResult(String fileName, String content, ProtoLanguage language)
+ {
+ FileName = fileName;
+ Content = content;
+ Language = language;
+ }
+
+ /// <summary>
+ /// Saves the result to the specified folder.
+ /// </summary>
+ /// <param name="folder">The folder.</param>
+ public void Save(String folder)
+ {
+
+ }
+ }
+}
diff --git a/Software/Tango.NET/Tango.Protobuf/Tango.Protobuf.csproj b/Software/Tango.NET/Tango.Protobuf/Tango.Protobuf.csproj
index 87aa6bc31..80caa9c98 100644
--- a/Software/Tango.NET/Tango.Protobuf/Tango.Protobuf.csproj
+++ b/Software/Tango.NET/Tango.Protobuf/Tango.Protobuf.csproj
@@ -40,9 +40,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Compilers\CSharpCompiler.cs" />
<Compile Include="ProtoCompiler.cs" />
<Compile Include="IProtoCompiler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="ProtoLanguage.cs" />
+ <Compile Include="ProtoResult.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="..\..\External Repositories\Protobuf\protoc-1.0M4.jar">
@@ -58,5 +61,11 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Tango.Core\Tango.Core.csproj">
+ <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project>
+ <Name>Tango.Core</Name>
+ </ProjectReference>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> \ No newline at end of file
diff --git a/Software/Tango.NET/Tango.sln b/Software/Tango.NET/Tango.sln
index f6a1c1940..dbcaa7e2a 100644
--- a/Software/Tango.NET/Tango.sln
+++ b/Software/Tango.NET/Tango.sln
@@ -10,6 +10,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Versioning", "Versioning",
Versioning\GlobalVersionInfo.cs = Versioning\GlobalVersionInfo.cs
EndProjectSection
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tango.Core", "Tango.Core\Tango.Core.csproj", "{A34EE0F0-649D-41C8-8489-B6F1CC6924EE}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -20,6 +22,10 @@ Global
{40073806-914E-4E78-97AB-FA9639308EBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40073806-914E-4E78-97AB-FA9639308EBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40073806-914E-4E78-97AB-FA9639308EBE}.Release|Any CPU.Build.0 = Release|Any CPU
+ {A34EE0F0-649D-41C8-8489-B6F1CC6924EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {A34EE0F0-649D-41C8-8489-B6F1CC6924EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {A34EE0F0-649D-41C8-8489-B6F1CC6924EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {A34EE0F0-649D-41C8-8489-B6F1CC6924EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE