internal protected override async Task UpdateAsync(Stream stream, string newVersion, CancellationToken cancellationToken)
        {
            var encoding = await StreamUtilities.GetEncodingAsync(stream, cancellationToken).ConfigureAwait(false);

            stream.Seek(0, SeekOrigin.Begin);

            string text;

            using (var textReader = StreamUtilities.CreateReader(stream, encoding))
            {
                text = await textReader.ReadToEndAsync().ConfigureAwait(false);
            }

            var jobject = JObject.Parse(text);

            if (jobject.SelectToken(JsonPath) is JValue token)
            {
                token.Value = newVersion;

                stream.SetLength(0);

                using var textWriter = StreamUtilities.CreateWriter(stream, encoding);
                using var jsonWriter = new JsonTextWriter(textWriter)
                      {
                          Formatting = Formatting.Indented,
                      };

                await jobject.WriteToAsync(jsonWriter, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                throw new DependencyScannerException("Dependency not found");
            }
        }
示例#2
0
        protected internal override async Task UpdateAsync(Stream stream, string newVersion, CancellationToken cancellationToken)
        {
            if (newVersion.IndexOfAny(s_newLineCharacters) >= 0)
            {
                throw new ArgumentException("New version contains a \\r or \\n", nameof(newVersion));
            }

            // Line and Column are 1-based index.
            var line   = LineNumber - 1;
            var column = LinePosition - 1;

            if (line >= 0 && column >= 0)
            {
                var encoding = await StreamUtilities.GetEncodingAsync(stream, cancellationToken).ConfigureAwait(false);

                stream.Seek(0, SeekOrigin.Begin);

                string content;
                using (var reader = StreamUtilities.CreateReader(stream, encoding))
                {
                    content = await reader.ReadToEndAsync().ConfigureAwait(false);
                }

                var currentLine  = 0;
                var currentIndex = 0;
                int endOfLine;
                if (line > 0)
                {
                    while (currentLine < line && currentIndex < content.Length)
                    {
                        var newIndex = content.IndexOf('\n', currentIndex);
                        if (newIndex == -1)
                        {
                            throw new DependencyScannerException("Dependency not found. File was probably modified since last scan.");
                        }

                        currentIndex = newIndex + 1;
                        currentLine++;
                    }
                }

                endOfLine = content.IndexOf('\n', currentIndex);
                if (currentIndex + column + Length > (endOfLine == -1 ? content.Length : endOfLine))
                {
                    throw new DependencyScannerException("Dependency not found. File was probably modified since last scan.");
                }

                stream.SetLength(0);
                var writer = StreamUtilities.CreateWriter(stream, encoding);
                try
                {
                    var contentAsMemory = content.AsMemory();
                    await writer.WriteAsync(contentAsMemory[0..(currentIndex + column)], cancellationToken).ConfigureAwait(false);