-
-
Notifications
You must be signed in to change notification settings - Fork 63
Strategy pattern for document loading #1165
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
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7f9c828
feat: add IOutputPlanner and IFileWriter interfaces
christianhelle 2b284a6
feat: add OutputPlannerAdapter implementing IOutputPlanner
christianhelle 4144715
feat: add CliFileWriter implementing IFileWriter
christianhelle d91c710
feat: add MsBuildFileWriter implementing IFileWriter
christianhelle 1d43365
feat: add SourceGeneratorFileWriter implementing IFileWriter
christianhelle 23ce51a
refactor: GenerationOrchestrator uses IOutputPlanner and IFileWriter
christianhelle 398bd84
refactor: RefitterGenerateTask uses IOutputPlanner and IFileWriter
christianhelle 4330ec0
refactor: source generator uses IFileWriter for file writing
christianhelle 4bd4c14
chore: remove unused imports from source generator
christianhelle 5e5abb5
feat: add IDocumentLoadingStrategy interface for strategy pattern
christianhelle d3563dc
feat: add PathUtilities helper (IsHttp, IsYaml)
christianhelle 244e337
feat: add FileDocumentStrategy for loading local OpenAPI specs
christianhelle 1838e74
feat: add HttpDocumentStrategy for loading remote OpenAPI specs
christianhelle 9d10924
feat: add OpenApiReaderDocumentStrategy for external resolution
christianhelle 62348f0
refactor: DocumentLoader becomes compositor using strategy pattern
christianhelle bef7b76
fix: apply CodeRabbit auto-fixes
coderabbitai[bot] 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,20 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Refitter.Core; | ||
|
|
||
| /// <summary> | ||
| /// Writes a <see cref="PlannedFile"/> to disk, creating directories as needed. | ||
| /// Each distribution form (CLI, MSBuild, Source Generator) provides its own adapter | ||
| /// that integrates with its own reporting/progress pipeline. | ||
| /// </summary> | ||
| public interface IFileWriter | ||
| { | ||
| /// <summary> | ||
| /// Writes the planned file to disk, creating directories as necessary. | ||
| /// </summary> | ||
| /// <param name="file">The planned file to write.</param> | ||
| /// <param name="cancellationToken">A cancellation token.</param> | ||
| /// <returns>A task representing the asynchronous write operation.</returns> | ||
| Task WriteAsync(PlannedFile file, CancellationToken cancellationToken = default); | ||
| } |
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,28 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace Refitter.Core; | ||
|
|
||
| /// <summary> | ||
| /// Resolves a <see cref="GeneratorOutput"/> into a list of <see cref="PlannedFile"/> instances. | ||
| /// Pure logic — no I/O. | ||
| /// </summary> | ||
| public interface IOutputPlanner | ||
| { | ||
| /// <summary> | ||
| /// Plans the output file paths for the given generator output. | ||
| /// </summary> | ||
| /// <param name="output">The generator output containing generated files.</param> | ||
| /// <param name="config">The output configuration.</param> | ||
| /// <param name="settingsFilePath"> | ||
| /// The path to the settings file, or <c>null</c> for direct CLI generation. | ||
| /// </param> | ||
| /// <param name="cliOutputPath"> | ||
| /// The output path specified via CLI, or <c>null</c>. | ||
| /// </param> | ||
| /// <returns>A read-only list of planned files with resolved paths and content.</returns> | ||
| IReadOnlyList<PlannedFile> Plan( | ||
| GeneratorOutput output, | ||
| IOutputConfiguration config, | ||
| string? settingsFilePath, | ||
| string? cliOutputPath); | ||
| } |
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,39 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace Refitter.Core; | ||
|
|
||
| /// <summary> | ||
| /// Adapts the static <see cref="OutputPlanner"/> methods to the <see cref="IOutputPlanner"/> interface. | ||
| /// </summary> | ||
| public class OutputPlannerAdapter : IOutputPlanner | ||
| { | ||
| /// <inheritdoc /> | ||
| public IReadOnlyList<PlannedFile> Plan( | ||
| GeneratorOutput output, | ||
| IOutputConfiguration config, | ||
| string? settingsFilePath, | ||
| string? cliOutputPath) | ||
| { | ||
| if (config.GenerateMultipleFiles) | ||
| { | ||
| return OutputPlanner.PlanMultipleFiles( | ||
| settingsFilePath, | ||
| cliOutputPath, | ||
| config, | ||
| output); | ||
| } | ||
|
|
||
| var code = output.Files.Count > 0 | ||
| ? output.Files[0].Content | ||
| : string.Empty; | ||
|
|
||
| return | ||
| [ | ||
| OutputPlanner.PlanSingleFile( | ||
| settingsFilePath, | ||
| cliOutputPath, | ||
| config, | ||
| code) | ||
| ]; | ||
| } | ||
| } |
35 changes: 35 additions & 0 deletions
35
src/Refitter.Core/Abstractions/SourceGeneratorFileWriter.cs
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,35 @@ | ||
| using System.IO; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Refitter.Core; | ||
|
|
||
| /// <summary> | ||
| /// Writes planned files to disk with content-equality checking. | ||
| /// Skips writing when the file already exists with identical content, | ||
| /// avoiding unnecessary disk writes during incremental / design-time builds. | ||
| /// </summary> | ||
| public class SourceGeneratorFileWriter : IFileWriter | ||
| { | ||
| /// <inheritdoc /> | ||
| public Task WriteAsync(PlannedFile file, CancellationToken cancellationToken = default) | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| var dir = Path.GetDirectoryName(file.Path); | ||
| if (!string.IsNullOrWhiteSpace(dir) && !Directory.Exists(dir)) | ||
| Directory.CreateDirectory(dir); | ||
|
|
||
| var existingContent = File.Exists(file.Path) | ||
| ? File.ReadAllText(file.Path, Encoding.UTF8) | ||
| : null; | ||
|
|
||
| if (existingContent is null || !string.Equals(existingContent, file.Content, System.StringComparison.Ordinal)) | ||
| { | ||
| File.WriteAllText(file.Path, file.Content, Encoding.UTF8); | ||
| } | ||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
| } |
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,31 @@ | ||
| using NSwag; | ||
| using OpenApiDocument = NSwag.OpenApiDocument; | ||
|
|
||
| namespace Refitter.Core; | ||
|
|
||
| internal sealed class FileDocumentStrategy : IDocumentLoadingStrategy | ||
| { | ||
| public async Task<OpenApiDocument?> TryLoadAsync( | ||
| string path, | ||
| CancellationToken cancellationToken = default) | ||
| { | ||
| if (PathUtilities.IsHttp(path)) | ||
| return null; | ||
|
|
||
| try | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| return PathUtilities.IsYaml(path) | ||
| ? await OpenApiYamlDocument.FromFileAsync(path, cancellationToken).ConfigureAwait(false) | ||
| : await OpenApiDocument.FromFileAsync(path, cancellationToken).ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (ex is OperationCanceledException or TaskCanceledException) | ||
| throw; | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.