mirror of
https://github.com/Thealexbarney/LibHac.git
synced 2024-11-14 10:49:41 +01:00
Build script updates (#33)
- Make generated NuGet packages deterministic. - Add a signing option to the build script. - Set versions for pre-release builds. - Publish packages to a MyGet feed. - Make Windows-produced artifacts match Linux-produced ones
This commit is contained in:
parent
0e118ae170
commit
b68261a092
9 changed files with 671 additions and 235 deletions
|
@ -1 +1 @@
|
|||
2.1.500
|
||||
2.2.103
|
5
GitVersion.yml
Normal file
5
GitVersion.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
mode: ContinuousDeployment
|
||||
increment: Patch
|
||||
branches:
|
||||
master:
|
||||
tag: alpha
|
|
@ -1,5 +1,8 @@
|
|||
version: 0.1.3-{build}
|
||||
image: Visual Studio 2017
|
||||
environment:
|
||||
myget_api_key:
|
||||
secure: 0xJoYAtR6psXCRvk1qm5czDObkeRjHKPjfe5gIExXVFPwA0VVODYv/hBZYUtz2F3
|
||||
build_script:
|
||||
- ps: .\build.ps1
|
||||
test: off
|
18
build.ps1
18
build.ps1
|
@ -1,6 +1,7 @@
|
|||
[CmdletBinding()]
|
||||
Param(
|
||||
#[switch]$CustomParam,
|
||||
[switch]$BuildDotnetCoreOnly,
|
||||
[Parameter(Position = 0, Mandatory = $false, ValueFromRemainingArguments = $true)]
|
||||
[string[]]$BuildArguments
|
||||
)
|
||||
|
@ -43,14 +44,17 @@ try {
|
|||
if (Test-Path $DotNetGlobalFile) {
|
||||
$DotNetVersion = $(Get-Content $DotNetGlobalFile | Out-String | ConvertFrom-Json).sdk.version
|
||||
}
|
||||
|
||||
$DotNetDirectory = "$TempDirectory\dotnet-win"
|
||||
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
|
||||
|
||||
# If dotnet is installed locally, and expected version is not set or installation matches the expected version
|
||||
if ($null -ne (Get-Command "dotnet" -ErrorAction SilentlyContinue) -and `
|
||||
(!(Test-Path variable:DotNetVersion) -or $(& cmd.exe /c 'dotnet --version 2>&1') -eq $DotNetVersion)) {
|
||||
$env:DOTNET_EXE = (Get-Command "dotnet").Path
|
||||
}
|
||||
else {
|
||||
$DotNetDirectory = "$TempDirectory\dotnet-win"
|
||||
$env:DOTNET_EXE = "$DotNetDirectory\dotnet.exe"
|
||||
elseif ($null -eq (Get-Command $env:DOTNET_EXE -ErrorAction SilentlyContinue) -and `
|
||||
(!(Test-Path variable:DotNetVersion) -or $(& cmd.exe /c '$env:DOTNET_EXE --version 2>&1') -ne $DotNetVersion)) {
|
||||
|
||||
# Download install script
|
||||
$DotNetInstallFile = "$TempDirectory\dotnet-install.ps1"
|
||||
|
@ -68,8 +72,16 @@ try {
|
|||
|
||||
Write-Output "Microsoft (R) .NET Core SDK version $(& $env:DOTNET_EXE --version)"
|
||||
|
||||
if($BuildDotnetCoreOnly) {
|
||||
$BuildArguments += "-DoCoreBuildOnly"
|
||||
$BuildArguments += "true"
|
||||
}
|
||||
|
||||
ExecSafe { & $env:DOTNET_EXE run --project $BuildProjectFile -- $BuildArguments }
|
||||
}
|
||||
catch {
|
||||
Write-Output $_.Exception.Message
|
||||
}
|
||||
finally {
|
||||
if (Test-Path $DotNetGlobalFile) {
|
||||
Remove-Item $DotNetGlobalFile
|
||||
|
|
16
build.sh
16
build.sh
|
@ -3,7 +3,7 @@
|
|||
echo $(bash --version 2>&1 | head -n 1)
|
||||
|
||||
#CUSTOMPARAM=0
|
||||
BUILD_ARGUMENTS=()
|
||||
BUILD_ARGUMENTS=("-DoCoreBuildOnly")
|
||||
for i in "$@"; do
|
||||
case $(echo $1 | awk '{print tolower($0)}') in
|
||||
# -custom-param) CUSTOMPARAM=1;;
|
||||
|
@ -37,30 +37,34 @@ export NUGET_XMLDOC_MODE="skip"
|
|||
function FirstJsonValue {
|
||||
perl -nle 'print $1 if m{"'$1'": "([^"\-]+)",?}' <<< ${@:2}
|
||||
}
|
||||
trap "rm -f $DOTNET_GLOBAL_FILE" INT TERM EXIT
|
||||
trap "rm -f \"$DOTNET_GLOBAL_FILE\"" INT TERM EXIT
|
||||
|
||||
dotnetCliVersion=$(cat DotnetCliVersion.txt)
|
||||
|
||||
json="{\"sdk\":{\"version\":\"$dotnetCliVersion\"}}"
|
||||
echo "$json" > $DOTNET_GLOBAL_FILE
|
||||
echo "$json" > "$DOTNET_GLOBAL_FILE"
|
||||
|
||||
# If global.json exists, load expected version
|
||||
if [ -f "$DOTNET_GLOBAL_FILE" ]; then
|
||||
DOTNET_VERSION=$dotnetCliVersion
|
||||
fi
|
||||
|
||||
DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix"
|
||||
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"
|
||||
|
||||
# If dotnet is installed locally, and expected version is not set or installation matches the expected version
|
||||
if [[ -x "$(command -v dotnet)" && (-z ${DOTNET_VERSION+x} || $(dotnet --version) == "$DOTNET_VERSION") ]]; then
|
||||
export DOTNET_EXE="$(command -v dotnet)"
|
||||
else
|
||||
DOTNET_DIRECTORY="$TEMP_DIRECTORY/dotnet-unix"
|
||||
export DOTNET_EXE="$DOTNET_DIRECTORY/dotnet"
|
||||
elif [[ ! (-x "$DOTNET_EXE" && (-z ${DOTNET_VERSION+x} || $($DOTNET_EXE --version) == "$DOTNET_VERSION")) ]]; then
|
||||
|
||||
# Download install script
|
||||
DOTNET_INSTALL_FILE="$TEMP_DIRECTORY/dotnet-install.sh"
|
||||
mkdir -p "$TEMP_DIRECTORY"
|
||||
|
||||
if [ ! -x "$DOTNET_INSTALL_FILE" ]; then
|
||||
curl -Lsfo "$DOTNET_INSTALL_FILE" "$DOTNET_INSTALL_URL"
|
||||
chmod +x "$DOTNET_INSTALL_FILE"
|
||||
fi
|
||||
|
||||
# Install by channel or version
|
||||
if [ -z ${DOTNET_VERSION+x} ]; then
|
||||
|
|
313
build/Build.cs
313
build/Build.cs
|
@ -4,29 +4,41 @@ using System.Diagnostics;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using ICSharpCode.SharpZipLib.Zip;
|
||||
using ILRepacking;
|
||||
using Nuke.Common;
|
||||
using Nuke.Common.BuildServers;
|
||||
using Nuke.Common.Git;
|
||||
using Nuke.Common.ProjectModel;
|
||||
using Nuke.Common.Tools.DotNet;
|
||||
using Nuke.Common.Tools.GitVersion;
|
||||
using Nuke.Common.Tools.SignTool;
|
||||
using static Nuke.Common.IO.FileSystemTasks;
|
||||
using static Nuke.Common.IO.PathConstruction;
|
||||
using static Nuke.Common.Tools.DotNet.DotNetTasks;
|
||||
|
||||
class Build : NukeBuild
|
||||
namespace LibHacBuild
|
||||
{
|
||||
partial class Build : NukeBuild
|
||||
{
|
||||
public static int Main() => Execute<Build>(x => x.Results);
|
||||
|
||||
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
|
||||
readonly string Configuration = IsLocalBuild ? "Debug" : "Release";
|
||||
|
||||
[Parameter("Build only .NET Core targets if true. Default is false on Windows")]
|
||||
readonly bool DoCoreBuildOnly;
|
||||
|
||||
[Solution("LibHac.sln")] readonly Solution Solution;
|
||||
[GitRepository] readonly GitRepository GitRepository;
|
||||
[GitVersion] readonly GitVersion GitVersion;
|
||||
|
||||
AbsolutePath SourceDirectory => RootDirectory / "src";
|
||||
AbsolutePath TestsDirectory => RootDirectory / "tests";
|
||||
AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";
|
||||
AbsolutePath SignedArtifactsDirectory => ArtifactsDirectory / "signed";
|
||||
AbsolutePath TempDirectory => RootDirectory / ".tmp";
|
||||
AbsolutePath CliCoreDir => TempDirectory / "hactoolnet_netcoreapp2.1";
|
||||
AbsolutePath CliFrameworkDir => TempDirectory / "hactoolnet_net46";
|
||||
|
@ -39,6 +51,31 @@ class Build : NukeBuild
|
|||
Project LibHacTestProject => Solution.GetProject("LibHac.Tests").NotNull();
|
||||
Project HactoolnetProject => Solution.GetProject("hactoolnet").NotNull();
|
||||
|
||||
string AppVeyorVersion { get; set; }
|
||||
Dictionary<string, object> VersionProps { get; set; } = new Dictionary<string, object>();
|
||||
|
||||
const string CertFileName = "cert.pfx";
|
||||
|
||||
Target SetVersion => _ => _
|
||||
.OnlyWhenStatic(() => GitRepository != null)
|
||||
.Executes(() =>
|
||||
{
|
||||
AppVeyorVersion = $"{GitVersion.AssemblySemVer}-{GitVersion.PreReleaseTag}+{GitVersion.Sha.Substring(0, 8)}";
|
||||
|
||||
VersionProps = new Dictionary<string, object>
|
||||
{
|
||||
["VersionPrefix"] = GitVersion.AssemblySemVer,
|
||||
["VersionSuffix"] = GitVersion.PreReleaseTag
|
||||
};
|
||||
|
||||
Console.WriteLine($"Building version {AppVeyorVersion}");
|
||||
|
||||
if (Host == HostType.AppVeyor)
|
||||
{
|
||||
SetAppVeyorVersion(AppVeyorVersion);
|
||||
}
|
||||
});
|
||||
|
||||
Target Clean => _ => _
|
||||
.Executes(() =>
|
||||
{
|
||||
|
@ -56,21 +93,20 @@ class Build : NukeBuild
|
|||
DotNetRestoreSettings settings = new DotNetRestoreSettings()
|
||||
.SetProjectFile(Solution);
|
||||
|
||||
if (EnvironmentInfo.IsUnix) settings = settings.RemoveRuntimes("net46");
|
||||
|
||||
DotNetRestore(s => settings);
|
||||
});
|
||||
|
||||
Target Compile => _ => _
|
||||
.DependsOn(Restore)
|
||||
.DependsOn(Restore, SetVersion)
|
||||
.Executes(() =>
|
||||
{
|
||||
DotNetBuildSettings buildSettings = new DotNetBuildSettings()
|
||||
.SetProjectFile(Solution)
|
||||
.EnableNoRestore()
|
||||
.SetConfiguration(Configuration);
|
||||
.SetConfiguration(Configuration)
|
||||
.SetProperties(VersionProps);
|
||||
|
||||
if (EnvironmentInfo.IsUnix) buildSettings = buildSettings.SetFramework("netcoreapp2.1");
|
||||
if (DoCoreBuildOnly) buildSettings = buildSettings.SetFramework("netcoreapp2.1");
|
||||
|
||||
DotNetBuild(s => buildSettings);
|
||||
|
||||
|
@ -81,14 +117,16 @@ class Build : NukeBuild
|
|||
DotNetPublish(s => publishSettings
|
||||
.SetProject(HactoolnetProject)
|
||||
.SetFramework("netcoreapp2.1")
|
||||
.SetOutput(CliCoreDir));
|
||||
.SetOutput(CliCoreDir)
|
||||
.SetProperties(VersionProps));
|
||||
|
||||
if (EnvironmentInfo.IsWin)
|
||||
if (!DoCoreBuildOnly)
|
||||
{
|
||||
DotNetPublish(s => publishSettings
|
||||
.SetProject(HactoolnetProject)
|
||||
.SetFramework("net46")
|
||||
.SetOutput(CliFrameworkDir));
|
||||
.SetOutput(CliFrameworkDir)
|
||||
.SetProperties(VersionProps));
|
||||
}
|
||||
|
||||
// Hack around OS newline differences
|
||||
|
@ -110,17 +148,23 @@ class Build : NukeBuild
|
|||
.EnableNoBuild()
|
||||
.SetConfiguration(Configuration)
|
||||
.EnableIncludeSymbols()
|
||||
.SetOutputDirectory(ArtifactsDirectory);
|
||||
.SetSymbolPackageFormat(DotNetSymbolPackageFormat.snupkg)
|
||||
.SetOutputDirectory(ArtifactsDirectory)
|
||||
.SetProperties(VersionProps);
|
||||
|
||||
if (EnvironmentInfo.IsUnix)
|
||||
settings = settings.SetProperties(
|
||||
new Dictionary<string, object> { ["TargetFrameworks"] = "netcoreapp2.1" });
|
||||
if (DoCoreBuildOnly)
|
||||
settings = settings.SetProperty("TargetFrameworks", "netcoreapp2.1");
|
||||
|
||||
DotNetPack(s => settings);
|
||||
|
||||
foreach (string filename in Directory.EnumerateFiles(ArtifactsDirectory, "*.*nupkg"))
|
||||
{
|
||||
RepackNugetPackage(filename);
|
||||
}
|
||||
|
||||
if (Host != HostType.AppVeyor) return;
|
||||
|
||||
foreach (string filename in Directory.EnumerateFiles(ArtifactsDirectory, "*.nupkg"))
|
||||
foreach (string filename in Directory.EnumerateFiles(ArtifactsDirectory, "*.*nupkg"))
|
||||
{
|
||||
PushArtifact(filename);
|
||||
}
|
||||
|
@ -128,7 +172,7 @@ class Build : NukeBuild
|
|||
|
||||
Target Merge => _ => _
|
||||
.DependsOn(Compile)
|
||||
.OnlyWhen(() => EnvironmentInfo.IsWin)
|
||||
.OnlyWhenStatic(() => !DoCoreBuildOnly)
|
||||
.Executes(() =>
|
||||
{
|
||||
string[] libraries = Directory.GetFiles(CliFrameworkDir, "*.dll");
|
||||
|
@ -144,6 +188,11 @@ class Build : NukeBuild
|
|||
|
||||
new ILRepack(cliOptions).Repack();
|
||||
|
||||
foreach (AbsolutePath file in ArtifactsDirectory.GlobFiles("*.exe.config"))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
|
||||
if (Host == HostType.AppVeyor)
|
||||
{
|
||||
PushArtifact(CliMergedExe);
|
||||
|
@ -159,7 +208,7 @@ class Build : NukeBuild
|
|||
.EnableNoBuild()
|
||||
.SetConfiguration(Configuration);
|
||||
|
||||
if (EnvironmentInfo.IsUnix) settings = settings.SetFramework("netcoreapp2.1");
|
||||
if (DoCoreBuildOnly) settings = settings.SetFramework("netcoreapp2.1");
|
||||
|
||||
DotNetTest(s => settings);
|
||||
});
|
||||
|
@ -176,7 +225,7 @@ class Build : NukeBuild
|
|||
.Concat(Directory.EnumerateFiles(CliCoreDir, "*.dll"))
|
||||
.ToArray();
|
||||
|
||||
if (EnvironmentInfo.IsWin)
|
||||
if (!DoCoreBuildOnly)
|
||||
{
|
||||
ZipFiles(CliFrameworkZip, namesFx);
|
||||
Console.WriteLine($"Created {CliFrameworkZip}");
|
||||
|
@ -193,8 +242,28 @@ class Build : NukeBuild
|
|||
}
|
||||
});
|
||||
|
||||
Target Publish => _ => _
|
||||
.DependsOn(Test)
|
||||
.OnlyWhenStatic(() => Host == HostType.AppVeyor)
|
||||
.OnlyWhenStatic(() => AppVeyor.Instance.PullRequestTitle == null)
|
||||
.Executes(() =>
|
||||
{
|
||||
AbsolutePath nupkgFile = ArtifactsDirectory.GlobFiles("*.nupkg").Single();
|
||||
AbsolutePath snupkgFile = ArtifactsDirectory.GlobFiles("*.snupkg").Single();
|
||||
|
||||
string apiKey = EnvironmentInfo.Variable("myget_api_key");
|
||||
DotNetNuGetPushSettings settings = new DotNetNuGetPushSettings()
|
||||
.SetApiKey(apiKey)
|
||||
.SetSymbolApiKey(apiKey)
|
||||
.SetSource("https://www.myget.org/F/libhac/api/v2/package")
|
||||
.SetSymbolSource("https://www.myget.org/F/libhac/symbols/api/v2/package");
|
||||
|
||||
DotNetNuGetPush(settings.SetTargetPath(nupkgFile));
|
||||
DotNetNuGetPush(settings.SetTargetPath(snupkgFile));
|
||||
});
|
||||
|
||||
Target Results => _ => _
|
||||
.DependsOn(Test, Zip, Merge)
|
||||
.DependsOn(Test, Zip, Merge, Sign, Publish)
|
||||
.Executes(() =>
|
||||
{
|
||||
Console.WriteLine("SHA-1:");
|
||||
|
@ -211,7 +280,24 @@ class Build : NukeBuild
|
|||
}
|
||||
});
|
||||
|
||||
public static void ZipFiles(string outFile, string[] files)
|
||||
Target Sign => _ => _
|
||||
.DependsOn(Test, Zip, Merge)
|
||||
.OnlyWhenStatic(() => !DoCoreBuildOnly)
|
||||
.OnlyWhenStatic(() => File.Exists(CertFileName))
|
||||
.Executes(() =>
|
||||
{
|
||||
string pwd = ReadPassword();
|
||||
|
||||
if (pwd == string.Empty)
|
||||
{
|
||||
Console.WriteLine("Skipping sign task");
|
||||
return;
|
||||
}
|
||||
|
||||
SignAndReZip(pwd);
|
||||
});
|
||||
|
||||
public static void ZipFiles(string outFile, IEnumerable<string> files)
|
||||
{
|
||||
using (var s = new ZipOutputStream(File.Create(outFile)))
|
||||
{
|
||||
|
@ -232,6 +318,80 @@ class Build : NukeBuild
|
|||
}
|
||||
}
|
||||
|
||||
public static void ZipDirectory(string outFile, string directory)
|
||||
{
|
||||
using (var s = new ZipOutputStream(File.Create(outFile)))
|
||||
{
|
||||
s.SetLevel(9);
|
||||
|
||||
foreach (string filePath in Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
string relativePath = Path.GetRelativePath(directory, filePath);
|
||||
|
||||
var entry = new ZipEntry(relativePath);
|
||||
entry.DateTime = DateTime.UnixEpoch;
|
||||
|
||||
using (FileStream fs = File.OpenRead(filePath))
|
||||
{
|
||||
entry.Size = fs.Length;
|
||||
s.PutNextEntry(entry);
|
||||
fs.CopyTo(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void ZipDirectory(string outFile, string directory, IEnumerable<string> files)
|
||||
{
|
||||
using (var s = new ZipOutputStream(File.Create(outFile)))
|
||||
{
|
||||
s.SetLevel(9);
|
||||
|
||||
foreach (string filePath in files)
|
||||
{
|
||||
string absolutePath = Path.Combine(directory, filePath);
|
||||
|
||||
var entry = new ZipEntry(filePath);
|
||||
entry.DateTime = DateTime.UnixEpoch;
|
||||
|
||||
using (FileStream fs = File.OpenRead(absolutePath))
|
||||
{
|
||||
entry.Size = fs.Length;
|
||||
s.PutNextEntry(entry);
|
||||
fs.CopyTo(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void UnzipFiles(string zipFile, string outDir)
|
||||
{
|
||||
using (var s = new ZipInputStream(File.OpenRead(zipFile)))
|
||||
{
|
||||
ZipEntry entry;
|
||||
while ((entry = s.GetNextEntry()) != null)
|
||||
{
|
||||
string outPath = Path.Combine(outDir, entry.Name);
|
||||
|
||||
string directoryName = Path.GetDirectoryName(outPath);
|
||||
string fileName = Path.GetFileName(outPath);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(directoryName))
|
||||
{
|
||||
Directory.CreateDirectory(directoryName);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(fileName))
|
||||
{
|
||||
using (FileStream outFile = File.Create(outPath))
|
||||
{
|
||||
s.CopyTo(outFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void PushArtifact(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
|
@ -260,9 +420,122 @@ class Build : NukeBuild
|
|||
Console.WriteLine($"Added AppVeyor artifact {path}");
|
||||
}
|
||||
|
||||
public static void SetAppVeyorVersion(string version)
|
||||
{
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
FileName = "appveyor",
|
||||
Arguments = $"UpdateBuild -Version \"{version}\"",
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
|
||||
var proc = new Process
|
||||
{
|
||||
StartInfo = psi
|
||||
};
|
||||
|
||||
proc.Start();
|
||||
|
||||
proc.WaitForExit();
|
||||
}
|
||||
|
||||
public static void ReplaceLineEndings(string filename)
|
||||
{
|
||||
string text = File.ReadAllText(filename);
|
||||
File.WriteAllText(filename, text.Replace("\n", "\r\n"));
|
||||
File.WriteAllText(filename, Regex.Replace(text, @"\r\n|\n\r|\n|\r", "\r\n"));
|
||||
}
|
||||
|
||||
public static void SignAssemblies(string password, params string[] fileNames)
|
||||
{
|
||||
SignToolSettings settings = new SignToolSettings()
|
||||
.SetFileDigestAlgorithm("SHA256")
|
||||
.SetFile(CertFileName)
|
||||
.SetFiles(fileNames)
|
||||
.SetPassword(password)
|
||||
.SetTimestampServerDigestAlgorithm("SHA256")
|
||||
.SetRfc3161TimestampServerUrl("http://timestamp.digicert.com");
|
||||
|
||||
SignToolTasks.SignTool(settings);
|
||||
}
|
||||
|
||||
public void SignAndReZip(string password)
|
||||
{
|
||||
AbsolutePath nupkgFile = ArtifactsDirectory.GlobFiles("*.nupkg").Single();
|
||||
AbsolutePath snupkgFile = ArtifactsDirectory.GlobFiles("*.snupkg").Single();
|
||||
AbsolutePath nupkgDir = TempDirectory / ("sign_" + Path.GetFileName(nupkgFile));
|
||||
AbsolutePath netFxDir = TempDirectory / ("sign_" + Path.GetFileName(CliFrameworkZip));
|
||||
AbsolutePath coreFxDir = TempDirectory / ("sign_" + Path.GetFileName(CliCoreZip));
|
||||
AbsolutePath signedMergedExe = SignedArtifactsDirectory / Path.GetFileName(CliMergedExe);
|
||||
|
||||
try
|
||||
{
|
||||
UnzipFiles(CliFrameworkZip, netFxDir);
|
||||
UnzipFiles(CliCoreZip, coreFxDir);
|
||||
List<string> pkgFileList = UnzipPackage(nupkgFile, nupkgDir);
|
||||
|
||||
var toSign = new List<AbsolutePath>();
|
||||
toSign.AddRange(nupkgDir.GlobFiles("**/LibHac.dll"));
|
||||
toSign.Add(netFxDir / "hactoolnet.exe");
|
||||
toSign.Add(coreFxDir / "hactoolnet.dll");
|
||||
toSign.Add(signedMergedExe);
|
||||
|
||||
Directory.CreateDirectory(SignedArtifactsDirectory);
|
||||
File.Copy(CliMergedExe, signedMergedExe, true);
|
||||
|
||||
SignAssemblies(password, toSign.Select(x => x.ToString()).ToArray());
|
||||
|
||||
// Avoid having multiple signed versions of the same file
|
||||
File.Copy(nupkgDir / "lib" / "net46" / "LibHac.dll", netFxDir / "LibHac.dll", true);
|
||||
File.Copy(nupkgDir / "lib" / "netcoreapp2.1" / "LibHac.dll", coreFxDir / "LibHac.dll", true);
|
||||
|
||||
ZipDirectory(SignedArtifactsDirectory / Path.GetFileName(nupkgFile), nupkgDir, pkgFileList);
|
||||
ZipDirectory(SignedArtifactsDirectory / Path.GetFileName(CliFrameworkZip), netFxDir);
|
||||
ZipDirectory(SignedArtifactsDirectory / Path.GetFileName(CliCoreZip), coreFxDir);
|
||||
|
||||
File.Copy(snupkgFile, SignedArtifactsDirectory / Path.GetFileName(snupkgFile));
|
||||
|
||||
SignNupkg(SignedArtifactsDirectory / Path.GetFileName(nupkgFile), password);
|
||||
SignNupkg(SignedArtifactsDirectory / Path.GetFileName(snupkgFile), password);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Directory.Delete(SignedArtifactsDirectory, true);
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
Directory.Delete(nupkgDir, true);
|
||||
Directory.Delete(netFxDir, true);
|
||||
Directory.Delete(coreFxDir, true);
|
||||
}
|
||||
}
|
||||
|
||||
public static string ReadPassword()
|
||||
{
|
||||
var pwd = new StringBuilder();
|
||||
ConsoleKeyInfo key;
|
||||
|
||||
Console.Write("Enter certificate password (Empty password to skip): ");
|
||||
do
|
||||
{
|
||||
key = Console.ReadKey(true);
|
||||
|
||||
// Ignore any key out of range.
|
||||
if (((int)key.Key) >= '!' && ((int)key.Key <= '~'))
|
||||
{
|
||||
// Append the character to the password.
|
||||
pwd.Append(key.KeyChar);
|
||||
Console.Write("*");
|
||||
}
|
||||
|
||||
// Exit if Enter key is pressed.
|
||||
} while (key.Key != ConsoleKey.Enter);
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
return pwd.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
136
build/RepackNuget.cs
Normal file
136
build/RepackNuget.cs
Normal file
|
@ -0,0 +1,136 @@
|
|||
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.Tools.NuGet;
|
||||
using static Nuke.Common.IO.FileSystemTasks;
|
||||
using static Nuke.Common.IO.PathConstruction;
|
||||
|
||||
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
|
||||
{
|
||||
EnsureCleanDirectory(tempDir);
|
||||
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);
|
||||
}
|
||||
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 (SHA256 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 (SHA256 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} -Timestamper http://timestamp.digicert.com",
|
||||
outputFilter: x => x.Replace(password, "hunter2"));
|
||||
}
|
||||
|
||||
public static string ToHexString(byte[] arr)
|
||||
{
|
||||
return BitConverter.ToString(arr).ToLower().Replace("-", "");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,15 +4,17 @@
|
|||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<RootNamespace></RootNamespace>
|
||||
<RootNamespace>LibHacBuild</RootNamespace>
|
||||
<IsPackable>False</IsPackable>
|
||||
<NoWarn>CS0649;CS0169</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Nuke.Common" Version="0.12.0" />
|
||||
<PackageReference Include="SharpZipLib" Version="1.0.0" />
|
||||
<PackageReference Include="GitVersion.CommandLine.DotNetCore" Version="4.0.0" />
|
||||
<PackageReference Include="ILRepack.Lib" Version="2.0.16" NoWarn="NU1701" />
|
||||
<PackageReference Include="NuGet.CommandLine" Version="4.9.3" />
|
||||
<PackageReference Include="Nuke.Common" Version="0.16.0" />
|
||||
<PackageReference Include="SharpZipLib" Version="1.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
|
|
@ -19,8 +19,9 @@
|
|||
<VersionPrefix>0.2.0</VersionPrefix>
|
||||
<PathMap>$(MSBuildProjectDirectory)=C:/LibHac/</PathMap>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<IncludeSource>true</IncludeSource>
|
||||
<NoWarn>$(NoWarn);1591</NoWarn>
|
||||
<NoWarn>$(NoWarn);1591;NU5105</NoWarn>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
|
|
Loading…
Reference in a new issue