Skip to content

Commit

Permalink
(#80) SDK: self-written code for process argument passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Apr 21, 2024
1 parent fc16814 commit 84321e0
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 108 deletions.
17 changes: 17 additions & 0 deletions Cesium.Sdk.Tests/ArgumentUtilTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Cesium.Sdk.Tests;

public class ArgumentUtilTests
{
[Fact]
public void PerformTests()
{
Assert.Equal("\"\"", ArgumentUtil.ToCommandLineString([""]));
Assert.Equal("a b c", ArgumentUtil.ToCommandLineString(["a", "b", "c"]));
Assert.Equal("\"a b\" c", ArgumentUtil.ToCommandLineString(["a b", "c"]));
Assert.Equal("a\\b c", ArgumentUtil.ToCommandLineString([@"a\b", "c"]));
Assert.Equal("\"\\\"\"", ArgumentUtil.ToCommandLineString(["\""]));
Assert.Equal("\"a \\\"b\\\"\"", ArgumentUtil.ToCommandLineString(["a \"b\""]));
Assert.Equal("\"\\\\\\\"\"", ArgumentUtil.ToCommandLineString(["\\\""]));
Assert.Equal("\"a\\ \\\\\\\"b\\\"\"", ArgumentUtil.ToCommandLineString(["a\\ \\\"b\""]));
}
}
1 change: 1 addition & 0 deletions Cesium.Sdk.Tests/Cesium.Sdk.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Cesium.Sdk\Cesium.Sdk.csproj" />
<ProjectReference Include="..\Cesium.TestFramework\Cesium.TestFramework.csproj" />
</ItemGroup>

Expand Down
73 changes: 73 additions & 0 deletions Cesium.Sdk/ArgumentUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cesium.Sdk;

public static class ArgumentUtil
{
public static string ToCommandLineString(IEnumerable<string> args)
{
var result = new StringBuilder();
var first = true;
foreach (var a in args)
{
if (first)
{
first = false;
}
else
{
result.Append(' ');
}
if (a.Length == 0 || a.Any(c => char.IsWhiteSpace(c) || c == '"'))
{
result.Append(Quoted(a));
}
else
{
result.Append(a);
}
}
return result.ToString();
}

private static string Quoted(string arg)
{
// The simplified rules:
// 1. Every quote should be escaped by \
// 2. Slashes preceding the quotes should be escaped by \
//
// Meaning any amount of slashes following a quote should be converted to twice as many slashes + slash + quote.
// The final quote (not part of the argument per se) also counts as quote for this purpose.
var result = new StringBuilder(arg.Length + 2);
result.Append('"');
var slashes = 0;
foreach (var c in arg)
{
switch (c)
{
case '\\':
slashes++;
break;
case '"':
result.Append('\\', slashes * 2);
slashes = 0;

result.Append("\\\"");
break;
default:
result.Append('\\', slashes);
slashes = 0;

result.Append(c);
break;
}
}

result.Append('\\', slashes * 2);
result.Append('"');

return result.ToString();
}
}
44 changes: 22 additions & 22 deletions Cesium.Sdk/CesiumCompile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,67 +187,67 @@ private bool TryValidate(out ValidatedOptions? options)

private string CollectCommandLineArguments(ValidatedOptions options)
{
var builder = new CommandArgumentsBuilder();
var args = new List<string>();

builder.RawArgument("--nologo");
args.Add("--nologo");

if (options.Framework is { } framework)
{
builder.RawArgument("--framework");
builder.RawArgument(framework.ToString());
args.Add("--framework");
args.Add(framework.ToString());
}

if (options.Architecture is { } arch)
{
builder.RawArgument("--arch");
builder.RawArgument(arch.ToString());
args.Add("--arch");
args.Add(arch.ToString());
}

if (options.ModuleKind is { } moduleKind)
{
builder.RawArgument("--modulekind");
builder.RawArgument(moduleKind.ToString());
args.Add("--modulekind");
args.Add(moduleKind.ToString());
}

if (!string.IsNullOrWhiteSpace(options.Namespace))
{
builder.RawArgument("--namespace");
builder.RawArgument(options.Namespace!);
args.Add("--namespace");
args.Add(options.Namespace!);
}

foreach (var import in options.ImportItems)
{
builder.RawArgument("--import");
builder.Argument(import);
args.Add("--import");
args.Add(import);
}

if (!string.IsNullOrWhiteSpace(options.CoreLibPath))
{
builder.RawArgument("--corelib");
builder.Argument(options.CoreLibPath!);
args.Add("--corelib");
args.Add(options.CoreLibPath!);
}

if (!string.IsNullOrWhiteSpace(options.RuntimePath))
{
builder.RawArgument("--runtime");
builder.Argument(options.RuntimePath!);
args.Add("--runtime");
args.Add(options.RuntimePath!);
}

foreach (var item in options.PreprocessorItems)
{
builder.RawArgument("-D");
builder.Argument(item);
args.Add("-D");
args.Add(item);
}

builder.RawArgument("--out");
builder.Argument(options.OutputFile);
args.Add("--out");
args.Add(options.OutputFile);

foreach (var input in options.InputItems)
{
builder.Argument(input);
args.Add(input);
}

return builder.Build();
return ArgumentUtil.ToCommandLineString(args);
}

private void ReportValidationError(string code, string message) =>
Expand Down
86 changes: 0 additions & 86 deletions Cesium.Sdk/CommandArgumentsBuilder.cs

This file was deleted.

0 comments on commit 84321e0

Please sign in to comment.