-
-
Notifications
You must be signed in to change notification settings - Fork 8
Introduce the per-run BuildContext foundation (FT-2) #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisonSimtian
wants to merge
2
commits into
Fallout-build:main
Choose a base branch
from
ChrisonSimtian:engine/ft2-build-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using Fallout.Common.Tooling; | ||
| using Fallout.Common.Utilities.Collections; | ||
| using Fallout.Common.ValueInjection; | ||
|
|
||
| namespace Fallout.Common.Execution; | ||
|
|
||
| /// <summary> | ||
| /// Per-run, process-ambient build state. Activated once at the top of <see cref="BuildManager.Execute{T}"/> | ||
| /// and disposed at the end of the run, so the process-global statics a build touches are owned by a | ||
| /// scope rather than leaking across invocations (the cleanup FT-1 centralised now lives here). | ||
| /// The static surface (e.g. <see cref="BuildManager.CancellationHandler"/>) reads through | ||
| /// <see cref="Current"/>; <c>AsyncLocal</c> keeps concurrent/nested runs isolated. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// FT-2 / <see href="https://github.com/Fallout-build/Fallout/issues/307">#307</see>. Intentionally | ||
| /// <c>internal</c> — not a public contract until the SDK lands (milestone #7). Subsequent steps move | ||
| /// the per-run services (parameters, logging scope, tool-path config) onto this context. | ||
| /// </remarks> | ||
| internal sealed class BuildContext : IDisposable | ||
| { | ||
| private static readonly AsyncLocal<BuildContext> s_current = new(); | ||
|
|
||
| /// <summary>The context for the current build run, or <c>null</c> outside a run.</summary> | ||
| public static BuildContext Current => s_current.Value; | ||
|
|
||
| private readonly LinkedList<Action> _cancellationHandlers = new(); | ||
| private readonly ConsoleCancelEventHandler _onCancelKeyPress; | ||
| private readonly EventHandler _onToolOptionsCreated; | ||
|
|
||
| private BuildContext() | ||
| { | ||
| _onCancelKeyPress = (_, _) => _cancellationHandlers.ForEach(x => x()); | ||
| _onToolOptionsCreated = (options, _) => VerbosityMapping.Apply((ToolOptions)options); | ||
| Console.CancelKeyPress += _onCancelKeyPress; | ||
| ToolOptions.Created += _onToolOptionsCreated; | ||
| } | ||
|
|
||
| /// <summary>Creates the ambient context for a build run and installs it as <see cref="Current"/>.</summary> | ||
| public static BuildContext Activate() => s_current.Value = new BuildContext(); | ||
|
|
||
| public void RegisterCancellationHandler(Action handler) => _cancellationHandlers.AddFirst(handler); | ||
|
|
||
| public void UnregisterCancellationHandler(Action handler) => _cancellationHandlers.Remove(handler); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Undo this run's process-global state (the FT-1 cleanup, now owned by the scope). | ||
| Console.CancelKeyPress -= _onCancelKeyPress; | ||
| ToolOptions.Created -= _onToolOptionsCreated; | ||
| Logging.InMemorySink.Instance.Clear(); | ||
| ValueInjectionUtility.ClearCache(); | ||
| NuGetToolPathResolver.Reset(); | ||
| NpmToolPathResolver.Reset(); | ||
|
|
||
| if (ReferenceEquals(s_current.Value, this)) | ||
| s_current.Value = null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably predating the rules change we made this week, so wrong naming convention...