LibHac/build/RepackNuget.cs

135 lines
4.4 KiB
C#
Raw Normal View History

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;
2020-02-29 09:18:10 +01:00
using Nuke.Common.IO;
using Nuke.Common.Tools.NuGet;
using static Nuke.Common.IO.FileSystemTasks;
2021-11-14 20:08:57 +01:00
namespace LibHacBuild;
public partial class Build
{
2021-11-14 20:08:57 +01:00
public void RepackNugetPackage(string path)
{
2021-11-14 20:08:57 +01:00
AbsolutePath tempDir = TempDirectory / Path.GetFileName(path);
AbsolutePath libDir = tempDir / "lib";
AbsolutePath relsFile = tempDir / "_rels" / ".rels";
2021-11-14 20:08:57 +01:00
try
{
EnsureCleanDirectory(tempDir);
List<string> fileList = UnzipPackage(path, tempDir);
2021-11-14 20:08:57 +01:00
string newPsmdcpName = CalcPsmdcpName(libDir);
string newPsmdcpPath = RenamePsmdcp(tempDir, newPsmdcpName);
EditManifestRelationships(relsFile, newPsmdcpPath);
2021-11-14 20:08:57 +01:00
int index = fileList.FindIndex(x => x.Contains(".psmdcp"));
fileList[index] = newPsmdcpPath;
2021-11-14 20:08:57 +01:00
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));
2021-11-14 20:08:57 +01:00
foreach (string filename in files)
{
2021-11-14 20:08:57 +01:00
Console.WriteLine(filename);
ReplaceLineEndings(filename);
}
ZipDirectory(path, tempDir, fileList, CommitTime);
2021-11-14 20:08:57 +01:00
}
finally
{
2021-11-14 20:08:57 +01:00
Directory.Delete(tempDir, true);
}
}
2021-11-14 20:08:57 +01:00
public List<string> UnzipPackage(string package, string dest)
{
var fileList = new List<string>();
UnzipFiles(package, dest);
2021-11-14 20:08:57 +01:00
using (var s = new ZipInputStream(File.OpenRead(package)))
{
ZipEntry entry;
while ((entry = s.GetNextEntry()) != null)
{
2021-11-14 20:08:57 +01:00
fileList.Add(entry.Name);
}
}
2021-11-14 20:08:57 +01:00
return fileList;
}
public static string CalcPsmdcpName(string libDir)
{
using (var sha = SHA256.Create())
{
2021-11-14 20:08:57 +01:00
foreach (string file in Directory.EnumerateFiles(libDir))
{
2021-11-14 20:08:57 +01:00
byte[] data = File.ReadAllBytes(file);
sha.TransformBlock(data, 0, data.Length, data, 0);
}
2021-11-14 20:08:57 +01:00
sha.TransformFinalBlock(new byte[0], 0, 0);
2021-11-14 20:08:57 +01:00
return ToHexString(sha.Hash).ToLower().Substring(0, 32);
}
2021-11-14 20:08:57 +01:00
}
2021-11-14 20:08:57 +01:00
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);
2021-11-14 20:08:57 +01:00
return Path.GetRelativePath(packageDir, newFileName).Replace('\\', '/');
}
2021-11-14 20:08:57 +01:00
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
public void EditManifestRelationships(string path, string psmdcpPath)
{
XDocument doc = XDocument.Load(path);
XNamespace ns = doc.Root.GetDefaultNamespace();
2021-11-14 20:08:57 +01:00
foreach (XElement rs in doc.Root.Elements(ns + "Relationship"))
{
using (var sha = SHA256.Create())
{
2021-11-14 20:08:57 +01:00
if (rs.Attribute("Target").Value.Contains(".psmdcp"))
{
2021-11-14 20:08:57 +01:00
rs.Attribute("Target").Value = "/" + psmdcpPath;
}
2021-11-14 20:08:57 +01:00
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;
}
}
2021-11-14 20:08:57 +01:00
doc.Save(path);
}
2021-11-14 20:08:57 +01:00
public void SignNupkg(string pkgPath, string password)
{
NuGetTasks.NuGet(
$"sign \"{pkgPath}\" -CertificatePath cert.pfx -CertificatePassword {password} -Timestamper http://timestamp.digicert.com",
outputFilter: x => x.Replace(password, "hunter2"));
}
public static string ToHexString(byte[] arr)
{
return BitConverter.ToString(arr).ToLower().Replace("-", "");
}
}