Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lang/csharp/src/apache/codegen/AvroGen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand All @@ -17,13 +17,13 @@
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace Avro
{
class AvroGen
public class AvroGenTool
{
static int Main(string[] args)
public static int Main(string[] args)
{
// Print usage if no arguments provided
if (args.Length == 0)
Expand All @@ -33,7 +33,7 @@ static int Main(string[] args)
}

// Print usage if help requested
if (args[0] == "-h" || args[0] == "--help")
if (args.Contains("-h") || args.Contains("--help"))
{
Usage();
return 0;
Expand Down Expand Up @@ -140,9 +140,9 @@ static void Usage()
" The format is \"my.avro.namespace:my.csharp.namespace\".\n" +
" May be specified multiple times to map multiple namespaces.\n",
AppDomain.CurrentDomain.FriendlyName);
return;
}
static int GenProtocol(string infile, string outdir,

public static int GenProtocol(string infile, string outdir,
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
{
try
Expand All @@ -167,7 +167,8 @@ static int GenProtocol(string infile, string outdir,

return 0;
}
static int GenSchema(string infile, string outdir,

public static int GenSchema(string infile, string outdir,
IEnumerable<KeyValuePair<string, string>> namespaceMapping)
{
try
Expand Down
6 changes: 5 additions & 1 deletion lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ is regenerated
------------------------------------------------------------------------------");

// Visual Studio 2010 https://msdn.microsoft.com/en-us/library/x53a06bb.aspx
// Note:
// 1. Contextual keywords are not reserved keywords e.g. value, partial
// 2. __arglist, __makeref, __reftype, __refvalue are undocumented keywords, but recognised by the C# compiler
ReservedKeywords = new HashSet<string>() {
"abstract","as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class",
"const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event",
Expand All @@ -96,7 +99,8 @@ is regenerated
"null", "object", "operator", "out", "override", "params", "private", "protected", "public",
"readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static",
"string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong",
"unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "value", "partial" };
"unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while",
"__arglist", "__makeref", "__reftype", "__refvalue" };
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions lang/csharp/src/apache/test/Avro.test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="$(MicrosoftCodeAnalysisCSharpVersion)" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
<PackageReference Include="NUnit" Version="$(NUnitVersion)" />
<PackageReference Include="NUnit3TestAdapter" Version="$(NUnit3TestAdapterVersion)" />
<PackageReference Include="NUnit.ConsoleRunner" Version="$(NUnitConsoleRunnerVersion)" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\main\Avro.main.csproj" />
<ProjectReference Include="..\codegen\Avro.codegen.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
155 changes: 155 additions & 0 deletions lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using NUnit.Framework;

namespace Avro.Test.AvroGen
{
class AvroGenToolResult
{
public int ExitCode { get; set; }
public string[] StdOut { get; set; }
public string[] StdErr { get; set; }
}

class AvroGenHelper
{
public static AvroGenToolResult RunAvroGenTool(params string[] args)
{
// Save stdout and stderr
TextWriter conOut = Console.Out;
TextWriter conErr = Console.Error;

try
{
AvroGenToolResult result = new AvroGenToolResult();
StringBuilder strBuilderOut = new StringBuilder();
StringBuilder strBuilderErr = new StringBuilder();

using (StringWriter writerOut = new StringWriter(strBuilderOut))
using (StringWriter writerErr = new StringWriter(strBuilderErr))
{
writerOut.NewLine = "\n";
writerErr.NewLine = "\n";

// Overwrite stdout and stderr to be able to capture console output
Console.SetOut(writerOut);
Console.SetError(writerErr);

result.ExitCode = AvroGenTool.Main(args.ToArray());

writerOut.Flush();
writerErr.Flush();

result.StdOut = strBuilderOut.Length == 0 ? Array.Empty<string>() : strBuilderOut.ToString().Split(writerOut.NewLine);
result.StdErr = strBuilderErr.Length == 0 ? Array.Empty<string>() : strBuilderErr.ToString().Split(writerErr.NewLine);
}

return result;
}
finally
{
// Restore console
Console.SetOut(conOut);
Console.SetError(conErr);
}
}

public static Assembly CompileCSharpFilesIntoLibrary(IEnumerable<string> sourceFiles, string assemblyName = null, bool loadAssembly = true)
{
// Create random assembly name if not specified
if (assemblyName == null)
assemblyName = Path.GetRandomFileName();

// Base path to assemblies .NET assemblies
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);

using (var compilerStream = new MemoryStream())
{
List<string> assemblies = new List<string>()
{
typeof(object).Assembly.Location,
typeof(Schema).Assembly.Location,
Path.Combine(assemblyPath, "System.Runtime.dll"),
Path.Combine(assemblyPath, "netstandard.dll")
};

// Create compiler
CSharpCompilation compilation = CSharpCompilation
.Create(assemblyName)
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
.AddReferences(assemblies.Select(path => MetadataReference.CreateFromFile(path)))
.AddSyntaxTrees(sourceFiles.Select(sourceFile =>
{
string sourceText = System.IO.File.ReadAllText(sourceFile);
return CSharpSyntaxTree.ParseText(sourceText);
}));

// Compile
EmitResult compilationResult = compilation.Emit(compilerStream);

//Note: Comment the following out to analyze the compiler errors if needed
//if (!compilationResult.Success)
//{
// foreach (Diagnostic diagnostic in compilationResult.Diagnostics)
// {
// if (diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error)
// {
// TestContext.WriteLine($"{diagnostic.Id} - {diagnostic.GetMessage()} - {diagnostic.Location}");
// }
// }
//}

Assert.That(compilationResult.Success, Is.True);

if (!loadAssembly)
{
return null;
}

compilerStream.Seek(0, SeekOrigin.Begin);
return Assembly.Load(compilerStream.ToArray());
}
}

public static string CreateEmptyTemporyFolder(out string uniqueId, string path = null)
{
// Create unique id
uniqueId = Guid.NewGuid().ToString();

// Temporary folder name in working folder or the specified path
string tempFolder = Path.Combine(path ?? TestContext.CurrentContext.WorkDirectory, uniqueId);

// Create folder
Directory.CreateDirectory(tempFolder);

// Make sure it is empty
Assert.That(new DirectoryInfo(tempFolder), Is.Empty);

return tempFolder;
}
}
}
Loading