11using System ;
22using System . Collections . Generic ;
33using System . IO . Abstractions ;
4+ using Microsoft . Extensions . DependencyInjection ;
45
56namespace PG . StarWarsGame . Engine . Testing ;
67
78/// <summary>Builds a temp-directory-backed <see cref="VirtualGameRepo"/> from raw file content.</summary>
89public sealed class VirtualGameRepoBuilder
910{
11+ private readonly IServiceProvider _services ;
1012 private readonly IFileSystem _fs ;
1113 private readonly string _tempRoot ;
1214 private readonly string _gameRoot ;
13- private readonly List < string > _modPaths = new ( ) ;
14- private readonly List < string > _fallbackPaths = new ( ) ;
15+ private readonly List < string > _modPaths = [ ] ;
16+ private readonly List < string > _fallbackPaths = [ ] ;
1517 private string ? _fallbackGamePath ;
1618
1719 /// <summary>Initializes a new instance of the <see cref="VirtualGameRepoBuilder"/> class.</summary>
18- /// <param name="fileSystem ">The file system used for all builder I/O .</param>
19- /// <exception cref="ArgumentNullException"><paramref name="fileSystem "/> is <see langword="null"/>.</exception>
20- public VirtualGameRepoBuilder ( IFileSystem fileSystem )
20+ /// <param name="services ">The service provider supplying the file system and file-format services used by the builder.</param>
21+ /// <exception cref="ArgumentNullException"><paramref name="services "/> is <see langword="null"/>.</exception>
22+ public VirtualGameRepoBuilder ( IServiceProvider services )
2123 {
22- _fs = fileSystem ?? throw new ArgumentNullException ( nameof ( fileSystem ) ) ;
24+ _services = services ?? throw new ArgumentNullException ( nameof ( services ) ) ;
25+ _fs = services . GetRequiredService < IFileSystem > ( ) ;
2326 _tempRoot = _fs . Path . Combine ( _fs . Path . GetTempPath ( ) ,
2427 $ "PG.StarWarsGame.Engine.Testing.{ Guid . NewGuid ( ) : N} ") ;
2528 _gameRoot = _fs . Path . Combine ( _tempRoot , "game" ) ;
@@ -31,10 +34,10 @@ public VirtualGameRepoBuilder(IFileSystem fileSystem)
3134 /// <exception cref="ArgumentNullException"><paramref name="configure"/> is <see langword="null"/>.</exception>
3235 public VirtualGameRepoBuilder WithGame ( Action < IRepoOriginWriter > configure )
3336 {
34- if ( configure == null )
37+ if ( configure == null )
3538 throw new ArgumentNullException ( nameof ( configure ) ) ;
36-
37- configure ( new RepoOriginWriter ( _fs , _gameRoot ) ) ;
39+
40+ configure ( new RepoOriginWriter ( _services , _gameRoot ) ) ;
3841 return this ;
3942 }
4043
@@ -45,13 +48,13 @@ public VirtualGameRepoBuilder WithFallbackGame(Action<IRepoOriginWriter> configu
4548 {
4649 if ( configure == null )
4750 throw new ArgumentNullException ( nameof ( configure ) ) ;
48-
51+
4952 if ( _fallbackGamePath == null )
5053 {
5154 _fallbackGamePath = _fs . Path . Combine ( _tempRoot , "fallback" , "_primary" ) ;
5255 _fs . Directory . CreateDirectory ( _fallbackGamePath ) ;
5356 }
54- configure ( new RepoOriginWriter ( _fs , _fallbackGamePath ) ) ;
57+ configure ( new RepoOriginWriter ( _services , _fallbackGamePath ) ) ;
5558 return this ;
5659 }
5760
@@ -62,18 +65,18 @@ public VirtualGameRepoBuilder WithFallbackGame(Action<IRepoOriginWriter> configu
6265 /// <exception cref="ArgumentNullException"><paramref name="configure"/> is <see langword="null"/>.</exception>
6366 public VirtualGameRepoBuilder WithFallback ( string name , Action < IRepoOriginWriter > configure )
6467 {
65- if ( string . IsNullOrEmpty ( name ) )
68+ if ( string . IsNullOrEmpty ( name ) )
6669 throw new ArgumentException ( "Fallback name must be non-empty." , nameof ( name ) ) ;
67- if ( configure == null )
70+ if ( configure == null )
6871 throw new ArgumentNullException ( nameof ( configure ) ) ;
69-
72+
7073 var dir = _fs . Path . Combine ( _tempRoot , "fallback" , name ) ;
7174 if ( ! _fallbackPaths . Contains ( dir ) )
7275 {
7376 _fs . Directory . CreateDirectory ( dir ) ;
7477 _fallbackPaths . Add ( dir ) ;
7578 }
76- configure ( new RepoOriginWriter ( _fs , dir ) ) ;
79+ configure ( new RepoOriginWriter ( _services , dir ) ) ;
7780 return this ;
7881 }
7982
@@ -89,14 +92,14 @@ public VirtualGameRepoBuilder WithMod(string name, Action<IRepoOriginWriter> con
8992 throw new ArgumentException ( "Mod name must be non-empty." , nameof ( name ) ) ;
9093 if ( configure == null )
9194 throw new ArgumentNullException ( nameof ( configure ) ) ;
92-
95+
9396 var dir = _fs . Path . Combine ( _tempRoot , "mods" , name ) ;
9497 if ( ! _modPaths . Contains ( dir ) )
9598 {
9699 _fs . Directory . CreateDirectory ( dir ) ;
97100 _modPaths . Add ( dir ) ;
98101 }
99- configure ( new RepoOriginWriter ( _fs , dir ) ) ;
102+ configure ( new RepoOriginWriter ( _services , dir ) ) ;
100103 return this ;
101104 }
102105
0 commit comments