forked from Thealexbarney/LibHac
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRepackNuget.cs
132 lines (109 loc) · 4.33 KB
/
RepackNuget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using ICSharpCode.SharpZipLib.Zip;
using Nuke.Common.IO;
using Nuke.Common.Tools.NuGet;
namespace LibHacBuild;
public partial class Build
{
public void RepackNugetPackage(string path)
{
AbsolutePath tempDir = TempDirectory / Path.GetFileName(path);
AbsolutePath libDir = tempDir / "lib";
AbsolutePath relsFile = tempDir / "_rels" / ".rels";
try
{
tempDir.CreateOrCleanDirectory();
List<string> fileList = UnzipPackage(path, tempDir);
string newPsmdcpName = CalcPsmdcpName(libDir);
string newPsmdcpPath = RenamePsmdcp(tempDir, newPsmdcpName);
EditManifestRelationships(relsFile, newPsmdcpPath);
int index = fileList.FindIndex(x => x.Contains(".psmdcp"));
fileList[index] = newPsmdcpPath;
IEnumerable<string> files = Directory.EnumerateFiles(tempDir, "*.json", SearchOption.AllDirectories)
.Concat(Directory.EnumerateFiles(tempDir, "*.xml", SearchOption.AllDirectories))
.Concat(Directory.EnumerateFiles(tempDir, "*.rels", SearchOption.AllDirectories))
.Concat(Directory.EnumerateFiles(tempDir, "*.psmdcp", SearchOption.AllDirectories))
.Concat(Directory.EnumerateFiles(tempDir, "*.nuspec", SearchOption.AllDirectories));
foreach (string filename in files)
{
Console.WriteLine(filename);
ReplaceLineEndings(filename);
}
ZipDirectory(path, tempDir, fileList, CommitTime);
}
finally
{
Directory.Delete(tempDir, true);
}
}
public List<string> UnzipPackage(string package, string dest)
{
var fileList = new List<string>();
UnzipFiles(package, dest);
using (var s = new ZipInputStream(File.OpenRead(package)))
{
ZipEntry entry;
while ((entry = s.GetNextEntry()) != null)
{
fileList.Add(entry.Name);
}
}
return fileList;
}
public static string CalcPsmdcpName(string libDir)
{
using (var sha = SHA256.Create())
{
foreach (string file in Directory.EnumerateFiles(libDir))
{
byte[] data = File.ReadAllBytes(file);
sha.TransformBlock(data, 0, data.Length, data, 0);
}
sha.TransformFinalBlock(new byte[0], 0, 0);
return ToHexString(sha.Hash).ToLower().Substring(0, 32);
}
}
public static string RenamePsmdcp(string packageDir, string name)
{
string fileName = Directory.EnumerateFiles(packageDir, "*.psmdcp", SearchOption.AllDirectories).Single();
string newFileName = Path.Combine(Path.GetDirectoryName(fileName), name + ".psmdcp");
Directory.Move(fileName, newFileName);
return Path.GetRelativePath(packageDir, newFileName).Replace('\\', '/');
}
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
public void EditManifestRelationships(string path, string psmdcpPath)
{
XDocument doc = XDocument.Load(path);
XNamespace ns = doc.Root.GetDefaultNamespace();
foreach (XElement rs in doc.Root.Elements(ns + "Relationship"))
{
using (var sha = SHA256.Create())
{
if (rs.Attribute("Target").Value.Contains(".psmdcp"))
{
rs.Attribute("Target").Value = "/" + psmdcpPath;
}
string s = "/" + psmdcpPath + rs.Attribute("Target").Value;
byte[] hash = sha.ComputeHash(Encoding.UTF8.GetBytes(s));
string id = "R" + ToHexString(hash).Substring(0, 16);
rs.Attribute("Id").Value = id;
}
}
doc.Save(path);
}
public void SignNupkg(string pkgPath, string password)
{
NuGetTasks.NuGet($"sign \"{pkgPath}\" -CertificatePath cert.pfx -CertificatePassword {password:r} -Timestamper http://timestamp.digicert.com");
}
public static string ToHexString(byte[] arr)
{
return BitConverter.ToString(arr).ToLower().Replace("-", "");
}
}