ps/Modules/clean-project.ps1

95 lines
4.4 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
<#
.SYNOPSIS
Performs the appropriate clean action on a given project in the Alkami.PowerShell module system
.EXAMPLE
.\clean-project.ps1 .\Alkami.PowerShell.IIS\
.PARAMETER FolderPath
The name of the folder to examine all files under
#>
[CmdletBinding()]
Param(
$FolderPath,
[switch]$AsBuildServer
)
process {
$FolderPath = Convert-Path $FolderPath
Write-Host "Cleaning project located at $FolderPath" -ForegroundColor Cyan
. $PSScriptRoot\.build\Load-Includes.ps1
$csprojPaths = (Get-ChildItem *.csproj -Path $FolderPath)
if (!$AsBuildServer -and (@($csprojPaths).Count -gt 0)) {
Write-Verbose "Running msbuild in $FolderPath at /t:Clean"
foreach($csprojPath in $csprojPaths) {
$buildConfigs = (Get-BuildConfigs (Join-Path $FolderPath $csprojPath))
foreach($buildConfig in $buildConfigs) {
& (Get-MSBuildPath) $csprojPath.FullName /m /t:Clean /p:"Configuration=$buildConfig" /clp:ErrorsOnly /nologo
}
}
} elseif (Test-Path (Join-Path $FolderPath Public)) {
## Get some cronchy filenames
$projectName = (Split-Path $FolderPath -Leaf)
$psd1Name = $projectName + ".psd1"
$psd1Filepath = (Join-Path $FolderPath $psd1Name)
## Keep the minimum changes on the psd1
if (@(git status --short --no-renames --porcelain) | Where-Object { $_ -match $psd1Name }) {
$capturePSDasVariable = Import-PowerShellDataFile -Path $psd1Filepath
$psd1Version = $capturePSDasVariable.ModuleVersion
$psd1Modules = ($capturePSDasVariable.RequiredModules -join ' ').Replace(' ',"','") ## Weirdness on reading in comma delimited values
## The next line says "*>&1" which means "redirect all output streams to the first one"
## This is because when we go to "reset" the one specified file, it prints a git output line.
## We want it to be quiet so we can see what's important instead of them deciding how things get written, to which stream, because there's no quiet value
## This is standard PowerShell redirection, nothing special about git.
(git checkout $psd1Filepath) | Out-Null
$lines = Get-Content -Path $psd1Filepath
$newLines = @()
foreach ($line in $lines) {
if ($line -match 'ModuleVersion' -and $line -notmatch $psd1Version) {
$split = $line.Split('=')[0].TrimEnd()
$line = $split + " = '" + $psd1Version + "'"
}
if ($line -match 'RequiredModules' -and $line -notmatch $psd1Modules) {
$split = $line.Split('=')[0].TrimEnd()
$line = $split + " = '" + $psd1Modules + "'"
}
$newLines += $line
}
Set-Content -Path $psd1Filepath -Value $newLines
}
## Empty the psm1 file.
$psm1Filename = $projectName + ".psm1"
$psm1Filepath = (Join-Path $FolderPath $psm1Filename)
Set-Content -Path $psm1Filepath -Value $null
## Remove any nupkg files that were created by anyone testing
if (Test-Path (Join-Path $FolderPath "*.nupkg")) {
(Remove-Item (Join-Path $FolderPath "*.nupkg")) | Out-Null
}
} else {
Write-Verbose "There was no public folder and no csproj to clean in [$FolderPath]"
}
## https://stackoverflow.com/a/28637537/109749
# Define a script block that will remove empty folders under a root folder, using tail-recursion to ensure that it only walks the folder tree once. -Force is used to be able to process hidden files/folders as well.
$tailRecursion = {
param($Path)
foreach ($childDirectory in Get-ChildItem -Force -LiteralPath $Path -Directory) {
& $tailRecursion -Path $childDirectory.FullName
}
$currentChildren = Get-ChildItem -Force -LiteralPath $Path
$isEmpty = $currentChildren -eq $null
if ($isEmpty) {
Write-Verbose "Removing empty folder at path '${Path}'."
(Remove-Item -Force -LiteralPath $Path) | Out-Null
}
}
# Invoke the script block and pass in a root path where you want it to start. This will remove all empty folders in the folder you specify, including empty folders that contain nothing but empty folders, including the start folder if that winds up as empty.
& $tailRecursion -Path $FolderPath
}