mirror of
https://github.com/zap-proto/cs.git
synced 2026-10-30 15:05:32 +00:00
Finishes the in-flight capnp→ZAP rebrand on this branch. The prior commits
renamed identifiers/namespaces/files/projects (Capnp.Net.Runtime→Zap.Net.Runtime,
CapnpC→ZapC, .capnp→.zap) but left the human-readable brand name "Cap'n Proto"
throughout doc-comments, .csproj/.nuspec descriptions, README, MSBuild .xaml
rule display names, and the generated XML doc. This completes brand neutrality:
- Replace 154 "Cap'n Proto" / 4 "Cap'n-Proto" / 1 "Cap'n'p" prose occurrences
with "ZAP" across 32 first-party files (runtime, generator, zapc-csharp,
MSBuild integration, chocolatey nuspecs, README, docs, XML doc).
- Revert the LICENSE provenance URL to the real upstream fork
(c80k/capnproto-dotnetcore) — upstream-fork license headers must stay.
- Canonicalize project/repo URLs to github.com/zap-proto/cs (were fabricated
c80k/zap-dotnetcore) in csproj/nuspec/VERIFICATION/README badges.
- Canonicalize docs URLs zap-protocol(.github.io|/zap-cs|/zap-cpp-core)
→ zap-proto.dev / github.com/zap-proto/{cs,cpp-core}.
- Collapse redundant "zap \"ZAP\"" PackageTags to "zap".
Verified consistent (no toolchain on host): every .sln/.csproj ProjectReference
resolves; all namespaces are Zap*/ZapC*; MSBuild task chain
(GenerateZapFileCodeBehindTask, ZapFiles ItemGroup, _ZapcCsharp_TaskAssembly)
is uniformly renamed; props/targets/xaml/coverage/chocolatey carry no capnp.
Deliberately preserved as upstream-derived test data (not first-party source):
upstream Cap'n Proto schema fixtures (rpc.zap, rpc-twoparty.zap, json.zap,
rpc-csharp.zap), golden compiler-error outputs (errors.txt), and the
machine-generated DO-NOT-EDIT test.zap.{c++,h}. Their "Cap'n Proto" strings are
the literal output of the external schema compiler the compat test matches.
130 lines
5.1 KiB
C#
130 lines
5.1 KiB
C#
using Zap;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.RegularExpressions;
|
|
|
|
[assembly: InternalsVisibleTo("ZapC.CSharp.Generator.Tests")]
|
|
|
|
namespace ZapC.CSharp.Generator
|
|
{
|
|
/// <summary>
|
|
/// Provides methods for controlling both the C# code generator backend and the frontend "zapc"
|
|
/// </summary>
|
|
public static class ZapCompilation
|
|
{
|
|
/// <summary>
|
|
/// Returns the basename of the zap executable
|
|
/// </summary>
|
|
public static string ZapCompilerFilename
|
|
{
|
|
get => RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "zap.exe" : "zap";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates C# code from given input stream
|
|
/// </summary>
|
|
/// <param name="input">Input stream containing the binary code generation request, which the frontend zapc emits</param>
|
|
/// <param name="options">Configuration options for code generator. If null, default options will be used.</param>
|
|
/// <returns>generation result</returns>
|
|
/// <exception cref="ArgumentNullException">if <paramref name="input"/> is null</exception>
|
|
public static GenerationResult GenerateFromStream(Stream input, CodeGen.GeneratorOptions options)
|
|
{
|
|
if (input == null)
|
|
throw new ArgumentNullException(nameof(input));
|
|
|
|
try
|
|
{
|
|
var segments = Framing.ReadSegments(input);
|
|
var dec = DeserializerState.CreateRoot(segments);
|
|
var reader = Schema.CodeGeneratorRequest.READER.create(dec);
|
|
var model = Model.SchemaModel.Create(reader);
|
|
var codeGen = new CodeGen.CodeGenerator(model, options ?? new CodeGen.GeneratorOptions());
|
|
return new GenerationResult(codeGen.Generate());
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
return new GenerationResult(exception);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Generates C# code from given input stream
|
|
/// </summary>
|
|
/// <param name="input">input stream containing the binary code generation request, which the frontend zapc emits</param>
|
|
/// <returns>generation result</returns>
|
|
/// <exception cref="ArgumentNullException">if <paramref name="input"/> is null</exception>
|
|
public static GenerationResult GenerateFromStream(Stream input) => GenerateFromStream(input, null);
|
|
|
|
/// <summary>
|
|
/// Invokes "zap.exe -o-" with given additional arguments and redirects the output to the C# generator backend.
|
|
/// </summary>
|
|
/// <param name="arguments">additional command line arguments</param>
|
|
/// <param name="workingDirectory">optional working directory</param>
|
|
/// <returns>generation result</returns>
|
|
/// <exception cref="ArgumentNullException"><paramref name="arguments"/>is null</exception>
|
|
public static GenerationResult InvokeZapAndGenerate(IEnumerable<string> arguments, string workingDirectory = null)
|
|
{
|
|
if (arguments == null)
|
|
throw new ArgumentNullException(nameof(arguments));
|
|
|
|
using (var compiler = new Process())
|
|
{
|
|
var argList = new List<string>();
|
|
argList.Add("compile");
|
|
argList.Add($"-o-");
|
|
argList.AddRange(arguments);
|
|
|
|
compiler.StartInfo.FileName = ZapCompilerFilename;
|
|
compiler.StartInfo.Arguments = string.Join(" ", argList);
|
|
compiler.StartInfo.UseShellExecute = false;
|
|
compiler.StartInfo.RedirectStandardOutput = true;
|
|
compiler.StartInfo.RedirectStandardError = true;
|
|
if (!string.IsNullOrWhiteSpace(workingDirectory))
|
|
{
|
|
compiler.StartInfo.WorkingDirectory = workingDirectory;
|
|
}
|
|
|
|
try
|
|
{
|
|
compiler.Start();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
return new GenerationResult(exception)
|
|
{
|
|
ErrorCategory = ZapProcessFailure.NotFound
|
|
};
|
|
}
|
|
|
|
var result = GenerateFromStream(compiler.StandardOutput.BaseStream);
|
|
|
|
var messageList = new List<ZapMessage>();
|
|
|
|
while (!compiler.StandardError.EndOfStream)
|
|
{
|
|
messageList.Add(new ZapMessage(compiler.StandardError.ReadLine()));
|
|
}
|
|
|
|
result.Messages = messageList;
|
|
|
|
if (!result.IsSuccess)
|
|
{
|
|
compiler.WaitForExit();
|
|
int exitCode = compiler.ExitCode;
|
|
|
|
if (exitCode == 0)
|
|
result.ErrorCategory = ZapProcessFailure.BadOutput;
|
|
else
|
|
result.ErrorCategory = ZapProcessFailure.BadInput;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|