From ebbe91b998270a174bc59870a4984f827c682c57 Mon Sep 17 00:00:00 2001
From: line0 <line0@inbox.lv>
Date: Tue, 12 Feb 2019 00:57:00 +0100
Subject: [PATCH] PowerShell version script: work correctly from any cwd; do
 not error out when version.h doesn't already exist; adjust git_version.h and
 git_version.xml paths for meson build system

---
 version.ps1 | 50 +++++++++++++++++++++++---------------------------
 version.sh  |  0
 2 files changed, 23 insertions(+), 27 deletions(-)
 mode change 100644 => 100755 version.sh

diff --git a/version.ps1 b/version.ps1
index caea09f02..03d02dbef 100644
--- a/version.ps1
+++ b/version.ps1
@@ -5,48 +5,44 @@ $defineNumberMatch = [regex] '^#define\s+(\w+)\s+(\d+)$'
 $defineStringMatch = [regex] "^#define\s+(\w+)\s+[`"']?(.+?)[`"']?$"
 $semVerMatch = [regex] 'v?(\d+)\.(\d+).(\d+)(?:-(\w+))?'
 
-if (!(git rev-parse --is-inside-work-tree 2>$null)) {
-  throw 'git repo not found'
-}
-
-$repositoryRootPath = git rev-parse --git-path .  | Join-Path -ChildPath .. | Resolve-Path
-$buildPath = Join-Path $repositoryRootPath 'build'
-$gitVersionHeaderPath = Join-Path $buildPath 'git_version.h'
-$gitVersionXmlPath = Join-Path $buildPath 'git_version.xml'
 
-if (!(Test-Path $gitVersionHeaderPath)) {
-  throw "missing git_version.h in ${buildPath}"
+$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"
 }
 
+$gitVersionHeaderPath = Join-Path $repositoryRootPath 'src' | Join-Path -ChildPath 'include' | Join-Path -ChildPath 'aegisub' `
+  | Join-Path -ChildPath 'git_version.h'
+$gitVersionXmlPath = Join-Path $repositoryRootPath 'packages' | Join-Path -ChildPath 'win_installer' `
+  | Join-Path -ChildPath 'git_version.xml'
+
 $version = @{}
-Get-Content $gitVersionHeaderPath | %{$_.Trim()} | ?{$_} | %{
-  switch -regex ($_) {
-    $defineNumberMatch {
-      $version[$Matches[1]] = [int]$Matches[2];
-    }
-    $defineStringMatch {
-      $version[$Matches[1]] = $Matches[2];
+if (Test-Path $gitVersionHeaderPath) {
+  Get-Content $gitVersionHeaderPath | %{$_.Trim()} | ?{$_} | %{
+    switch -regex ($_) {
+      $defineNumberMatch {
+        $version[$Matches[1]] = [int]$Matches[2];
+      }
+      $defineStringMatch {
+        $version[$Matches[1]] = $Matches[2];
+      }
     }
   }
 }
 
-if(!($version.ContainsKey('BUILD_GIT_VERSION_NUMBER') -and $version.ContainsKey('BUILD_GIT_VERSION_STRING'))) {
-  throw 'invalid git_version.h'
-}
-
-$gitRevision = $lastSvnRevision + ((git log --pretty=oneline "$($lastSvnHash)..HEAD" 2>$null | Measure-Object).Count)
-$gitBranch = git symbolic-ref --short HEAD 2>$null
-$gitHash = git rev-parse --short HEAD 2>$null
+$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 describe --exact-match --tags 2>$null
+$exactGitTag = git -C $repositoryRootPath describe --exact-match --tags 2>$null
 
 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 rev-list --tags 2>$null)) {
-    $tag = git describe --exact-match --tags $rev 2>$null
+  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] + $gitRevision
diff --git a/version.sh b/version.sh
old mode 100644
new mode 100755
-- 
GitLab