2021-10-14 06:55:19 -07:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security;
|
2021-12-19 09:01:26 -08:00
|
|
|
using System.Text.RegularExpressions;
|
2021-10-14 06:55:19 -07:00
|
|
|
using NJsonSchema;
|
|
|
|
|
using Nustache.Core;
|
|
|
|
|
|
2021-12-22 17:16:44 -08:00
|
|
|
namespace LibKubernetesGenerator
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2021-10-24 08:14:22 -07:00
|
|
|
internal class StringHelpers : INustacheHelper
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
|
|
|
|
private readonly GeneralNameHelper generalNameHelper;
|
|
|
|
|
|
|
|
|
|
public StringHelpers(GeneralNameHelper generalNameHelper)
|
|
|
|
|
{
|
|
|
|
|
this.generalNameHelper = generalNameHelper;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterHelper()
|
|
|
|
|
{
|
|
|
|
|
Helpers.Register(nameof(ToXmlDoc), ToXmlDoc);
|
2021-12-19 09:01:26 -08:00
|
|
|
Helpers.Register(nameof(ToInterpolationPathString), ToInterpolationPathString);
|
|
|
|
|
Helpers.Register(nameof(IfGroupPathParamContainsGroup), IfGroupPathParamContainsGroup);
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
|
|
|
|
|
RenderBlock fn, RenderBlock inverse)
|
|
|
|
|
{
|
|
|
|
|
if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is string)
|
|
|
|
|
{
|
|
|
|
|
var first = true;
|
|
|
|
|
|
|
|
|
|
using (var reader = new StringReader(arguments[0] as string))
|
|
|
|
|
{
|
|
|
|
|
string line = null;
|
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var wline in WordWrap(line, 80))
|
|
|
|
|
{
|
|
|
|
|
if (!first)
|
|
|
|
|
{
|
|
|
|
|
context.Write("\n");
|
|
|
|
|
context.Write(" /// ");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Write(SecurityElement.Escape(wline));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<string> WordWrap(string text, int width)
|
|
|
|
|
{
|
|
|
|
|
var lines = text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
|
|
|
foreach (var line in lines)
|
|
|
|
|
{
|
|
|
|
|
var processedLine = line.Trim();
|
|
|
|
|
|
|
|
|
|
// yield empty lines as they are (probably) intensional
|
|
|
|
|
if (processedLine.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
yield return processedLine;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// feast on the line until it's gone
|
|
|
|
|
while (processedLine.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
// determine potential wrapping points
|
|
|
|
|
var whitespacePositions = Enumerable
|
|
|
|
|
.Range(0, processedLine.Length)
|
|
|
|
|
.Where(i => char.IsWhiteSpace(processedLine[i]))
|
|
|
|
|
.Concat(new[] { processedLine.Length })
|
|
|
|
|
.Cast<int?>();
|
|
|
|
|
var preWidthWrapAt = whitespacePositions.LastOrDefault(i => i <= width);
|
|
|
|
|
var postWidthWrapAt = whitespacePositions.FirstOrDefault(i => i > width);
|
|
|
|
|
|
|
|
|
|
// choose preferred wrapping point
|
|
|
|
|
var wrapAt = preWidthWrapAt ?? postWidthWrapAt ?? processedLine.Length;
|
|
|
|
|
|
|
|
|
|
// wrap
|
|
|
|
|
yield return processedLine.Substring(0, wrapAt);
|
|
|
|
|
processedLine = processedLine.Substring(wrapAt).Trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 09:01:26 -08:00
|
|
|
public void ToInterpolationPathString(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
|
2021-10-14 06:55:19 -07:00
|
|
|
RenderBlock fn, RenderBlock inverse)
|
|
|
|
|
{
|
2021-12-19 09:01:26 -08:00
|
|
|
var p = arguments?.FirstOrDefault() as string;
|
|
|
|
|
if (p != null)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2021-12-19 09:01:26 -08:00
|
|
|
context.Write(Regex.Replace(p, "{(.+?)}", (m) => "{" + generalNameHelper.GetDotNetName(m.Groups[1].Value) + "}"));
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-19 09:01:26 -08:00
|
|
|
public void IfGroupPathParamContainsGroup(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
|
2021-10-14 06:55:19 -07:00
|
|
|
RenderBlock fn, RenderBlock inverse)
|
|
|
|
|
{
|
2021-12-19 09:01:26 -08:00
|
|
|
var p = arguments?.FirstOrDefault() as string;
|
|
|
|
|
if (p?.StartsWith("apis/{group}") == true)
|
2021-10-14 06:55:19 -07:00
|
|
|
{
|
2021-12-19 09:01:26 -08:00
|
|
|
fn(null);
|
2021-10-14 06:55:19 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|