From bba92d589e125f22b5f0d3eee2b85b6503ca0935 Mon Sep 17 00:00:00 2001 From: aboulart <37553604+aboulart@users.noreply.github.com> Date: Tue, 10 Dec 2019 17:12:49 +0000 Subject: [PATCH] capnp fixes for Linux --- .../FeatureSteps/CodeGeneratorSteps.cs | 31 +++++++++++++------ .../Util/InlineAssemblyCompiler.cs | 7 ++++- CapnpC.CSharp.Generator/CapnpCompilation.cs | 11 ++++++- .../CodeGen/CodeGenerator.cs | 2 +- .../CapnpC.CSharp.MsBuild.Generation.csproj | 2 +- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/CapnpC.CSharp.Generator.Tests/FeatureSteps/CodeGeneratorSteps.cs b/CapnpC.CSharp.Generator.Tests/FeatureSteps/CodeGeneratorSteps.cs index ee75822..8096b8e 100644 --- a/CapnpC.CSharp.Generator.Tests/FeatureSteps/CodeGeneratorSteps.cs +++ b/CapnpC.CSharp.Generator.Tests/FeatureSteps/CodeGeneratorSteps.cs @@ -27,16 +27,27 @@ namespace CapnpC.CSharp.Generator.Tests return assembly.GetManifestResourceStream(urn); } - internal static bool IsCapnpExeInstalled() + internal static bool IsCapnpInstalled() { - using (var process = Process.Start("where", "capnp.exe")) + try { - if (process == null) - Assert.Fail("Unable to start 'where'"); + var startInfo = new ProcessStartInfo(CapnpCompilation.CapnpCompilerFilename, "--version"); + startInfo.UseShellExecute = false; + startInfo.RedirectStandardOutput = true; + startInfo.RedirectStandardError = true; - process.WaitForExit(); + using (var process = Process.Start(startInfo)) + { + Assert.IsNotNull(process, $"Unable to start '{CapnpCompilation.CapnpCompilerFilename}'"); - return process.ExitCode == 0; + process.WaitForExit(); + + return process.ExitCode == 0; + } + } + catch (Exception) + { + return false; } } @@ -112,9 +123,9 @@ namespace CapnpC.CSharp.Generator.Tests [Given(@"capnp\.exe is installed on my system")] public void GivenCapnp_ExeIsInstalledOnMySystem() { - if (!IsCapnpExeInstalled()) + if (!IsCapnpInstalled()) { - Assert.Inconclusive("capnp.exe not found. Precondition of this test is not met."); + Assert.Inconclusive("Capnp compiler not found. Precondition of this test is not met."); } } @@ -151,9 +162,9 @@ namespace CapnpC.CSharp.Generator.Tests [Given(@"capnp\.exe is not installed on my system")] public void GivenCapnp_ExeIsNotInstalledOnMySystem() { - if (IsCapnpExeInstalled()) + if (IsCapnpInstalled()) { - Assert.Inconclusive("capnp.exe found. Precondition of this test is not met."); + Assert.Inconclusive("Capnp compiler found. Precondition of this test is not met."); } } diff --git a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs index 7abc30f..d7d877e 100644 --- a/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs +++ b/CapnpC.CSharp.Generator.Tests/Util/InlineAssemblyCompiler.cs @@ -22,7 +22,12 @@ namespace CapnpC.CSharp.Generator.Tests.Util string capnpRuntimePath = Path.GetFullPath(Path.Combine( Assembly.GetExecutingAssembly().Location, - @"..\..\..\..\..\Capnp.Net.Runtime\bin\Debug\netcoreapp2.1\Capnp.Net.Runtime.dll")); + "..", "..", "..", "..", "..", + "Capnp.Net.Runtime", + "bin", + "Debug", + "netcoreapp2.1", + "Capnp.Net.Runtime.dll")); var capnpRuntimeMetadataRef = MetadataReference.CreateFromFile(capnpRuntimePath); diff --git a/CapnpC.CSharp.Generator/CapnpCompilation.cs b/CapnpC.CSharp.Generator/CapnpCompilation.cs index 3c01261..9e9f8d0 100644 --- a/CapnpC.CSharp.Generator/CapnpCompilation.cs +++ b/CapnpC.CSharp.Generator/CapnpCompilation.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text.RegularExpressions; [assembly: InternalsVisibleTo("CapnpC.CSharp.Generator.Tests")] @@ -15,6 +16,14 @@ namespace CapnpC.CSharp.Generator /// public static class CapnpCompilation { + /// + /// Returns the basename of the capnp executable + /// + public static string CapnpCompilerFilename + { + get => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "capnp.exe" : "capnp"; + } + /// /// Generates C# code from given input stream /// @@ -60,7 +69,7 @@ namespace CapnpC.CSharp.Generator argList.Add($"-o-"); argList.AddRange(arguments); - compiler.StartInfo.FileName = "capnp.exe"; + compiler.StartInfo.FileName = CapnpCompilerFilename; compiler.StartInfo.Arguments = string.Join(" ", argList); compiler.StartInfo.UseShellExecute = false; compiler.StartInfo.RedirectStandardOutput = true; diff --git a/CapnpC.CSharp.Generator/CodeGen/CodeGenerator.cs b/CapnpC.CSharp.Generator/CodeGen/CodeGenerator.cs index 6ac4d0a..e3c5db6 100644 --- a/CapnpC.CSharp.Generator/CodeGen/CodeGenerator.cs +++ b/CapnpC.CSharp.Generator/CodeGen/CodeGenerator.cs @@ -210,7 +210,7 @@ cu = cu.AddMembers(ns); - return cu.NormalizeWhitespace().ToFullString(); + return cu.NormalizeWhitespace(" ", Environment.NewLine).ToFullString(); } public IReadOnlyList Generate() diff --git a/CapnpC.CSharp.MsBuild.Generation/CapnpC.CSharp.MsBuild.Generation.csproj b/CapnpC.CSharp.MsBuild.Generation/CapnpC.CSharp.MsBuild.Generation.csproj index eea4f4c..fc757d6 100644 --- a/CapnpC.CSharp.MsBuild.Generation/CapnpC.CSharp.MsBuild.Generation.csproj +++ b/CapnpC.CSharp.MsBuild.Generation/CapnpC.CSharp.MsBuild.Generation.csproj @@ -107,7 +107,7 @@ - + \ No newline at end of file