-
Notifications
You must be signed in to change notification settings - Fork 176
/
setup.cs
227 lines (184 loc) · 7.67 KB
/
setup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//css_dir ..\..\..\;
//css_ref Wix_bin\WixToolset.Dtf.WindowsInstaller.dll;
//css_ref System.Core.dll;
using System;
using System.IO;
using System.Windows.Forms;
using WixSharp;
using WixSharp.CommonTasks;
using WixSharp.Nsis;
using WixSharp.Nsis.WinVer;
using WixToolset.Dtf.WindowsInstaller;
public static class Script
{
public static void Main(string[] args)
{
// if running the all under debugger (but not under msbuild) then the current dir needs to be adjusted
if (!@"Assets\script.ps1".PathExists())
Environment.CurrentDirectory = Environment.CurrentDirectory.PathCombine(@"..\..\..\");
// If `Prerequisite.msi` does not exist yet execute <Wix# Samples>\Bootstrapper\NsisBootstrapper\Build.cmd
var project = new ManagedProject("MainProduct",
new ManagedAction(CustomActions.MyAction, Return.ignore, When.After, Step.InstallInitialize, Condition.NOT_Installed));
project.UI = WUI.WixUI_Minimal;
var msiFile = project.BuildMsi();
if (System.IO.File.Exists(msiFile))
{
// Uncomment to preserve temp NSI files.
// Compiler.PreserveTempFiles = true;
// General sample
BuildGeneralSample(msiFile);
// Powershell script sample
BuildScriptSample(msiFile, @"Assets\script.ps1");
// BAT script sample
BuildScriptSample(msiFile, @"Assets\script.bat");
// CMD script sample
BuildScriptSample(msiFile, @"Assets\script.cmd");
// VBScript script sample
BuildScriptSample(msiFile, @"Assets\script.vbs");
// JScript script sample
BuildScriptSample(msiFile, @"Assets\script.js");
// BAT script sample with not supported win versions
BuildScriptSampleWithOSValidationWin7(msiFile, @"Assets\script.bat");
// Another BAT script sample with not supported win versions
BuildScriptSampleWithOsValidationMultiple(msiFile, @"Assets\script.bat");
// Sample of Compressor usage
BuildScriptWithCompressor(msiFile);
// Arguments sample
BuildArgumentsSample(msiFile);
}
}
public static void BuildGeneralSample(string msiFile)
{
var bootstrapper = new NsisBootstrapper
{
Prerequisite =
{
FileName = "Prerequisite.msi",
PostVerify = true,
RegKeyValue = @"HKLM:Software\My Company\My Product:Installed"
},
Primary = { FileName = msiFile },
OutputFile = "MyProduct.exe",
IconFile = "app_icon.ico",
VersionInfo = new VersionInformation("1.0.0.0")
{
ProductName = "Test Application",
LegalCopyright = "Copyright Test company",
FileDescription = "Test Application",
FileVersion = "1.0.0",
CompanyName = "Test company",
InternalName = "setup.exe",
OriginalFilename = "setup.exe"
},
SplashScreen = new SplashScreen("wixsharp.bmp")
{
FadeIn = TimeSpan.FromMilliseconds(600),
FadeOut = TimeSpan.FromMilliseconds(400)
},
OptionalArguments = " /V1"
};
bootstrapper.Build();
}
public static void BuildScriptSample(string msiFile, string prerequisiteFile)
{
var bootstrapper = new NsisBootstrapper
{
Prerequisite = { FileName = prerequisiteFile },
Primary = { FileName = msiFile },
OutputFile = "MyProduct" + Path.GetExtension(prerequisiteFile) + ".exe",
OptionalArguments = " /V1"
};
bootstrapper.Build();
}
public static void BuildScriptSampleWithOsValidationMultiple(string msiFile, string prerequisiteFile)
{
var bootstrapper = new NsisBootstrapper
{
Prerequisite = { FileName = prerequisiteFile },
Primary = { FileName = msiFile },
OutputFile = "NotSupportedMultiple" + Path.GetExtension(prerequisiteFile) + ".exe",
OptionalArguments = " /V1"
};
bootstrapper.OSValidation.MinVersion = WindowsVersionNumber._7;
bootstrapper.OSValidation.UnsupportedVersions.Add(new WindowsVersion(WindowsVersionNumber._7, 1));
bootstrapper.OSValidation.UnsupportedVersions.Add(new WindowsVersion(WindowsVersionNumber._10));
bootstrapper.OSValidation.TerminateInstallation = false;
bootstrapper.OSValidation.ErrorMessage = "Hello from custom error message!";
bootstrapper.Build();
}
public static void BuildScriptSampleWithOSValidationWin7(string msiFile, string prerequisiteFile)
{
var bootstrapper = new NsisBootstrapper
{
Prerequisite = { FileName = prerequisiteFile },
Primary = { FileName = msiFile },
OutputFile = "NotSupported7" + Path.GetExtension(prerequisiteFile) + ".exe",
OptionalArguments = " /V1"
};
bootstrapper.OSValidation.UnsupportedVersions.Add(new WindowsVersion(WindowsVersionNumber._7));
bootstrapper.Build();
}
public static void BuildScriptWithCompressor(string msiFile)
{
var bootstrapper = new NsisBootstrapper
{
Primary = { FileName = msiFile },
OutputFile = "BuildScriptWithCompressor.exe",
OptionalArguments = " /V1"
};
// Uncomment following to see resulting NSIS script in console
// bootstrapper.NsiSourceGenerated += builder => Console.WriteLine(builder);
bootstrapper.Compressor = new Compressor(Compressor.Method.Lzma, Compressor.Options.Final | Compressor.Options.Solid);
bootstrapper.Build();
}
// Arguments passing examples:
// MyProductArgs.exe /primary:"/qn" /prerequisite:"/passive /norestart"
// MyProductArgs.exe /primary:"PARAM1=VAL1"
// MyProductArgs.exe /qn
public static void BuildArgumentsSample(string msiFile)
{
var bootstrapper = new NsisBootstrapper
{
Prerequisite =
{
// See @"Assets\DotNet.ps1" for VBS example
FileName = @"Assets\DotNet.ps1",
OptionName = "/prerequisite:",
Arguments = @"/log %TEMP%\MyProductArgs_DotNetLog.html",
CreateNoWindow = false,
// Condition to check for presence of .Net 4.5 Framework.
// PrerequisiteRegKeyValue = @"HKLM:SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\.NETFramework,Version=v4.5:"
Payloads =
{
new Payload(@"Assets\Payload.txt"),
new Payload(@"Assets\Payload.txt") { Name = @"Destination\Payload.txt" }
}
},
Primary =
{
FileName = msiFile,
OptionName = "/primary:",
// $EXEPATH - The full path of the installer executable.
Arguments = @"/L*V %TEMP%\MyProductArgs_Msi.log EXEPATH=""$EXEPATH"""
},
OutputFile = "MyProductArgs.exe",
OptionalArguments = " /V1"
};
bootstrapper.Build();
}
}
public static class CustomActions
{
[CustomAction]
public static ActionResult MyAction(Session session)
{
MessageBox.Show("Hello World!", "Embedded Managed CA");
session.Log("Begin MyAction Hello World");
string exeFile = session.Property("EXEPATH");
if (!exeFile.IsNullOrEmpty())
{
session.Log("Exe file path: " + exeFile);
}
return ActionResult.Success;
}
}