forked from TypesettingTools/Aegisub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.ps1
89 lines (78 loc) · 3.11 KB
/
version.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env powershell
param (
[Parameter(Position = 0, Mandatory = $false)]
[string]$BuildRoot = $null,
[Parameter(Position = 1, Mandatory = $false)]
[string]$SourceRoot = $null
)
$lastSvnRevision = 6962
$lastSvnHash = '16cd907fe7482cb54a7374cd28b8501f138116be'
$defineNumberMatch = [regex] '^#define\s+(\w+)\s+(\d+)$'
$defineStringMatch = [regex] "^#define\s+(\w+)\s+[`"']?(.+?)[`"']?$"
$semVerMatch = [regex] 'v?(\d+)\.(\d+).(\d+)(?:-(\w+))?'
$repositoryRootPath = Join-Path $PSScriptRoot .. | Resolve-Path
if (!(git -C $repositoryRootPath rev-parse --is-inside-work-tree 2>$null)) {
throw "$repositoryRootPath is not a git repository"
}
if ($BuildRoot -eq $null -or $BuildRoot.Trim() -eq "") {
$BuildRoot = $repositoryRootPath
}
# support legacy in-tree builds
if ([System.IO.Path]::GetFullPath([System.IO.Path]::Combine((pwd).Path, $BuildRoot)) -eq
[System.IO.Path]::GetFullPath([System.IO.Path]::Combine((pwd).Path, $repositoryRootPath))) {
$BuildRoot = Join-Path $repositoryRootPath 'build'
}
$gitVersionHeaderPath = Join-Path $BuildRoot 'git_version.h'
$version = @{}
if (Test-Path $gitVersionHeaderPath) {
Get-Content $gitVersionHeaderPath | %{$_.Trim()} | ?{$_} | %{
switch -regex ($_) {
$defineNumberMatch {
$version[$Matches[1]] = [int]$Matches[2];
}
$defineStringMatch {
$version[$Matches[1]] = $Matches[2];
}
}
}
}
$gitRevision = $lastSvnRevision + ((git -C $repositoryRootPath log --pretty=oneline "$($lastSvnHash)..HEAD" 2>$null | Measure-Object).Count)
$gitBranch = git -C $repositoryRootPath symbolic-ref --short HEAD 2>$null
$gitHash = git -C $repositoryRootPath rev-parse --short HEAD 2>$null
$gitVersionString = $gitRevision, $gitBranch, $gitHash -join '-'
$exactGitTag = git -C $repositoryRootPath describe --exact-match --tags 2>$null
if ($gitVersionString -eq $version['BUILD_GIT_VERSION_STRING']) {
exit 0
}
if ($exactGitTag -match $semVerMatch) {
$version['TAGGED_RELEASE'] = $true
$version['RESOURCE_BASE_VERSION'] = $Matches[1..3]
$version['INSTALLER_VERSION'] = $gitVersionString = ($Matches[1..3] -join '.') + @("-$($Matches[4])",'')[!$Matches[4]]
} else {
foreach ($rev in (git -C $repositoryRootPath rev-list --tags 2>$null)) {
$tag = git -C $repositoryRootPath describe --exact-match --tags $rev 2>$null
if ($tag -match $semVerMatch) {#
$version['TAGGED_RELEASE'] = $false
$version['RESOURCE_BASE_VERSION'] = $Matches[1..3]
$version['INSTALLER_VERSION'] = ($Matches[1..3] -join '.')
break;
}
}
}
$version['BUILD_GIT_VERSION_NUMBER'] = $gitRevision
$version['BUILD_GIT_VERSION_STRING'] = $gitVersionString
$version.GetEnumerator() | %{
$type = $_.Value.GetType()
$value = $_.Value
$fmtValue = switch ($type) {
([string]) {"`"$value`""}
([int]) {$value.ToString()}
([bool]) {([int]$value).ToString()}
([object[]]) {$value -join ', '}
default {
Write-Host "no format known for type '$type' - trying default string conversion" -ForegroundColor Red
{"`"$($value.ToString())`""}
}
}
"`n#define $($_.Key) $($fmtValue)"
} | Out-File -FilePath $gitVersionHeaderPath -Encoding utf8