Skip to content

Commit f330a76

Browse files
author
Abhishek Kumar Singh
committed
ApplyVersion created
1 parent 9a1d962 commit f330a76

38 files changed

+4428
-2
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}
28.8 KB
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[CmdletBinding()]
2+
param()
3+
4+
Trace-VstsEnteringInvocation $MyInvocation
5+
6+
try {
7+
# Get inputs.
8+
$SourceFolder = Get-VstsInput -Name 'SourceFolder' -Require
9+
$Company = Get-VstsInput -Name 'Company' -Require
10+
$Product = Get-VstsInput -Name 'Product' -Require
11+
$Copyright = Get-VstsInput -Name 'Copyright' -Require
12+
13+
# Call the ApplyVersion function with the inputs
14+
.\ApplyVersion.ps1 -SourceFolder $SourceFolder -Company $Company -Product $Product -Copyright $Copyright
15+
16+
} finally {
17+
Trace-VstsLeavingInvocation $MyInvocation
18+
}

0 commit comments

Comments
 (0)