1+ [CmdletBinding ()]
2+ param (
3+ [Parameter (Mandatory = $true )]
4+ [string ]$SourceFolder ,
5+
6+ [Parameter (Mandatory = $true )]
7+ [string ]$Company ,
8+
9+ [Parameter (Mandatory = $true )]
10+ [string ]$Copyright ,
11+
12+ [Parameter (Mandatory = $true )]
13+ [string ]$Product
14+ )
15+
16+ $ErrorActionPreference = " Stop"
17+
18+ function Update-Or-AddLine {
19+ param (
20+ [string ]$FileContent ,
21+ [string ]$AttributeName ,
22+ [string ]$NewValue
23+ )
24+
25+ try {
26+ $escapedAttr = [regex ]::Escape($AttributeName )
27+ $pattern = " \[assembly:\s*$escapedAttr \(\"" .*?\"" \)\]"
28+ $replacement = " [assembly: $AttributeName (`" $NewValue `" )]"
29+
30+ if ($FileContent -match $pattern ) {
31+ return [regex ]::Replace($FileContent , $pattern , $replacement )
32+ } else {
33+ return $FileContent + " `r`n $replacement "
34+ }
35+ } catch {
36+ Write-Error " Failed to update or add line for attribute: $AttributeName . Error: $_ "
37+ throw
38+ }
39+ }
40+
41+ try {
42+ # Locate AssemblyInfo.cs
43+ $assemblyFile = Get-ChildItem - Path $SourceFolder - Filter " AssemblyInfo.cs" - Recurse - ErrorAction Stop | Select-Object - First 1
44+
45+ if (-not $assemblyFile ) {
46+ throw " AssemblyInfo.cs not found under folder: $SourceFolder "
47+ }
48+
49+ Write-Host " Company name: $Company "
50+ Write-Host " Copyright year: $Copyright "
51+ Write-Host " Product name: $Product "
52+ Write-Host " Product version: $env: BUILD_BUILDNUMBER "
53+ Write-Host " Updating file: $ ( $assemblyFile.FullName ) "
54+
55+
56+ # Read file content
57+ $content = Get-Content - Path $assemblyFile.FullName - Raw - ErrorAction Stop
58+
59+ # Update fields
60+ $content = Update-Or - AddLine - FileContent $content - AttributeName " AssemblyCompany" - NewValue $Company
61+ $content = Update-Or - AddLine - FileContent $content - AttributeName " AssemblyCopyright" - NewValue $Copyright
62+ $content = Update-Or - AddLine - FileContent $content - AttributeName " AssemblyProduct" - NewValue $Product
63+ $content = Update-Or - AddLine - FileContent $content - AttributeName " AssemblyFileVersion" - NewValue $env: BUILD_BUILDNUMBER
64+
65+ # Write back to file
66+ Set-Content - Path $assemblyFile.FullName - Value $content - Encoding UTF8 - ErrorAction Stop
67+
68+ Write-Host " AssemblyInfo.cs updated successfully."
69+
70+ } catch {
71+ Write-Error " An error occurred during script execution: $_ "
72+ exit 1
73+ }
0 commit comments