22
33[CmdletBinding (PositionalBinding = $false )]
44param (
5- [switch ]$OnlyBuild = $false
5+ [switch ]$OnlyBuild = $false ,
6+ [string ]$Version
67)
78
8- $appName = " Arius.Explorer" # 👈 Replace with your application project name.
9- $projDir = " src\Arius.UI" # 👈 Replace with your project directory (where .csproj resides).
10-
9+ $appName = " Arius.Explorer"
10+ $projDir = " src\Arius.Explorer"
1111Set-StrictMode - version 2.0
1212$ErrorActionPreference = " Stop"
1313
1414Write-Output " Working directory: $pwd "
15-
1615# Find MSBuild.
1716$msBuildPath = & " ${env: ProgramFiles(x86)} \Microsoft Visual Studio\Installer\vswhere.exe" `
1817 - latest - requires Microsoft.Component.MSBuild - find MSBuild\** \Bin\MSBuild.exe `
19- - prerelease | select-object - first 1
18+ - prerelease | Select-Object - First 1
2019Write-Output " MSBuild: $ ( (Get-Command $msBuildPath ).Path) "
2120
22- # Load current Git tag.
23- $tag = $ (git describe -- tags)
24- Write-Output " Tag: $tag "
21+ # Determine version.
22+ $version = $null
23+ if ($PSBoundParameters.ContainsKey (' Version' ) -and $null -ne $Version -and $Version.Trim ().Length -gt 0 ) {
24+ $version = $Version.Trim ()
25+ } elseif ($env: VERSION ) {
26+ $version = $env: VERSION.Trim ()
27+ }
28+
29+ if (-not $version ) {
30+ throw " Unable to determine version. Provide -Version parameter or set VERSION environment variable."
31+ }
2532
26- # Parse tag into a three-number version.
27- $version = $tag.Split (' -' )[0 ].TrimStart(' v' )
28- $version = " $version .0"
29- Write-Output " Version: $version "
33+ $baseVersion = $version.Split (' -' , 2 )[0 ]
34+ $versionParts = $baseVersion -split ' \.'
35+ $numericParts = @ ()
36+ foreach ($part in $versionParts ) {
37+ if ($part -notmatch ' ^[0-9]+$' ) {
38+ throw " Version segment '$part ' is not numeric. Unable to derive ClickOnce version from '$version '."
39+ }
40+ $numericParts += $part
41+ }
42+ if ($numericParts.Length -gt 4 ) {
43+ $numericParts = $numericParts [0 .. 3 ]
44+ }
45+ while ($numericParts.Length -lt 4 ) {
46+ $numericParts += ' 0'
47+ }
48+ $normalizedVersion = ($numericParts -join ' .' )
49+ $applicationRevision = $numericParts [-1 ]
50+ Write-Output " Version: $normalizedVersion "
3051
3152# Clean output directory.
3253$publishDir = " bin/publish"
3354$outDir = " $projDir /$publishDir "
3455if (Test-Path $outDir ) {
35- Remove-Item - Path $outDir - Recurse
56+ Remove-Item - Path $outDir - Recurse - Force
3657}
3758
59+ $outDirFullPath = $null
60+
3861# Publish the application.
3962Push-Location $projDir
4063try {
4164 Write-Output " Restoring:"
42- # dotnet restore -r win-x64 ## this does not play well with <ItemGroup Condition="'$(Configuration)'=='Release'">
4365 dotnet build - r win- x64 - c Release
4466 Write-Output " Publishing:"
4567 $msBuildVerbosityArg = " /v:m"
4668 if ($env: CI ) {
4769 $msBuildVerbosityArg = " "
4870 }
49- & $msBuildPath / target:publish / p:PublishProfile= ClickOnceProfile `
50- / p:ApplicationVersion= $version / p:Configuration= Release `
51- / p:PublishDir= $publishDir / p:PublishUrl= $publishDir `
52- $msBuildVerbosityArg
71+ $msbuildArgs = @ (
72+ ' /target:publish' ,
73+ ' /p:PublishProfile=ClickOnceProfile' ,
74+ " /p:ApplicationVersion=$normalizedVersion " ,
75+ " /p:ApplicationRevision=$applicationRevision " ,
76+ ' /p:Configuration=Release' ,
77+ " /p:PublishDir=$publishDir " ,
78+ " /p:PublishUrl=$publishDir " ,
79+ ' /p:SignManifests=false'
80+ )
81+
82+ if ($msBuildVerbosityArg ) {
83+ $msbuildArgs += $msBuildVerbosityArg
84+ }
85+
86+ & $msBuildPath @msbuildArgs
87+
88+ $outDirFullPath = (Resolve-Path $publishDir ).Path
5389
5490 # Measure publish size.
5591 $publishSize = (Get-ChildItem - Path " $publishDir /Application Files" - Recurse |
@@ -62,7 +98,7 @@ finally {
6298
6399if ($OnlyBuild ) {
64100 Write-Output " Build finished."
65- exit
101+ return
66102}
67103
68104# Clone `gh-pages` branch.
@@ -74,28 +110,72 @@ if (-Not (Test-Path $ghPagesDir)) {
74110
75111Push-Location $ghPagesDir
76112try {
77- # Remove previous application files.
78- Write-Output " Removing previous files..."
79- if (Test-Path " Application Files" ) {
80- Remove-Item - Path " Application Files" - Recurse
113+ if ($env: GITHUB_TOKEN ) {
114+ $originUrl = git remote get-url origin
115+ if (-not $originUrl ) {
116+ $originUrl = " https://github.com/${env: GITHUB_REPOSITORY} "
117+ }
118+
119+ if ($originUrl -like ' https://*' ) {
120+ $authUrl = $originUrl -replace ' ^https://' , " https://x-access-token:${env: GITHUB_TOKEN} @"
121+ } else {
122+ $authUrl = " https://x-access-token:${env: GITHUB_TOKEN} @github.com/${env: GITHUB_REPOSITORY} "
123+ }
124+
125+ git remote set-url origin $authUrl | Out-Null
126+ }
127+
128+ if (-not (git config user.email)) {
129+ git config user.email " github-actions@github.com"
81130 }
82- if (Test-Path " $appName .application " ) {
83- Remove-Item - Path " $appName .application "
131+ if (-not (git config user.name) ) {
132+ git config user.name " github-actions "
84133 }
85134
86- # Copy new application files.
87- Write-Output " Copying new files..."
88- Copy-Item - Path " ../$outDir /Application Files" , " ../$outDir /$appName .application" `
89- - Destination . - Recurse
135+ $deployRoot = Join-Path (Get-Location ) ' explorer'
136+ if (-not (Test-Path $deployRoot )) {
137+ New-Item - ItemType Directory - Path $deployRoot | Out-Null
138+ }
139+
140+ Push-Location $deployRoot
141+ try {
142+ # Remove previous application files.
143+ Write-Output " Removing previous files..."
144+ if (Test-Path " Application Files" ) {
145+ Remove-Item - Path " Application Files" - Recurse - Force
146+ }
147+ if (Test-Path " $appName .application" ) {
148+ Remove-Item - Path " $appName .application" - Force
149+ }
150+
151+ if (-not $outDirFullPath ) {
152+ throw " Publish directory was not resolved."
153+ }
154+
155+ # Copy new application files.
156+ Write-Output " Copying new files..."
157+ $appFilesPath = Join-Path $outDirFullPath ' Application Files'
158+ $manifestPath = Join-Path $outDirFullPath " $appName .application"
159+ Copy-Item - Path $appFilesPath , $manifestPath - Destination . - Recurse - Force
160+ }
161+ finally {
162+ Pop-Location
163+ }
90164
91165 # Stage and commit.
92166 Write-Output " Staging..."
93167 git add - A
94- Write-Output " Committing..."
95- git commit - m " Update to v$version "
96-
97- # Push.
98- git push
99- } finally {
168+ $status = git status -- porcelain
169+ if (-not $status ) {
170+ Write-Output " No changes to commit."
171+ } else {
172+ Write-Output " Committing..."
173+ git commit - m " Update to v$normalizedVersion "
174+
175+ # Push.
176+ git push
177+ }
178+ }
179+ finally {
100180 Pop-Location
101181}
0 commit comments