Skip to content

Commit

Permalink
Merge pull request #268 from reupen/add-version-to-release-components
Browse files Browse the repository at this point in the history
Include version in component archives for releases
  • Loading branch information
reupen authored Dec 11, 2019
2 parents 884365a + e76c4e7 commit 39747d7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 38 deletions.
25 changes: 18 additions & 7 deletions scripts/post-build.ps1
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
Param(
[Parameter(Mandatory=$true)][String]$ComponentPath,
[Parameter(Mandatory=$true)][String]$OutputDir
[Parameter(Mandatory=$true)][string]$ComponentPath,
[Parameter(Mandatory=$true)][string]$OutputDir
)

$input_base_name = [io.path]::GetFileNameWithoutExtension($ComponentPath)
$output_path = "$OutputDir$input_base_name.fb2k-component"
$intermediate_file_path = "$output_path.zip"
Import-Module "$PSScriptRoot\versioning"

$version = Get-Version

$inputBaseName = [io.path]::GetFileNameWithoutExtension($ComponentPath)

$verionPart = ''

if ($version -and $version.IsRelease()) {
$verionPart = "-$version"
}

$outputPath = "$OutputDir$inputBaseName$verionPart.fb2k-component"
$intermediateFilePath = "$outputPath.zip"

# Note that Compress-Archive won't accept a file extension other than .zip. Hence, we add .zip to the file name
# and then remove it using Move-Item.
# (Also note that both Compress-Archive and Move-Item should overwite the destination file if it exists.)
Compress-Archive -Path $ComponentPath -CompressionLevel Optimal -DestinationPath $intermediate_file_path -Force
Move-Item $intermediate_file_path $output_path -Force
Compress-Archive -Path $ComponentPath -CompressionLevel Optimal -DestinationPath $intermediateFilePath -Force
Move-Item $intermediateFilePath $outputPath -Force
4 changes: 2 additions & 2 deletions scripts/pre-build.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Param(
[Parameter(Mandatory=$true)][String]$TemplatePath,
[Parameter(Mandatory=$true)][String]$OutputPath
[Parameter(Mandatory=$true)][string]$TemplatePath,
[Parameter(Mandatory=$true)][string]$OutputPath
)

Import-Module "$PSScriptRoot\versioning"
Expand Down
74 changes: 45 additions & 29 deletions scripts/versioning.psm1
Original file line number Diff line number Diff line change
@@ -1,41 +1,57 @@
function Get-GitDescription {
$description_regex = '^v(?<version>.+)-(?<distance>\d+)-g(?<commit>[0-9a-f]{7})(-(?<dirty>dirty))?$'

Try {
$description = git describe --tags --dirty --match 'v[0-9]*' --long
} Catch {
return $null
class Version {
[string]$BaseVersion;
[int]$Distance;
[string]$CommitHash;
[bool]$Dirty;

Version([string]$BaseVersion, [int]$Distance, [string]$CommitHash, [bool]$Dirty) {
$this.BaseVersion = $BaseVersion
$this.Distance = $Distance
$this.CommitHash = $CommitHash
$this.Dirty = $Dirty
}

[string]ToString() {
$annotations = @()

if ($this.Distance) {
$annotations += @($this.Distance, "g$($this.CommitHash)")
}

If (-not ($description -match $description_regex)) {
return $null
if ($this.Dirty) {
$annotations += 'dirty'
}

if ($annotations) {
$joinedAnnotations = $annotations -Join '.'
return "$($this.BaseVersion)+$joinedAnnotations"
}

return $this.BaseVersion
}

return $Matches
}

function Get-Version {
$description = Get-GitDescription

if (-not $description) {
return '0.0.0+unknown'
[bool]IsRelease() {
return $this.Distance -eq 0 -and -not $this.Dirty
}

$base_version = $description.version
$annotations = @()
static [Version]FromGit() {
$descriptionRegex = '^v(?<version>.+)-(?<distance>\d+)-g(?<commit>[0-9a-f]{7})(-(?<dirty>dirty))?$'

if ([int]$description.distance) {
$annotations += @($description.distance, "g$($description.commit)")
$description = git describe --tags --dirty --match 'v[0-9]*' --long

If (-not ($description -match $descriptionRegex)) {
throw
}

return [Version]::new($Matches.version, [int]$Matches.distance, $Matches.commit, [bool]$Matches.dirty)
}
}

if ($description.dirty) {
$annotations += @($description.dirty)
function Get-Version {
try {
return [Version]::FromGit()
}

if ($annotations) {
$joined_annotations = $annotations -Join '.'
return "$base_version+$joined_annotations"
catch {
return [Version]::new(0, 0, 'unknown', $false)
}

return $base_version
}

0 comments on commit 39747d7

Please sign in to comment.