You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.0 KiB

5 years ago
  1. if (-not ($PSVersionTable.PSCompatibleVersions -contains "5.0")) {
  2. Write-Error "This script requires at least powershell version 5.0!";
  3. return 255;
  4. }
  5. $ErrorActionPreference = "Stop"
  6. $baseCommit = "d779383cb85003d6dabeb976f0845631e07bf463";
  7. $baseCommitRev = 1;
  8. # make sure this matches artifacts-only branches list in appveyor.yml!
  9. $masterBranches = '^(master|release/.+)$';
  10. $decompilerVersionInfoTemplateFile = "ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.template.cs";
  11. function Test-File([string]$filename) {
  12. return [System.IO.File]::Exists((Join-Path (Get-Location) $filename));
  13. }
  14. function Test-Dir([string]$name) {
  15. return [System.IO.Directory]::Exists((Join-Path (Get-Location) $name));
  16. }
  17. function Find-Git() {
  18. try {
  19. $executable = (get-command git).Path;
  20. return $executable -ne $null;
  21. } catch {
  22. #git not found in path, continue;
  23. }
  24. #we're on Windows
  25. if ($env:PROGRAMFILES -ne $null) {
  26. #hack for x86 powershell used by default (yuck!)
  27. if (${env:PROGRAMFILES(X86)} -eq ${env:PROGRAMFILES}) {
  28. $env:PROGRAMFILES = $env:PROGRAMFILES.Substring(0, $env:PROGRAMFILES.Length - 6);
  29. }
  30. #try to add git to path
  31. if ([System.IO.Directory]::Exists("$env:PROGRAMFILES\git\cmd\")) {
  32. $env:PATH = "$env:PATH;$env:PROGRAMFILES\git\cmd\";
  33. return $true;
  34. }
  35. }
  36. return $false;
  37. }
  38. function No-Git() {
  39. return -not (((Test-Dir ".git") -or (Test-File ".git")) -and (Find-Git));
  40. }
  41. function gitVersion() {
  42. if (No-Git) {
  43. return 0;
  44. }
  45. try {
  46. return [Int32]::Parse((git rev-list --count "$baseCommit..HEAD" 2>&1 | Tee-Object -Variable cmdOutput)) + $baseCommitRev;
  47. } catch {
  48. Write-Host $cmdOutput
  49. return 0;
  50. }
  51. }
  52. function gitCommitHash() {
  53. if (No-Git) {
  54. return "0000000000000000000000000000000000000000";
  55. }
  56. try {
  57. return (git rev-list --max-count 1 HEAD 2>&1 | Tee-Object -Variable cmdOutput);
  58. } catch {
  59. Write-Host $cmdOutput
  60. return "0000000000000000000000000000000000000000";
  61. }
  62. }
  63. function gitShortCommitHash() {
  64. if (No-Git) {
  65. return "00000000";
  66. }
  67. try {
  68. return (git rev-parse --short=8 (git rev-list --max-count 1 HEAD 2>&1 | Tee-Object -Variable cmdOutput) 2>&1 | Tee-Object -Variable cmdOutput);
  69. } catch {
  70. Write-Host $cmdOutput
  71. return "00000000";
  72. }
  73. }
  74. function gitBranch() {
  75. if (No-Git) {
  76. return "no-branch";
  77. }
  78. if ($env:APPVEYOR_REPO_BRANCH -ne $null) {
  79. return $env:APPVEYOR_REPO_BRANCH;
  80. } elseif ($env:BUILD_SOURCEBRANCHNAME -ne $null) {
  81. return $env:BUILD_SOURCEBRANCHNAME;
  82. } else {
  83. return ((git branch --no-color).Split([System.Environment]::NewLine) | where { $_ -match "^\* " } | select -First 1).Substring(2);
  84. }
  85. }
  86. $templateFiles = (
  87. @{Input=$decompilerVersionInfoTemplateFile; Output="ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.cs"},
  88. @{Input="ILSpy.AddIn/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn/source.extension.vsixmanifest"},
  89. @{Input="ILSpy.AddIn.VS2022/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn.VS2022/source.extension.vsixmanifest"}
  90. );
  91. [string]$mutexId = "ILSpyUpdateAssemblyInfo" + (Get-Location).ToString().GetHashCode();
  92. Write-Host $mutexId;
  93. [bool]$createdNew = $false;
  94. $mutex = New-Object System.Threading.Mutex($true, $mutexId, [ref]$createdNew);
  95. try {
  96. if (-not $createdNew) {
  97. try {
  98. $mutex.WaitOne(10000);
  99. } catch [System.Threading.AbandonedMutexException] {
  100. }
  101. return 0;
  102. }
  103. if (-not (Test-File "ILSpy.sln")) {
  104. Write-Error "Working directory must be the ILSpy repo root!";
  105. return 2;
  106. }
  107. $versionParts = @{};
  108. Get-Content $decompilerVersionInfoTemplateFile | where { $_ -match 'string (\w+) = "?(\w+)"?;' } | foreach { $versionParts.Add($Matches[1], $Matches[2]) }
  109. $major = $versionParts.Major;
  110. $minor = $versionParts.Minor;
  111. $build = $versionParts.Build;
  112. $versionName = $versionParts.VersionName;
  113. $revision = gitVersion;
  114. $branchName = gitBranch;
  115. $gitCommitHash = gitCommitHash;
  116. $gitShortCommitHash = gitShortCommitHash;
  117. if ($branchName -match $masterBranches) {
  118. $postfixBranchName = "";
  119. } else {
  120. $postfixBranchName = "-$branchName";
  121. }
  122. if ($versionName -eq "null") {
  123. $versionName = "";
  124. $postfixVersionName = "";
  125. } else {
  126. $postfixVersionName = "-$versionName";
  127. }
  128. $buildConfig = $args[0].ToString().ToLower();
  129. if ($buildConfig -eq "release") {
  130. $buildConfig = "";
  131. } else {
  132. $buildConfig = "-" + $buildConfig;
  133. }
  134. $fullVersionNumber = "$major.$minor.$build.$revision";
  135. if ((-not (Test-File "VERSION")) -or (((Get-Content "VERSION") -Join [System.Environment]::NewLine) -ne "$fullVersionNumber$postfixVersionName")) {
  136. "$fullVersionNumber$postfixVersionName" | Out-File -Encoding utf8 -NoNewLine "VERSION";
  137. }
  138. foreach ($file in $templateFiles) {
  139. [string]$in = (Get-Content $file.Input) -Join [System.Environment]::NewLine;
  140. $out = $in.Replace('$INSERTVERSION$', $fullVersionNumber);
  141. $out = $out.Replace('$INSERTMAJORVERSION$', $major);
  142. $out = $out.Replace('$INSERTREVISION$', $revision);
  143. $out = $out.Replace('$INSERTCOMMITHASH$', $gitCommitHash);
  144. $out = $out.Replace('$INSERTSHORTCOMMITHASH$', $gitShortCommitHash);
  145. $out = $out.Replace('$INSERTDATE$', [System.DateTime]::Now.ToString("MM/dd/yyyy"));
  146. $out = $out.Replace('$INSERTYEAR$', [System.DateTime]::Now.Year.ToString());
  147. $out = $out.Replace('$INSERTBRANCHNAME$', $branchName);
  148. $out = $out.Replace('$INSERTBRANCHPOSTFIX$', $postfixBranchName);
  149. $out = $out.Replace('$INSERTVERSIONNAME$', $versionName);
  150. $out = $out.Replace('$INSERTVERSIONNAMEPOSTFIX$', $postfixVersionName);
  151. $out = $out.Replace('$INSERTBUILDCONFIG$', $buildConfig);
  152. if ((-not (Test-File $file.Output)) -or (((Get-Content $file.Output) -Join [System.Environment]::NewLine) -ne $out)) {
  153. $out | Out-File -Encoding utf8 $file.Output;
  154. }
  155. }
  156. } finally {
  157. $mutex.ReleaseMutex();
  158. $mutex.Close();
  159. }