Repository navigation
fix: Read minimum version requirements from the project runner pin #1506
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
Merged
Merged
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| using io.github.hatayama.UnityCliLoop.Domain; | ||
| using io.github.hatayama.UnityCliLoop.ToolContracts; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.Application | ||
| { | ||
| /// <summary> | ||
| /// Immutable snapshot of the CLI pin JSON shipped with the package. | ||
| /// </summary> | ||
| public readonly struct CliPin | ||
| { | ||
| public CliPin(string projectRunnerVersion, string minimumDispatcherVersion) | ||
| { | ||
| ProjectRunnerVersion = projectRunnerVersion; | ||
| MinimumDispatcherVersion = minimumDispatcherVersion; | ||
| } | ||
|
|
||
| public string ProjectRunnerVersion { get; } | ||
| public string MinimumDispatcherVersion { get; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Result of loading the CLI pin JSON. | ||
| /// </summary> | ||
| public readonly struct CliPinLoadResult | ||
| { | ||
| private CliPinLoadResult(bool success, CliPin pin, string errorMessage) | ||
| { | ||
| Success = success; | ||
| Pin = pin; | ||
| ErrorMessage = errorMessage; | ||
| } | ||
|
|
||
| public bool Success { get; } | ||
| public CliPin Pin { get; } | ||
| public string ErrorMessage { get; } | ||
|
|
||
| public static CliPinLoadResult FromSuccess(CliPin pin) | ||
| { | ||
| return new CliPinLoadResult(true, pin, string.Empty); | ||
| } | ||
|
|
||
| public static CliPinLoadResult FromFailure(string errorMessage) | ||
| { | ||
| return new CliPinLoadResult(false, default, errorMessage); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Reads the CLI pin JSON that ships with the Unity package so callers can | ||
| /// consult a single source of truth for minimum-version requirements. | ||
| /// </summary> | ||
| public static class CliPinReader | ||
| { | ||
| // Why: keep pin JSON keys named in one place so callers do not spread string literals. | ||
| private const string PIN_JSON_PROJECT_RUNNER_VERSION_KEY = "projectRunnerVersion"; | ||
| private const string PIN_JSON_MINIMUM_DISPATCHER_VERSION_KEY = "minimumDispatcherVersion"; | ||
|
|
||
| public static CliPinLoadResult LoadPackagePin() | ||
| { | ||
| string pinPath = Path.Combine( | ||
| UnityCliLoopConstants.PackageResolvedPath, | ||
| UnityCliLoopConstants.ULOOP_PROJECT_RUNNER_PIN_FILE_NAME); | ||
| return LoadPinFromPath(pinPath); | ||
| } | ||
|
|
||
| // Why: exposed so tests and edge-case callers can point at an alternate pin file layout. | ||
| public static CliPinLoadResult LoadPinFromPath(string pinPath) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(pinPath)) | ||
| { | ||
| return CliPinLoadResult.FromFailure("Unity CLI Loop pin file path is empty."); | ||
| } | ||
|
|
||
| if (!File.Exists(pinPath)) | ||
| { | ||
| return CliPinLoadResult.FromFailure( | ||
| $"Unity CLI Loop pin file not found at {pinPath}."); | ||
| } | ||
|
|
||
| string content = File.ReadAllText(pinPath); | ||
| if (string.IsNullOrWhiteSpace(content)) | ||
| { | ||
| return CliPinLoadResult.FromFailure( | ||
| $"Unity CLI Loop pin file at {pinPath} is empty."); | ||
| } | ||
|
|
||
| JObject parsed = JObject.Parse(content); | ||
| string projectRunnerVersion = parsed[PIN_JSON_PROJECT_RUNNER_VERSION_KEY]?.ToString(); | ||
| string minimumDispatcherVersion = parsed[PIN_JSON_MINIMUM_DISPATCHER_VERSION_KEY]?.ToString(); | ||
| if (string.IsNullOrWhiteSpace(projectRunnerVersion)) | ||
| { | ||
| return CliPinLoadResult.FromFailure( | ||
| $"Unity CLI Loop pin file at {pinPath} is missing {PIN_JSON_PROJECT_RUNNER_VERSION_KEY}."); | ||
| } | ||
| if (string.IsNullOrWhiteSpace(minimumDispatcherVersion)) | ||
| { | ||
| return CliPinLoadResult.FromFailure( | ||
| $"Unity CLI Loop pin file at {pinPath} is missing {PIN_JSON_MINIMUM_DISPATCHER_VERSION_KEY}."); | ||
| } | ||
|
|
||
| return CliPinLoadResult.FromSuccess( | ||
| new CliPin(projectRunnerVersion, minimumDispatcherVersion)); | ||
| } | ||
|
|
||
| // Why: composes the dispatcher release tag from the pin so callers do not have to know the prefix. | ||
| public static string BuildDispatcherReleaseTag(string minimumDispatcherVersion) | ||
| { | ||
| return CliConstants.DISPATCHER_RELEASE_TAG_PREFIX + minimumDispatcherVersion; | ||
| } | ||
|
|
||
| // Why: setup/detection paths must fail closed when the pin is unreadable rather than defaulting | ||
| // to "compatible", so both call sites share one Fail-Fast helper instead of duplicating it. | ||
| public static string LoadMinimumDispatcherVersionOrThrow() | ||
| { | ||
| CliPinLoadResult pinResult = LoadPackagePin(); | ||
| if (!pinResult.Success) | ||
| { | ||
| throw new InvalidOperationException( | ||
| "Unity CLI Loop cannot resolve minimum dispatcher version from the package pin: " | ||
| + pinResult.ErrorMessage); | ||
| } | ||
| return pinResult.Pin.MinimumDispatcherVersion; | ||
| } | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Unwrap
JObject.Parsefailures intoCliPinLoadResult.FromFailure.Every other failure mode here (missing file, empty content, missing keys) is surfaced via
CliPinLoadResult.FromFailurewith a descriptive message. A malformed pin JSON, however, will letJObject.Parsethrow a rawJsonExceptionstraight throughLoadPackagePin()/LoadMinimumDispatcherVersionOrThrow(), bypassing the friendly error wrapping and surfacing an opaque stack trace during setup instead of"Unity CLI Loop cannot resolve minimum dispatcher version from the package pin: ...".CliInstallationDetector.ParseCliContractOutputalready catchesJsonExceptionfor similarly untrusted content, so this would keep the pattern consistent.🛡️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents
Source: Learnings