|
| 1 | + |
| 2 | +function Has-Version { |
| 3 | + param ($version) |
| 4 | + |
| 5 | + # Check if the version argument is provided |
| 6 | + if ($version.Count -eq 0) { |
| 7 | + Write-Host "Please provide the version number as an argument. Usage: .\bump-version.ps1 <new-version>" |
| 8 | + exit |
| 9 | + } |
| 10 | + |
| 11 | + # Get the new version from the CLI argument |
| 12 | + return $version[0] |
| 13 | +} |
| 14 | + |
| 15 | +$newVersion = Has-Version($args) |
| 16 | + |
| 17 | +# Function to validate the version format (X.X.X where X is a number) |
| 18 | +function Validate-VersionFormat { |
| 19 | + param ( |
| 20 | + [string]$version |
| 21 | + ) |
| 22 | + |
| 23 | + # Regex pattern for validating version format (X.X.X-beta) |
| 24 | + $versionPattern = '^\d+\.\d+\.\d+$' |
| 25 | + |
| 26 | + # Check if version matches the pattern |
| 27 | + return $version -match $versionPattern |
| 28 | +} |
| 29 | + |
| 30 | +# Function to update version in a file |
| 31 | +function Update-Version { |
| 32 | + param ( |
| 33 | + [string]$filePath, |
| 34 | + [string]$searchPattern, |
| 35 | + [string]$newVersion, |
| 36 | + [string]$replacementPattern |
| 37 | + ) |
| 38 | + |
| 39 | + # Read the content of the file |
| 40 | + $content = Get-Content $filePath |
| 41 | + |
| 42 | + # Replace the version based on the provided pattern and replacement |
| 43 | + $updatedContent = $content -replace $searchPattern, $replacementPattern |
| 44 | + |
| 45 | + # Write the updated content back to the file |
| 46 | + Set-Content $filePath -Value $updatedContent |
| 47 | + |
| 48 | + Write-Host "Updated version in $filePath to $newVersion" |
| 49 | +} |
| 50 | + |
| 51 | +# Check if the version format is valid |
| 52 | +if (-not (Validate-VersionFormat $newVersion)) { |
| 53 | + Write-Host "Invalid version format. Please use the format: X.X.X where X is a number." |
| 54 | + exit |
| 55 | +} |
| 56 | + |
| 57 | +# Define the paths and patterns for each file |
| 58 | +$filesToUpdate = @( |
| 59 | + @{ |
| 60 | + FilePath = ".\ComposGH\ComposGH.csproj" |
| 61 | + SearchPattern = '<Version>(.*?)<\/Version>' |
| 62 | + ReplacementPattern = "<Version>$newVersion-beta</Version>" |
| 63 | + }, |
| 64 | + @{ |
| 65 | + FilePath = ".\Compos\ComposAPI.csproj" |
| 66 | + SearchPattern = '<Version>(.*?)<\/Version>' |
| 67 | + ReplacementPattern = "<Version>$newVersion-beta</Version>" |
| 68 | + }, |
| 69 | + @{ |
| 70 | + FilePath = ".\ComposGH\ComposGHInfo.cs" |
| 71 | + SearchPattern = 'string GrasshopperVersion = "(.*?)"' |
| 72 | + ReplacementPattern = 'string GrasshopperVersion = "' + $newVersion + '-beta"' |
| 73 | + } |
| 74 | +) |
| 75 | + |
| 76 | +# Loop through each file and update the version |
| 77 | +foreach ($file in $filesToUpdate) { |
| 78 | + Update-Version -filePath $file.FilePath -searchPattern $file.SearchPattern -newVersion $newVersion -replacementPattern $file.ReplacementPattern |
| 79 | +} |
| 80 | + |
| 81 | +Write-Host "Version update completed." |
0 commit comments