コード例 #1
0
        public void Build()
        {
            Console.WriteLine($"Generating {GetFilePath()}");
            var deserializer = new DeserializerBuilder().Build();
            var serializer   = new SerializerBuilder().Build();

            Console.WriteLine($"With fragments:");
            foreach (var fragment in Fragments.ToList())
            {
                var fragmentPath = GetFragmentLocation(fragment);
                if (!File.Exists(fragmentPath))
                {
                    Console.WriteLine($"\t{fragment} not found in {fragmentPath}, ignoring...");
                    Fragments.Remove(fragment);
                }
                else
                {
                    Console.WriteLine($"\t{fragment}");
                }
            }

            var services = new List <KeyValuePair <YamlNode, YamlNode> >();
            var volumes  = new List <KeyValuePair <YamlNode, YamlNode> >();
            var networks = new List <KeyValuePair <YamlNode, YamlNode> >();

            foreach (var doc in Fragments.Select(f => ParseDocument(f)))
            {
                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)
                {
                    volumes.AddRange(fragmentVolumesRoot.Children);
                }
                if (doc.Children.ContainsKey("networks") && doc.Children["networks"] is YamlMappingNode fragmentNetworksRoot)
                {
                    networks.AddRange(fragmentNetworksRoot.Children);
                }
            }


            YamlMappingNode output = new YamlMappingNode();

            output.Add("version", new YamlScalarNode("3")
            {
                Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted
            });
            output.Add("services", new YamlMappingNode(Merge(services)));
            output.Add("volumes", new YamlMappingNode(volumes));
            output.Add("networks", new YamlMappingNode(networks));
            var result     = serializer.Serialize(output);
            var outputFile = GetFilePath();

            File.WriteAllText(outputFile, result.Replace("''", ""));
            Console.WriteLine($"Generated {outputFile}");
            Console.WriteLine();
        }
        public void Build()
        {
            Console.WriteLine($"Generating {GetFilePath()}");
            var deserializer = new DeserializerBuilder().Build();
            var serializer   = new SerializerBuilder().Build();

            Console.WriteLine($"With fragments:");
            foreach (var fragment in Fragments.ToList())
            {
                var fragmentPath = GetFragmentLocation(fragment);
                if (!File.Exists(fragmentPath))
                {
                    Console.WriteLine($"\t{fragment} not found in {fragmentPath}, ignoring...");
                    Fragments.Remove(fragment);
                }
                else
                {
                    Console.WriteLine($"\t{fragment}");
                }
            }

            var services = new List <KeyValuePair <YamlNode, YamlNode> >();
            var volumes  = new List <KeyValuePair <YamlNode, YamlNode> >();
            var networks = new List <KeyValuePair <YamlNode, YamlNode> >();

            foreach (var doc in Fragments.Select(f => ParseDocument(f)))
            {
                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)
                {
                    volumes.AddRange(fragmentVolumesRoot.Children);
                }
                if (doc.Children.ContainsKey("networks") && doc.Children["networks"] is YamlMappingNode fragmentNetworksRoot)
                {
                    networks.AddRange(fragmentNetworksRoot.Children);
                }
            }


            YamlMappingNode output = new YamlMappingNode();

            output.Add("version", new YamlScalarNode("3")
            {
                Style = YamlDotNet.Core.ScalarStyle.DoubleQuoted
            });
            output.Add("services", new YamlMappingNode(Merge(services)));
            output.Add("volumes", new YamlMappingNode(volumes));
            output.Add("networks", new YamlMappingNode(networks));


            var dockerImages = ((YamlMappingNode)output["services"]).Children.Select(kv => kv.Value["image"].ToString()).ToList();

            dockerImages.Add("btcpayserver/docker-compose-builder:1.23.2");
            dockerImages.Add("btcpayserver/docker-compose-generator:latest");
            StringBuilder pullImageSh = new StringBuilder();

            pullImageSh.Append($"#!/bin/bash\n\n");
            pullImageSh.Append($"# This script is automatically generated via the docker-compose generator and can be use to pull all required docker images \n");
            foreach (var image in dockerImages)
            {
                pullImageSh.Append($"docker pull $BTCPAY_DOCKER_PULL_FLAGS \"{image}\"\n");
            }
            var outputFile = GetFilePath("pull-images.sh");

            File.WriteAllText(outputFile, pullImageSh.ToString());
            Console.WriteLine($"Generated {outputFile}");

            StringBuilder saveImages = new StringBuilder();

            saveImages.Append($"#!/bin/bash\n\n");
            saveImages.Append($"# This script is automatically generated via the docker-compose generator and can be use to save the docker images in an archive \n");
            saveImages.Append($"# ./save-images.sh output.tar \n");
            saveImages.Append($"docker save -o \"$1\" \\\n {string.Join(" \\\n", dockerImages.Select(o => $"\"{o}\""))}");
            outputFile = GetFilePath("save-images.sh");
            File.WriteAllText(outputFile, saveImages.ToString());
            Console.WriteLine($"Generated {outputFile}");

            var result = serializer.Serialize(output);

            outputFile = GetFilePath();
            File.WriteAllText(outputFile, result.Replace("''", ""));
            Console.WriteLine($"Generated {outputFile}");
            Console.WriteLine();
        }