1
+ # Class which holds the project entry
2
+ class ProjectEntry {
3
+ [string ]$SolutionFile
4
+ [string ]$ProjectFile
5
+ [string ]$PublishProfile
6
+ [string ]$BinDir
7
+ [bool ]$CreateRelease
8
+
9
+ # Default constructor
10
+ ProjectEntry() { }
11
+
12
+ # Constructor
13
+ ProjectEntry([string ]$rootDir , [string ]$solutionFile , [string ]$projectFile , [bool ]$createRelease ) {
14
+ $this.SolutionFile = $solutionFile
15
+ $this.CreateRelease = $createRelease
16
+ # Set the other properties
17
+ $this.SetValues ($rootDir , $projectFile )
18
+ }
19
+
20
+ # Sets the properties
21
+ [void ]SetValues([string ]$rootDir , [string ]$path ) {
22
+ $this.ProjectFile = Join-Path - Path $rootDir - ChildPath $path
23
+ $this.PublishProfile = Join-Path - Path $rootDir - ChildPath " Properties\PublishProfiles\FolderProfile.pubxml"
24
+ $this.BinDir = Join-Path - Path $rootDir - ChildPath " bin"
25
+ }
26
+ }
27
+
28
+ # Checks if the specified file path is valid
29
+ function IsFileValid ($filePath ) {
30
+ # Check if the value is empty or not
31
+ if ([string ]::IsNullOrEmpty($filePath )) {
32
+ return $false ;
33
+ }
34
+
35
+ # Get the complete path and check if the file exists
36
+ $tmpPath = $filePath
37
+ try {
38
+ $tmpPath = Resolve-Path - Path $filePath
39
+ }
40
+ catch {
41
+ return $false
42
+ }
43
+
44
+ if ([System.IO.File ]::Exists($tmpPath )) {
45
+ return $true
46
+ }
47
+
48
+ return $false
49
+ }
50
+
51
+ # Gets the current version number
52
+ function GetVersionNumber ($filePath ) {
53
+ $fileValid = IsFileValid $filePath
54
+
55
+ if (-not $fileValid ) {
56
+ Write-Host " Can't determine current version number."
57
+ return " 1.0.0.0"
58
+ }
59
+
60
+ $doc = [XML ](Get-Content - Path $filePath )
61
+
62
+ # Get the specified element
63
+ $element = $doc.SelectSingleNode (" //AssemblyVersion" )
64
+
65
+ if ($element -eq $null ) {
66
+ Write-Host " Can't determine current version number. Fallback to 1.0.0.0"
67
+ return " 1.0.0.0"
68
+ }
69
+
70
+ # Return the version number
71
+ return $element.InnerText
72
+ }
73
+
74
+ # Generates a new version number
75
+ function GenerateVersionNumber ($oldVersion ) {
76
+ $tmpVersion = [Version ]::new()
77
+
78
+ if (-not [string ]::IsNullOrEmpty($oldVersion )) {
79
+ try {
80
+ $tmpVersion = [Version ]::new($oldVersion )
81
+ }
82
+ catch {
83
+ Write-Host " Can't determine old version. Fallback to new version"
84
+ }
85
+ }
86
+
87
+ $year = Get-Date - Format " yy"
88
+ $dayOfYear = (Get-Date ).DayOfYear
89
+ $build = 0
90
+ $minutesSinceMidnight = [System.Math ]::Round((Get-Date ).ToUniversalTime().TimeOfDay.TotalMinutes)
91
+
92
+ Write-Host " Compare old and new version"
93
+ if ($year -eq $tmpVersion.Major -and $dayOfYear -eq $tmpVersion.Minor ) {
94
+ # Add on to the build number if the major and minor versions are equal
95
+ $build = $tmpVersion.Build + 1
96
+ }
97
+
98
+ $newVersion = [Version ]::new($year , $dayOfYear , $build , $minutesSinceMidnight )
99
+
100
+ return $newVersion
101
+ }
102
+
103
+ # Changes the value of a specified XML node
104
+ function ChangeXmlNode ($filePath , $nodeName , $newValue ) {
105
+ $fileValid = IsFileValid $filePath
106
+
107
+ if (-not $fileValid ) {
108
+ Write-Host " ERROR > Can't load file '$filePath '"
109
+ return
110
+ }
111
+
112
+ $doc = [XML ](Get-Content - Path $filePath )
113
+
114
+ # Get the specified element
115
+ $element = $doc.SelectSingleNode ($nodeName )
116
+
117
+ Write-Host " Node: $nodeName - Old value: $ ( $element.InnerText ) ; New value: $newValue "
118
+
119
+ # Change the element
120
+ $element.InnerText = $newValue
121
+
122
+ # Save the changes
123
+ $doc.Save ($filePath )
124
+ }
125
+
126
+ # Changes the version of the project file
127
+ function ChangeProjectFile ($filePath , $newVersion ) {
128
+ # Assembly version
129
+ ChangeXmlNode $filePath " //AssemblyVersion" $newVersion
130
+
131
+ # File Version
132
+ ChangeXmlNode $filePath " //FileVersion" $newVersion
133
+
134
+ # Version
135
+ ChangeXmlNode $filePath " //Version" $newVersion
136
+ }
137
+
138
+ # Creates a zip archive
139
+ function CreateZipArchive ($sourceDir , $targetPath ) {
140
+ Compress-Archive - Path $sourceDir - DestinationPath $targetPath - CompressionLevel Optimal
141
+ }
142
+
143
+ function CreateRelease ([ProjectEntry ]$project ) {
144
+ $projectFile = $project.ProjectFile
145
+ # Check if the file is valid
146
+ $validProjectFile = IsFileValid $project.ProjectFile
147
+
148
+ if (-not $validProjectFile ) {
149
+ Write-Host " The project file is not valid."
150
+ return 1
151
+ }
152
+
153
+ # ===========================
154
+ # Change version number
155
+ # Set the complete path
156
+ $projectFile = Resolve-Path - Path $projectFile
157
+
158
+ # Get the old and new version number
159
+ $oldVersion = GetVersionNumber $projectFile
160
+ $newVersion = GenerateVersionNumber $oldVersion
161
+
162
+ Write-Host " Change version from $oldVersion to $newVersion "
163
+
164
+ # Update the files
165
+ Write-Host " Update project file"
166
+ ChangeProjectFile $projectFile $newVersion
167
+
168
+ if ($project.CreateRelease -eq $false ) {
169
+ Write-Host " Skip release creation."
170
+ return 0 ;
171
+ }
172
+ # ===========================
173
+ # Create the release
174
+ $dotnet = " C:\Program Files\dotnet\dotnet.exe"
175
+ $currentLocation = Get-Location
176
+ $output = Join-Path - Path $currentLocation - ChildPath $project.BinDir
177
+
178
+ # Clear the previous builds
179
+ Write-Host " Clear output directory $output "
180
+ if ([System.IO.Directory ]::Exists($output )) {
181
+ Remove-Item " $output \*" - Recurse - Confirm:$false
182
+ }
183
+
184
+ # Create the release
185
+ Write-Host " Create release"
186
+ $publishProfile = $project.PublishProfile
187
+ & $dotnet publish $project.SolutionFile - p:PublishProfile= $publishProfile
188
+
189
+ return 0
190
+ }
191
+
192
+ # ===========================
193
+ # Main entry point
194
+ # ===========================
195
+ $mainSolution = " MsSql.ClassGenerator.sln"
196
+ $core = [ProjectEntry ]::new(" MsSql.ClassGenerator.Core" , $mainSolution , " MsSql.ClassGenerator.Core.csproj" , $false )
197
+ $desktopApp = [ProjectEntry ]::new(" MsSql.ClassGenerator" , $mainSolution , " MsSql.ClassGenerator.csproj" , $true )
198
+ $cliApp = [ProjectEntry ]::new(" MsSql.ClassGenerator.Cli" , $mainSolution , " MsSql.ClassGenerator.Cli.csproj" , $true )
199
+ $projects = @ ($core , $desktopApp , $cliApp );
200
+
201
+ Write-Host " Start version update. Files:"
202
+ Write-Host " - Project files:"
203
+ foreach ($project in $projects ) {
204
+ Write-Host " > $ ( $project.ProjectFile ) "
205
+ }
206
+
207
+ Write-Host " Create releases..."
208
+
209
+ try {
210
+
211
+ foreach ($project in $projects ) {
212
+
213
+ $result = CreateRelease $project
214
+
215
+ if ($result -eq 1 ) {
216
+ Write-Host " An error has occurred while creating the release for $ ( $project.ProjectFile ) "
217
+ return 1
218
+ }
219
+ }
220
+
221
+ return 0
222
+ }
223
+ catch {
224
+ Write-Host " An error has occurred during the process. Error: $_ "
225
+ return 1
226
+ }
0 commit comments