diff --git a/lang/c++/CMakeLists.txt b/lang/c++/CMakeLists.txt index 4a3793152e6..b02aea2c856 100644 --- a/lang/c++/CMakeLists.txt +++ b/lang/c++/CMakeLists.txt @@ -92,6 +92,8 @@ endif (SNAPPY_FOUND) add_definitions (${Boost_LIB_DIAGNOSTIC_DEFINITIONS}) +add_definitions (-DAVRO_VERSION="${AVRO_VERSION_MAJOR}.${AVRO_VERSION_MINOR}.${AVRO_VERSION_PATCH}") + include_directories (api ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIRS}) set (AVRO_SOURCE_FILES diff --git a/lang/c++/impl/avrogencpp.cc b/lang/c++/impl/avrogencpp.cc index c287bbd9cfa..01c4cdbf3c3 100644 --- a/lang/c++/impl/avrogencpp.cc +++ b/lang/c++/impl/avrogencpp.cc @@ -810,23 +810,28 @@ int main(int argc, char **argv) { const string NO_UNION_TYPEDEF("no-union-typedef"); po::options_description desc("Allowed options"); - desc.add_options()("help,h", "produce help message")("include-prefix,p", po::value()->default_value("avro"), + desc.add_options()("help,h", "produce help message")("version,V", "produce version information")("include-prefix,p", po::value()->default_value("avro"), "prefix for include headers, - for none, default: avro")("no-union-typedef,U", "do not generate typedefs for unions in records")("namespace,n", po::value(), "set namespace for generated code")("input,i", po::value(), "input file")("output,o", po::value(), "output file to generate"); po::variables_map vm; po::store(po::parse_command_line(argc, argv, desc), vm); po::notify(vm); - if (vm.count(IN_FILE) == 0 || vm.count(OUT_FILE) == 0) { + if (vm.count("help")) { std::cout << desc << std::endl; - return 1; + return 0; } - if (vm.count("help")) { - std::cout << desc << std::endl; + if (vm.count("version")) { + std::cout << AVRO_VERSION << std::endl; return 0; } + if (vm.count(IN_FILE) == 0 || vm.count(OUT_FILE) == 0) { + std::cout << desc << std::endl; + return 1; + } + string ns = vm.count(NS) > 0 ? vm[NS].as() : string(); string outf = vm.count(OUT_FILE) > 0 ? vm[OUT_FILE].as() : string(); string inf = vm.count(IN_FILE) > 0 ? vm[IN_FILE].as() : string(); diff --git a/lang/csharp/src/apache/codegen/Avro.codegen.csproj b/lang/csharp/src/apache/codegen/Avro.codegen.csproj index 371d2d77771..ad49521fbfa 100644 --- a/lang/csharp/src/apache/codegen/Avro.codegen.csproj +++ b/lang/csharp/src/apache/codegen/Avro.codegen.csproj @@ -26,10 +26,9 @@ unless framework is explicitly specified with 'dotnet tool install' https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-tool-install --> - netcoreapp3.1;net5.0;net6.0 + $(DefaultExeTargetFrameworks) avrogen Avro.codegen - false true ..\..\..\Avro.snk diff --git a/lang/csharp/src/apache/codegen/AvroGen.cs b/lang/csharp/src/apache/codegen/AvroGen.cs index cb01671fe07..4b401a359ae 100644 --- a/lang/csharp/src/apache/codegen/AvroGen.cs +++ b/lang/csharp/src/apache/codegen/AvroGen.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; namespace Avro { @@ -39,6 +40,15 @@ public static int Main(string[] args) return 0; } + if (args.Contains("--version") || args.Contains("-V")) + { + // Print version information + // Note: Use InformationalVersion attributre + // It is capable to include semver prerelease information label (if prerelease), e.g. 1.x.y-beta.z + Console.WriteLine(typeof(AvroGenTool).Assembly.GetCustomAttribute().InformationalVersion); + return 0; + } + // Parse command line arguments bool? isProtocol = null; string inputFile = null; @@ -135,10 +145,11 @@ static void Usage() " avrogen -p [--namespace ]\n" + " avrogen -s [--namespace ]\n\n" + "Options:\n" + - " -h --help Show this screen.\n" + - " --namespace Map an Avro schema/protocol namespace to a C# namespace.\n" + - " The format is \"my.avro.namespace:my.csharp.namespace\".\n" + - " May be specified multiple times to map multiple namespaces.\n", + " -h --help Show this screen.\n" + + " -V --version Show version.\n" + + " --namespace Map an Avro schema/protocol namespace to a C# namespace.\n" + + " The format is \"my.avro.namespace:my.csharp.namespace\".\n" + + " May be specified multiple times to map multiple namespaces.\n", AppDomain.CurrentDomain.FriendlyName); } diff --git a/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs b/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs deleted file mode 100644 index 6175167803a..00000000000 --- a/lang/csharp/src/apache/codegen/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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.Reflection; -using System.Runtime.InteropServices; - -[assembly: AssemblyTitle("Avro.codegen")] -[assembly: AssemblyDescription("")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Apache")] -[assembly: AssemblyProduct("Avro.codegen")] -[assembly: AssemblyCopyright("Copyright © Apache 2013")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] -[assembly: ComVisible(false)] -[assembly: Guid("3C23DD33-DD4F-42B1-B71F-8F9C86929E58")] -[assembly: AssemblyVersion("0.9.0.0")] -[assembly: AssemblyFileVersion("0.9.0.0")] \ No newline at end of file diff --git a/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs b/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs index a5a46b4c4b0..c1433bcc057 100644 --- a/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs +++ b/lang/csharp/src/apache/test/AvroGen/AvroGenToolTests.cs @@ -16,9 +16,7 @@ * limitations under the License. */ using System; -using System.IO; -using System.Linq; -using System.Text; +using System.Reflection; using NUnit.Framework; namespace Avro.Test.AvroGen @@ -51,6 +49,26 @@ public void CommandLineHelp(params string[] args) Assert.That(result.StdErr, Is.Empty); } + [TestCase("--version")] + [TestCase("-V")] + public void CommandLineVersion(params string[] args) + { + AvroGenToolResult result = AvroGenHelper.RunAvroGenTool(args); + + Assert.That(result.ExitCode, Is.EqualTo(0)); + Assert.That(result.StdOut, Is.Not.Empty); + Assert.That(result.StdErr, Is.Empty); + + // Check if returned version is SemVer 2.0 compliant + Assert.That(result.StdOut[0], Does.Match(Utils.VersionTests.SemVerRegex)); + + // Returned version must be the same as the avrogen tool assembly's version + Assert.That(result.StdOut[0], Is.EqualTo(typeof(AvroGenTool).Assembly.GetCustomAttribute().InformationalVersion)); + + // Returned version must be the same as the avro library assembly's version + Assert.That(result.StdOut[0], Is.EqualTo(typeof(Schema).Assembly.GetCustomAttribute().InformationalVersion)); + } + [TestCase("-p")] [TestCase("-s")] [TestCase("-p", "whatever.avpr")] diff --git a/lang/csharp/src/apache/test/Utils/VersionTests.cs b/lang/csharp/src/apache/test/Utils/VersionTests.cs new file mode 100644 index 00000000000..7492230d5c0 --- /dev/null +++ b/lang/csharp/src/apache/test/Utils/VersionTests.cs @@ -0,0 +1,55 @@ +/** + * 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.Reflection; +using NUnit.Framework; + +namespace Avro.Test.Utils +{ + public class VersionTests + { + // SemVer2.0 regex + public static string SemVerRegex = @"^((([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)$"; + + [Test] + public void VersionTest() + { + // Avro library's assembly + Assembly assembly = typeof(Schema).Assembly; + + // Note: InformationalVersion contains pre-release tag if available (e.g. 1.x.y-beta.z) + string libraryVersion = assembly.GetCustomAttribute().InformationalVersion; + + // Check version is SmeVer 2.0 compliant + Assert.That(libraryVersion, Does.Match(SemVerRegex)); + } + + [Test] + public void MandatoryAttributesTest() + { + // Avro library's assembly + Assembly assembly = typeof(Schema).Assembly; + + Assert.That(assembly.GetCustomAttribute(), Is.Not.Null); + Assert.That(assembly.GetCustomAttribute(), Is.Not.Null); + Assert.That(assembly.GetCustomAttribute(), Is.Not.Null); + Assert.That(assembly.GetCustomAttribute(), Is.Not.Null); + Assert.That(assembly.GetCustomAttribute(), Is.Not.Null); + } + } +}