mirror of
https://github.com/aljazceru/btcpayserver-docker.git
synced 2025-12-18 21:24:21 +01:00
Ignore none existing fragment for back compat
This commit is contained in:
@@ -8,146 +8,159 @@ using System.IO;
|
|||||||
|
|
||||||
namespace DockerGenerator
|
namespace DockerGenerator
|
||||||
{
|
{
|
||||||
public class DockerComposeDefinition
|
public class DockerComposeDefinition
|
||||||
{
|
{
|
||||||
public List<string> Fragments
|
public List<string> Fragments
|
||||||
{
|
{
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
private string _Name;
|
private string _Name;
|
||||||
|
|
||||||
public DockerComposeDefinition(string name, List<string> fragments)
|
public DockerComposeDefinition(string name, List<string> fragments)
|
||||||
{
|
{
|
||||||
Fragments = fragments;
|
Fragments = fragments;
|
||||||
_Name = name;
|
_Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string FragmentLocation
|
public string FragmentLocation
|
||||||
{
|
{
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
public string BuildOutputDirectory
|
public string BuildOutputDirectory
|
||||||
{
|
{
|
||||||
get; set;
|
get; set;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetFilePath()
|
public string GetFilePath()
|
||||||
{
|
{
|
||||||
return Path.Combine(BuildOutputDirectory, $"docker-compose.{_Name}.yml");
|
return Path.Combine(BuildOutputDirectory, $"docker-compose.{_Name}.yml");
|
||||||
}
|
}
|
||||||
public void Build()
|
public void Build()
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Generating {GetFilePath()}");
|
Console.WriteLine($"Generating {GetFilePath()}");
|
||||||
var deserializer = new DeserializerBuilder().Build();
|
var deserializer = new DeserializerBuilder().Build();
|
||||||
var serializer = new SerializerBuilder().Build();
|
var serializer = new SerializerBuilder().Build();
|
||||||
|
|
||||||
Console.WriteLine($"With fragments:");
|
Console.WriteLine($"With fragments:");
|
||||||
foreach(var fragment in Fragments)
|
foreach (var fragment in Fragments)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"\t{fragment}");
|
var fragmentPath = GetFragmentLocation(fragment);
|
||||||
}
|
if (!File.Exists(fragmentPath))
|
||||||
var services = new List<KeyValuePair<YamlNode, YamlNode>>();
|
{
|
||||||
var volumes = new List<KeyValuePair<YamlNode, YamlNode>>();
|
Console.WriteLine($"\t{fragment} not found in {fragmentPath}, ignoring...");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"\t{fragment}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var services = new List<KeyValuePair<YamlNode, YamlNode>>();
|
||||||
|
var volumes = new List<KeyValuePair<YamlNode, YamlNode>>();
|
||||||
|
|
||||||
foreach(var doc in Fragments.Select(f => ParseDocument(f)))
|
foreach (var doc in Fragments.Select(f => ParseDocument(f)))
|
||||||
{
|
{
|
||||||
if(doc.Children.ContainsKey("services") && doc.Children["services"] is YamlMappingNode fragmentServicesRoot)
|
if (doc.Children.ContainsKey("services") && doc.Children["services"] is YamlMappingNode fragmentServicesRoot)
|
||||||
{
|
{
|
||||||
services.AddRange(fragmentServicesRoot.Children);
|
services.AddRange(fragmentServicesRoot.Children);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(doc.Children.ContainsKey("volumes") && doc.Children["volumes"] is YamlMappingNode fragmentVolumesRoot)
|
if (doc.Children.ContainsKey("volumes") && doc.Children["volumes"] is YamlMappingNode fragmentVolumesRoot)
|
||||||
{
|
{
|
||||||
volumes.AddRange(fragmentVolumesRoot.Children);
|
volumes.AddRange(fragmentVolumesRoot.Children);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
YamlMappingNode output = new YamlMappingNode();
|
YamlMappingNode output = new YamlMappingNode();
|
||||||
output.Add("version", new YamlScalarNode("3") { Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted });
|
output.Add("version", new YamlScalarNode("3") { Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted });
|
||||||
output.Add("services", new YamlMappingNode(Merge(services)));
|
output.Add("services", new YamlMappingNode(Merge(services)));
|
||||||
output.Add("volumes", new YamlMappingNode(volumes));
|
output.Add("volumes", new YamlMappingNode(volumes));
|
||||||
var result = serializer.Serialize(output);
|
var result = serializer.Serialize(output);
|
||||||
var outputFile = GetFilePath();
|
var outputFile = GetFilePath();
|
||||||
File.WriteAllText(outputFile, result.Replace("''", ""));
|
File.WriteAllText(outputFile, result.Replace("''", ""));
|
||||||
Console.WriteLine($"Generated {outputFile}");
|
Console.WriteLine($"Generated {outputFile}");
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
private KeyValuePair<YamlNode, YamlNode>[] Merge(List<KeyValuePair<YamlNode, YamlNode>> services)
|
private KeyValuePair<YamlNode, YamlNode>[] Merge(List<KeyValuePair<YamlNode, YamlNode>> services)
|
||||||
{
|
{
|
||||||
return services
|
return services
|
||||||
.GroupBy(s => s.Key.ToString(), s => s.Value)
|
.GroupBy(s => s.Key.ToString(), s => s.Value)
|
||||||
.Select(group =>
|
.Select(group =>
|
||||||
(GroupName: group.Key,
|
(GroupName: group.Key,
|
||||||
MainNode: group.OfType<YamlMappingNode>().SingleOrDefault(n => n.Children.ContainsKey("image")),
|
MainNode: group.OfType<YamlMappingNode>().SingleOrDefault(n => n.Children.ContainsKey("image")),
|
||||||
MergedNodes: group.OfType<YamlMappingNode>().Where(n => !n.Children.ContainsKey("image"))))
|
MergedNodes: group.OfType<YamlMappingNode>().Where(n => !n.Children.ContainsKey("image"))))
|
||||||
.Where(_ => _.MainNode != null)
|
.Where(_ => _.MainNode != null)
|
||||||
.Select(_ =>
|
.Select(_ =>
|
||||||
{
|
{
|
||||||
foreach(var node in _.MergedNodes)
|
foreach (var node in _.MergedNodes)
|
||||||
{
|
{
|
||||||
foreach(var child in node)
|
foreach (var child in node)
|
||||||
{
|
{
|
||||||
var childValue = child.Value;
|
var childValue = child.Value;
|
||||||
if(!_.MainNode.Children.TryGetValue(child.Key, out var mainChildValue))
|
if (!_.MainNode.Children.TryGetValue(child.Key, out var mainChildValue))
|
||||||
{
|
{
|
||||||
mainChildValue = child.Value;
|
mainChildValue = child.Value;
|
||||||
_.MainNode.Add(child.Key, child.Value);
|
_.MainNode.Add(child.Key, child.Value);
|
||||||
}
|
}
|
||||||
else if(childValue is YamlMappingNode childMapping && mainChildValue is YamlMappingNode mainChildMapping)
|
else if (childValue is YamlMappingNode childMapping && mainChildValue is YamlMappingNode mainChildMapping)
|
||||||
{
|
{
|
||||||
foreach(var leaf in childMapping)
|
foreach (var leaf in childMapping)
|
||||||
{
|
{
|
||||||
if(mainChildMapping.Children.TryGetValue(leaf.Key, out var mainLeaf))
|
if (mainChildMapping.Children.TryGetValue(leaf.Key, out var mainLeaf))
|
||||||
{
|
{
|
||||||
if(leaf.Value is YamlScalarNode leafScalar && mainLeaf is YamlScalarNode leafMainScalar)
|
if (leaf.Value is YamlScalarNode leafScalar && mainLeaf is YamlScalarNode leafMainScalar)
|
||||||
{
|
{
|
||||||
var eof = EOF(leafMainScalar.Value) ?? EOF(leaf.Value.ToString());
|
var eof = EOF(leafMainScalar.Value) ?? EOF(leaf.Value.ToString());
|
||||||
if(eof != null)
|
if (eof != null)
|
||||||
{
|
{
|
||||||
leafMainScalar.Value = leafMainScalar.Value + eof + leaf.Value;
|
leafMainScalar.Value = leafMainScalar.Value + eof + leaf.Value;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
leafMainScalar.Value = leafMainScalar.Value + "," + leaf.Value;
|
leafMainScalar.Value = leafMainScalar.Value + "," + leaf.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
mainChildMapping.Add(leaf.Key, leaf.Value);
|
mainChildMapping.Add(leaf.Key, leaf.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(childValue is YamlSequenceNode childSequence && mainChildValue is YamlSequenceNode mainSequence)
|
else if (childValue is YamlSequenceNode childSequence && mainChildValue is YamlSequenceNode mainSequence)
|
||||||
{
|
{
|
||||||
foreach(var c in childSequence.Children)
|
foreach (var c in childSequence.Children)
|
||||||
{
|
{
|
||||||
mainSequence.Add(c);
|
mainSequence.Add(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new KeyValuePair<YamlNode, YamlNode>(_.GroupName, _.MainNode);
|
return new KeyValuePair<YamlNode, YamlNode>(_.GroupName, _.MainNode);
|
||||||
}).ToArray();
|
}).ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private string EOF(string value)
|
private string EOF(string value)
|
||||||
{
|
{
|
||||||
if(value.Contains("\r\n", StringComparison.OrdinalIgnoreCase))
|
if (value.Contains("\r\n", StringComparison.OrdinalIgnoreCase))
|
||||||
return "\r\n";
|
return "\r\n";
|
||||||
if(value.Contains("\n", StringComparison.OrdinalIgnoreCase))
|
if (value.Contains("\n", StringComparison.OrdinalIgnoreCase))
|
||||||
return "\n";
|
return "\n";
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private YamlMappingNode ParseDocument(string fragment)
|
private YamlMappingNode ParseDocument(string fragment)
|
||||||
{
|
{
|
||||||
var input = new StringReader(File.ReadAllText(Path.Combine(FragmentLocation, $"{fragment}.yml")));
|
var input = new StringReader(File.ReadAllText(GetFragmentLocation(fragment)));
|
||||||
YamlStream stream = new YamlStream();
|
YamlStream stream = new YamlStream();
|
||||||
stream.Load(input);
|
stream.Load(input);
|
||||||
return (YamlMappingNode)stream.Documents[0].RootNode;
|
return (YamlMappingNode)stream.Documents[0].RootNode;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private string GetFragmentLocation(string fragment)
|
||||||
|
{
|
||||||
|
return Path.Combine(FragmentLocation, $"{fragment}.yml");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user