From c63125950557c37cf075f432d79487e909881f95 Mon Sep 17 00:00:00 2001
From: Niko Ehrenfeuchter <nikolaus.ehrenfeuchter@unibas.ch>
Date: Thu, 21 Dec 2017 14:52:13 +0100
Subject: [PATCH] Move pre-build event to an external PowerShell script.

Plus also check if the repository's commit state is dirty and report
accordingly.
---
 AutoTx/ATXProject.csproj  |  2 +-
 Scripts/Prepare-Build.ps1 | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 Scripts/Prepare-Build.ps1

diff --git a/AutoTx/ATXProject.csproj b/AutoTx/ATXProject.csproj
index 5af4a78..65e71b0 100644
--- a/AutoTx/ATXProject.csproj
+++ b/AutoTx/ATXProject.csproj
@@ -152,7 +152,7 @@
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
-    <PreBuildEvent>PowerShell -Command "$BCommit = '$(ProjectDir)\Resources\BuildCommit.txt' ; $BDate = '$(ProjectDir)\Resources\BuildDate.txt' ; if (-Not (Test-Path $BCommit)) { Write-Output 'UNKNOWN' &gt; $BCommit } ; Get-Date -Format 'yyyy-MM-dd HH:mm:ss' &gt; $BDate ; Get-Content $BDate"</PreBuildEvent>
+    <PreBuildEvent>PowerShell -NoProfile -ExecutionPolicy RemoteSigned $(SolutionDir)\Scripts\Prepare-Build.ps1 $(ProjectDir)</PreBuildEvent>
   </PropertyGroup>
   <PropertyGroup>
     <PostBuildEvent>PowerShell -Command "Write-Host $(ConfigurationName)" &gt; $(ProjectDir)\Resources\BuildConfiguration.txt
diff --git a/Scripts/Prepare-Build.ps1 b/Scripts/Prepare-Build.ps1
new file mode 100644
index 0000000..ca337d2
--- /dev/null
+++ b/Scripts/Prepare-Build.ps1
@@ -0,0 +1,36 @@
+[CmdletBinding()]
+Param(
+    [Parameter(Mandatory=$True)][string] $ProjectDir
+)
+
+$oldpwd = pwd
+cd $ProjectDir -ErrorAction Stop
+
+try {
+    $CommitName = git describe --tags
+    $GitStatus = git status --porcelain
+}
+catch {
+    $CommitName = "commit unknown"
+    $GitStatus = "status unknown"
+}
+
+
+if ($GitStatus.Length -gt 0) {
+    Write-Output "NOTE: repository has uncommitted changes!"
+    $CommitName = "$($CommitName)-unclean"
+}
+
+$Date = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
+
+
+$BCommit = "$($ProjectDir)\Resources\BuildCommit.txt"
+$BuildDate = "$($ProjectDir)\Resources\BuildDate.txt"
+
+Write-Output $CommitName > $BCommit
+Write-Output $Date > $BuildDate
+
+Write-Output $Date
+Write-Output $CommitName
+
+cd $oldpwd
\ No newline at end of file
-- 
GitLab