Skip to content

Commit 4680579

Browse files
authored
Merge pull request #2 from begroff/feature/newupdates
Feature/newupdates
2 parents 7c2f1a5 + b0c3b15 commit 4680579

4 files changed

Lines changed: 118 additions & 52 deletions

File tree

ReadMe.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ Setting the MinimumRequiredVersion property forces the ClickOnce application to
88

99
# Script Parameters
1010
* **ProjectFilePath** (required) - Path of the .csproj and .vbproj file to process.
11-
11+
1212
* **Version** - The Version to update the ClickOnce version number to. This version must be of the format Major.Minor.Build or Major.Minor.Build.Revision. The Build and Revision parts will be overridden by the BuildSystemsBuildId parameter, if it is provided. The Revision parts will be overriden by the IncrementProjectFilesRevision parameter, if it is provided.
1313

1414
* **BuildSystemsBuildId** - The build system's unique and auto-incrementing Build ID. This will be used to generate the Build and Revision parts of the new Version number. This will override the Build and Revision specified in the Version parameter, if it was provided. This parameter cannot be used with the IncrementProjectFilesRevision parameter.
15-
15+
1616
* **IncrementProjectFilesRevision** - If this switch is provided, the Revision from the project file will be incremented and used in the new ClickOnce Version. This will override the Revision specified in the Version parameter, if it was provided. This parameter cannot be used with the BuildSystemsBuildId parameter.
17-
17+
1818
* **UpdateMinimumRequiredVersionToCurrentVersion** - If this switch is provided, the ClickOnce MinimumRequiredVersion will be updated to match the new Version. Setting the MinimumRequiredVersion property forces the ClickOnce application to update automatically without prompting the user.
1919

20+
* **PublishUrl** - If this switch is provided, the ClickOnce PublishUrl will be updated. The publish url format is \\fileshare\foldername
21+
22+
* **InstallUrl** - If this switch is provided, the ClickOnce InstallUrl will be updated. The install url format is http://fileshare/foldername
2023

2124
# Examples
2225
Update a project file's ClickOnce version to the specified version.
@@ -52,4 +55,11 @@ Update a project file's ClickOnce version, ignoring the Revision part and increm
5255
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version '1.2.3' -IncrementProjectFilesRevision -UpdateMinimumRequiredVersionToCurrentVersion
5356
```
5457

55-
[AutoUpdateProjectsMinimumRequiredClickOnceVersionNugetPackageWebpage]: https://www.nuget.org/packages/AutoUpdateProjectsMinimumRequiredClickOnceVersion
58+
---
59+
60+
Update a project file's ClickOnce version and its install and publish url values.
61+
```
62+
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version 1.0.1.9 -PublishUrl "\\fileshare\foldername" -InstallUrl "http://fileshare/foldername"
63+
```
64+
65+
[AutoUpdateProjectsMinimumRequiredClickOnceVersionNugetPackageWebpage]: https://www.nuget.org/packages/AutoUpdateProjectsMinimumRequiredClickOnceVersion

Set-ProjectFilesClickOnceVersion.ps1

Lines changed: 84 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<#
33
.SYNOPSIS
44
This script updates the ClickOnce version in a project file (.csproj or .vbproj), and may update the MinimumRequiredVersion to be this same version.
5-
5+
66
.DESCRIPTION
77
This script updates the current ClickOnce version in a project file (.csproj or .vbproj), and may update the MinimumRequiredVersion to be this same version.
88
Setting the MinimumRequiredVersion property forces the ClickOnce application to update automatically without prompting the user.
9-
9+
1010
.PARAMETER ProjectFilePath
1111
(required) Path of the .csproj and .vbproj file to process.
12-
12+
1313
.PARAMETER Version
1414
The Version to update the ClickOnce version number to. This version must be of the format Major.Minor.Build or Major.Minor.Build.Revision.
1515
The Build and Revision parts will be overridden by the BuildSystemsBuildId parameter, if it is provided.
@@ -19,44 +19,61 @@
1919
The build system's unique and auto-incrementing Build ID. This will be used to generate the Build and Revision parts of the new Version number.
2020
This will override the Build and Revision specified in the Version parameter, if it was provided.
2121
This parameter cannot be used with the IncrementProjectFilesRevision parameter.
22-
22+
2323
.PARAMETER IncrementProjectFilesRevision
2424
If this switch is provided, the Revision from the project file will be incremented and used in the new ClickOnce Version.
2525
This will override the Revision specified in the Version parameter, if it was provided.
2626
This parameter cannot be used with the BuildSystemsBuildId parameter.
27-
27+
2828
.PARAMETER UpdateMinimumRequiredVersionToCurrentVersion
2929
If this switch is provided, the ClickOnce MinimumRequiredVersion will be updated to match the new Version.
3030
Setting the MinimumRequiredVersion property forces the ClickOnce application to update automatically without prompting the user.
3131
32+
.PARAMETER PublishUrl
33+
If this string is provided, it will update the PublishUrl. If it is not provided, the PublishUrl will remain what it is currently. This should be a UNC type file path (e.g. \\servername\foldername)
34+
35+
.PARAMETER InstallUrl
36+
If this string is provided, it will update the InstallUrl. If it is not provided the InstallUrl will remain what it is currently. This should be a URL type path (e.g. http://servername/foldername)
37+
3238
.EXAMPLE
3339
Update a project file's ClickOnce version to the specified version.
34-
40+
3541
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version '1.2.3.4'
3642
3743
.EXAMPLE
3844
Update the Build and Revision parts of a project file's ClickOnce version, based on a unique, auto-incrementing integer, such as a build system's Build ID.
39-
45+
4046
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -BuildSystemsBuildId 123456
4147
4248
.EXAMPLE
4349
Increment the Revision of a project file's ClickOnce version.
44-
50+
4551
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -IncrementProjectFilesRevision
4652
4753
.EXAMPLE
4854
Update a project file's ClickOnce Minimum Required Version to match its current version.
49-
55+
5056
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -UpdateMinimumRequiredVersionToCurrentVersion
51-
57+
5258
.EXAMPLE
5359
Update a project file's ClickOnce version, ignoring the Revision part and incrementing the Revision stored in the file, and update the Minimum Required Version to be this new version.
54-
60+
5561
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version '1.2.3' -IncrementProjectFilesRevision -UpdateMinimumRequiredVersionToCurrentVersion
56-
62+
63+
.EXAMPLE
64+
Update a project file's ClickOnce version using both Version and a unique, auto-incrementing integer, such as a build system's Build ID. This will keep the major and minor versions you specify but update the build and revision (e.g. 1.0.1.5745)
65+
66+
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version 1.0.0.0 -BuildSystemBuildId 123456
67+
68+
.EXAMPLE
69+
Update a project file's ClickOnce version and its install and publish url values.
70+
71+
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version 1.0.1.9 -PublishUrl "\\fileshare\foldername" -InstallUrl "http://fileshare/foldername"
72+
73+
5774
.LINK
5875
Project Home: https://github.com/deadlydog/Set-ProjectFilesClickOnceVersion
59-
76+
6077
.NOTES
6178
Author: Daniel Schroeder
6279
Version: 2.0.0
@@ -81,7 +98,13 @@ Param
8198
[switch]$IncrementProjectFilesRevision = $false,
8299

83100
[Parameter(Mandatory=$false,HelpMessage="When the switch is provided, the ClickOnce Minimum Required Version will be updated to this new version.")]
84-
[switch]$UpdateMinimumRequiredVersionToCurrentVersion = $false
101+
[switch]$UpdateMinimumRequiredVersionToCurrentVersion = $false,
102+
103+
[Parameter(Mandatory = $false, HelpMessage="The Publish URL to update to.")]
104+
[string]$PublishUrl = '',
105+
106+
[Parameter(Mandatory = $false, HelpMessage="The Install URL to update to.")]
107+
[string]$InstallUrl = ''
85108
)
86109

87110
# If we can't find the project file path to update, exit with an error.
@@ -92,17 +115,17 @@ if (!(Test-Path $ProjectFilePath -PathType Leaf))
92115
}
93116

94117
# If there are no changes to make, just exit.
95-
if ([string]::IsNullOrEmpty($Version) -and $BuildSystemsBuildId -lt 0 -and !$IncrementProjectFilesRevision -and !$UpdateMinimumRequiredVersionToCurrentVersion)
118+
if ([string]::IsNullOrEmpty($Version) -and $BuildSystemsBuildId -lt 0 -and !$IncrementProjectFilesRevision -and !$UpdateMinimumRequiredVersionToCurrentVersion -and !$InstallUrl -and !$PublishUrl)
96119
{
97-
Write-Warning "None of the following parameters were provided, so nothing will be changed: Version, BuildSystemsBuildId, IncrementProjectFilesRevision, UpdateMinimumRequiredVersionToCurrentVersion"
120+
Write-Warning "None of the following parameters were provided, so nothing will be changed: Version, BuildSystemsBuildId, IncrementProjectFilesRevision, UpdateMinimumRequiredVersionToCurrentVersion, InstallUrl and PublishUrl"
98121
return
99122
}
100123

101124
function Get-XmlNamespaceManager([xml]$XmlDocument, [string]$NamespaceURI = "")
102125
{
103126
# If a Namespace URI was not given, use the Xml document's default namespace.
104-
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }
105-
127+
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }
128+
106129
# In order for SelectSingleNode() to actually work, we need to use the fully qualified node path along with an Xml Namespace Manager, so set them up.
107130
[System.Xml.XmlNamespaceManager]$xmlNsManager = New-Object System.Xml.XmlNamespaceManager($XmlDocument.NameTable)
108131
$xmlNsManager.AddNamespace("ns", $NamespaceURI)
@@ -118,7 +141,7 @@ function Get-XmlNode([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceURI
118141
{
119142
$xmlNsManager = Get-XmlNamespaceManager -XmlDocument $XmlDocument -NamespaceURI $NamespaceURI
120143
[string]$fullyQualifiedNodePath = Get-FullyQualifiedXmlNodePath -NodePath $NodePath -NodeSeparatorCharacter $NodeSeparatorCharacter
121-
144+
122145
# Try and get the node, then return it. Returns $null if the node was not found.
123146
$node = $XmlDocument.SelectSingleNode($fullyQualifiedNodePath, $xmlNsManager)
124147
return $node
@@ -136,36 +159,36 @@ function Get-XmlNodes([xml]$XmlDocument, [string]$NodePath, [string]$NamespaceUR
136159

137160
function Get-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
138161
{
139-
# Try and get the node.
162+
# Try and get the node.
140163
$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
141-
164+
142165
# If the node already exists, return its value, otherwise return null.
143166
if ($node) { return $node.InnerText } else { return $null }
144167
}
145168

146169
function Set-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [string]$TextValue, [string]$NamespaceURI = "", [string]$NodeSeparatorCharacter = '.')
147170
{
148-
# Try and get the node.
171+
# Try and get the node.
149172
$node = Get-XmlNode -XmlDocument $XmlDocument -NodePath $ElementPath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
150-
173+
151174
# If the node already exists, update its value.
152175
if ($node)
153-
{
176+
{
154177
$node.InnerText = $TextValue
155178
}
156179
# Else the node doesn't exist yet, so create it with the given value.
157180
else
158181
{
159182
# Create the new element with the given value.
160183
$elementName = $ElementPath.Substring($ElementPath.LastIndexOf($NodeSeparatorCharacter) + 1)
161-
$element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI)
184+
$element = $XmlDocument.CreateElement($elementName, $XmlDocument.DocumentElement.NamespaceURI)
162185
$textNode = $XmlDocument.CreateTextNode($TextValue)
163186
$element.AppendChild($textNode) > $null
164-
187+
165188
# Try and get the parent node.
166189
$parentNodePath = $ElementPath.Substring(0, $ElementPath.LastIndexOf($NodeSeparatorCharacter))
167190
$parentNode = Get-XmlNode -XmlDocument $XmlDocument -NodePath $parentNodePath -NamespaceURI $NamespaceURI -NodeSeparatorCharacter $NodeSeparatorCharacter
168-
191+
169192
if ($parentNode)
170193
{
171194
$parentNode.AppendChild($element) > $null
@@ -179,9 +202,9 @@ function Set-XmlElementsTextValue([xml]$XmlDocument, [string]$ElementPath, [stri
179202

180203
function Set-XmlNodesElementTextValue([xml]$xml, $node, $elementName, $textValue)
181204
{
182-
if ($node.($elementName) -eq $null)
205+
if ($null -eq $node.($elementName))
183206
{
184-
$element = $xml.CreateElement($elementName, $xml.DocumentElement.NamespaceURI)
207+
$element = $xml.CreateElement($elementName, $xml.DocumentElement.NamespaceURI)
185208
$textNode = $xml.CreateTextNode($textValue)
186209
$element.AppendChild($textNode) > $null
187210
$node.AppendChild($element) > $null
@@ -201,7 +224,7 @@ $versionNumberRegex = New-Object System.Text.RegularExpressions.Regex "(?<MajorM
201224
# Open the Xml file and get the <PropertyGroup> elements with the ClickOnce properties in it.
202225
[xml]$xml = Get-Content -Path $ProjectFilePath
203226
$propertyGroups = Get-XmlNodes -XmlDocument $xml -NodePath 'Project.PropertyGroup'
204-
[Array]$clickOncePropertyGroups = $propertyGroups | Where-Object {
227+
[Array]$clickOncePropertyGroups = $propertyGroups | Where-Object {
205228
try
206229
{
207230
return ($_.ApplicationVersion -ne $null)
@@ -210,7 +233,7 @@ $propertyGroups = Get-XmlNodes -XmlDocument $xml -NodePath 'Project.PropertyGrou
210233
}
211234

212235
# If no ClickOnce deployment settings were found throw an error.
213-
if ($clickOncePropertyGroups -eq $null -or $clickOncePropertyGroups.Count -eq 0)
236+
if ($null -eq $clickOncePropertyGroups -or $clickOncePropertyGroups.Count -eq 0)
214237
{
215238
throw "'$ProjectFilePath' does not appear to have any ClickOnce deployment settings in it. You must publish the project at least once to create the ClickOnce deployment settings."
216239
}
@@ -223,13 +246,31 @@ foreach ($clickOncePropertyGroup in $clickOncePropertyGroups)
223246
$numberOfClickOncePropertyGroupsProcessed++
224247
Write-Verbose "Processing ClickOnce property group $numberOfClickOncePropertyGroupsProcessed of $numberOfClickOncePropertyGroups in file '$ProjectFilePath'."
225248

249+
# If publish url is provided, update it
250+
$publishUrl = $PublishUrl
251+
if (![string]::IsNullOrEmpty($publishUrl))
252+
{
253+
Write-Verbose "Publish Url is '$publishUrl'"
254+
Write-Output "Updating PublishUrl to be '$publishUrl'"
255+
Set-XmlNodesElementTextValue -xml $xml -node $clickOncePropertyGroup -elementName 'PublishUrl' -textValue "$publishUrl"
256+
}
257+
258+
# If install url is provided, update it
259+
$installUrl = $InstallUrl
260+
if (![string]::IsNullOrEmpty($installUrl))
261+
{
262+
Write-Verbose "Install Url is '$installUrl'"
263+
Write-Output "Updating Install Url to be '$installUrl'"
264+
Set-XmlNodesElementTextValue -xml $xml -node $clickOncePropertyGroup -elementName 'InstallUrl' -textValue "$installUrl"
265+
}
266+
226267
# If the Version to use was not provided, get it from the project file.
227268
$appVersion = $Version
228269
if ([string]::IsNullOrEmpty($appVersion))
229270
{
230271
$appVersion = $clickOncePropertyGroup.ApplicationVersion
231272
}
232-
273+
233274
# Get the Major, Minor, and Build parts of the version number.
234275
$majorMinorBuildMatch = $versionNumberRegex.Match($appVersion)
235276
if (!$majorMinorBuildMatch.Success)
@@ -239,20 +280,20 @@ foreach ($clickOncePropertyGroup in $clickOncePropertyGroups)
239280
$majorMinor = $majorMinorBuildMatch.Groups["MajorMinor"].Value
240281
[int]$build = $majorMinorBuildMatch.Groups["Build"].Value
241282
[int]$revision = -1
242-
283+
243284
# If a Revision was specified in the Version, get it.
244285
if (![string]::IsNullOrWhiteSpace($majorMinorBuildMatch.Groups["Revision"]))
245286
{
246287
$revision = [int]::Parse($majorMinorBuildMatch.Groups["Revision"])
247288
}
248-
289+
249290
# If we should be using the BuildSystemsBuildId for the Build and Revision.
250291
if ($BuildSystemsBuildId -gt -1)
251292
{
252293
# Use a calculation for the Build and Revision to prevent the Revision value from being too large, and to increment the Build value as the BuildSystemsBuildId continues to grow larger.
253294
$build = [int][Math]::Floor($BuildSystemsBuildId / $maxVersionPartValueAllowed)
254295
$revision = $BuildSystemsBuildId % $maxVersionPartValueAllowed
255-
296+
256297
Write-Verbose "Translated BuildSystemsBuildId '$BuildSystemsBuildId' into Build.Revision '$build.$revision'."
257298
}
258299

@@ -261,22 +302,22 @@ foreach ($clickOncePropertyGroup in $clickOncePropertyGroups)
261302
{
262303
# If the Revision is missing from the file, or not in a valid format, throw an error.
263304
$applicationRevisionString = $clickOncePropertyGroup.ApplicationRevision
264-
if ($applicationRevisionString -eq $null)
305+
if ($null -eq $applicationRevisionString)
265306
{
266307
throw "Could not find the <ApplicationRevision> element in the project file '$ProjectFilePath'."
267-
}
308+
}
268309
if (!($applicationRevisionString -imatch '^\d+$'))
269310
{
270311
throw "The <ApplicationRevision> elements value '$applicationRevisionString' in the file '$ProjectFilePath' does not appear to be a valid integer."
271312
}
272-
313+
273314
$revision = [int]::Parse($applicationRevisionString)
274-
315+
275316
# If the Revision should be incremented, do it.
276317
if ($IncrementProjectFilesRevision)
277318
{
278319
$revision = $revision + 1
279-
320+
280321
# Make sure the Revision version part is not greater than the max allowed value.
281322
if ($revision -gt $maxVersionPartValueAllowed)
282323
{
@@ -285,12 +326,12 @@ foreach ($clickOncePropertyGroup in $clickOncePropertyGroups)
285326
}
286327
}
287328
}
288-
329+
289330
# Create the version number to use for the ClickOnce version.
290331
$newMajorMinorBuild = "$majorMinor.$build"
291332
$newVersionNumber = "$newMajorMinorBuild.$revision"
292333
Write-Output "Updating version number to be '$newVersionNumber'."
293-
334+
294335
# Write the new values to the file.
295336
Set-XmlNodesElementTextValue -xml $xml -node $clickOncePropertyGroup -elementName 'ApplicationVersion' -textValue "$newMajorMinorBuild.%2a"
296337
Set-XmlNodesElementTextValue -xml $xml -node $clickOncePropertyGroup -elementName 'ApplicationRevision' -textValue $revision.ToString()
@@ -304,4 +345,4 @@ foreach ($clickOncePropertyGroup in $clickOncePropertyGroups)
304345
}
305346

306347
# Save the changes.
307-
$xml.Save($ProjectFilePath)
348+
$xml.Save($ProjectFilePath)

Tests/RunTests.ps1

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ function RunScriptWithParameters($parameters)
3333
return $matches["Version"]
3434
}
3535
# If $output contains multiple lines, it will be treated as a collection instead of a string and $matches will not be defined, so catch that case.
36-
catch { return $output }
36+
catch { return $output }
3737
}
3838
return $output
3939
}
40-
40+
4141
$testNumber = 0
4242
# Some tests are dependent on the order in which they are ran, which isn't ideal, but good enough for now.
4343

@@ -60,3 +60,17 @@ if ($output -eq '1.2.1.57922') { Write-Host "Passed" } else { throw "Test $testN
6060
Write-Host ("{0}. Use version number parameter and update minimum required version..." -f ++$testNumber)
6161
$output = RunScriptWithParameters "-Version '5.6.7.8' -UpdateMinimumRequiredVersionToCurrentVersion"
6262
if ($output.Contains("Updating minimum required version to be '5.6.7.8'.")) { Write-Host "Passed" } else { throw "Test $testNumber failed. Output was '$output'." }
63+
64+
Write-Host ("{0}. Use major/minor of version number parameter and Build Id parameter...." -f ++$testNumber)
65+
$output = RunScriptWithParameters "-Version '1.1.0.0' -BuildSystemsBuildId 123456"
66+
if ($output -eq '1.1.1.57921') { Write-Host "Passed" } else { throw "Test $testNumber failed. Output was '$output'." }
67+
68+
Write-Host ("{0}. Use version and publish url parameter...." -f ++$testNumber)
69+
$output = RunScriptWithParameters "-Version '1.0.5.9' -PublishUrl '\\fileshare\foldername'"
70+
# Multiple output statements are objects in an array in the order they are outputted
71+
if ($output[0].Contains('\\fileshare\foldername') -AND $output[1].Contains('1.0.5.9')) { Write-Host "Passed" } else { throw "Test $testNumber failed. Output was '$output'." }
72+
73+
Write-Host ("{0}. Use BuildId parameter, publish url parameter and install url parameter...." -f ++$testNumber)
74+
$output = RunScriptWithParameters "-BuildSystemsBuildId 123456 -PublishUrl '\\fileshare\foldername' -InstallUrl 'http://fileshare/foldername'"
75+
# Multiple output statements are objects in an array in the order they are outputted
76+
if ($output[0].Contains('\\fileshare\foldername') -AND $output[1].Contains('http://fileshare/foldername') -AND $output[2].Contains('1.0.1.57921')) { Write-Host "Passed" } else { throw "Test $testNumber failed. Output was '$output'." }

0 commit comments

Comments
 (0)