Browse Source

dev: Standardize ps1 files to tabs

pull/3546/head
Peter Crabtree 4 months ago
parent
commit
b4fb59ae5d
  1. 222
      BuildTools/bom-classify-encodings.ps1
  2. 248
      BuildTools/bom-strip.ps1
  3. 38
      BuildTools/create-filelists.ps1
  4. 4
      BuildTools/ghactions-install.ps1
  5. 182
      BuildTools/update-assemblyinfo.ps1
  6. 4
      ICSharpCode.Decompiler.PowerShell/Demo.ps1

222
BuildTools/bom-classify-encodings.ps1

@ -5,17 +5,17 @@ Classify text files by encoding under the current subtree, respecting .gitignore
.DESCRIPTION .DESCRIPTION
Enumerates tracked files and untracked-but-not-ignored files (via Git) beneath Enumerates tracked files and untracked-but-not-ignored files (via Git) beneath
PWD. Skips likely-binary files (NUL probe). Classifies remaining files as: PWD. Skips likely-binary files (NUL probe). Classifies remaining files as:
- 'utf8' : valid UTF-8 (no BOM) or empty file - 'utf8' : valid UTF-8 (no BOM) or empty file
- 'utf8-with-bom' : starts with UTF-8 BOM (EF BB BF) - 'utf8-with-bom' : starts with UTF-8 BOM (EF BB BF)
- 'other' : text but not valid UTF-8 (e.g., UTF-16/ANSI) - 'other' : text but not valid UTF-8 (e.g., UTF-16/ANSI)
Outputs: Outputs:
1) Relative paths of files classified as 'other' 1) Relative paths of files classified as 'other'
2) A table by extension: UTF8 / UTF8-with-BOM / Other / Total 2) A table by extension: UTF8 / UTF8-with-BOM / Other / Total
Notes: Notes:
- Read-only: this script makes no changes. - Read-only: this script makes no changes.
- Requires Git and must be run inside a Git work tree. - Requires Git and must be run inside a Git work tree.
#> #>
[CmdletBinding()] [CmdletBinding()]
@ -26,93 +26,93 @@ $ErrorActionPreference = 'Stop'
# --- Git enumeration --------------------------------------------------------- # --- Git enumeration ---------------------------------------------------------
function Assert-InGitWorkTree { function Assert-InGitWorkTree {
# Throws if not inside a Git work tree. # Throws if not inside a Git work tree.
$inside = (& git rev-parse --is-inside-work-tree 2>$null).Trim() $inside = (& git rev-parse --is-inside-work-tree 2>$null).Trim()
if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') { if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') {
throw 'Not in a Git work tree.' throw 'Not in a Git work tree.'
} }
} }
function Get-GitFilesUnderPwd { function Get-GitFilesUnderPwd {
<# <#
Returns full paths to tracked + untracked-not-ignored files under PWD. Returns full paths to tracked + untracked-not-ignored files under PWD.
#> #>
Assert-InGitWorkTree Assert-InGitWorkTree
$repoRoot = (& git rev-parse --show-toplevel).Trim() $repoRoot = (& git rev-parse --show-toplevel).Trim()
$pwdPath = (Get-Location).Path $pwdPath = (Get-Location).Path
# cached (tracked) + others (untracked not ignored) # cached (tracked) + others (untracked not ignored)
$nulSeparated = & git -C $repoRoot ls-files -z --cached --others --exclude-standard $nulSeparated = & git -C $repoRoot ls-files -z --cached --others --exclude-standard
$relativePaths = $nulSeparated.Split( $relativePaths = $nulSeparated.Split(
[char]0, [System.StringSplitOptions]::RemoveEmptyEntries) [char]0, [System.StringSplitOptions]::RemoveEmptyEntries)
foreach ($relPath in $relativePaths) { foreach ($relPath in $relativePaths) {
$fullPath = Join-Path $repoRoot $relPath $fullPath = Join-Path $repoRoot $relPath
# Only include files under the current subtree. # Only include files under the current subtree.
if ($fullPath.StartsWith($pwdPath, if ($fullPath.StartsWith($pwdPath,
[System.StringComparison]::OrdinalIgnoreCase)) { [System.StringComparison]::OrdinalIgnoreCase)) {
if (Test-Path -LiteralPath $fullPath -PathType Leaf) { $fullPath } if (Test-Path -LiteralPath $fullPath -PathType Leaf) { $fullPath }
} }
} }
} }
# --- Probes ------------------------------------------------------------------ # --- Probes ------------------------------------------------------------------
function Test-ProbablyBinary { function Test-ProbablyBinary {
# Heuristic: treat as binary if the first 8 KiB contains any NUL byte. # Heuristic: treat as binary if the first 8 KiB contains any NUL byte.
param([Parameter(Mandatory)][string]$Path) param([Parameter(Mandatory)][string]$Path)
try { try {
$stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite')
try { try {
$len = [int][Math]::Min(8192,$stream.Length) $len = [int][Math]::Min(8192,$stream.Length)
if ($len -le 0) { return $false } if ($len -le 0) { return $false }
$buffer = [byte[]]::new($len) $buffer = [byte[]]::new($len)
[void]$stream.Read($buffer,0,$len) [void]$stream.Read($buffer,0,$len)
return ($buffer -contains 0) return ($buffer -contains 0)
} }
finally { $stream.Dispose() } finally { $stream.Dispose() }
} }
catch { return $false } catch { return $false }
} }
function Get-TextEncodingCategory { function Get-TextEncodingCategory {
# Returns 'utf8', 'utf8-with-bom', 'other', or $null for likely-binary. # Returns 'utf8', 'utf8-with-bom', 'other', or $null for likely-binary.
param([Parameter(Mandatory)][string]$Path) param([Parameter(Mandatory)][string]$Path)
$stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite')
try { try {
$fileLength = $stream.Length $fileLength = $stream.Length
if ($fileLength -eq 0) { return 'utf8' } if ($fileLength -eq 0) { return 'utf8' }
# BOM check (EF BB BF) # BOM check (EF BB BF)
$header = [byte[]]::new([Math]::Min(3,$fileLength)) $header = [byte[]]::new([Math]::Min(3,$fileLength))
[void]$stream.Read($header,0,$header.Length) [void]$stream.Read($header,0,$header.Length)
if ($header.Length -ge 3 -and if ($header.Length -ge 3 -and
$header[0] -eq 0xEF -and $header[1] -eq 0xBB -and $header[2] -eq 0xBF) { $header[0] -eq 0xEF -and $header[1] -eq 0xBB -and $header[2] -eq 0xBF) {
return 'utf8-with-bom' return 'utf8-with-bom'
} }
# Quick binary probe before expensive decoding # Quick binary probe before expensive decoding
$stream.Position = 0 $stream.Position = 0
$sampleLen = [int][Math]::Min(8192,$fileLength) $sampleLen = [int][Math]::Min(8192,$fileLength)
$sample = [byte[]]::new($sampleLen) $sample = [byte[]]::new($sampleLen)
[void]$stream.Read($sample,0,$sampleLen) [void]$stream.Read($sample,0,$sampleLen)
if ($sample -contains 0) { return $null } if ($sample -contains 0) { return $null }
} }
finally { $stream.Dispose() } finally { $stream.Dispose() }
# Validate UTF-8 by decoding with throw-on-invalid option (no BOM). # Validate UTF-8 by decoding with throw-on-invalid option (no BOM).
try { try {
$bytes = [System.IO.File]::ReadAllBytes($Path) $bytes = [System.IO.File]::ReadAllBytes($Path)
$utf8 = [System.Text.UTF8Encoding]::new($false,$true) $utf8 = [System.Text.UTF8Encoding]::new($false,$true)
[void]$utf8.GetString($bytes) [void]$utf8.GetString($bytes)
return 'utf8' return 'utf8'
} }
catch { return 'other' } catch { return 'other' }
} }
# --- Main -------------------------------------------------------------------- # --- Main --------------------------------------------------------------------
@ -122,50 +122,50 @@ $byExtension = @{}
$allFiles = Get-GitFilesUnderPwd $allFiles = Get-GitFilesUnderPwd
foreach ($fullPath in $allFiles) { foreach ($fullPath in $allFiles) {
# Avoid decoding likely-binary files. # Avoid decoding likely-binary files.
if (Test-ProbablyBinary $fullPath) { continue } if (Test-ProbablyBinary $fullPath) { continue }
$category = Get-TextEncodingCategory $fullPath $category = Get-TextEncodingCategory $fullPath
if (-not $category) { continue } if (-not $category) { continue }
$ext = [IO.Path]::GetExtension($fullPath).ToLower() $ext = [IO.Path]::GetExtension($fullPath).ToLower()
if (-not $byExtension.ContainsKey($ext)) { if (-not $byExtension.ContainsKey($ext)) {
$byExtension[$ext] = @{ 'utf8' = 0; 'utf8-with-bom' = 0; 'other' = 0 } $byExtension[$ext] = @{ 'utf8' = 0; 'utf8-with-bom' = 0; 'other' = 0 }
} }
$byExtension[$ext][$category]++ $byExtension[$ext][$category]++
if ($category -eq 'other') { if ($category -eq 'other') {
$otherFiles += (Resolve-Path -LiteralPath $fullPath -Relative) $otherFiles += (Resolve-Path -LiteralPath $fullPath -Relative)
} }
} }
# 1) Files in 'other' # 1) Files in 'other'
if ($otherFiles.Count -gt 0) { if ($otherFiles.Count -gt 0) {
'Files classified as ''other'':' 'Files classified as ''other'':'
$otherFiles | Sort-Object | ForEach-Object { " $_" } $otherFiles | Sort-Object | ForEach-Object { " $_" }
'' ''
} }
# 2) Table by extension # 2) Table by extension
$rows = foreach ($kv in $byExtension.GetEnumerator()) { $rows = foreach ($kv in $byExtension.GetEnumerator()) {
$ext = if ($kv.Key) { $kv.Key } else { '[noext]' } $ext = if ($kv.Key) { $kv.Key } else { '[noext]' }
$u = [int]$kv.Value['utf8'] $u = [int]$kv.Value['utf8']
$b = [int]$kv.Value['utf8-with-bom'] $b = [int]$kv.Value['utf8-with-bom']
$o = [int]$kv.Value['other'] $o = [int]$kv.Value['other']
[PSCustomObject]@{ [PSCustomObject]@{
Extension = $ext Extension = $ext
UTF8 = $u UTF8 = $u
'UTF8-with-BOM' = $b 'UTF8-with-BOM' = $b
Other = $o Other = $o
Total = $u + $b + $o Total = $u + $b + $o
} }
} }
$rows | $rows |
Sort-Object -Property ( Sort-Object -Property (
@{Expression='Total';Descending=$true}, @{Expression='Total';Descending=$true},
@{Expression='Extension';Descending=$false} @{Expression='Extension';Descending=$false}
) | ) |
Format-Table -AutoSize Format-Table -AutoSize

248
BuildTools/bom-strip.ps1

@ -14,7 +14,7 @@ Supports -WhatIf/-Confirm via ShouldProcess.
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')] [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Low')]
param( param(
[switch]$Force [switch]$Force
) )
Set-StrictMode -Version Latest Set-StrictMode -Version Latest
@ -22,130 +22,130 @@ $ErrorActionPreference = 'Stop'
# --- File sets (ILSpy) ------------------------------------------------------ # --- File sets (ILSpy) ------------------------------------------------------
$Dotfiles = @( $Dotfiles = @(
'.gitignore', '.editorconfig', '.gitattributes', '.gitmodules', '.gitignore', '.editorconfig', '.gitattributes', '.gitmodules',
'.tgitconfig', '.vsconfig' '.tgitconfig', '.vsconfig'
) )
$AllowedExts = @( $AllowedExts = @(
'.bat','.config','.cs','.csproj','.css','.filelist','.fs','.html','.il', '.bat','.config','.cs','.csproj','.css','.filelist','.fs','.html','.il',
'.ipynb','.js','.json','.less','.manifest','.md','.projitems','.props', '.ipynb','.js','.json','.less','.manifest','.md','.projitems','.props',
'.ps1','.psd1','.ruleset','.shproj','.sln','.slnf','.svg','.template', '.ps1','.psd1','.ruleset','.shproj','.sln','.slnf','.svg','.template',
'.tt', '.txt','.vb','.vsct','.vsixlangpack','.wxl','.xaml','.xml','.xshd','.yml' '.tt', '.txt','.vb','.vsct','.vsixlangpack','.wxl','.xaml','.xml','.xshd','.yml'
) )
$IncludeNoExt = $true # include names like LICENSE $IncludeNoExt = $true # include names like LICENSE
# --- Git checks / enumeration ----------------------------------------------- # --- Git checks / enumeration -----------------------------------------------
function Assert-InGitWorkTree { function Assert-InGitWorkTree {
$inside = (& git rev-parse --is-inside-work-tree 2>$null).Trim() $inside = (& git rev-parse --is-inside-work-tree 2>$null).Trim()
if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') { if ($LASTEXITCODE -ne 0 -or $inside -ne 'true') {
throw 'Not in a Git work tree.' throw 'Not in a Git work tree.'
} }
} }
function Assert-CleanWorkingTree { function Assert-CleanWorkingTree {
if ($Force) { return } if ($Force) { return }
$status = & git status --porcelain -z $status = & git status --porcelain -z
if ($LASTEXITCODE -ne 0) { throw 'git status failed.' } if ($LASTEXITCODE -ne 0) { throw 'git status failed.' }
if (-not [string]::IsNullOrEmpty($status)) { if (-not [string]::IsNullOrEmpty($status)) {
throw 'Working tree not clean. Commit/stash changes or use -Force.' throw 'Working tree not clean. Commit/stash changes or use -Force.'
} }
} }
function Get-GitFilesUnderPwd { function Get-GitFilesUnderPwd {
Assert-InGitWorkTree Assert-InGitWorkTree
$repoRoot = (& git rev-parse --show-toplevel).Trim() $repoRoot = (& git rev-parse --show-toplevel).Trim()
$pwdPath = (Get-Location).Path $pwdPath = (Get-Location).Path
$tracked = & git -C $repoRoot ls-files -z $tracked = & git -C $repoRoot ls-files -z
$others = & git -C $repoRoot ls-files --others --exclude-standard -z $others = & git -C $repoRoot ls-files --others --exclude-standard -z
$allRel = ("$tracked$others").Split( $allRel = ("$tracked$others").Split(
[char]0, [System.StringSplitOptions]::RemoveEmptyEntries) [char]0, [System.StringSplitOptions]::RemoveEmptyEntries)
foreach ($relPath in $allRel) { foreach ($relPath in $allRel) {
$fullPath = Join-Path $repoRoot $relPath $fullPath = Join-Path $repoRoot $relPath
if ($fullPath.StartsWith($pwdPath, if ($fullPath.StartsWith($pwdPath,
[System.StringComparison]::OrdinalIgnoreCase)) { [System.StringComparison]::OrdinalIgnoreCase)) {
if (Test-Path -LiteralPath $fullPath -PathType Leaf) { if (Test-Path -LiteralPath $fullPath -PathType Leaf) {
$fullPath $fullPath
} }
} }
} }
} }
# --- Probes ----------------------------------------------------------------- # --- Probes -----------------------------------------------------------------
function Test-HasUtf8Bom { function Test-HasUtf8Bom {
param([Parameter(Mandatory)][string]$Path) param([Parameter(Mandatory)][string]$Path)
try { try {
$stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite')
try { try {
if ($stream.Length -lt 3) { return $false } if ($stream.Length -lt 3) { return $false }
$header = [byte[]]::new(3) $header = [byte[]]::new(3)
[void]$stream.Read($header,0,3) [void]$stream.Read($header,0,3)
return ($header[0] -eq 0xEF -and return ($header[0] -eq 0xEF -and
$header[1] -eq 0xBB -and $header[1] -eq 0xBB -and
$header[2] -eq 0xBF) $header[2] -eq 0xBF)
} }
finally { finally {
$stream.Dispose() $stream.Dispose()
} }
} }
catch { return $false } catch { return $false }
} }
function Test-ProbablyBinary { function Test-ProbablyBinary {
# Binary if the first 8 KiB contains any NUL byte. # Binary if the first 8 KiB contains any NUL byte.
param([Parameter(Mandatory)][string]$Path) param([Parameter(Mandatory)][string]$Path)
try { try {
$stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite') $stream = [System.IO.File]::Open($Path,'Open','Read','ReadWrite')
try { try {
$len = [int][Math]::Min(8192,$stream.Length) $len = [int][Math]::Min(8192,$stream.Length)
if ($len -le 0) { return $false } if ($len -le 0) { return $false }
$buffer = [byte[]]::new($len) $buffer = [byte[]]::new($len)
[void]$stream.Read($buffer,0,$len) [void]$stream.Read($buffer,0,$len)
return ($buffer -contains 0) return ($buffer -contains 0)
} }
finally { finally {
$stream.Dispose() $stream.Dispose()
} }
} }
catch { return $false } catch { return $false }
} }
# --- Mutation --------------------------------------------------------------- # --- Mutation ---------------------------------------------------------------
function Remove-Utf8BomInPlace { function Remove-Utf8BomInPlace {
# Write the existing buffer from offset 3, no extra full-size allocation. # Write the existing buffer from offset 3, no extra full-size allocation.
param([Parameter(Mandatory)][string]$Path) param([Parameter(Mandatory)][string]$Path)
$bytes = [System.IO.File]::ReadAllBytes($Path) $bytes = [System.IO.File]::ReadAllBytes($Path)
if ($bytes.Length -lt 3) { return $false } if ($bytes.Length -lt 3) { return $false }
if ($bytes[0] -ne 0xEF -or if ($bytes[0] -ne 0xEF -or
$bytes[1] -ne 0xBB -or $bytes[1] -ne 0xBB -or
$bytes[2] -ne 0xBF) { $bytes[2] -ne 0xBF) {
return $false return $false
} }
$stream = [System.IO.File]::Open($Path,'Create','Write','ReadWrite') $stream = [System.IO.File]::Open($Path,'Create','Write','ReadWrite')
try { try {
$stream.Write($bytes, 3, $bytes.Length - 3) $stream.Write($bytes, 3, $bytes.Length - 3)
$stream.SetLength($bytes.Length - 3) $stream.SetLength($bytes.Length - 3)
} }
finally { finally {
$stream.Dispose() $stream.Dispose()
} }
return $true return $true
} }
# --- Main ------------------------------------------------------------------- # --- Main -------------------------------------------------------------------
@ -155,16 +155,16 @@ Assert-CleanWorkingTree
$allFiles = Get-GitFilesUnderPwd $allFiles = Get-GitFilesUnderPwd
$targets = $allFiles | % { $targets = $allFiles | % {
$fileName = [IO.Path]::GetFileName($_) $fileName = [IO.Path]::GetFileName($_)
$ext = [IO.Path]::GetExtension($fileName) $ext = [IO.Path]::GetExtension($fileName)
$isDot = $Dotfiles -contains $fileName $isDot = $Dotfiles -contains $fileName
$isNoExt = -not $fileName.Contains('.') $isNoExt = -not $fileName.Contains('.')
if ($isDot -or ($AllowedExts -contains $ext) -or if ($isDot -or ($AllowedExts -contains $ext) -or
($IncludeNoExt -and $isNoExt -and -not $isDot)) { ($IncludeNoExt -and $isNoExt -and -not $isDot)) {
$_ $_
} }
} }
| ? { Test-HasUtf8Bom $_ } | ? { Test-HasUtf8Bom $_ }
| ? { -not (Test-ProbablyBinary $_) } | ? { -not (Test-ProbablyBinary $_) }
@ -174,35 +174,35 @@ $byExtension = @{}
$dotfileChanges = 0 $dotfileChanges = 0
$targets | % { $targets | % {
$relative = Resolve-Path -LiteralPath $_ -Relative $relative = Resolve-Path -LiteralPath $_ -Relative
if ($PSCmdlet.ShouldProcess($relative,'Strip UTF-8 BOM')) { if ($PSCmdlet.ShouldProcess($relative,'Strip UTF-8 BOM')) {
if (Remove-Utf8BomInPlace -Path $_) { if (Remove-Utf8BomInPlace -Path $_) {
$changed++ $changed++
$fileName = [IO.Path]::GetFileName($_) $fileName = [IO.Path]::GetFileName($_)
if ($Dotfiles -contains $fileName) { $dotfileChanges++ } if ($Dotfiles -contains $fileName) { $dotfileChanges++ }
$ext = [IO.Path]::GetExtension($fileName) $ext = [IO.Path]::GetExtension($fileName)
if (-not $byExtension.ContainsKey($ext)) { $byExtension[$ext] = 0 } if (-not $byExtension.ContainsKey($ext)) { $byExtension[$ext] = 0 }
$byExtension[$ext]++ $byExtension[$ext]++
"stripped BOM: $relative" "stripped BOM: $relative"
} }
} }
} }
"Done. Stripped BOM from $changed file(s)." "Done. Stripped BOM from $changed file(s)."
if ($byExtension.Keys.Count -gt 0) { if ($byExtension.Keys.Count -gt 0) {
"" ""
"By extension:" "By extension:"
$byExtension.GetEnumerator() | Sort-Object Name | % { $byExtension.GetEnumerator() | Sort-Object Name | % {
$key = if ([string]::IsNullOrEmpty($_.Name)) { '[noext]' } else { $_.Name } $key = if ([string]::IsNullOrEmpty($_.Name)) { '[noext]' } else { $_.Name }
" {0}: {1}" -f $key, $_.Value " {0}: {1}" -f $key, $_.Value
} }
} }
if ($dotfileChanges -gt 0) { if ($dotfileChanges -gt 0) {
" [dotfiles]: $dotfileChanges" " [dotfiles]: $dotfileChanges"
} }

38
BuildTools/create-filelists.ps1

@ -3,23 +3,23 @@ $ErrorActionPreference = "Stop";
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False $Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
gci -Include *.vsix, *.msi -recurse | foreach ($_) { gci -Include *.vsix, *.msi -recurse | foreach ($_) {
if (-not ($_.FullName -contains "\bin\Debug\")) { if (-not ($_.FullName -contains "\bin\Debug\")) {
continue; continue;
} }
$idx=-1; $idx=-1;
$body=$false; $body=$false;
$outputFileName = ".\BuildTools\$($_.Name -replace '-\d+\.\d+\.\d+\.\d+', '').filelist"; $outputFileName = ".\BuildTools\$($_.Name -replace '-\d+\.\d+\.\d+\.\d+', '').filelist";
$lines = 7z l $_.FullName | foreach { $lines = 7z l $_.FullName | foreach {
if ($idx -eq -1) { if ($idx -eq -1) {
$idx = $_.IndexOf("Name"); $idx = $_.IndexOf("Name");
} }
$p = $body; $p = $body;
if ($idx -gt 0) { if ($idx -gt 0) {
$body = ($body -ne ($_ -match ' *-[ -]+')) $body = ($body -ne ($_ -match ' *-[ -]+'))
} }
if ($p -and $body) { if ($p -and $body) {
$_.Substring($idx) $_.Substring($idx)
} }
} | sort } | sort
[System.IO.File]::WriteAllLines($outputFileName, $lines, $Utf8NoBomEncoding) [System.IO.File]::WriteAllLines($outputFileName, $lines, $Utf8NoBomEncoding)
} }

4
BuildTools/ghactions-install.ps1

@ -17,9 +17,9 @@ $build = $versionParts.Build;
$versionName = $versionParts.VersionName; $versionName = $versionParts.VersionName;
if ($versionName -ne "null") { if ($versionName -ne "null") {
$versionName = "-$versionName"; $versionName = "-$versionName";
} else { } else {
$versionName = ""; $versionName = "";
} }
Write-Host "GITHUB_REF: '$env:GITHUB_REF'"; Write-Host "GITHUB_REF: '$env:GITHUB_REF'";

182
BuildTools/update-assemblyinfo.ps1

@ -1,6 +1,6 @@
if (-not ($PSVersionTable.PSCompatibleVersions -contains "5.0")) { if (-not ($PSVersionTable.PSCompatibleVersions -contains "5.0")) {
Write-Error "This script requires at least powershell version 5.0!"; Write-Error "This script requires at least powershell version 5.0!";
return 255; return 255;
} }
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
@ -14,11 +14,11 @@ $masterBranches = '^(master|release/.+)$';
$decompilerVersionInfoTemplateFile = "ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.template.cs"; $decompilerVersionInfoTemplateFile = "ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.template.cs";
function Test-File([string]$filename) { function Test-File([string]$filename) {
return [System.IO.File]::Exists((Join-Path (Get-Location) $filename)); return [System.IO.File]::Exists((Join-Path (Get-Location) $filename));
} }
function Test-Dir([string]$name) { function Test-Dir([string]$name) {
return [System.IO.Directory]::Exists((Join-Path (Get-Location) $name)); return [System.IO.Directory]::Exists((Join-Path (Get-Location) $name));
} }
function Find-Git() { function Find-Git() {
@ -44,63 +44,63 @@ function Find-Git() {
} }
function No-Git() { function No-Git() {
return -not (((Test-Dir ".git") -or (Test-File ".git")) -and (Find-Git)); return -not (((Test-Dir ".git") -or (Test-File ".git")) -and (Find-Git));
} }
function gitVersion() { function gitVersion() {
if (No-Git) { if (No-Git) {
return 0; return 0;
} }
try { try {
return [Int32]::Parse((git rev-list --count "$baseCommit..HEAD" 2>&1 | Tee-Object -Variable cmdOutput)) + $baseCommitRev; return [Int32]::Parse((git rev-list --count "$baseCommit..HEAD" 2>&1 | Tee-Object -Variable cmdOutput)) + $baseCommitRev;
} catch { } catch {
Write-Host $cmdOutput Write-Host $cmdOutput
return 0; return 0;
} }
} }
function gitCommitHash() { function gitCommitHash() {
if (No-Git) { if (No-Git) {
return "0000000000000000000000000000000000000000"; return "0000000000000000000000000000000000000000";
} }
try { try {
return (git rev-list --max-count 1 HEAD 2>&1 | Tee-Object -Variable cmdOutput); return (git rev-list --max-count 1 HEAD 2>&1 | Tee-Object -Variable cmdOutput);
} catch { } catch {
Write-Host $cmdOutput Write-Host $cmdOutput
return "0000000000000000000000000000000000000000"; return "0000000000000000000000000000000000000000";
} }
} }
function gitShortCommitHash() { function gitShortCommitHash() {
if (No-Git) { if (No-Git) {
return "00000000"; return "00000000";
} }
try { try {
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); 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);
} catch { } catch {
Write-Host $cmdOutput Write-Host $cmdOutput
return "00000000"; return "00000000";
} }
} }
function gitBranch() { function gitBranch() {
if (No-Git) { if (No-Git) {
return "no-branch"; return "no-branch";
} }
if ($env:APPVEYOR_REPO_BRANCH -ne $null) { if ($env:APPVEYOR_REPO_BRANCH -ne $null) {
return $env:APPVEYOR_REPO_BRANCH; return $env:APPVEYOR_REPO_BRANCH;
} elseif ($env:BUILD_SOURCEBRANCHNAME -ne $null) { } elseif ($env:BUILD_SOURCEBRANCHNAME -ne $null) {
return $env:BUILD_SOURCEBRANCHNAME; return $env:BUILD_SOURCEBRANCHNAME;
} else { } else {
return ((git branch --no-color).Split([System.Environment]::NewLine) | where { $_ -match "^\* " } | select -First 1).Substring(2); return ((git branch --no-color).Split([System.Environment]::NewLine) | where { $_ -match "^\* " } | select -First 1).Substring(2);
} }
} }
$templateFiles = ( $templateFiles = (
@{Input=$decompilerVersionInfoTemplateFile; Output="ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.cs"}, @{Input=$decompilerVersionInfoTemplateFile; Output="ICSharpCode.Decompiler/Properties/DecompilerVersionInfo.cs"},
@{Input="ILSpy.AddIn/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn/source.extension.vsixmanifest"}, @{Input="ILSpy.AddIn/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn/source.extension.vsixmanifest"},
@{Input="ILSpy.AddIn.VS2022/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn.VS2022/source.extension.vsixmanifest"} @{Input="ILSpy.AddIn.VS2022/source.extension.vsixmanifest.template"; Output = "ILSpy.AddIn.VS2022/source.extension.vsixmanifest"}
); );
[string]$mutexId = "ILSpyUpdateAssemblyInfo" + (Get-Location).ToString().GetHashCode(); [string]$mutexId = "ILSpyUpdateAssemblyInfo" + (Get-Location).ToString().GetHashCode();
@ -108,43 +108,43 @@ Write-Host $mutexId;
[bool]$createdNew = $false; [bool]$createdNew = $false;
$mutex = New-Object System.Threading.Mutex($true, $mutexId, [ref]$createdNew); $mutex = New-Object System.Threading.Mutex($true, $mutexId, [ref]$createdNew);
try { try {
if (-not $createdNew) { if (-not $createdNew) {
try { try {
$mutex.WaitOne(10000); $mutex.WaitOne(10000);
} catch [System.Threading.AbandonedMutexException] { } catch [System.Threading.AbandonedMutexException] {
} }
return 0; return 0;
} }
if (-not (Test-File "ILSpy.sln")) { if (-not (Test-File "ILSpy.sln")) {
Write-Error "Working directory must be the ILSpy repo root!"; Write-Error "Working directory must be the ILSpy repo root!";
return 2; return 2;
} }
$versionParts = @{}; $versionParts = @{};
Get-Content $decompilerVersionInfoTemplateFile | where { $_ -match 'string (\w+) = "?(\w+)"?;' } | foreach { $versionParts.Add($Matches[1], $Matches[2]) } Get-Content $decompilerVersionInfoTemplateFile | where { $_ -match 'string (\w+) = "?(\w+)"?;' } | foreach { $versionParts.Add($Matches[1], $Matches[2]) }
$major = $versionParts.Major; $major = $versionParts.Major;
$minor = $versionParts.Minor; $minor = $versionParts.Minor;
$build = $versionParts.Build; $build = $versionParts.Build;
$versionName = $versionParts.VersionName; $versionName = $versionParts.VersionName;
$revision = gitVersion; $revision = gitVersion;
$branchName = gitBranch; $branchName = gitBranch;
$gitCommitHash = gitCommitHash; $gitCommitHash = gitCommitHash;
$gitShortCommitHash = gitShortCommitHash; $gitShortCommitHash = gitShortCommitHash;
if ($branchName -match $masterBranches) { if ($branchName -match $masterBranches) {
$postfixBranchName = ""; $postfixBranchName = "";
} else { } else {
$postfixBranchName = "-$branchName"; $postfixBranchName = "-$branchName";
} }
if ($versionName -eq "null") { if ($versionName -eq "null") {
$versionName = ""; $versionName = "";
$postfixVersionName = ""; $postfixVersionName = "";
} else { } else {
$postfixVersionName = "-$versionName"; $postfixVersionName = "-$versionName";
} }
$buildConfig = $args[0].ToString().ToLower(); $buildConfig = $args[0].ToString().ToLower();
if ($buildConfig -eq "release") { if ($buildConfig -eq "release") {
@ -153,13 +153,13 @@ try {
$buildConfig = "-" + $buildConfig; $buildConfig = "-" + $buildConfig;
} }
$fullVersionNumber = "$major.$minor.$build.$revision"; $fullVersionNumber = "$major.$minor.$build.$revision";
if ((-not (Test-File "VERSION")) -or (((Get-Content "VERSION") -Join [System.Environment]::NewLine) -ne "$fullVersionNumber$postfixVersionName")) { if ((-not (Test-File "VERSION")) -or (((Get-Content "VERSION") -Join [System.Environment]::NewLine) -ne "$fullVersionNumber$postfixVersionName")) {
"$fullVersionNumber$postfixVersionName" | Out-File -Encoding utf8 -NoNewLine "VERSION"; "$fullVersionNumber$postfixVersionName" | Out-File -Encoding utf8 -NoNewLine "VERSION";
} }
foreach ($file in $templateFiles) { foreach ($file in $templateFiles) {
[string]$in = (Get-Content $file.Input) -Join [System.Environment]::NewLine; [string]$in = (Get-Content $file.Input) -Join [System.Environment]::NewLine;
$out = $in.Replace('$INSERTVERSION$', $fullVersionNumber); $out = $in.Replace('$INSERTVERSION$', $fullVersionNumber);
$out = $out.Replace('$INSERTMAJORVERSION$', $major); $out = $out.Replace('$INSERTMAJORVERSION$', $major);
@ -169,18 +169,18 @@ try {
$out = $out.Replace('$INSERTDATE$', [System.DateTime]::Now.ToString("MM/dd/yyyy")); $out = $out.Replace('$INSERTDATE$', [System.DateTime]::Now.ToString("MM/dd/yyyy"));
$out = $out.Replace('$INSERTYEAR$', [System.DateTime]::Now.Year.ToString()); $out = $out.Replace('$INSERTYEAR$', [System.DateTime]::Now.Year.ToString());
$out = $out.Replace('$INSERTBRANCHNAME$', $branchName); $out = $out.Replace('$INSERTBRANCHNAME$', $branchName);
$out = $out.Replace('$INSERTBRANCHPOSTFIX$', $postfixBranchName); $out = $out.Replace('$INSERTBRANCHPOSTFIX$', $postfixBranchName);
$out = $out.Replace('$INSERTVERSIONNAME$', $versionName); $out = $out.Replace('$INSERTVERSIONNAME$', $versionName);
$out = $out.Replace('$INSERTVERSIONNAMEPOSTFIX$', $postfixVersionName); $out = $out.Replace('$INSERTVERSIONNAMEPOSTFIX$', $postfixVersionName);
$out = $out.Replace('$INSERTBUILDCONFIG$', $buildConfig); $out = $out.Replace('$INSERTBUILDCONFIG$', $buildConfig);
if ((-not (Test-File $file.Output)) -or (((Get-Content $file.Output) -Join [System.Environment]::NewLine) -ne $out)) { if ((-not (Test-File $file.Output)) -or (((Get-Content $file.Output) -Join [System.Environment]::NewLine) -ne $out)) {
$utf8NoBom = New-Object System.Text.UTF8Encoding($false); $utf8NoBom = New-Object System.Text.UTF8Encoding($false);
[System.IO.File]::WriteAllText($file.Output, $out, $utf8NoBom); [System.IO.File]::WriteAllText($file.Output, $out, $utf8NoBom);
} }
} }
} finally { } finally {
$mutex.ReleaseMutex(); $mutex.ReleaseMutex();
$mutex.Close(); $mutex.Close();
} }

4
ICSharpCode.Decompiler.PowerShell/Demo.ps1

@ -1,7 +1,7 @@
$basePath = $PSScriptRoot $basePath = $PSScriptRoot
if ([string]::IsNullOrEmpty($basePath)) if ([string]::IsNullOrEmpty($basePath))
{ {
$basePath = Split-Path -parent $psISE.CurrentFile.Fullpath $basePath = Split-Path -parent $psISE.CurrentFile.Fullpath
} }
$modulePath = $basePath + '\bin\Debug\netstandard2.0\ICSharpCode.Decompiler.Powershell.dll' $modulePath = $basePath + '\bin\Debug\netstandard2.0\ICSharpCode.Decompiler.Powershell.dll'
@ -21,7 +21,7 @@ $classes.Count
foreach ($c in $classes) foreach ($c in $classes)
{ {
Write-Output $c.FullName Write-Output $c.FullName
} }

Loading…
Cancel
Save