Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit b735dab

Browse files
committed
Drivers and Uefi download tasks
1 parent c3e6853 commit b735dab

File tree

9 files changed

+245
-2
lines changed

9 files changed

+245
-2
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
DisplayMarkdown "Core\license.md"
2-
DeployWindows2
3-
InjectDrivers "Bla"
2+
UefiDownload
3+
DriversDownload
4+
DeployWindows
5+
InjectDrivers "Downloaded\Drivers"
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
5+
using System.IO.Compression;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Deployer.Execution;
9+
10+
namespace Deployer.Raspberry.Tasks
11+
{
12+
public class DriversDownload : IDeploymentTask
13+
{
14+
private readonly IGitHubDownloader downloader;
15+
private readonly IFileSystemOperations operations;
16+
17+
public DriversDownload(IGitHubDownloader downloader, IFileSystemOperations operations)
18+
{
19+
this.downloader = downloader;
20+
this.operations = operations;
21+
}
22+
23+
public async Task Execute()
24+
{
25+
using (var stream = await downloader.OpenZipStream("https://github.com/andreiw/RaspberryPiPkg"))
26+
{
27+
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
28+
29+
var root = zipArchive.Entries.First(x => x.FullName.EndsWith("Drivers/"));
30+
31+
var contents = zipArchive.Entries.Where(x => x.FullName.StartsWith(root.FullName) && !x.FullName.EndsWith("/"));
32+
await ExtractContents(@"Downloaded\Drivers", root, contents);
33+
}
34+
}
35+
36+
private async Task ExtractContents(string destination, ZipArchiveEntry baseEntry,
37+
IEnumerable<ZipArchiveEntry> entries)
38+
{
39+
foreach (var entry in entries)
40+
{
41+
var filePath = entry.FullName.Substring(baseEntry.FullName.Length);
42+
43+
var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
44+
var dir = Path.GetDirectoryName(destFile);
45+
if (!operations.DirectoryExists(dir))
46+
{
47+
operations.CreateDirectory(dir);
48+
}
49+
50+
using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
51+
using (var stream = entry.Open())
52+
{
53+
await stream.CopyToAsync(destStream);
54+
}
55+
}
56+
}
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+
}
98+
}
99+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
5+
using System.IO.Compression;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Deployer.Execution;
9+
10+
namespace Deployer.Raspberry.Tasks
11+
{
12+
public class UefiDownload : IDeploymentTask
13+
{
14+
private readonly IGitHubDownloader downloader;
15+
private readonly IFileSystemOperations operations;
16+
17+
public UefiDownload(IGitHubDownloader downloader, IFileSystemOperations operations)
18+
{
19+
this.downloader = downloader;
20+
this.operations = operations;
21+
}
22+
23+
public async Task Execute()
24+
{
25+
using (var stream = await downloader.OpenZipStream("https://github.com/andreiw/RaspberryPiPkg"))
26+
{
27+
var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read);
28+
29+
var mostRecentFolderEntry = GetMostRecentDirEntry(zipArchive);
30+
31+
var contents = zipArchive.Entries.Where(x => x.FullName.StartsWith(mostRecentFolderEntry.FullName) && !x.FullName.EndsWith("/"));
32+
await ExtractContents(@"Downloaded\UEFI", mostRecentFolderEntry, contents);
33+
}
34+
}
35+
36+
private async Task ExtractContents(string destination, ZipArchiveEntry baseEntry,
37+
IEnumerable<ZipArchiveEntry> entries)
38+
{
39+
foreach (var entry in entries)
40+
{
41+
var filePath = entry.FullName.Substring(baseEntry.FullName.Length);
42+
43+
var destFile = Path.Combine(destination, filePath.Replace("/", "\\"));
44+
var dir = Path.GetDirectoryName(destFile);
45+
if (!operations.DirectoryExists(dir))
46+
{
47+
operations.CreateDirectory(dir);
48+
}
49+
50+
using (var destStream = File.Open(destFile, FileMode.OpenOrCreate))
51+
using (var stream = entry.Open())
52+
{
53+
await stream.CopyToAsync(destStream);
54+
}
55+
}
56+
}
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+
}
98+
}
99+
}

Source/Deployer.Tests/Deployer.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20+
<ProjectReference Include="..\Deployer.Raspberry\Deployer.Raspberry.csproj" />
2021
<ProjectReference Include="..\Deployer\Deployer.csproj" />
2122
</ItemGroup>
2223

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using Deployer.Raspberry.Tasks;
3+
using Xunit;
4+
5+
namespace Deployer.Tests.Tasks
6+
{
7+
public class DriversDownloadTests
8+
{
9+
[Fact]
10+
public async Task Test()
11+
{
12+
var task = new DriversDownload(new GitHubDownloader(), new FileSystemOperations());
13+
await task.Execute();
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Threading.Tasks;
2+
using Deployer.Raspberry.Tasks;
3+
using Xunit;
4+
5+
namespace Deployer.Tests.Tasks
6+
{
7+
public class UefiDownloadTests
8+
{
9+
[Fact]
10+
public async Task Test()
11+
{
12+
var task = new UefiDownload(new GitHubDownloader(), new FileSystemOperations());
13+
await task.Execute();
14+
}
15+
}
16+
}

Source/Deployer/Execution/Testing/TestFileSystemOperations.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ public bool DirectoryExists(string path)
2828
{
2929
return Directory.Exists(path);
3030
}
31+
32+
public void CreateDirectory(string path)
33+
{
34+
}
3135
}
3236
}

Source/Deployer/FileSystemOperations.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public bool DirectoryExists(string path)
3030
{
3131
return Directory.Exists(path);
3232
}
33+
34+
public void CreateDirectory(string path)
35+
{
36+
Directory.CreateDirectory(path);
37+
}
3338
}
3439
}

Source/Deployer/IFileSystemOperations.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public interface IFileSystemOperations
88
Task CopyDirectory(string source, string destination);
99
Task DeleteDirectory(string path);
1010
bool DirectoryExists(string path);
11+
void CreateDirectory(string path);
1112
}
1213
}

0 commit comments

Comments
 (0)