diff --git a/src/components/tbc.host/Components/GlobalUsingsResolver/GlobalUsingsResolver.cs b/src/components/tbc.host/Components/GlobalUsingsResolver/GlobalUsingsResolver.cs index 04b996e..1fd362a 100644 --- a/src/components/tbc.host/Components/GlobalUsingsResolver/GlobalUsingsResolver.cs +++ b/src/components/tbc.host/Components/GlobalUsingsResolver/GlobalUsingsResolver.cs @@ -14,6 +14,8 @@ namespace Tbc.Host.Components.GlobalUsingsResolver; public class GlobalUsingsResolver : ComponentBase, IGlobalUsingsResolver { + private const string DefaultGlobalUsingsPattern = "*.GlobalUsings.g.cs"; + private IFileSystem _fileSystem; public GlobalUsingsResolver(ILogger logger, IFileSystem fileSystem) : base(logger) @@ -71,8 +73,9 @@ source.Kind switch string path, string? maybeResolutionMethod) { maybeResolutionMethod ??= KnownGlobalUsingSearchPathResolutionApproach.LastModified; - - var matches = _fileSystem.Directory.GetFiles(path, "*.GlobalUsings.g.cs", SearchOption.AllDirectories) + + var (search, mask) = GetFilePathAndMask(path); + var matches = _fileSystem.Directory.GetFiles(search, mask, SearchOption.AllDirectories) .OrderByDescending(x => _fileSystem.FileInfo.FromFileName(x).LastWriteTime) .ToList(); @@ -83,4 +86,24 @@ source.Kind switch return (usings.SelectMany(x => x.u).ToList(), usings.ToDictionary(x => x.f, x => (object) $"{x.u.Count} usings extracted")); } + + // use a gross heuristic to decide whether we think the user provided a filemask + // one day implement polymorphic deserialization in config so that the different kinds of global using references + // can use their own sensible fields + private (string Path, string Kask) GetFilePathAndMask(string path) + { + // this will be + // - nothing, if a directory was specified with trailing / + // - the leaf directory name, if a directory was specified without trailing / + // - a filename/mask, if one was specified + var maybeFn = _fileSystem.Path.GetFileName(path); + if (!maybeFn.EndsWith(".cs")) // assume any filemask specified ends in .cs + return (path, DefaultGlobalUsingsPattern); + + var search = _fileSystem.Path.GetDirectoryName(path); + if (String.IsNullOrWhiteSpace(search)) // allow the user to just specify a mask/filename + search = "."; + + return (search, maybeFn); + } }