自定义产品卸载方式
继续从上一次的基础上前进,现在我们已经知道了最简单的bootstrapper打包方法,现在我们对其中的每个节点深入自定义,争取可以达到我们需要的效果。先把最后全部的XML贴出来。
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:bal="http://schemas.microsoft.com/wix/BalExtension"
xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" >
<Bundle Name="CamCard" Version="4.0.0.0" Manufacturer="IntSig Information Co., Ltd." AboutUrl="http://www.intsig.net" IconSourceFile="icon_256.ico"
UpgradeCode="1EB9EC76-9E5F-4471-B522-314A62518A80" DisableRemove="no" DisableModify="yes" DisableRepair="yes">
<BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense">
<bal:WixStandardBootstrapperApplication LicenseFile="license.rtf" ThemeFile="MyTheme.xml" LocalizationFile="MyLocalize.wxl" LogoFile="logo.png" />
</BootstrapperApplicationRef>
<Chain>
<ExePackage Id="Netfx4Full"
Cache="no"
Compressed="no"
PerMachine="yes"
Permanent="yes"
Vital="yes" InstallCommand=" /q /norestart"
SourceFile="dotNetFx40_Full_x86_x64.exe"
DownloadUrl="http://go.microsoft.com/fwlink/?LinkId=164193"
DetectCondition="DotNetFramework40FullInstallRegValue=1" />
<MsiPackage Compressed="no" SourceFile="SSCERuntime-CHS.msi" Vital="yes" DisplayInternalUI="no" Permanent="yes" ForcePerMachine="yes"
DownloadUrl="http://go.microsoft.com/fwlink/?LinkID=166085"
InstallCondition="VersionNT = v5.1" />
<MsiPackage Compressed="no" SourceFile="IntSig.CamCard.Installer.msi" Vital="yes" DisplayInternalUI="no" Permanent="no" ForcePerMachine="yes">
<MsiProperty Name="TARGETDIR" Value="[InstallFolder]"/>
</MsiPackage>
</Chain>
<util:RegistrySearch Id="FindDotNet40FullInstallRegValue" Root="HKLM"
Key="SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" Value="Install"
Variable="DotNetFramework40FullInstallRegValue" />
</Bundle>
</Wix>
Bundle节点前面几个属性我们都已经知道了,IconSourceFile就是打包后exe的图标设置,DisableRemove、DisableModify这两个属性比较有讲究,他们分别设置了在“添加/删除程序”列表中,选中安装包后鼠标右击,是否会出现“卸载”和“修改”这两个选项。如果这两个选项都同时为yes,那这个产品安装后根本就不会出现在“添加/删除程序”列表中,只能通过再次双击bootstrapper安装exe进行卸载。
自定义产品安装界面
<BootstrapperApplicationRef Id="WixStandardBootstrapperApplication.RtfLicense">
<bal:WixStandardBootstrapperApplication LicenseFile="license.rtf" ThemeFile="MyTheme.xml" LocalizationFile="MyLocalize.wxl" LogoFile="logo.png" />
</BootstrapperApplicationRef>
BootstrapperApplicationRef这个节点上一篇已经讲解过,但是我们使用的是最原始的默认界面,事实上可以通过在该节点中加入WixStandardBootstrapperApplication来自定义安装界面。LicenseFile就是要显示在用户安装协议中的RTF文件名称。ThemeFile是自定义主题xml文件,该文件详细定义了安装界面中的每个按钮和控件的位置。LocalizationFile是本地化配置文件,这个版本的Burn框架还不支持运行时自动根据安装语言环境自动切换。如果你需要采用本地化安装策略,比较靠谱的方法就是在bootstrapper之前再执行另一个exe,用来判断语言环境并自动执行不同的bootstrapper进行安装。LogoFile是安装界面左上角那个图标,注意XP环境下好像是不能使用ICON文件的。
ThemeFile="MyTheme.xml" 这个文件可以在WiX源代码中找到,我贴在这篇博客里面,方便大家直接复制粘贴,这文件和后面的本地化文件的确不好找。
<?xml version="1.0" encoding="utf-8"?> <Theme xmlns="http://wixtoolset.org/schemas/thmutil/2010"> <Window Width="485" Height="300" HexStyle="100a0000" FontId="0">#(loc.Caption)</Window> <Font Id="0" Height="-12" Weight="500" Foreground="000000" Background="FFFFFF">Segoe UI</Font> <Font Id="1" Height="-24" Weight="500" Foreground="000000">Segoe UI</Font> <Font Id="2" Height="-22" Weight="500" Foreground="666666">Segoe UI</Font> <Font Id="3" Height="-12" Weight="500" Foreground="000000" Background="FFFFFF">Segoe UI</Font> <Font Id="4" Height="-12" Weight="500" Foreground="ff0000" Background="FFFFFF" Underline="yes">Segoe UI</Font> <Image X="11" Y="11" Width="64" Height="64" ImageFile="logo.png" Visible="yes"/> <Text X="80" Y="11" Width="-11" Height="64" FontId="1" Visible="yes" DisablePrefix="yes">#(loc.Title)</Text> <Page Name="Help"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.HelpHeader)</Text> <Text X="11" Y="112" Width="-11" Height="-35" FontId="3" DisablePrefix="yes">#(loc.HelpText)</Text> <Button Name="HelpCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.HelpCloseButton)</Button> </Page> <Page Name="Install"> <Richedit Name="EulaRichedit" X="11" Y="80" Width="-11" Height="-70" TabStop="yes" FontId="0" HexStyle="0x800000" /> <Checkbox Name="EulaAcceptCheckbox" X="-11" Y="-41" Width="260" Height="17" TabStop="yes" FontId="3" HideWhenDisabled="yes">#(loc.InstallAcceptCheckbox)</Checkbox> <Button Name="OptionsButton" X="-171" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0" HideWhenDisabled="yes">#(loc.InstallOptionsButton)</Button> <Button Name="InstallButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.InstallInstallButton)</Button> <Button Name="WelcomeCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.InstallCloseButton)</Button> </Page> <Page Name="Options"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.OptionsHeader)</Text> <Text X="11" Y="121" Width="-11" Height="17" FontId="3" DisablePrefix="yes">#(loc.OptionsLocationLabel)</Text> <Editbox Name="FolderEditbox" X="11" Y="143" Width="-91" Height="21" TabStop="yes" FontId="3" FileSystemAutoComplete="yes" /> <Button Name="BrowseButton" X="-11" Y="142" Width="75" Height="23" TabStop="yes" FontId="3">#(loc.OptionsBrowseButton)</Button> <Button Name="OptionsOkButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.OptionsOkButton)</Button> <Button Name="OptionsCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.OptionsCancelButton)</Button> </Page> <Page Name="Progress"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.ProgressHeader)</Text> <Text X="11" Y="121" Width="70" Height="17" FontId="3" DisablePrefix="yes">#(loc.ProgressLabel)</Text> <Text Name="OverallProgressPackageText" X="85" Y="121" Width="-11" Height="17" FontId="3" DisablePrefix="yes">#(loc.OverallProgressPackageText)</Text> <Progressbar Name="OverallCalculatedProgressbar" X="11" Y="143" Width="-11" Height="15" /> <Button Name="ProgressCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.ProgressCancelButton)</Button> </Page> <Page Name="Modify"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.ModifyHeader)</Text> <Button Name="RepairButton" X="-171" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0" HideWhenDisabled="yes">#(loc.ModifyRepairButton)</Button> <Button Name="UninstallButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.ModifyUninstallButton)</Button> <Button Name="ModifyCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.ModifyCloseButton)</Button> </Page> <Page Name="Success"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.SuccessHeader)</Text> <Button Name="LaunchButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0" HideWhenDisabled="yes">#(loc.SuccessLaunchButton)</Button> <Text Name="SuccessRestartText" X="-11" Y="-51" Width="400" Height="34" FontId="3" HideWhenDisabled="yes" DisablePrefix="yes">#(loc.SuccessRestartText)</Text> <Button Name="SuccessRestartButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0" HideWhenDisabled="yes">#(loc.SuccessRestartButton)</Button> <Button Name="SuccessCancelButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.SuccessCloseButton)</Button> </Page> <Page Name="Failure"> <Text X="11" Y="80" Width="-11" Height="30" FontId="2" DisablePrefix="yes">#(loc.FailureHeader)</Text> <Hypertext Name="FailureLogFileLink" X="11" Y="121" Width="-11" Height="42" FontId="3" TabStop="yes" HideWhenDisabled="yes">#(loc.FailureHyperlinkLogText)</Hypertext> <Hypertext Name="FailureMessageText" X="22" Y="163" Width="-11" Height="51" FontId="3" TabStop="yes" HideWhenDisabled="yes" /> <Text Name="FailureRestartText" X="-11" Y="-51" Width="400" Height="34" FontId="3" HideWhenDisabled="yes" DisablePrefix="yes">#(loc.FailureRestartText)</Text> <Button Name="FailureRestartButton" X="-91" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0" HideWhenDisabled="yes">#(loc.FailureRestartButton)</Button> <Button Name="FailureCloseButton" X="-11" Y="-11" Width="75" Height="23" TabStop="yes" FontId="0">#(loc.FailureCloseButton)</Button> </Page> </Theme>
<?xml version="1.0" encoding="utf-8"?> <WixLocalization xmlns="http://schemas.microsoft.com/wix/2006/localization"> <String Id="Caption">Setup [WixBundleName]</String> <String Id="Title">[WixBundleName]</String> <String Id="ConfirmCancelMessage">Are you sure you want to cancel?</String> <String Id="HelpHeader">Setup Help</String> <String Id="HelpText"> /install | /repair | /uninstall | /layout [directory] - installs, repairs, uninstalls or creates a complete local copy of the bundle in directory. Install is the default. /passive | /quiet - displays minimal UI with no prompts or displays no UI and no prompts. By default UI and all prompts are displayed. /norestart - suppress any attempts to restart. By default UI will prompt before restart. /log log.txt - logs to a specific file. By default a log file is created in %TEMP%. </String> <String Id="HelpCloseButton">&Close</String> <String Id="InstallAcceptCheckbox">I &agree to the license terms and conditions</String> <String Id="InstallOptionsButton">&Options</String> <String Id="InstallInstallButton">&Install</String> <String Id="InstallCloseButton">&Close</String> <String Id="OptionsHeader">Setup Options</String> <String Id="OptionsLocationLabel">Install location (disk root path is not recommanded):</String> <String Id="OptionsBrowseButton">&Browse</String> <String Id="OptionsOkButton">&OK</String> <String Id="OptionsCancelButton">&Cancel</String> <String Id="ProgressHeader">Setup Progress</String> <String Id="ProgressLabel">Processing:</String> <String Id="OverallProgressPackageText">Initializing...</String> <String Id="ProgressCancelButton">&Cancel</String> <String Id="ModifyHeader">Modify Setup</String> <String Id="ModifyRepairButton">&Repair</String> <String Id="ModifyUninstallButton">&Uninstall</String> <String Id="ModifyCloseButton">&Close</String> <String Id="SuccessHeader">Setup Successful</String> <String Id="SuccessLaunchButton">&Launch</String> <String Id="SuccessRestartText">You must restart your computer before you can use the software.</String> <String Id="SuccessRestartButton">&Restart</String> <String Id="SuccessCloseButton">&Close</String> <String Id="FailureHeader">Setup Failed</String> <String Id="FailureHyperlinkLogText">One or more issues caused the setup to fail. Please fix the issues and then retry setup. For more information see the <a href="#">log file</a>.</String> <String Id="FailureRestartText">You must restart your computer to complete the rollback of the software.</String> <String Id="FailureRestartButton">&Restart</String> <String Id="FailureCloseButton">&Close</String> </WixLocalization>
自定义安装序列
上面我们已经将安装界面全部自定义完毕,脸面上的事情总算是做完了,我们要解决的根本问题还在眼前,如何自定义调整安装顺序?答案就是通过Chain节点来完成。Chain节点定义了打包安装的顺序,默认是从上到下逐个完成,当然你也可以在子节点中通过After属性来调整安装顺序。
首先是判断并安装.NET Framework 4.0环境。
<ExePackage Id="Netfx4Full" Cache="no" Compressed="no" PerMachine="yes" Permanent="yes" Vital="yes" InstallCommand=" /q /norestart" SourceFile="dotNetFx40_Full_x86_x64.exe" DownloadUrl="http://go.microsoft.com/fwlink/?LinkId=164193" DetectCondition="DotNetFramework40FullInstallRegValue=1" />
Compressed表示是否要将该安装包打包在bootstrapper.exe文件中,我们回忆一下上一讲,上一讲我们的Setup1.msi文件因为没有带上该属性,就自动将安装包打包在bootstrapper中了,因此我们只得到了一个安装文件。如果Compressed="no",那就会在bootstrapper.exe边上出现另外一个单独的安装文件,这样可以减小我们发布的尺寸。
PerMachine是表示是否要采用UAC权限进行安装,因为有些安装文件需要以管理员身份运行才能安装成功,而我们现在采取的是静默安装策略,因此需要在定义安装的时候就表明是否需要UAC权限。
Permanent表示卸载时是否需要同时卸载该安装包。yes表示卸载产品时该安装包不需要同时卸载,将会永远留在客户的计算机里面。
Vital表示是否该安装时必须的,如果该安装是必须的,那如果该安装没有成功,后续的所有安装都不会进行。
InstallCommand是在安装时需要跟上的参数,我们知道.NET的静默安装需要带上“ /q /norestart”参数,这样我们用bootstrapper托管下就可以让.NET静默安装完毕了。
SourceFile表明安装文件的名称是什么,它和DownloadUrl是任选的,也就是说如果安装时发现该文件包不存在的话,需要从什么URL进行自动下载并安装。
DetectCondition是bootstrapper的精髓,它用一个表达式来判断一台计算机上该包是否已经安装过,DotNetFramework40FullInstallRegValue和文档最后的util:RegistrySearch节点ID是一致的,util:RegistrySearch代表查看注册表中的某个表项的值的变量,然后匹配该注册表项值满足条件,则说明已经安装,如果不满足则说明需要安装。
<MsiPackage Compressed="no" SourceFile="SSCERuntime-CHS.msi" Vital="yes" DisplayInternalUI="no" Permanent="yes" ForcePerMachine="yes" DownloadUrl="http://go.microsoft.com/fwlink/?LinkID=166085" InstallCondition="VersionNT = v5.1" />
<MsiPackage Compressed="no" SourceFile="IntSig.CamCard.Installer.msi" Vital="yes" DisplayInternalUI="no" Permanent="no" ForcePerMachine="yes"> <MsiProperty Name="TARGETDIR" Value="[InstallFolder]"/> </MsiPackage>
完成上面的这些工作,再把所需要的安装包,配置文件都放到工程里面后,经过项目生成,我们就拿到很完美的bootstrapper了。现在的结果是,最极限情况下我们只需要发布一个bootstrapper,后续所有的软件依赖就能下载并安装,极大得减少了用户下载安装的负担。并且因为可以动态判断依赖包,所以用户的安装速度也得到极大的提升。最最重要的是,只需要在安装第一个包的时候进行一次UAC询问,后续就不再会有类似的恼人确认对话框了。至此,我们之前所有提出的VS打包项目的不足就全部解决了。