Example #1
0
        public async Task <ProjectFile> DeserializeAsync(Stream stream, string projectFilePath, IMessageSink messageSink)
        {
            // Create a message repository that can be used to test if there were any errors.
            var messageRepository = new InMemoryMessageRepository();

            var compositeMessageSink = await this.CreateCompositeMessageSinkAsync(projectFilePath, messageSink, messageRepository, Constants.ProjectFileDeserializationFunctionalityName);

            // Test message output.
            await compositeMessageSink.AddOutputMessageAsync(this.NowUtcProvider, $"Deserialization of:\n{projectFilePath}");

            // Now deserialize.
            var projectFile = await this.RelativeFilePathsVisualStudioProjectFileSerializer.Deserialize(stream, compositeMessageSink);

            // Change all project reference paths to be absolute, not relative, using the input project file path.
            foreach (var projectReference in projectFile.ProjectReferences)
            {
                var projectReferenceAbsolutePath = this.StringlyTypedPathOperator.Combine(projectFilePath, projectReference.ProjectFilePath);

                projectReference.ProjectFilePath = projectReferenceAbsolutePath;
            }

            // Validate the project file.
            await this.ValidateProjectFileAsync(projectFile, compositeMessageSink);

            // If there are any error messages, and the deserializations settings say we should throw if there are any error messages, throw an exception.
            var errorMessages = await messageRepository.GetErrorsAsync();

            if (errorMessages.Any())
            {
                if (this.VisualStudioProjectFileDeserializationSettings.ThrowIfAnyErrorAtEnd)
                {
                    var messagesOutputFilePath = await this.GetMessagesOutputFilePathAsync(Constants.ProjectFileDeserializationFunctionalityName, projectFilePath);

                    throw new Exception($"There were deserialization errors. See:\n{messagesOutputFilePath}");
                }
            }

            return(projectFile);
        }