2019-11-19 06:44:55 +08:00
using System ;
2019-11-21 19:22:35 +08:00
using System.Diagnostics ;
2019-11-28 19:21:28 +08:00
using System.IO ;
2019-11-19 06:44:55 +08:00
using Microsoft.Deployment.WindowsInstaller ;
using WixSharp ;
using WixSharp.CommonTasks ;
using WixSharp.Controls ;
2019-11-21 19:22:35 +08:00
using System.Linq ;
2019-11-28 19:21:28 +08:00
using System.Windows.Forms ;
using File = WixSharp . File ;
2019-11-19 06:44:55 +08:00
namespace Setup
{
public class Program
{
static void Main ( )
{
// This project type has been superseded with the EmbeddedUI based "WixSharp Managed Setup - Custom Dialog"
// project type. Which provides by far better final result and user experience.
// However due to the Burn limitations (see this discussion: https://wixsharp.codeplex.com/discussions/645838)
// currently "Custom CLR Dialog" is the only working option for having bootstrapper silent UI displaying
// individual MSI packages UI implemented in managed code.
2019-11-21 19:22:35 +08:00
var uninstallerShortcut = new ExeFileShortcut ( "Uninstall LANraragi" , "[System64Folder]msiexec.exe" , "/x [ProductCode]" ) ;
2021-07-31 08:39:19 +08:00
var registerAction = new ManagedAction ( RegisterWslDistro ,
Return . check ,
When . After ,
Step . InstallFinalize ,
Condition . NOT_BeingRemoved ) ;
registerAction . ProgressText = "Installing the LANraragi WSL Distro... (This will show a cmd window)" ;
var unregisterAction = new ManagedAction ( UnRegisterWslDistro ,
Return . check ,
When . Before ,
Step . RemoveFiles ,
Condition . BeingUninstalled ) ;
unregisterAction . ProgressText = "Removing the previous LANraragi WSL Distro (This will show a cmd window)" ;
2019-11-21 19:22:35 +08:00
var project = new Project ( "LANraragi" ,
2020-03-09 21:12:40 +08:00
new Dir ( @"%AppData%\LANraragi" ,
2022-04-25 20:52:31 +08:00
new Files ( @"..\Karen\bin\Release\net472\win7-x64\*.*" ) ,
2022-04-25 21:08:22 +08:00
new File ( @"..\DistroInstaller\bin\Release\net472\win7-x64\DistroInstaller.exe" ) , //DesktopBridge.Helpers.dll already in the global Files match above
2019-11-21 19:22:35 +08:00
new File ( @"..\External\package.tar" ) ,
uninstallerShortcut
) ,
2019-11-28 19:21:28 +08:00
new Dir ( @"%ProgramMenu%\LANraragi for Windows" ,
2021-07-31 08:03:51 +08:00
new ExeFileShortcut ( "LANraragi" , "[INSTALLDIR]Karen.exe" , "" ) ) ,
2019-11-28 19:21:28 +08:00
new RegValue ( RegistryHive . LocalMachineOrUsers , @"Software\Microsoft\Windows\CurrentVersion\Run" , "Karen" , "[INSTALLDIR]Karen.exe" ) ,
2021-07-31 08:39:19 +08:00
registerAction ,
unregisterAction
2019-11-21 19:22:35 +08:00
) ;
project . GUID = new Guid ( "6fe30b47-2577-43ad-1337-1861ba25889b" ) ;
2019-11-19 06:44:55 +08:00
project . Platform = Platform . x64 ;
2020-03-09 21:12:40 +08:00
project . MajorUpgrade = new MajorUpgrade
{
2021-07-31 08:03:51 +08:00
Schedule = UpgradeSchedule . afterInstallValidate , // Remove previous version entirely before reinstalling, so that the WSL distro isn't uninstalled on upgrade.
2020-03-09 21:12:40 +08:00
DowngradeErrorMessage = "A later version of [ProductName] is already installed. Setup will now exit."
} ;
2019-11-28 20:39:32 +08:00
2019-12-13 00:04:27 +08:00
// Version number is based on the LRR_VERSION_NUM env variable
var version = "0.0.1" ;
2019-11-28 20:39:32 +08:00
if ( Environment . GetEnvironmentVariable ( "LRR_VERSION_NUM" ) ! = null )
version = Environment . GetEnvironmentVariable ( "LRR_VERSION_NUM" ) ;
2019-12-12 20:02:11 +08:00
try
{
2021-07-31 08:03:51 +08:00
project . Version = Version . Parse ( version ) ;
2020-03-09 21:12:40 +08:00
}
catch
2019-12-12 20:02:11 +08:00
{
2020-03-09 21:12:40 +08:00
Console . WriteLine ( "Couldn't get version from the environment variable " + version ) ;
2019-12-12 20:02:11 +08:00
project . Version = Version . Parse ( "0.0.1" ) ;
}
2019-11-19 06:44:55 +08:00
// Check for x64 Windows 10
2020-03-09 21:12:40 +08:00
project . LaunchConditions . Add ( new LaunchCondition ( "VersionNT64" , "LANraragi for Windows can only be installed on a 64-bit Windows." ) ) ;
2019-11-19 06:44:55 +08:00
project . LaunchConditions . Add ( new LaunchCondition ( "VersionNT>=\"603\"" , "LANraragi for Windows can only be installed on Windows 10 and up." ) ) ;
2019-11-19 21:09:06 +08:00
//Schedule custom dialog between WelcomeDlg and InstallDirDlg standard MSI dialogs.
project . InjectClrDialog ( nameof ( ShowDialogIfWslDisabled ) , NativeDialogs . WelcomeDlg , NativeDialogs . InstallDirDlg ) ;
2019-11-19 06:44:55 +08:00
//remove LicenceDlg
2019-11-19 21:09:06 +08:00
project . RemoveDialogsBetween ( NativeDialogs . InstallDirDlg , NativeDialogs . VerifyReadyDlg ) ;
2019-11-28 20:39:32 +08:00
// Customize
project . BackgroundImage = @"Images\dlgbmp.bmp" ;
project . BannerImage = @"Images\bannrbmp.bmp" ;
2019-12-05 22:55:27 +08:00
project . ControlPanelInfo . UrlInfoAbout = "https://github.com/Difegue/LANraragi" ;
project . ControlPanelInfo . UrlUpdateInfo = "https://sugoi.gitbook.io/lanraragi/" ;
project . ControlPanelInfo . ProductIcon = @"Images\favicon.ico" ;
project . ControlPanelInfo . Contact = "Difegue" ;
2019-12-12 19:54:27 +08:00
project . ControlPanelInfo . Manufacturer = "Difegue" ;
2019-12-05 22:55:27 +08:00
2019-11-19 06:44:55 +08:00
project . OutDir = "bin" ;
project . BuildMsi ( ) ;
}
2019-11-21 19:22:35 +08:00
[CustomAction]
public static ActionResult RegisterWslDistro ( Session session )
{
2019-11-28 19:21:28 +08:00
#if DEBUG
2022-06-17 00:57:14 +08:00
Debugger . Launch ( ) ;
2019-11-28 19:21:28 +08:00
#endif
2019-11-21 19:22:35 +08:00
if ( session . IsUninstalling ( ) )
2022-07-27 00:57:58 +08:00
{
return UnRegisterWslDistro ( session ) ;
}
2019-11-21 19:22:35 +08:00
2019-11-28 19:21:28 +08:00
var packageLocation = session . Property ( "INSTALLDIR" ) + @"package.tar" ;
2022-04-25 07:08:56 +08:00
var distroInstaller = session . Property ( "INSTALLDIR" ) + @"DistroInstaller.exe" ;
2019-11-21 19:22:35 +08:00
return session . HandleErrors ( ( ) = >
{
2022-06-17 00:57:14 +08:00
// Use distroinstaller to either install or uninstall the WSL distro.
2019-11-21 19:22:35 +08:00
session . Log ( "Installing WSL Distro from package.tar" ) ;
2022-07-24 07:21:43 +08:00
session . Log ( $"DistroInstaller location: {distroInstaller}" ) ;
session . Log ( $"package.tar location: {packageLocation}" ) ;
2019-11-28 19:21:28 +08:00
2022-07-24 08:40:40 +08:00
var wslProc = Process . Start ( distroInstaller , $"-upgrade \" { packageLocation } \ "" ) ;
2021-07-31 08:39:19 +08:00
wslProc . WaitForExit ( ) ;
2022-04-25 07:08:56 +08:00
session . Log ( "Exit code of DistroInstaller is " + wslProc . ExitCode ) ;
2019-11-21 19:22:35 +08:00
} ) ;
}
[CustomAction]
public static ActionResult UnRegisterWslDistro ( Session session )
{
2022-04-25 21:34:19 +08:00
// We don't use distroInstaller here since INSTALLDIR isn't set when uninstalling.
2019-11-21 19:22:35 +08:00
return session . HandleErrors ( ( ) = >
{
session . Log ( "Removing previous WSL Distro" ) ;
2022-04-25 21:34:19 +08:00
var wslProc = Process . Start ( "wslconfig.exe" , "/unregister lanraragi" ) ;
2019-11-25 06:51:50 +08:00
wslProc . WaitForExit ( ) ;
2019-11-21 19:22:35 +08:00
} ) ;
}
2019-11-19 06:44:55 +08:00
[CustomAction]
2019-11-19 21:09:06 +08:00
public static ActionResult ShowDialogIfWslDisabled ( Session session )
2019-11-19 06:44:55 +08:00
{
2019-11-28 19:21:28 +08:00
return WixCLRDialog . ShowAsMsiDialog ( new WslCheckDialog ( session ) ) ;
2019-11-19 06:44:55 +08:00
}
2019-11-21 19:22:35 +08:00
2019-11-19 06:44:55 +08:00
}
2021-09-27 22:04:52 +08:00
}