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\EntityMaxLengthTrim\EntityMaxLengthTrim.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\EntityModelStringTruncateTest\EntityModelStringTruncateTest.csproj" * > & 1
191
+
192
+ # Evaluate success/failure
193
+ if ($LASTEXITCODE -eq 0 )
194
+ {
195
+ return $true ;
196
+ }
197
+ else
198
+ {
199
+ $errorString = $result -join [System.Environment ]::NewLine;
200
+ Write-Host - foregroundcolor Red " An error occurred: $errorString " ;
201
+
202
+ return $false ;
203
+ }
204
+ }
205
+
206
+ If ($runTest -eq $true )
207
+ {
208
+ Write-Host " Init test solution...`n " - ForegroundColor Green;
209
+ $testExec = Exec- TestSolution;
210
+ }
211
+ Else { $testExec = $true ; }
212
+
213
+ If ($testExec -eq $true )
214
+ {
215
+ Write-Host " Path to pack: '$nugetPath '`n " - ForegroundColor Green;
216
+
217
+ $currentVersion = " " ;
218
+ If ($ownVersion -eq $null -or $ownVersion -eq " " ) { $currentVersion = Get-CurrentAssemblyVersion ; }
219
+ Else { $currentVersion = $ownVersion ; }
220
+
221
+ $directoryInfo = Get-ChildItem $nugetPath | Where-Object { $_.Name -match ' [a-z]*.1.0.0.nupkg$' } | Measure-Object ;
222
+ If ($defaultVersion -eq $currentVersion -and $directoryInfo.count -eq 0 )
223
+ {
224
+ Set-VersionAssembly - packVersion $currentVersion ;
225
+
226
+ $data | ForEach-Object {
227
+ $buildResult = Set-BuildAndPack - packVersion $currentVersion ;
228
+ If ($buildResult -eq $false -or $buildResult -contains $false )
229
+ {
230
+ Write-Host " `n Build/pack failed!!!" - ForegroundColor Red;
231
+
232
+ exit ;
233
+ }
234
+ }
235
+
236
+ Write-Host " `n Pack executed with success with version: $currentVersion !" - ForegroundColor Green;
237
+
238
+ exit ;
239
+ }
240
+ Else
241
+ {
242
+ $finalVersion = " " ;
243
+ If ($ownVersion -eq $null -or $ownVersion -eq " " )
244
+ {
245
+ $versArray = $currentVersion.Split (' .' );
246
+ $finalVersion = $versArray [0 ].ToString() + " ." + $versArray [1 ].ToString() + " ." + (([int ]$versArray [2 ]) + 1 ).ToString() + " ." + (Get-TimeStamp ).ToString();
247
+ }
248
+ Else { $finalVersion = $ownVersion ; }
249
+
250
+ Set-VersionAssembly - packVersion $finalVersion ;
251
+
252
+ $data | ForEach-Object {
253
+ $buildResult = Set-BuildAndPack - packVersion $finalVersion - currentVersion $currentVersion ;
254
+ If ($buildResult -eq $false -or $buildResult -contains $false )
255
+ {
256
+ Write-Host " `n Build/pack failed!!!" - ForegroundColor Red;
257
+
258
+ exit ;
259
+ }
260
+ }
261
+
262
+ Write-Host " `n Pack executed with success with version: $finalVersion !" - ForegroundColor Green;
263
+
264
+ exit ;
265
+ }
266
+ }
267
+ Else { exit ; }
0 commit comments