Class TransformMultiFileScope
- Namespace
- Invex.FileSystem
- Assembly
- Invex.FileSystem.dll
Provides disposable scopes for performing temporary, reversible transformations on multiple files simultaneously. Upon disposal, the original content of every managed file is restored unless restoration is explicitly cancelled.
public sealed class TransformMultiFileScope
- Inheritance
-
objectTransformMultiFileScope
Remarks
The behaviour mirrors TransformFileScope but operates on a collection of
files in parallel (via Task.WhenAll). Each file's original content is captured
independently: files that did not exist before the scope was created are deleted on
disposal, while existing files have their content written back.
This is useful when a build step needs to simultaneously patch a set of related files — for example injecting a shared version number into multiple project files — and must guarantee that all files are restored even if the step fails partway through.
Call CancelRestore() before disposing to commit all transformations permanently instead of rolling them back.
Methods
Add(Func<string, string>)
Applies an additional synchronous transformation to every file within an already-open scope.
public TransformMultiFileScope Add(Func<string, string> transform)
Parameters
transformFunc<string, string>A function that receives each file's current (already-transformed) content and returns the next content to write.
Returns
- TransformMultiFileScope
The same TransformMultiFileScope instance, for fluent chaining.
Remarks
The scope always restores to the original contents captured at creation time,
not the state after the most recent Add call.
If CancelRestore() has been called, the transformation is skipped and the scope is returned unchanged.
Exceptions
- ObjectDisposedException
Thrown if the scope has already been disposed.
AddAsync(Func<string, string>, CancellationToken)
Applies an additional asynchronous transformation to every file within an already-open scope.
public Task<TransformMultiFileScope> AddAsync(Func<string, string> transform, CancellationToken cancellationToken = default)
Parameters
transformFunc<string, string>A function that receives each file's current (already-transformed) content and returns the next content to write.
cancellationTokenCancellationTokenToken to observe for cancellation. If cancelled during the write, all files are restored to their state at the time the scope was created before the exception propagates.
Returns
- Task<TransformMultiFileScope>
The same TransformMultiFileScope instance, for fluent chaining.
Remarks
The scope always restores to the original contents captured at creation time,
not the state after the most recent Add call.
If CancelRestore() has been called, the transformation is skipped and the scope is returned unchanged.
Exceptions
- ObjectDisposedException
Thrown if the scope has already been disposed.
- OperationCanceledException
Thrown (and all files restored) if
cancellationTokenis cancelled during the write.
CancelRestore()
Prevents all files from being restored to their original content when the scope is disposed.
public void CancelRestore()
Remarks
Call this when the transformation should be made permanent — for example after a build step has successfully consumed the transformed file and the change no longer needs to be rolled back. Once called it cannot be undone.
Create(IEnumerable<RootedPath>, Func<string, string>)
Creates a new TransformMultiFileScope, applying a synchronous
transformation to each file in files.
public static TransformMultiFileScope Create(IEnumerable<RootedPath> files, Func<string, string> transform)
Parameters
filesIEnumerable<RootedPath>The files to transform.
transformFunc<string, string>A function applied to each file's current content (or an empty string for files that do not yet exist).
Returns
- TransformMultiFileScope
A TransformMultiFileScope that will restore all files when disposed.
Remarks
files is enumerated exactly once; files are read and written
sequentially. Use CreateAsync(IEnumerable<RootedPath>, Func<string, string>, CancellationToken) to process files in parallel.
Files that do not exist are created on disk, the transform receives an empty string for them, and disposing the scope deletes them rather than restoring content.
CreateAsync(IEnumerable<RootedPath>, Func<string, string>, CancellationToken)
Creates a new TransformMultiFileScope, applying an asynchronous
transformation to each file in files.
public static Task<TransformMultiFileScope> CreateAsync(IEnumerable<RootedPath> files, Func<string, string> transform, CancellationToken cancellationToken = default)
Parameters
filesIEnumerable<RootedPath>The files to transform.
transformFunc<string, string>A function applied to each file's current content (or an empty string for files that do not yet exist). The same function is called for every file; if you need per-file logic consider multiple TransformFileScope instances instead.
cancellationTokenCancellationTokenToken to observe for cancellation. If cancelled while the transformed content is being written, all files are restored to their state at the time the scope was created before the exception propagates.
Returns
- Task<TransformMultiFileScope>
A TransformMultiFileScope that will restore all files when disposed.
Remarks
files is enumerated exactly once; files are read and written in
parallel via System.Threading.Tasks.Task.WhenAll(System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>).
Files that do not exist are created on disk, the transform receives an empty string for them, and disposing the scope deletes them rather than restoring content.
Exceptions
- OperationCanceledException
Thrown (and all files restored) if
cancellationTokenis cancelled while the transformed content is being written.
Dispose()
Synchronously disposes the scope and restores all managed files to their original state.
public void Dispose()
Remarks
- If CancelRestore() was called, all files are left as-is.
- Files that did not exist when the scope was created are deleted.
- All other files have their original content written back, one at a time.
- Calling this method more than once is safe — subsequent calls are no-ops.
Prefer DisposeAsync() (via await using) where possible: it restores
files in parallel and does not block the calling thread.
DisposeAsync()
Asynchronously disposes the scope and restores all managed files to their original state.
public ValueTask DisposeAsync()
Returns
- ValueTask
Remarks
- If CancelRestore() was called, all files are left as-is.
- Files that did not exist when the scope was created are deleted.
- All other files have their original content written back in parallel.
- Calling this method more than once is safe — subsequent calls are no-ops.
No cancellation token is accepted: restoring files should always complete to avoid leaving the repository in a partially-modified state.