Skip to content

Commit

Permalink
Add InstallerProcessAboutToStart event
Browse files Browse the repository at this point in the history
Allows user to modify Process before it starts and optionally stop process from running
  • Loading branch information
Deadpikle committed Dec 7, 2023
1 parent 98a0917 commit a5cbbf1
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Removed `netcoreapp3.1` and `net5.0` compatibility as they are both EOL per https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core (87381f2d3422e36fe5f614c85e43687eed497c42)
* Moved to official/non-preview builds of `System.Drawing.Common` for 8.x and bump up to `8.0.0` for older versions of .NET (87381f2d3422e36fe5f614c85e43687eed497c42) [**]
* Use `.axaml` in all Avalonia-related projects (46de3e9c9525cac4026a7959e44764752cdf36ee, e6d5ad20fec37e018a23ab46ef34d728b8104e96)
* `Exec(string cmd, bool waitForExit = true)` now returns a `bool`: `true` for the process starting, `false` otherwise

**Changes/Fixes**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>NetSparkleUpdater.UI.WinForms.NetFramework</id>
<title>NetSparkleUpdater WinForms .NET Framework UI</title>
<version>3.0.0-preview20231207002</version>
<version>3.0.0-preview20231207003</version>
<authors>Deadpikle, Dirk Eisenberg</authors>
<owners>Deadpikle</owners>
<license type="file">LICENSE.md</license>
Expand All @@ -19,7 +19,7 @@
<releaseNotes>2.0: See https://github.com/NetSparkleUpdater/NetSparkle for all information and to file issues/pull requests for and ask questions about this project.</releaseNotes>
<dependencies>
<group targetFramework=".NETFramework4.5.2">
<dependency id="NetSparkleUpdater.SparkleUpdater" version="3.0.0-preview20231207002" exclude="Build,Analyzers" />
<dependency id="NetSparkleUpdater.SparkleUpdater" version="3.0.0-preview20231207003" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
Expand Down
2 changes: 1 addition & 1 deletion src/NetSparkle.UI.Avalonia/NetSparkle.UI.Avalonia.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>NetSparkleUpdater.UI.Avalonia</PackageId>
<Version>3.0.0-preview20231207002</Version>
<Version>3.0.0-preview20231207003</Version>
<Authors>Deadpikle</Authors>
<Company>Deadpikle</Company>
<Product>NetSparkleUpdater.UI.Avalonia</Product>
Expand Down
2 changes: 1 addition & 1 deletion src/NetSparkle.UI.WPF/NetSparkle.UI.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/NetSparkleUpdater/NetSparkle</PackageProjectUrl>
<RepositoryUrl>https://github.com/NetSparkleUpdater/NetSparkle.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Version>3.0.0-preview20231207002</Version>
<Version>3.0.0-preview20231207003</Version>
<PackageIcon>software-update-available.png</PackageIcon>
<PackageIconUrl />
<Description>NetSparkleUpdater/NetSparkle app updater framework with built-in WPF .NET Core and .NET Framework UI. NetSparkleUpdater/NetSparkle is a C# .NET software update framework that allows you to easily download installer files and update your C# .NET Framework or .NET Core software. Built-in UIs are available for WinForms, WPF, and Avalonia. You provide, somewhere on the internet, an XML appcast with software version information along with release notes in Markdown or HTML format. The NetSparkle framework then checks for an update in the background, displays the release notes to the user, and lets users download or skip the software update. The framework can also perform silent downloads so that you can present all of the UI yourself or set up your own silent software update system, as allowed by your software architecture. It was inspired by the Sparkle (https://sparkle-project.org/) project for Cocoa developers and the WinSparkle (https://winsparkle.org/) project (a Win32 port).</Description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Copyright>Copyright © 2023</Copyright>
<OutputPath>..\bin\$(Configuration)\NetSparkle.UI.WinForms\</OutputPath>
<PackageId>NetSparkleUpdater.UI.WinForms.NetCore</PackageId>
<Version>3.0.0-preview20231207002</Version>
<Version>3.0.0-preview20231207003</Version>
<Authors>Deadpikle</Authors>
<Company>Deadpikle</Company>
<Description>NetSparkleUpdater/NetSparkle app updater framework with built-in WinForms .NET Core UI. NetSparkleUpdater/NetSparkle is a C# .NET software update framework that allows you to easily download installer files and update your C# .NET Framework or .NET Core software. Built-in UIs are available for WinForms, WPF, and Avalonia. You provide, somewhere on the internet, an XML appcast with software version information along with release notes in Markdown or HTML format. The NetSparkle framework then checks for an update in the background, displays the release notes to the user, and lets users download or skip the software update. The framework can also perform silent downloads so that you can present all of the UI yourself or set up your own silent software update system, as allowed by your software architecture. It was inspired by the Sparkle (https://sparkle-project.org/) project for Cocoa developers and the WinSparkle (https://winsparkle.org/) project (a Win32 port).</Description>
Expand Down
2 changes: 1 addition & 1 deletion src/NetSparkle/NetSparkle.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>net8.0;net7.0;net6.0;netstandard2.0;net462</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>NetSparkleUpdater.SparkleUpdater</PackageId>
<Version>3.0.0-preview20231207002</Version>
<Version>3.0.0-preview20231207003</Version>
<Authors>Deadpikle, Dirk Eisenberg</Authors>
<Description>NetSparkleUpdater/NetSparkle is a C# .NET software update framework that allows you to easily download installer files and update your C# .NET Framework or .NET Core software. Built-in UIs are available for WinForms, WPF, and Avalonia; if you want a built-in UI, please reference a NetSparkleUpdater.UI package. You provide, somewhere on the internet, an XML appcast with software version information along with release notes in Markdown or HTML format. The NetSparkle framework then checks for an update in the background, displays the release notes to the user, and lets users download or skip the software update. The framework can also perform silent downloads so that you can present all of the UI yourself or set up your own silent software update system, as allowed by your software architecture. It was inspired by the Sparkle (https://sparkle-project.org/) project for Cocoa developers and the WinSparkle (https://winsparkle.org/) project (a Win32 port).</Description>
<Copyright>Copyright 2010 - 2023</Copyright>
Expand Down
11 changes: 11 additions & 0 deletions src/NetSparkle/NetSparkleDelegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace NetSparkleUpdater
{
Expand Down Expand Up @@ -108,4 +109,14 @@ namespace NetSparkleUpdater
/// <param name="toURL">The location that the redirect is pointing to</param>
/// <param name="responseMessage">The <seealso cref="HttpResponseMessage"/> for the response from the server</param>
public delegate bool RedirectHandler(string fromURL, string toURL, HttpResponseMessage responseMessage);

/// <summary>
/// A delegate to allow users to modify/see the installer process before it actually begins.
/// Return true to keep the installer process going, return false to cancel the installer process
/// (stop it from running).
/// </summary>
/// <param name="process">The installer process about to be started</param>
/// <param name="downloadFilePath">The path to the downloaded installer that will be started by the new process</param>
/// <returns>true if the installer should continue, false to stop the installer from starting</returns>
public delegate bool BeforeBeginInstallerProcess(Process process, string downloadFilePath);
}
37 changes: 27 additions & 10 deletions src/NetSparkle/SparkleUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ chcp 65001 > nul
LogWriter.PrintMessage("Going to execute script at path: {0}", batchFilePath);

// init the installer helper
var didStartInstaller = true;
if (isWindows)
{
_installerProcess = new Process
Expand All @@ -1579,16 +1580,24 @@ chcp 65001 > nul
}
};
// start the installer process. the batch file will wait for the host app to close before starting.
LogWriter.PrintMessage("Starting the installer process at {0}", batchFilePath);
_installerProcess.Start();
var shouldContinue = InstallerProcessAboutToStart?.Invoke(_installerProcess, downloadFilePath) ?? true;
if (shouldContinue)
{
LogWriter.PrintMessage("Starting the installer process at {0}", batchFilePath);
_installerProcess.Start();
}
else
{
didStartInstaller = false;
}
}
else
{
// on macOS need to use bash to execute the shell script
LogWriter.PrintMessage("Starting the installer script process at {0} via shell exec", batchFilePath);
Exec(batchFilePath, false); // _installerProcess will be set up in `Exec`
didStartInstaller = Exec(batchFilePath, false); // _installerProcess will be set up in `Exec`
}
if (ShouldKillParentProcessWhenStartingInstaller)
if (didStartInstaller && ShouldKillParentProcessWhenStartingInstaller)
{
await QuitApplication();
}
Expand All @@ -1603,7 +1612,9 @@ chcp 65001 > nul
/// </summary>
/// <param name="cmd">Path to script to run via a shell</param>
/// <param name="waitForExit">True for the calling process to wait for the command to finish before exiting; false otherwise</param>
protected void Exec(string cmd, bool waitForExit = true)
/// <param name="downloadFilePath">Optional param for download file that is being executed via installer</param>
/// <returns>true if process started, false otherwise</returns>
protected bool Exec(string cmd, bool waitForExit = true, string downloadFilePath = "")
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var shell = "";
Expand Down Expand Up @@ -1631,13 +1642,19 @@ protected void Exec(string cmd, bool waitForExit = true)
Arguments = $"-c \"{escapedArgs}\""
}
};
LogWriter.PrintMessage("Starting the process via {1} -c \"{0}\"", escapedArgs, shell);
_installerProcess.Start();
if (waitForExit)
var shouldContinue = InstallerProcessAboutToStart?.Invoke(_installerProcess, downloadFilePath) ?? true;
if (shouldContinue)
{
LogWriter.PrintMessage("Waiting for exit...");
_installerProcess.WaitForExit();
LogWriter.PrintMessage("Starting the process via {1} -c \"{0}\"", escapedArgs, shell);
_installerProcess.Start();
if (waitForExit)
{
LogWriter.PrintMessage("Waiting for exit...");
_installerProcess.WaitForExit();
}
return true;
}
return false;
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions src/NetSparkle/SparkleUpdaterEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel;
using System.Net;
using System.Text;
using System.Diagnostics;

namespace NetSparkleUpdater
{
Expand Down Expand Up @@ -112,5 +113,13 @@ public partial class SparkleUpdater : IDisposable
/// If you need an event that can be canceled, use <see cref="PreparingToExitAsync"/>.</para>
/// </summary>
public event CloseApplicationAsync CloseApplicationAsync;

/// <summary>
/// Event to modify/see the installer process before it actually begins. You can set properties on the <see cref="Process"/>
/// before it by using this event.
/// Return true to keep the installer process going, return false to cancel the installer process
/// (stop it from running).
/// </summary>
public event BeforeBeginInstallerProcess InstallerProcessAboutToStart;
}
}

0 comments on commit a5cbbf1

Please sign in to comment.