Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Remove WriteZipToSetup project; this can be done in C#
Browse files Browse the repository at this point in the history
  • Loading branch information
caesay committed Dec 13, 2021
1 parent 9d15b52 commit bb05c33
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 665 deletions.
50 changes: 1 addition & 49 deletions Squirrel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionLevel", "SolutionLe
version.json = version.json
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WriteZipToSetup", "src\WriteZipToSetup\WriteZipToSetup.vcxproj", "{4D3C8B70-075D-48A5-9FF3-EDB87347B136}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StubExecutable", "src\StubExecutable\StubExecutable.vcxproj", "{C028DB2A-E7C5-4232-8C22-D5FBA2176136}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SquirrelCli", "src\SquirrelCli\SquirrelCli.csproj", "{19E8EBF5-0277-422F-BF49-C66D9DBA5AA4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SquirrelCli", "src\SquirrelCli\SquirrelCli.csproj", "{19E8EBF5-0277-422F-BF49-C66D9DBA5AA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -244,52 +242,6 @@ Global
{1EEBACBC-6982-4696-BD4E-899ED0AC6CD2}.Release|x64.Build.0 = Release|Any CPU
{1EEBACBC-6982-4696-BD4E-899ED0AC6CD2}.Release|x86.ActiveCfg = Release|Any CPU
{1EEBACBC-6982-4696-BD4E-899ED0AC6CD2}.Release|x86.Build.0 = Release|Any CPU
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|Any CPU.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|Any CPU.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|Mixed Platforms.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|Mixed Platforms.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|x64.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|x64.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|x86.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.CIBuild|x86.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|Any CPU.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|Any CPU.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|Mixed Platforms.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|Mixed Platforms.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|x64.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|x64.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|x86.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Coverage|x86.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|Any CPU.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|Any CPU.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|x64.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|x86.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Debug|x86.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|Any CPU.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|Any CPU.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|Mixed Platforms.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|x64.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|x64.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|x86.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Debug|x86.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|Any CPU.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|Any CPU.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|Mixed Platforms.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|Mixed Platforms.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|x64.ActiveCfg = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|x64.Build.0 = Debug|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|x86.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Mono Release|x86.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|Any CPU.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|Any CPU.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|Mixed Platforms.Build.0 = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|x64.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|x86.ActiveCfg = Release|Win32
{4D3C8B70-075D-48A5-9FF3-EDB87347B136}.Release|x86.Build.0 = Release|Win32
{C028DB2A-E7C5-4232-8C22-D5FBA2176136}.CIBuild|Any CPU.ActiveCfg = Release|x64
{C028DB2A-E7C5-4232-8C22-D5FBA2176136}.CIBuild|Any CPU.Build.0 = Release|x64
{C028DB2A-E7C5-4232-8C22-D5FBA2176136}.CIBuild|Mixed Platforms.ActiveCfg = Release|Win32
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dotnet publish -v minimal -c Release -r win-x86 --self-contained=true "$PSScript
Copy-Item "$In\Win32\Setup.exe" -Destination "$Out"
# Copy-Item "$In\Win32\Setup.pdb" -Destination "$Out"
Copy-Item "$In\Win32\StubExecutable.exe" -Destination "$Out"
Copy-Item "$In\Win32\WriteZipToSetup.exe" -Destination "$Out"
# Copy-Item "$In\Win32\WriteZipToSetup.exe" -Destination "$Out"
# Copy-Item "$In\Win32\WriteZipToSetup.pdb" -Destination "$Out"

Copy-Item -Path "$PSScriptRoot\vendor\7zip\*" -Destination "$Out" -Recurse
Expand Down
33 changes: 0 additions & 33 deletions src/Squirrel/HelperExe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal static class HelperExe
private static string RceditPath => FindHelperExecutable("rcedit.exe", _searchPaths);
private static string SevenZipPath => FindHelperExecutable("7z.exe", _searchPaths);
private static string SignToolPath => FindHelperExecutable("signtool.exe", _searchPaths);
private static string SetupZipBuilderPath => FindHelperExecutable("WriteZipToSetup.exe", _searchPaths);

private static List<string> _searchPaths = new List<string>();
private static IFullLogger Log = SquirrelLocator.CurrentMutable.GetService<ILogManager>().GetLogger(typeof(HelperExe));
Expand Down Expand Up @@ -165,38 +164,6 @@ public static async Task ValidateFrameworkVersion(string frameworkVersion)
}
}

public static async Task CopyResourcesToTargetStubExe(string copyResourcesFromExe, string targetStubExe)
{
var processResult = await Utility.InvokeProcessAsync(
SetupZipBuilderPath,
new string[] { "--copy-stub-resources", copyResourcesFromExe, targetStubExe },
CancellationToken.None);

if (processResult.ExitCode != 0) {
throw new Exception("Unable to copy resources to stub exe: " + processResult.StdOutput);
}
}

public static async Task BundleZipIntoTargetSetupExe(string targetSetupExe, string zipPath, string frameworkVersion, string backgroundGif)
{
List<string> arguments = new List<string>() {
targetSetupExe,
zipPath
};
if (!String.IsNullOrWhiteSpace(frameworkVersion)) {
arguments.Add("--set-required-framework");
arguments.Add(frameworkVersion);
}
if (!String.IsNullOrWhiteSpace(backgroundGif)) {
arguments.Add("--set-splash");
arguments.Add(Path.GetFullPath(backgroundGif));
}

var result = await Utility.InvokeProcessAsync(SetupZipBuilderPath, arguments, CancellationToken.None);
if (result.ExitCode != 0)
throw new Exception("Failed to write Zip to Setup.exe!\n\n" + result.StdOutput);
}

public static async Task NugetPack(string nuspecPath, string baseDirectory, string outputDirectory)
{
var args = new string[] { "pack", nuspecPath, "-BasePath", baseDirectory, "-OutputDirectory", outputDirectory };
Expand Down
51 changes: 51 additions & 0 deletions src/SquirrelCli/HostModel/ResourceUpdater.LangNeutral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// If updating HostModel, mark the ResourceUpdater.cs class as partial so these functions can get mixed in

using System;

namespace Microsoft.NET.HostModel
{
public partial class ResourceUpdater
{
public ResourceUpdater(string peFile, bool bDeleteExistingResources)
{
hUpdate = Kernel32.BeginUpdateResource(peFile, bDeleteExistingResources);
if (hUpdate.IsInvalid) {
ThrowExceptionForLastWin32Error();
}
}

public ResourceUpdater AddResource(byte[] data, string lpType, IntPtr lpName, ushort langId)
{
if (hUpdate.IsInvalid) {
ThrowExceptionForInvalidUpdate();
}

if (!IsIntResource(lpName)) {
throw new ArgumentException("AddResource can only be used with integer resource names");
}

if (!Kernel32.UpdateResource(hUpdate, lpType, lpName, langId, data, (uint) data.Length)) {
ThrowExceptionForLastWin32Error();
}

return this;
}

public ResourceUpdater ClearResource(string lpType, IntPtr lpName, ushort langId)
{
if (hUpdate.IsInvalid) {
ThrowExceptionForInvalidUpdate();
}

if (!IsIntResource(lpName)) {
throw new ArgumentException("AddResource can only be used with integer resource names");
}

if (!Kernel32.UpdateResource(hUpdate, lpType, lpName, langId, null, 0)) {
ThrowExceptionForLastWin32Error();
}

return this;
}
}
}
4 changes: 2 additions & 2 deletions src/SquirrelCli/HostModel/ResourceUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
Expand All @@ -12,7 +12,7 @@ namespace Microsoft.NET.HostModel
/// in a PE image. It currently only works on Windows, because it
/// requires various kernel32 APIs.
/// </summary>
public class ResourceUpdater : IDisposable
public partial class ResourceUpdater : IDisposable
{
private sealed class Kernel32
{
Expand Down
18 changes: 7 additions & 11 deletions src/SquirrelCli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -220,23 +220,19 @@ static void Releasify(ReleasifyOptions options)
ReleaseEntry.WriteReleaseFile(releaseEntries, releaseFilePath);

var targetSetupExe = Path.Combine(di.FullName, "Setup.exe");
var newestFullRelease = Squirrel.EnumerableExtensions.MaxBy(releaseEntries, x => x.Version).Where(x => !x.IsDelta).First();

File.Copy(HelperExe.SetupPath, targetSetupExe, true);
Utility.Retry(() => HelperExe.SetPEVersionBlockFromPackageInfo(targetSetupExe, new ZipPackage(package), setupIcon).Wait());

var newestFullRelease = Squirrel.EnumerableExtensions.MaxBy(releaseEntries, x => x.Version).Where(x => !x.IsDelta).First();
var zipPath = createSetupEmbeddedZip(Path.Combine(di.FullName, newestFullRelease.Filename), updatePath);
//File.Copy(zipPath, Path.Combine(di.FullName, "Setup.zip"), true);

try {
HelperExe.BundleZipIntoTargetSetupExe(targetSetupExe, zipPath, frameworkVersion, backgroundGif).Wait();
} catch (Exception ex) {
Log.ErrorException("Failed to update Setup.exe with new Zip file", ex);
throw;
SetupResourceWriter.WriteZipToSetup(targetSetupExe, zipPath, frameworkVersion, backgroundGif);
} finally {
File.Delete(zipPath);
}

Utility.Retry(() =>
HelperExe.SetPEVersionBlockFromPackageInfo(targetSetupExe, new ZipPackage(package), setupIcon).Wait());

HelperExe.SignPEFile(targetSetupExe, signingOpts).Wait();

//if (generateMsi) {
Expand Down Expand Up @@ -298,7 +294,7 @@ static async Task createExecutableStubForExe(string exeToCopy)
Path.GetFileNameWithoutExtension(exeToCopy) + "_ExecutionStub.exe");

await Utility.CopyToAsync(HelperExe.StubExecutablePath, target);
await HelperExe.CopyResourcesToTargetStubExe(exeToCopy, target);
SetupResourceWriter.CopyStubExecutableResources(exeToCopy, target);
}
}

Expand Down
58 changes: 58 additions & 0 deletions src/SquirrelCli/SetupResourceWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.NET.HostModel;

namespace SquirrelCli
{
internal class SetupResourceWriter
{
// these values come from Setup.rc
private static readonly ushort RESOURCE_LANG = 0x0409;
private static readonly IntPtr IDR_UPDATE_ZIP = new IntPtr(131);
private static readonly IntPtr IDR_FX_VERSION_FLAG = new IntPtr(132);
private static readonly IntPtr IDR_SPLASH_IMG = new IntPtr(138);

public static void WriteZipToSetup(string targetSetupExe, string zipFile, string targetFramework, string splashImage)
{
try {
using var writer = new ResourceUpdater(targetSetupExe);

var zipBytes = File.ReadAllBytes(zipFile);
writer.AddResource(zipBytes, "DATA", IDR_UPDATE_ZIP, RESOURCE_LANG);

if (!String.IsNullOrWhiteSpace(targetFramework)) {
var stringBytes = Encoding.Unicode.GetBytes(String.Concat(targetFramework, "\0\0"));
writer.AddResource(stringBytes, "FLAGS", IDR_FX_VERSION_FLAG, RESOURCE_LANG);
}

if (!String.IsNullOrWhiteSpace(splashImage)) {
var splashBytes = File.ReadAllBytes(splashImage);
writer.AddResource(splashBytes, "DATA", IDR_SPLASH_IMG, RESOURCE_LANG);
} else {
// the template Setup.exe has a built-in splash image used for testing. we need to remove it
writer.ClearResource("DATA", IDR_SPLASH_IMG, RESOURCE_LANG);
}

writer.Update();
} catch (HResultException hr) {
throw new Win32Exception(hr.Win32HResult);
}
}

public static void CopyStubExecutableResources(string peToCopy, string targetStubExecutable)
{
try {
using var writer = new ResourceUpdater(targetStubExecutable, true);
writer.AddResourcesFromPEImage(peToCopy);
writer.Update();
} catch (HResultException hr) {
throw new Win32Exception(hr.Win32HResult);
}
}
}
}
Loading

0 comments on commit bb05c33

Please sign in to comment.