/// <summary> /// Asynchronously transforms a file. /// </summary> /// <param name="streamTaskFactory">A stream task factory.</param> /// <param name="targetPath">A path to the file to be transformed.</param> /// <param name="projectSystem">The project where this change is taking place.</param> /// <param name="cancellationToken">A cancellation token.</param> /// <returns>A task that represents the asynchronous operation.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="streamTaskFactory" /> /// is <c>null</c>.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="projectSystem" /> /// is <c>null</c>.</exception> /// <exception cref="OperationCanceledException">Thrown if <paramref name="cancellationToken" /> /// is cancelled.</exception> public async Task TransformFileAsync( Func <Task <Stream> > streamTaskFactory, string targetPath, IMSBuildProjectSystem projectSystem, CancellationToken cancellationToken) { if (streamTaskFactory == null) { throw new ArgumentNullException(nameof(streamTaskFactory)); } if (projectSystem == null) { throw new ArgumentNullException(nameof(projectSystem)); } cancellationToken.ThrowIfCancellationRequested(); // Get the xml fragment var xmlFragment = await GetXmlAsync(streamTaskFactory, projectSystem, cancellationToken); var transformDocument = XmlUtility.GetOrCreateDocument(xmlFragment.Name, targetPath, projectSystem); // Do a merge transformDocument.Root.MergeWith(xmlFragment, _nodeActions); MSBuildNuGetProjectSystemUtility.AddFile(projectSystem, targetPath, transformDocument.Save); }
public static XDocument CreateDocument(XName rootName, string path, IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem) { XDocument document = new XDocument(new XElement(rootName)); // Add it to the project system MSBuildNuGetProjectSystemUtility.AddFile(msBuildNuGetProjectSystem, path, document.Save); return(document); }
public void TransformFile(Func <Stream> fileStreamFactory, string targetPath, IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem) { // Get the xml fragment var xmlFragment = GetXml(fileStreamFactory, msBuildNuGetProjectSystem); var transformDocument = XmlUtility.GetOrCreateDocument(xmlFragment.Name, targetPath, msBuildNuGetProjectSystem); // Do a merge transformDocument.Root.MergeWith(xmlFragment, _nodeActions); MSBuildNuGetProjectSystemUtility.AddFile(msBuildNuGetProjectSystem, targetPath, transformDocument.Save); }
public void TransformFile(ZipArchiveEntry packageFile, string targetPath, IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem) { // Get the xml fragment XElement xmlFragment = GetXml(packageFile, msBuildNuGetProjectSystem); XDocument transformDocument = XmlUtility.GetOrCreateDocument(xmlFragment.Name, targetPath, msBuildNuGetProjectSystem); // Do a merge transformDocument.Root.MergeWith(xmlFragment, _nodeActions); MSBuildNuGetProjectSystemUtility.AddFile(msBuildNuGetProjectSystem, targetPath, transformDocument.Save); }
internal static async Task PerformXdtTransformAsync( Func <Task <Stream> > streamTaskFactory, string targetPath, IMSBuildProjectSystem msBuildNuGetProjectSystem, CancellationToken cancellationToken) { if (FileSystemUtility.FileExists(msBuildNuGetProjectSystem.ProjectFullPath, targetPath)) { var content = await Preprocessor.ProcessAsync(streamTaskFactory, msBuildNuGetProjectSystem, cancellationToken); try { using (var transformation = new XmlTransformation(content, isTransformAFile: false, logger: null)) { using (var document = new XmlTransformableDocument()) { // make sure we close the input stream immediately so that we can override // the file below when we save to it. string path = FileSystemUtility.GetFullPath(msBuildNuGetProjectSystem.ProjectFullPath, targetPath); using (FileStream fileStream = File.OpenRead(path)) { using var xmlReader = XmlReader.Create(fileStream, GetXmlReaderSettings(LoadOptions.PreserveWhitespace)); document.Load(xmlReader); } var succeeded = transformation.Apply(document); if (succeeded) { // save the result into a memoryStream first so that if there is any // exception during document.Save(), the original file won't be truncated. MSBuildNuGetProjectSystemUtility.AddFile(msBuildNuGetProjectSystem, targetPath, document.Save); } } } } catch (Exception exception) { throw new InvalidDataException( string.Format( CultureInfo.CurrentCulture, Strings.XdtError + " " + exception.Message, targetPath, msBuildNuGetProjectSystem.ProjectName), exception); } } }
private static void PerformXdtTransform(Func <Stream> fileStreamFactory, string targetPath, IMSBuildNuGetProjectSystem msBuildNuGetProjectSystem) { if (FileSystemUtility.FileExists(msBuildNuGetProjectSystem.ProjectFullPath, targetPath)) { var content = Preprocessor.Process(fileStreamFactory, msBuildNuGetProjectSystem); try { using (var transformation = new XmlTransformation(content, isTransformAFile: false, logger: null)) { using (var document = new XmlTransformableDocument()) { document.PreserveWhitespace = true; // make sure we close the input stream immediately so that we can override // the file below when we save to it. using (var inputStream = File.OpenRead(FileSystemUtility.GetFullPath(msBuildNuGetProjectSystem.ProjectFullPath, targetPath))) { document.Load(inputStream); } var succeeded = transformation.Apply(document); if (succeeded) { // save the result into a memoryStream first so that if there is any // exception during document.Save(), the original file won't be truncated. MSBuildNuGetProjectSystemUtility.AddFile(msBuildNuGetProjectSystem, targetPath, document.Save); } } } } catch (Exception exception) { throw new InvalidDataException( string.Format( CultureInfo.CurrentCulture, Strings.XdtError + " " + exception.Message, targetPath, msBuildNuGetProjectSystem.ProjectName), exception); } } }