Browse Source

Ignore none existing fragment for back compat

feature/auto_ssh
nicolas.dorier 6 years ago
parent
commit
c13cbc4559
  1. 47
      docker-compose-generator/src/DockerComposeDefinition.cs

47
docker-compose-generator/src/DockerComposeDefinition.cs

@ -42,21 +42,29 @@ namespace DockerGenerator
var serializer = new SerializerBuilder().Build();
Console.WriteLine($"With fragments:");
foreach(var fragment in Fragments)
foreach (var fragment in Fragments)
{
var fragmentPath = GetFragmentLocation(fragment);
if (!File.Exists(fragmentPath))
{
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);
}
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);
}
@ -85,26 +93,26 @@ namespace DockerGenerator
.Where(_ => _.MainNode != null)
.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;
if(!_.MainNode.Children.TryGetValue(child.Key, out var mainChildValue))
if (!_.MainNode.Children.TryGetValue(child.Key, out var mainChildValue))
{
mainChildValue = 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());
if(eof != null)
if (eof != null)
{
leafMainScalar.Value = leafMainScalar.Value + eof + leaf.Value;
}
@ -120,9 +128,9 @@ namespace DockerGenerator
}
}
}
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);
}
@ -135,19 +143,24 @@ namespace DockerGenerator
private string EOF(string value)
{
if(value.Contains("\r\n", StringComparison.OrdinalIgnoreCase))
if (value.Contains("\r\n", StringComparison.OrdinalIgnoreCase))
return "\r\n";
if(value.Contains("\n", StringComparison.OrdinalIgnoreCase))
if (value.Contains("\n", StringComparison.OrdinalIgnoreCase))
return "\n";
return null;
}
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();
stream.Load(input);
return (YamlMappingNode)stream.Documents[0].RootNode;
}
private string GetFragmentLocation(string fragment)
{
return Path.Combine(FragmentLocation, $"{fragment}.yml");
}
}
}

Loading…
Cancel
Save