-
Notifications
You must be signed in to change notification settings - Fork 1
/
vdproj2wix.ps1
397 lines (345 loc) · 10.9 KB
/
vdproj2wix.ps1
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
################################################################################
#
# \file vdproj2wix.ps1
# \brief Converts a VS setup project (.vdproj) to a WiX source file (.wxs).
# \author Chris Oldwood ([email protected] | http://www.cix.co.uk/~gort)
# \version 1.0
#
# This script does a trivial transformation of a Visual Studio setup project
# file (.vdproj) to a WiX format file (.wxs). It was only designed to handle
# simple server-side deployments, i.e. the GUID's, folders, files and registry
# keys.
#
################################################################################
set-strictmode -version latest
# Configure error handling
$ErrorActionPreference = 'stop'
trap
{
write-error $_ -erroraction continue
exit 1
}
# Validate command line
if ( ($args.count -ne 1) -or ($args[0] -eq '--help') )
{
if ($args[0] -eq '--help')
{
write-output "vdproj2wix v1.0"
write-output "(C) Chris Oldwood 2011 ([email protected])"
}
else
{
write-output "ERROR: Invalid command line"
}
write-output ""
write-output "USAGE: vdproj2wix <.vdproj file>"
write-output ""
write-output "e.g. PowerShell -File vdproj2wix.ps1 Test.vdproj"
exit 1
}
# Format output filename
$vdprojFile = $args[0]
$wixFolder = split-path -parent $vdprojFile
$wixFile = split-path -leaf $vdprojFile
$wixFile = $wixFile -replace '\.vdproj','.wxs'
if ($wixFolder -ne '')
{
$wixFile = join-path $wixFolder $wixFile
}
write-output "Input : $vdprojFile"
write-output "Output: $wixFile"
################################################################################
# Parse the .vdproj file
################################################################################
# Remove the escape characters from the path
function unescapeFilePath($filePath)
{
return $filePath -replace "\\\\","\"
}
# Initialise content variables
$productId = '<unknown>'
$productName = '<unknown>'
$languageId = '1033'
$productVersion = '<unknown>'
$manufacturer = '<unknown>'
$upgradeCode = '<unknown>'
$packageCode = '<unknown>'
$files = @()
$folders = @()
# Initialise parsing variables
$inProductSection = $false
$inFileSection = $false
$currentFile = $null
$inFolderSection = $false
$currentFolder = $null
$parentIndex = -1
$folderStack = @()
$folderId=''
$guid=''
$defaultLocation = ''
$folderMap = @{}
$nextFolderId = 1
$nextComponentId = 1
$nextFeatureId = 1
$components = @()
# For each line in the .vdproj file...
$lines = get-content $vdprojFile
foreach ($line in $lines)
{
# Handle entry/exit of the sections we're interested in
if ($line -match '^\s{8}"(?<section>\w+)"$')
{
if ($matches.section -eq 'Product')
{
$inProductSection = $true
}
elseif ($matches.section -eq 'File')
{
$inFileSection = $true
}
elseif ($matches.section -eq 'Folder')
{
$inFolderSection = $true
}
else
{
$inProductSection = $false
$inFileSection = $false
$currentFile = $null
$inFolderSection = $false
$currentFolder = $null
}
}
# Parse product section
elseif ($inProductSection -eq $true)
{
if ($line -match '^\s{8}"ProductCode" = "8:{(?<value>.*)}"$')
{
$productId = $matches.value
}
elseif ($line -match '^\s{8}"ProductName" = "8:(?<value>.*)"$')
{
$productName = $matches.value
}
elseif ($line -match '^\s{8}"ProductVersion" = "8:(?<value>.*)"$')
{
$productVersion = $matches.value
}
elseif ($line -match '^\s{8}"Manufacturer" = "8:(?<value>.*)"$')
{
$manufacturer = $matches.value
}
elseif ($line -match '^\s{8}"UpgradeCode" = "8:{(?<value>.*)}"$')
{
$upgradeCode = $matches.value
}
elseif ($line -match '^\s{8}"PackageCode" = "8:{(?<value>.*)}"$')
{
$packageCode = $matches.value
}
}
# Parse files section
elseif ($inFileSection -eq $true)
{
if ($line -match '^\s{12}"SourcePath" = "8:(?<value>.*)"$')
{
$currentFile = @{ SourcePath=$matches.value; TargetName=''; Folder='' }
$files += $currentFile
}
elseif ( ($line -match '^\s{12}"TargetName" = "8:(?<value>.*)"$') -and ($matches.value -ne '') )
{
$currentFile.TargetName = $matches.value
}
elseif ($line -match '^\s{12}"Folder" = "8:(?<value>.*)"$')
{
$currentFile.Folder = $matches.value
}
elseif ( ($line -match '^\s{12}"Exclude" = "11:(?<value>.*)"$') -and ($matches.value -eq 'true') )
{
$files = $files[0..($files.length-2)]
}
}
# Parse folder section
elseif ($inFolderSection -eq $true)
{
# Start of any folder entry?
if ($line -match '^\s{12,}"\{(?<guid>[A-Z0-9-]+)\}:(?<value>_[A-Z0-9-]+)"$')
{
$guid = $matches.guid
$folderId = $matches.value
$defaultLocation = ''
}
# Start of child folders section?
elseif ($line -match '^\s{16,}"Folders"$')
{
++$parentIndex
}
# Matching end of child folders section or the folder itself?
elseif ( ($line -match '^\s{16,}\}$') -and (($line.indexof('}') % 8) -eq 0) )
{
if ($parentIndex -ge 1)
{
--$parentIndex
$folderStack = $folderStack[0..$parentIndex]
}
else
{
$parentIndex = -1
$folderStack = @()
}
}
if ($line -match '^\s{12,}"DefaultLocation" = "8:(?<value>.*)"$')
{
$defaultLocation = $matches.value
}
elseif ($line -match '^\s{12,}"Name" = "8:(?<value>.*)"$')
{
$currentFolder = @{ Id=$folderId; Name=$matches.value;
Property=''; DefaultLocation=$defaultLocation;
Guid=$guid; Folders=@(); Files=@()
}
if ($parentIndex -lt 0)
{
$folders += $currentFolder
}
else
{
$folderStack[$parentIndex].Folders += $currentFolder
}
$folderStack += $currentFolder
$folderMap.$folderId = $currentFolder
}
elseif ($line -match '^\s{12,}"Property" = "8:(?<value>.*)"$')
{
$currentFolder.Property = $matches.value
if ($currentFolder.Property -eq 'TARGETDIR')
{
$currentFolder.Id = $currentFolder.Property
$currentFolder.Name = 'SourceDir'
}
}
}
}
# Sort the files by filename to make checking easier
$files = $files | sort -property { split-path -leaf $_.SourcePath }
# Link the files to their parent folders
if ($files -ne $null)
{
foreach ($file in $files)
{
$parent = $file.Folder
($folderMap.$parent).Files += $file
}
}
# Expand the DefaultLocation folder into a folder tree
if ($folders -ne $null)
{
foreach ($folder in $folders)
{
if ($folder.DefaultLocation -ne '')
{
$installFolder = unescapeFilePath $folder.DefaultLocation
$installFolder = $installFolder -replace '\[ProgramFilesFolder\]',"ProgramFilesFolder\"
$installFolder = $installFolder -replace '\[Manufacturer\]',"$manufacturer"
$installFolder = $installFolder -replace '\[ProductName\]',"$productName"
$childFolders = $installFolder.Split('\\')
foreach ($child in $childFolders)
{
# Insert new child folder between the current one and its children
$newFolder = @{ Id=$child; Name=$child; Property=''; DefaultLocation='';
Guid=$folder.Guid; Folders=$folder.Folders; Files=$folder.Files }
$folder.Guid = ''
$folder.Folders = @()
$folder.Files = @()
$folder.Folders += $newFolder
$folder = $newFolder
}
}
}
}
################################################################################
# Write the WiX file
################################################################################
# Write the tree of folders and files
function writeFileSystemTree($tree, $indent, $file)
{
foreach ($folder in $tree)
{
# Don't output empty common folders
if ( $folder.Name.startswith('#') -and ($folder.Folders.length -eq 0) )
{
continue;
}
if ( ($folder.Id -eq 'TARGETDIR') -or ($folder.Id -eq 'ProgramFilesFolder') )
{
$folderId = $folder.Id
}
else
{
$folderId = "_{0}" -f $script:nextFolderId++
}
"{0}<Directory Name=`"{1}`" Id=`"{2}`">" -f $indent,$folder.Name,$folderId | out-file -encoding ASCII $file -append
if ($folder.Folders.length -ne 0)
{
$indent += ' '
writeFileSystemTree $folder.Folders $indent $file
$indent = $indent.substring(0, $indent.length-4)
}
if ($folder.Files.length -ne 0)
{
$indent += ' '
$guid = $folder.Guid
$componentId = "_{0}" -f $script:nextComponentId++
#"" | out-file -encoding ASCII $wixFile -append
"{0}<Component Id=`"$componentId`" Guid=`"$guid`">" -f $indent | out-file -encoding ASCII $wixFile -append
#"" | out-file -encoding ASCII $wixFile -append
foreach ($file in $folder.Files)
{
$path = unescapeFilePath $file.SourcePath
$name = split-path -leaf $path
if ( ($file.TargetName -ne $name) -and ($file.TargetName -ne '') )
{
$name = $file.TargetName
"{0} <File Source=`"$path`" Name=`"$name`"/>" -f $indent | out-file -encoding ASCII $wixFile -append
}
else
{
"{0} <File Source=`"$path`"/>" -f $indent | out-file -encoding ASCII $wixFile -append
}
}
#"" | out-file -encoding ASCII $wixFile -append
"{0}</Component>" -f $indent | out-file -encoding ASCII $wixFile -append
#"" | out-file -encoding ASCII $wixFile -append
$script:components += @{ Id=$componentId; Guid=$guid }
$indent = $indent.substring(0, $indent.length-4)
}
"{0}</Directory>" -f $indent | out-file -encoding ASCII $wixFile -append
}
}
# Generate the .wxs file
"<?xml version=`"1.0`"?>" | out-file -encoding ASCII $wixFile
"<Wix xmlns=`"http://schemas.microsoft.com/wix/2006/wi`">" | out-file -encoding ASCII $wixFile -append
" <Product Id=`"$productId`"" | out-file -encoding ASCII $wixFile -append
" Name=`"$productName`"" | out-file -encoding ASCII $wixFile -append
" Language=`"$languageId`"" | out-file -encoding ASCII $wixFile -append
" Version=`"$productVersion`"" | out-file -encoding ASCII $wixFile -append
" Manufacturer=`"$manufacturer`"" | out-file -encoding ASCII $wixFile -append
" UpgradeCode=`"$upgradeCode`">" | out-file -encoding ASCII $wixFile -append
"" | out-file -encoding ASCII $wixFile -append
" <Package Compressed=`"yes`"/>" | out-file -encoding ASCII $wixFile -append
"" | out-file -encoding ASCII $wixFile -append
" <Media Id=`"1`" Cabinet=`"product.cab`" EmbedCab=`"yes`"/>" | out-file -encoding ASCII $wixFile -append
"" | out-file -encoding ASCII $wixFile -append
writeFileSystemTree $folders ' ' $wixFile
$featureId = "_{0}" -f $nextFeatureId
"" | out-file -encoding ASCII $wixFile -append
" <Feature Id=`"$featureId`" Level=`"1`">" | out-file -encoding ASCII $wixFile -append
foreach ($component in $components)
{
$id = $component.Id
" <ComponentRef Id=`"$id`"/>" | out-file -encoding ASCII $wixFile -append
}
" </Feature>" | out-file -encoding ASCII $wixFile -append
"" | out-file -encoding ASCII $wixFile -append
" </Product>" | out-file -encoding ASCII $wixFile -append
"</Wix>" | out-file -encoding ASCII $wixFile -append