Class TransformFileScope
- Namespace
- Invex.FileSystem
- Assembly
- Invex.FileSystem.dll
Provides disposable scopes for performing temporary, reversible transformations on a single file's content.
public sealed class TransformFileScope
- Inheritance
-
objectTransformFileScope
Examples
await using var scope = await TransformFileScope.CreateAsync(
myProjectFile,
content => content.Replace("1.0.0", "2.0.0"));
// File now contains "2.0.0". Original is restored when scope is disposed.
Remarks
On creation the file's current content is captured. The supplied transform is applied and written back immediately. On disposal the original content is restored, or — if the file did not exist before the scope was opened — the file is deleted.
This is useful during an operation when a file (e.g. a project file or a config file) needs to be temporarily modified for a specific step without permanently altering it.
Call CancelRestore() before disposing to commit the transformation instead of rolling it back.
Methods
Add(Func<string, string>)
Applies an additional transformation to the file within an already-open scope, using synchronous I/O.
public TransformFileScope Add(Func<string, string> transform)
Parameters
transformFunc<string, string>A function that receives the file's current (already-transformed) content and returns the next content to write.
Returns
- TransformFileScope
The same TransformFileScope instance, for fluent chaining.
Remarks
The scope still restores to the original content (captured at creation time),
not the content at the point of the last Add call. Multiple calls build up
transformations; disposal always unwinds them all at once.
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 transformation to the file within an already-open scope, using asynchronous I/O.
public Task<TransformFileScope> AddAsync(Func<string, string> transform, CancellationToken cancellationToken = default)
Parameters
transformFunc<string, string>A function that receives the file's current (already-transformed) content and returns the next content to write.
cancellationTokenCancellationTokenToken to observe for cancellation. If cancelled during the write, the file is restored to its state at the time the scope was created before the exception propagates.
Returns
- Task<TransformFileScope>
The same TransformFileScope instance, for fluent chaining.
Remarks
The scope still restores to the original content (captured at creation time),
not the content at the point of the last Add call. Multiple calls build up
transformations; disposal always unwinds them all at once.
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 the file restored) if
cancellationTokenis cancelled during the write.
CancelRestore()
Prevents the file from being restored to its 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(RootedPath, Func<string, string>)
Creates a new TransformFileScope, applying a synchronous transformation to the target file.
public static TransformFileScope Create(RootedPath file, Func<string, string> transform)
Parameters
fileRootedPathThe file to transform.
transformFunc<string, string>A function that receives the file's current content (or an empty string if the file does not yet exist) and returns the new content to write.
Returns
- TransformFileScope
A TransformFileScope that will restore the file when disposed.
Remarks
If the file does not exist it is created on disk, the transform receives an empty string, and disposing the scope deletes the file rather than restoring content.
CreateAsync(RootedPath, Func<string, string>, CancellationToken)
Creates a new TransformFileScope, applying an asynchronous transformation to the target file.
public static Task<TransformFileScope> CreateAsync(RootedPath file, Func<string, string> transform, CancellationToken cancellationToken = default)
Parameters
fileRootedPathThe file to transform.
transformFunc<string, string>A function that receives the file's current content (or an empty string if the file does not yet exist) and returns the new content to write.
cancellationTokenCancellationTokenToken to observe for cancellation. If cancelled while the transformed content is being written, the file is restored to its state at the time the scope was created before the exception propagates.
Returns
- Task<TransformFileScope>
A TransformFileScope that will restore the file when disposed.
Remarks
If the file does not exist it is created on disk, the transform receives an empty string, and disposing the scope deletes the file rather than restoring content.
Exceptions
- OperationCanceledException
Thrown (and the file restored) if
cancellationTokenis cancelled while the transformed content is being written.
Dispose()
Synchronously disposes the scope and restores the file to its original state.
public void Dispose()
Remarks
- If CancelRestore() was called, the file is left as-is.
- If the file did not exist when the scope was created, it is deleted.
- Otherwise the original content is written back.
- Calling this method more than once is safe — subsequent calls are no-ops.
Prefer DisposeAsync() (via await using) where possible so the restore
write does not block the calling thread.
DisposeAsync()
Asynchronously disposes the scope and restores the file to its original state.
public ValueTask DisposeAsync()
Returns
- ValueTask
Remarks
- If CancelRestore() was called, the file is left as-is.
- If the file did not exist when the scope was created, it is deleted.
- Otherwise the original content is written back.
- Calling this method more than once is safe — subsequent calls are no-ops.
No cancellation token is accepted here: we prefer to wait for the write to complete so the file is not left in a partially-written state.