Esempio n. 1
0
        private static void UpdateYamlFile(Options options, FilePusher.Models.Config config)
        {
            YamlStream yamlStream = new();

            using (StreamReader streamReader = new(config.SourcePath))
            {
                yamlStream.Load(streamReader);
            }

            if (yamlStream.Documents.Count > 1)
            {
                throw new NotSupportedException("Multi-document YAML files are not supported.");
            }

            YamlDocument doc         = yamlStream.Documents[0];
            YamlNode     currentNode = doc.RootNode;

            string[] queryParts = options.NodeQueryPath.Split('/');
            for (int i = 0; i < queryParts.Length; i++)
            {
                currentNode = currentNode[queryParts[i]];
            }

            if (currentNode is YamlScalarNode scalarNode)
            {
                scalarNode.Value = options.NewValue;
            }
            else
            {
                throw new NotSupportedException("Last node in the path must be a scalar value.");
            }

            StringBuilder stringBuilder = new();

            using StringWriter writer = new(stringBuilder);
            yamlStream.Save(new CustomEmitter(writer), assignAnchors: false);

            string newContent = stringBuilder.ToString();

            Console.WriteLine(
                $"Writing the following content to file '{config.SourcePath}':{Environment.NewLine}{newContent}");

            File.WriteAllText(config.SourcePath, stringBuilder.ToString());
        }
Esempio n. 2
0
        private static async Task ExecuteAsync(Options options)
        {
            // Hookup a TraceListener to capture details from Microsoft.DotNet.VersionTools
            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            string configJson = File.ReadAllText(options.ConfigPath);

            FilePusher.Models.Config config = JsonConvert.DeserializeObject <FilePusher.Models.Config>(configJson);

            UpdateYamlFile(options, config);

            FilePusher.Options filePusherOptions = new()
            {
                GitAuthToken = options.GitAuthToken,
                GitEmail     = options.GitEmail,
                GitUser      = options.GitUser
            };

            await FilePusher.FilePusher.PushFilesAsync(filePusherOptions, config);
        }