Exemplo n.º 1
0
        private string AddCommentElement(string contents, string xmlKey, string sentence)
        {
            var line = xmlNoneCommentOutSectionRegEx.Match(sentence);

            if (!line.Success)
            {
                return("");
            }

            // get none white spaced section
            var section = line.Groups.Values
                          .Where(x => !string.IsNullOrWhiteSpace(x.Value))
                          .Where(group => group.Value.StartsWith($"<{xmlKey}"))
                          .FirstOrDefault();

            // indent should be white space or equivalants.
            var indent = line.Groups.Values.FirstOrDefault(x => string.IsNullOrWhiteSpace(x.Value));

            if (section == null || string.IsNullOrWhiteSpace(section.Value))
            {
                return("");
            }

            // SHOULD BE: {indent}<-- {section} -->
            var sectionEol       = FileEolDetector.TrimEnd(section.Value.AsSpan(), line.Value);
            var commentedSection = $"{indent.ToString()}<!-- {sectionEol} -->";

            var newProp = contents.Replace(line.Value, commentedSection);

            return(newProp);
        }
Exemplo n.º 2
0
        private void Save(string path, string content, bool isDryrun)
        {
            var encoding = FileBomDetector.Detect(path);
            var eol      = FileEolDetector.Detect(path).GetLabel();

            content = content.Replace("\r", "").Replace("\n", eol);

            if (isDryrun)
            {
                _logger.LogInformation($"dry run detected. skip perform change; {nameof(path)}={path}");
                _logger.LogDebug($"```xml");
                _logger.LogDebug(content);
                _logger.LogDebug($"```");
                return;
            }

            File.WriteAllText(path, content, encoding);
        }