Skip to content

Commit ebd29f5

Browse files
author
Brett Groff
committed
Add ability to update publish url and install url
- Added the ability to pass in either the publish url or install url and it will update the csproj file. If no parameter is provided they will remain the same - Switched around some right side null comparisons to left ones due to warning encountered from VS Code and after doing a little research. Found this article https://rencore.com/blog/powershell-null-comparison/ - Added tests for the publish url and install parameters - Added test for using both the Version parameter and the BuildId parameter. Passing in 1.0.0.0 as the Version and along with a BuildId the version will keep the hardcoded major/minor(1.0) and update the build/revision(0.0) with the autogenerated id
1 parent 7c2f1a5 commit ebd29f5

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

Set-ProjectFilesClickOnceVersion.ps1

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
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.
3440
@@ -54,6 +60,17 @@
5460
5561
& .\Set-ProjectFilesClickOnceVersion.ps1 -ProjectFilePath "C:\SomeProject.csproj" -Version '1.2.3' -IncrementProjectFilesRevision -UpdateMinimumRequiredVersionToCurrentVersion
5662
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 "\\servername\foldername" -InstallUrl "http://servername/foldername"
72+
73+
5774
.LINK
5875
Project Home: https://github.com/deadlydog/Set-ProjectFilesClickOnceVersion
5976
@@ -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,16 +115,16 @@ 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 }
127+
if ([string]::IsNullOrEmpty($NamespaceURI)) { $NamespaceURI = $XmlDocument.DocumentElement.NamespaceURI }
105128

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)
@@ -179,7 +202,7 @@ 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
{
184207
$element = $xml.CreateElement($elementName, $xml.DocumentElement.NamespaceURI)
185208
$textNode = $xml.CreateTextNode($textValue)
@@ -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,6 +246,24 @@ 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))
@@ -261,7 +302,7 @@ 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'."
267308
}
@@ -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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 'http://testserver/folder'"
70+
# Multiple output statements are objects in an array in the order they are outputted
71+
if ($output[0].Contains('http://testserver/folder') -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 'http://testserver/folder' -InstallUrl '\\fileshare\folder'"
75+
# Multiple output statements are objects in an array in the order they are outputted
76+
if ($output[0].Contains('http://testserver/folder') -AND $output[1].Contains('\\fileshare\folder') -AND $output[2].Contains('1.0.1.57921')) { Write-Host "Passed" } else { throw "Test $testNumber failed. Output was '$output'." }

Tests/TestFiles/TestProject.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<UpdatePeriodically>false</UpdatePeriodically>
2525
<UpdateRequired>true</UpdateRequired>
2626
<MapFileExtensions>true</MapFileExtensions>
27+
<InstallUrl>\\publish\app</InstallUrl>
2728
<MinimumRequiredVersion>1.0.0.1</MinimumRequiredVersion>
2829
<ApplicationRevision>1</ApplicationRevision>
2930
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
@@ -134,4 +135,4 @@
134135
<Target Name="AfterBuild">
135136
</Target>
136137
-->
137-
</Project>
138+
</Project>

0 commit comments

Comments
 (0)