How can i access the current semantic version during build?
PeterDaniels
Posts: 89 Bronze 3
I am using semantic versioning for migration folder ordering. Ilike how it populates the version column in the __migrationLog table with the current/ latest migration folder name. I'd like to access this value on my build server. Is there a way to get that value before building the project? Perhaps in a PoSh script in the build/ci server?
I'm hoping to avoid writing a script to look for the latest migration folder in the project.
TIA,
- Peter
I'm hoping to avoid writing a script to look for the latest migration folder in the project.
TIA,
- Peter
Tagged:
Best Answers
-
PeterDaniels Posts: 89 Bronze 3I did, @Monday. Here's the function:
$VersionInfo = Get-LatestVersionAndMigrationNumber -ProjectFilePath $ProjectFilePath $PackageVersion = $VersionInfo.Version + '-migration' + $VersionInfo.MigrationNumber $ReleaseVersion = $PackageVersion </code># Get latest version and migration from project file path # This is cool. Version like 1.1.0 and Migration number like 5 - to build ReleaseVersion and/or PackageVersion function Get-LatestVersionAndMigrationNumber { [CmdletBinding()] param ( [parameter(Mandatory=$true)] [string] $ProjectFilePath ) process { # First, strip the project file name and find the Migrations folder $ProjectRootFolder = Split-Path -Path $ProjectFilePath $MigrationsFolder = Join-Path -Path $ProjectRootFolder -ChildPath "Migrations" if (-not (Test-Path -Path $MigrationsFolder)) { Write-Error -Message "Invalid path: $MigrationsFolder" exit } # Get the latest migration folder sem ver [string]$Latest = Get-ChildItem -Path $MigrationsFolder -Directory | ForEach-Object {[Version]($_.Name -split "-")[0]} | Sort-Object -Descending | Select -First 1 # Get the base folder back now that I found the latest version: $LatestFolder = Get-ChildItem -Path $MigrationsFolder -Directory | Where-Object {$_.Name -like "$Latest*"} # Now get the migrations in that folder $LatestMigrationsFolder = Join-Path -Path $MigrationsFolder -ChildPath $LatestFolder $LatestMigration = (Get-ChildItem -Path $LatestMigrationsFolder -File).Name | Sort-Object -Descending | select -First 1 [int]$LatestMigrationSequenceNumber = ($LatestMigration -split "_")[0] # Return a custom object with the latest sem version and migration sequence num #$Latest + "-migration" + $LatestMigrationSequenceNumber $obj = New-Object -TypeName PSCustomObject -Property @{Version = $Latest;MigrationNumber = $LatestMigrationSequenceNumber} $obj } } </pre><div>I then use it:<br><pre class="CodeBlock"><code>
To create a version that looks like (for example) 1.1.0-migration14
Answers
I think I'll have to add a custom build step to recurse through the migrations folder to look for the latest sem ver folder name....
<DeployOnceSubFolder>Migrations</DeployOnceSubFolder>
<MigrationOrdering>FilePath</MigrationOrdering>