1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text . RegularExpressions ;
5+ using System . Threading . Tasks ;
6+
7+ namespace Deployer . Raspberry
8+ {
9+ public class RaspberryPathBuilder : IPathBuilder
10+ {
11+ private readonly IDevice device ;
12+
13+ public RaspberryPathBuilder ( IDevice device )
14+ {
15+ this . device = device ;
16+ }
17+
18+ public async Task < string > Replace ( string str )
19+ {
20+ IDictionary < string , Func < Task < string > > > mappings = new Dictionary < string , Func < Task < string > > > ( )
21+ {
22+ { "WindowsARM" , async ( ) => ( await device . GetWindowsVolume ( ) ) . RootDir . Name } ,
23+ } ;
24+
25+ var matching = mappings . Keys . FirstOrDefault ( s => str . StartsWith ( s , StringComparison . OrdinalIgnoreCase ) ) ;
26+ if ( matching != null )
27+ {
28+ var replacement = await mappings [ matching ] ( ) ;
29+ var replaced = Regex . Replace ( str , $ "^{ matching } ", replacement , RegexOptions . IgnoreCase ) ;
30+ return Regex . Replace ( replaced , $@ "\\+", @"\" , RegexOptions . IgnoreCase ) ;
31+ }
32+
33+ return str ;
34+ }
35+
36+ private async Task < string > Replace ( string str , string identifier , Func < Task < string > > func )
37+ {
38+ var matching = str . StartsWith ( identifier , StringComparison . OrdinalIgnoreCase ) ;
39+ if ( matching )
40+ {
41+ var replacement = await func ( ) ;
42+ return Regex . Replace ( str , $ "^{ identifier } ", replacement , RegexOptions . IgnoreCase ) ;
43+ }
44+
45+ return str ;
46+ }
47+ }
48+ }
0 commit comments