1- using System ;
2- using System . Collections . Generic ;
3- using System . Globalization ;
1+ using System . Collections . Generic ;
42using System . IO ;
53using System . IO . Compression ;
64using System . Linq ;
97
108namespace Deployer . Raspberry . Tasks
119{
10+ [ TaskDescription ( "Downloading Main Driver Package" ) ]
1211 public class DriversDownload : IDeploymentTask
1312 {
1413 private readonly IGitHubDownloader downloader ;
15- private readonly IFileSystemOperations operations ;
14+ private readonly IFileSystemOperations fileSystemOperations ;
15+ private const string DownloadFolder = @"Downloaded\Drivers" ;
1616
17- public DriversDownload ( IGitHubDownloader downloader , IFileSystemOperations operations )
17+ public DriversDownload ( IGitHubDownloader downloader , IFileSystemOperations fileSystemOperations )
1818 {
1919 this . downloader = downloader ;
20- this . operations = operations ;
20+ this . fileSystemOperations = fileSystemOperations ;
2121 }
2222
2323 public async Task Execute ( )
2424 {
25+ if ( fileSystemOperations . DirectoryExists ( "Drivers" ) )
26+ {
27+ return ;
28+ }
29+
2530 using ( var stream = await downloader . OpenZipStream ( "https://github.com/andreiw/RaspberryPiPkg" ) )
2631 {
2732 var zipArchive = new ZipArchive ( stream , ZipArchiveMode . Read ) ;
2833
2934 var root = zipArchive . Entries . First ( x => x . FullName . EndsWith ( "Drivers/" ) ) ;
3035
3136 var contents = zipArchive . Entries . Where ( x => x . FullName . StartsWith ( root . FullName ) && ! x . FullName . EndsWith ( "/" ) ) ;
32- await ExtractContents ( @"Downloaded\Drivers" , root , contents ) ;
37+
38+ await ExtractContents ( DownloadFolder , root , contents ) ;
3339 }
3440 }
3541
@@ -42,9 +48,9 @@ private async Task ExtractContents(string destination, ZipArchiveEntry baseEntry
4248
4349 var destFile = Path . Combine ( destination , filePath . Replace ( "/" , "\\ " ) ) ;
4450 var dir = Path . GetDirectoryName ( destFile ) ;
45- if ( ! operations . DirectoryExists ( dir ) )
51+ if ( ! fileSystemOperations . DirectoryExists ( dir ) )
4652 {
47- operations . CreateDirectory ( dir ) ;
53+ fileSystemOperations . CreateDirectory ( dir ) ;
4854 }
4955
5056 using ( var destStream = File . Open ( destFile , FileMode . OpenOrCreate ) )
@@ -54,46 +60,5 @@ private async Task ExtractContents(string destination, ZipArchiveEntry baseEntry
5460 }
5561 }
5662 }
57-
58- private ZipArchiveEntry GetMostRecentDirEntry ( ZipArchive p )
59- {
60- var dirs = from e in p . Entries
61- where e . FullName . EndsWith ( "/" )
62- select e ;
63-
64- var splitted = from e in dirs
65- select new
66- {
67- e ,
68- Parts = e . FullName . Split ( '/' ) ,
69- } ;
70-
71- var parsed = from r in splitted
72- select new
73- {
74- r . e ,
75- Date = FirstParseableOrNull ( r . Parts ) ,
76- } ;
77-
78- return parsed . OrderByDescending ( x => x . Date ) . First ( ) . e ;
79- }
80-
81- private DateTime ? FirstParseableOrNull ( string [ ] parts )
82- {
83- foreach ( var part in parts )
84- {
85- var candidate = part . Split ( '-' ) ;
86- if ( candidate . Length > 1 )
87- {
88- var datePart = candidate [ 0 ] ;
89- if ( DateTime . TryParseExact ( datePart , "yyyyMMMdd" , CultureInfo . InvariantCulture , DateTimeStyles . None , out var date ) )
90- {
91- return date ;
92- }
93- }
94- }
95-
96- return null ;
97- }
9863 }
9964}
0 commit comments