-
Notifications
You must be signed in to change notification settings - Fork 0
Add --required-files CLI argument with glob pattern matching and solution reference check
#43
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
6 commits
Select commit
Hold shift + click to select a range
1564a23
Initial plan
Copilot 4d22767
Implement --required-files CLI argument with glob matching, tests, an…
Copilot 6e1405e
Add last check: required files must be referenced as <File> in the .s…
Copilot a9600b4
Refactor --required-files: move to Core with DI, integrate with valid…
Copilot ec1f478
Add SlnxFileRefs DTO, options records, extend IFileSystem; convert te…
Copilot e956c34
Rename SlnxFileRefs→SlnxFile, remove try-catch, add #regions/AAA, spl…
Copilot 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
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
22 changes: 22 additions & 0 deletions
22
src/SLNX-validator.Core/Validation/IRequiredFilesChecker.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,22 @@ | ||
| using JulianVerdurmen.SlnxValidator.Core.ValidationResults; | ||
|
|
||
| namespace JulianVerdurmen.SlnxValidator.Core.Validation; | ||
|
|
||
| public interface IRequiredFilesChecker | ||
| { | ||
| /// <summary> | ||
| /// Resolves semicolon-separated glob patterns against <paramref name="rootDirectory"/> | ||
| /// and returns the matched absolute paths. Returns an empty list when no files match. | ||
| /// </summary> | ||
| IReadOnlyList<string> ResolveMatchedPaths(string patternsRaw, string rootDirectory); | ||
|
|
||
| /// <summary> | ||
| /// Checks which of the <paramref name="requiredAbsolutePaths"/> are NOT present in | ||
| /// <paramref name="slnxFile"/>. | ||
| /// Returns a <see cref="ValidationError"/> for each missing file. | ||
| /// </summary> | ||
| IReadOnlyList<ValidationError> CheckInSlnx( | ||
| IReadOnlyList<string> requiredAbsolutePaths, | ||
| SlnxFile slnxFile); | ||
| } | ||
|
|
53 changes: 53 additions & 0 deletions
53
src/SLNX-validator.Core/Validation/RequiredFilesChecker.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,53 @@ | ||
| using JulianVerdurmen.SlnxValidator.Core.ValidationResults; | ||
| using Microsoft.Extensions.FileSystemGlobbing; | ||
| using Microsoft.Extensions.FileSystemGlobbing.Abstractions; | ||
|
|
||
| namespace JulianVerdurmen.SlnxValidator.Core.Validation; | ||
|
|
||
| internal sealed class RequiredFilesChecker : IRequiredFilesChecker | ||
| { | ||
| /// <inheritdoc /> | ||
| public IReadOnlyList<string> ResolveMatchedPaths(string patternsRaw, string rootDirectory) | ||
| { | ||
| var patterns = patternsRaw.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
|
|
||
| var matcher = new Matcher(StringComparison.OrdinalIgnoreCase, preserveFilterOrder: true); | ||
|
|
||
| foreach (var pattern in patterns) | ||
| { | ||
| if (pattern.StartsWith('!')) | ||
| matcher.AddExclude(pattern[1..]); | ||
| else | ||
| matcher.AddInclude(pattern); | ||
| } | ||
|
|
||
| var directoryInfo = new DirectoryInfoWrapper(new DirectoryInfo(rootDirectory)); | ||
| var result = matcher.Execute(directoryInfo); | ||
|
|
||
| return result.HasMatches | ||
| ? result.Files.Select(f => Path.GetFullPath(Path.Combine(rootDirectory, f.Path))).ToList() | ||
| : []; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IReadOnlyList<ValidationError> CheckInSlnx( | ||
| IReadOnlyList<string> requiredAbsolutePaths, | ||
| SlnxFile slnxFile) | ||
| { | ||
| var errors = new List<ValidationError>(); | ||
| foreach (var requiredPath in requiredAbsolutePaths) | ||
|
Check warning on line 38 in src/SLNX-validator.Core/Validation/RequiredFilesChecker.cs
|
||
| { | ||
| if (!slnxFile.Files.Contains(requiredPath, StringComparer.OrdinalIgnoreCase)) | ||
| { | ||
| var relativePath = Path.GetRelativePath(slnxFile.SlnxDirectory, requiredPath).Replace('\\', '/'); | ||
| errors.Add(new ValidationError( | ||
| ValidationErrorCode.RequiredFileNotReferencedInSolution, | ||
| $"Required file is not referenced in the solution: {requiredPath}" + | ||
| $" — add: <File Path=\"{relativePath}\" />")); | ||
| } | ||
| } | ||
|
|
||
| return errors; | ||
| } | ||
| } | ||
|
|
||
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,57 @@ | ||
| using System.Xml.Linq; | ||
|
|
||
| namespace JulianVerdurmen.SlnxValidator.Core.Validation; | ||
|
|
||
| /// <summary> | ||
| /// Represents the set of absolute file paths that are referenced as | ||
| /// <c><File Path="..."></c> elements inside a .slnx solution file. | ||
| /// </summary> | ||
| public sealed class SlnxFile | ||
| { | ||
| /// <summary>The directory that contains the .slnx file.</summary> | ||
| public string SlnxDirectory { get; } | ||
|
|
||
| /// <summary>Absolute, normalised paths for every <c><File></c> entry in the solution.</summary> | ||
| public IReadOnlyList<string> Files { get; } | ||
|
|
||
| private SlnxFile(string slnxDirectory, IReadOnlyList<string> files) | ||
| { | ||
| SlnxDirectory = slnxDirectory; | ||
| Files = files; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Parses <paramref name="slnxContent"/> and returns the resolved absolute paths of all | ||
| /// <c><File Path="..."></c> elements. Relative paths are resolved against | ||
| /// <paramref name="slnxDirectory"/>. | ||
| /// </summary> | ||
| /// <returns>The parsed <see cref="SlnxFile"/>, or <see langword="null"/> when the XML is malformed.</returns> | ||
| public static SlnxFile? Parse(string slnxContent, string slnxDirectory) | ||
| { | ||
| XDocument doc; | ||
| try | ||
| { | ||
| doc = XDocument.Parse(slnxContent); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| var refs = new HashSet<string>(StringComparer.OrdinalIgnoreCase); | ||
| foreach (var fileElement in doc.Descendants("File")) | ||
| { | ||
| var path = fileElement.Attribute("Path")?.Value; | ||
| if (path is null) | ||
| continue; | ||
|
|
||
| var fullPath = Path.IsPathRooted(path) | ||
| ? Path.GetFullPath(path) | ||
| : Path.GetFullPath(Path.Combine(slnxDirectory, path)); | ||
|
|
||
| refs.Add(fullPath); | ||
| } | ||
|
|
||
| return new SlnxFile(slnxDirectory, [.. refs]); | ||
| } | ||
| } |
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,15 @@ | ||
| namespace JulianVerdurmen.SlnxValidator; | ||
|
|
||
| /// <summary> | ||
| /// Bundled options for the <c>--required-files</c> feature passed to | ||
| /// <see cref="ValidationCollector.CollectAsync"/>. | ||
| /// </summary> | ||
| /// <param name="MatchedPaths"> | ||
| /// Absolute disk paths that were matched by <see cref="Pattern"/>. | ||
| /// An empty list means the pattern matched no files. | ||
| /// <see langword="null"/> means the <c>--required-files</c> option was not used. | ||
| /// </param> | ||
| /// <param name="Pattern">The raw semicolon-separated pattern string supplied by the user.</param> | ||
| internal sealed record RequiredFilesOptions( | ||
| IReadOnlyList<string>? MatchedPaths, | ||
| string? Pattern); |
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.
Create options type for the parameters
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.
Done in ec1f478. Created
RequiredFilesOptions(IReadOnlyList<string>? MatchedPaths, string? Pattern)record.CollectAsyncnow takesRequiredFilesOptions? requiredFilesOptionsinstead of two separate nullable parameters.