1+ Param
2+ (
3+ [Parameter (Mandatory = $false )]
4+ [string ]$ownVersion ,
5+ [Parameter (Mandatory = $false )]
6+ [bool ]$runTest
7+ );
8+
9+ $assemblyPath = " ..\src\shared\GeneralAssemblyInfo.cs" ;
10+ $defaultVersion = " 1.0.0.0" ;
11+ $nugetPath = " ../nuget" ;
12+ $data = (" ..\src\ItemDistribution\ItemDistribution.csproj" );
13+ $testExec = $false ;
14+
15+ <#
16+ . SYNOPSIS
17+ A brief description of the Get-CurrentAssemblyVersion function.
18+
19+ . DESCRIPTION
20+ Get current assembly version
21+
22+ . EXAMPLE
23+ PS C:\> Get-CurrentAssemblyVersion
24+
25+ . NOTES
26+ Additional information about the function.
27+ #>
28+ function Get-CurrentAssemblyVersion
29+ {
30+ [OutputType ([string ])]
31+ param ()
32+
33+ $assemblyInfo = (Get-Content $assemblyPath );
34+ $asVersion = ($assemblyInfo -match ' AssemblyVersion\(".*"\)' );
35+ $asVersion = $asVersion -split (' "' );
36+ $asVersion = $asVersion [1 ];
37+
38+ return $asVersion ;
39+ }
40+
41+ <#
42+ . SYNOPSIS
43+ A brief description of the Build-And-Pack-BuildPack function.
44+
45+ . DESCRIPTION
46+ Build project and pack as package (u pkg)
47+
48+ . PARAMETER packVersion
49+ Package/Build version
50+
51+ . PARAMETER currentVersion
52+ A description of the currentVersion parameter.
53+
54+ . EXAMPLE
55+ PS C:\> Build-And-Pack-BuildPack -packVersion 'Value1'
56+
57+ . NOTES
58+ Additional information about the function.
59+ #>
60+ function Set-BuildAndPack
61+ {
62+ [CmdletBinding ()]
63+ [OutputType ([bool ])]
64+ param
65+ (
66+ [Parameter (Mandatory = $true )]
67+ [string ]$packVersion ,
68+ [string ]$currentVersion
69+ )
70+
71+ try
72+ {
73+ Write-Host " Project restore '$ ( $_ ) '!" - ForegroundColor Green;
74+ dotnet restore $ ($_ );
75+
76+ Write-Host " Build in Release '$ ( $_ ) '!" - ForegroundColor Green;
77+ $buildResult = dotnet build $ ($_ ) -- source https:// api.nuget.org/ v3/ index.json - c Release / p:AssemblyVersion= $packVersion / p:AssemblyFileVersion= $packVersion / p:AssemblyInformationalVersion= $packVersion ;
78+ if ($LASTEXITCODE -ne 0 )
79+ {
80+ Set-VersionAssembly - packVersion $currentVersion ;
81+ Write-Host $buildResult ;
82+
83+ return $false ;
84+ }
85+
86+ Write-Host " Pack in Release '$ ( $_ ) '!" - ForegroundColor Green;
87+ $packResult = dotnet pack $ ($_ ) - p:PackageVersion= $packVersion -- no- build - c Release -- output $nugetPath ;
88+ if ($LASTEXITCODE -ne 0 )
89+ {
90+ Set-VersionAssembly - packVersion $currentVersion ;
91+ Write-Host $buildResult ;
92+
93+ return $false ;
94+ }
95+
96+ return $true ;
97+ }
98+ catch
99+ {
100+ Write-Host - foregroundcolor Red " An error occurred: $_ "
101+
102+ return $false ;
103+ }
104+ }
105+
106+ <#
107+ . SYNOPSIS
108+ A brief description of the Get-TimeStamp function.
109+
110+ . DESCRIPTION
111+ Get time stamp version
112+
113+ . EXAMPLE
114+ PS C:\> Get-TimeStamp
115+
116+ . NOTES
117+ Additional information about the function.
118+ #>
119+ function Get-TimeStamp
120+ {
121+ [CmdletBinding ()]
122+ [OutputType ([int ])]
123+ param ()
124+
125+ $current = [System.DateTime ]::Now;
126+ $end = [System.DateTime ]::Now.Date;
127+ $diff = (New-TimeSpan - Start $current - End $end ).TotalSeconds / 10 ;
128+ $timeSec = If ($diff -le 0 ) { $diff * -1 }
129+ Else { $diff };
130+
131+ return [int ]$timeSec ;
132+ }
133+
134+ <#
135+ . SYNOPSIS
136+ A brief description of the Set-VersionAssembly function.
137+
138+ . DESCRIPTION
139+ Set current version in assembly file
140+
141+ . PARAMETER packVersion
142+ A description of the packVersion parameter.
143+
144+ . EXAMPLE
145+ PS C:\> Set-VersionAssembly -packVersion 'Value1'
146+
147+ . NOTES
148+ Additional information about the function.
149+ #>
150+ function Set-VersionAssembly
151+ {
152+ [CmdletBinding ()]
153+ [OutputType ([void ])]
154+ param
155+ (
156+ [Parameter (Mandatory = $true )]
157+ [string ]$packVersion
158+ )
159+ $NewVersion = ' AssemblyVersion("' + $packVersion + ' ")' ;
160+ $NewFileVersion = ' AssemblyFileVersion("' + $packVersion + ' ")' ;
161+ $NewAssemblyInformationalVersion = ' AssemblyInformationalVersion("' + $packVersion + ' ")' ;
162+
163+ (Get-Content $assemblyPath - encoding utf8) |
164+ % { $_ -replace ' AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' , $NewVersion } |
165+ % { $_ -replace ' AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)' , $NewFileVersion } |
166+ % { $_ -replace ' AssemblyInformationalVersion\("[0-9x]+(\.([0-9x]+|\*)){1,3}"\)' , $NewAssemblyInformationalVersion } |
167+ Set-Content $assemblyPath - encoding utf8
168+ }
169+
170+ <#
171+ . SYNOPSIS
172+ A brief description of the Exec-TestSolution function.
173+
174+ . DESCRIPTION
175+ Execute solution test
176+
177+ . EXAMPLE
178+ PS C:\> Exec-TestSolution
179+
180+ . NOTES
181+ Additional information about the function.
182+ #>
183+ function Exec-TestSolution
184+ {
185+ [CmdletBinding ()]
186+ [OutputType ([bool ])]
187+ param ()
188+
189+ # Merge all streams into stdout
190+ # $result = dotnet test "..\src\tests\*.csproj" *>&1
191+
192+ # No test
193+ return $true ;
194+
195+ # Evaluate success/failure
196+ if ($LASTEXITCODE -eq 0 )
197+ {
198+ return $true ;
199+ }
200+ else
201+ {
202+ $errorString = $result -join [System.Environment ]::NewLine;
203+ Write-Host - foregroundcolor Red " An error occurred: $errorString " ;
204+
205+ return $false ;
206+ }
207+ }
208+
209+ If ($runTest -eq $true )
210+ {
211+ Write-Host " Init test solution...`n " - ForegroundColor Green;
212+ $testExec = Exec- TestSolution;
213+ }
214+ Else { $testExec = $true ; }
215+
216+ If ($testExec -eq $true )
217+ {
218+ Write-Host " Path to pack: '$nugetPath '`n " - ForegroundColor Green;
219+
220+ $currentVersion = " " ;
221+ If ($ownVersion -eq $null -or $ownVersion -eq " " ) { $currentVersion = Get-CurrentAssemblyVersion ; }
222+ Else { $currentVersion = $ownVersion ; }
223+
224+ $directoryInfo = Get-ChildItem $nugetPath | Where-Object { $_.Name -match ' [a-z]*.1.0.0.nupkg$' } | Measure-Object ;
225+ If ($defaultVersion -eq $currentVersion -and $directoryInfo.count -eq 0 )
226+ {
227+ Set-VersionAssembly - packVersion $currentVersion ;
228+
229+ $data | ForEach-Object {
230+ $buildResult = Set-BuildAndPack - packVersion $currentVersion ;
231+ If ($buildResult -eq $false -or $buildResult -contains $false )
232+ {
233+ Write-Host " `n Build/pack failed!!!" - ForegroundColor Red;
234+
235+ exit ;
236+ }
237+ }
238+
239+ Write-Host " `n Pack executed with success with version: $currentVersion !" - ForegroundColor Green;
240+
241+ exit ;
242+ }
243+ Else
244+ {
245+ $finalVersion = " " ;
246+ If ($ownVersion -eq $null -or $ownVersion -eq " " )
247+ {
248+ $versArray = $currentVersion.Split (' .' );
249+ $finalVersion = $versArray [0 ].ToString() + " ." + $versArray [1 ].ToString() + " ." + (([int ]$versArray [2 ]) + 1 ).ToString() + " ." + (Get-TimeStamp ).ToString();
250+ }
251+ Else { $finalVersion = $ownVersion ; }
252+
253+ Set-VersionAssembly - packVersion $finalVersion ;
254+
255+ $data | ForEach-Object {
256+ $buildResult = Set-BuildAndPack - packVersion $finalVersion - currentVersion $currentVersion ;
257+ If ($buildResult -eq $false -or $buildResult -contains $false )
258+ {
259+ Write-Host " `n Build/pack failed!!!" - ForegroundColor Red;
260+
261+ exit ;
262+ }
263+ }
264+
265+ Write-Host " `n Pack executed with success with version: $finalVersion !" - ForegroundColor Green;
266+
267+ exit ;
268+ }
269+ }
270+ Else { exit ; }
0 commit comments