Skip to content

Instantly share code, notes, and snippets.

@RussKie
Last active May 22, 2024 06:32
Show Gist options
  • Save RussKie/4137e9b5b52798909e331f17bd7d37d1 to your computer and use it in GitHub Desktop.
Save RussKie/4137e9b5b52798909e331f17bd7d37d1 to your computer and use it in GitHub Desktop.
clean-arcade.ps1 script
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[String]
$PackagesPropsFilePath,
[Parameter(Mandatory=$true)]
[String]
$EngFolderPath
)
function ReportErrorAndExit {
param (
[string] $message
)
Write-Host "[FAIL] $message" -ForegroundColor Red;
Exit -1;
}
function ReportInfo {
param (
[string] $message
)
Write-Host "[INFO] $message" -ForegroundColor DarkGreen;
}
function ReportWarning {
param (
[string] $message
)
Write-Host "[WARN] $message" -ForegroundColor Yellow;
}
function Test-FileExist {
param (
[string] $filePath
)
if(!(Test-Path $filePath)) {
ReportErrorAndExit -message "$filePath does not exist"
}
}
# Check that all required files are present
$versionPropsPath = Join-Path -Path $EngFolderPath -ChildPath Versions.props
$versionDetailsXmlPath = Join-Path -Path $EngFolderPath -ChildPath Version.Details.xml
Test-FileExist -filePath $PackagesPropsFilePath;
Test-FileExist -filePath $versionPropsPath;
Test-FileExist -filePath $versionDetailsXmlPath;
$repoRootPath = Split-Path -Path $EngFolderPath -Parent
class PackageVersionDefinition {
[string] $PackageName
[string] $PackageVersion
[boolean] $IsUsed
}
$declaredPackages = @();
[xml]$packagesPropsXml = Get-Content $PackagesPropsFilePath;
$packagesPropsXml.Project.ItemGroup.PackageVersion | `
Where-Object { $_.Version.StartsWith("$`(") } | `
ForEach-Object {
$packageName = $_.Include;
$packageVersion = $_.Version;
$expectedPackageVersion = $packageName.Replace('.', '');
if (!$packageVersion.StartsWith("`$`($expectedPackageVersion")) {
ReportWarning -message "'$packageName' version should be defined as '`$`($($expectedPackageVersion)Version`)' but '$packageVersion' was found instead."
}
$definition = [PackageVersionDefinition]::new()
$definition.PackageName = $packageName
$definition.PackageVersion = $packageVersion
$definition.IsUsed = $false
$declaredPackages += $definition
}
[string[]]$excludedFolders = @('.git', '.vs', '.vscode', '.dotnet', 'eng', 'artifacts')
Get-ChildItem -Path $repoRootPath -Exclude $excludedFolders | `
ForEach-Object {
Get-ChildItem -Path $_ -Include *.csproj,*.props,*.targets -Recurse | `
ForEach-Object {
$projFile = $_.FullName;
[xml]$xml = Get-Content $projFile;
$declaredPackages | `
ForEach-Object {
$packageName = $_.PackageName;
if ($_.IsUsed -eq $true) {
# We already checked this package
return;
}
$match = $xml.Project.ItemGroup.PackageReference.Include | ?{ $_ -eq $packageName }
if ($match) {
# Mark package as used
#ReportInfo -message "'$packageName' found in $projFile."
$_.IsUsed = $true
}
}
}
}
$unsedPackages = $declaredPackages | Where-Object { $_.IsUsed -eq $false };
if (!$unsedPackages) {
ReportInfo 'No unused packages detected. All good.'
exit 0
}
Write-Host "`r`n"
ReportWarning "The following unused packages detected and will be removed:"
$unsedPackages
[xml]$xmlVersionDetailsXml = Get-Content $versionDetailsXmlPath;
[xml]$xmlVersionProps = Get-Content $versionPropsPath;
$unsedPackages | `
ForEach-Object {
$packageName = $_.PackageName;
$packageVersion = $_.PackageVersion.Replace('$(', '').Replace(')', '');
$node = $xmlVersionDetailsXml.SelectSingleNode("//*[local-name()='Dependencies']/*[local-name()='ProductDependencies']/*[local-name()='Dependency'][@Name='$packageName']")
if ($node) {
$node.ParentNode.RemoveChild($node) | Out-Null
}
$node = $xmlVersionProps.SelectSingleNode("//*[local-name()='PropertyGroup']/*[local-name()='$packageVersion']")
if ($node) {
$node.ParentNode.RemoveChild($node) | Out-Null
}
$node = $packagesPropsXml.SelectSingleNode("//*[local-name()='PackageVersion'][@Include='$packageName']")
if ($node) {
$node.ParentNode.RemoveChild($node) | Out-Null
}
}
$xmlVersionDetailsXml.Save($versionDetailsXmlPath)
$xmlVersionProps.Save($versionPropsPath)
$packagesPropsXml.Save($PackagesPropsFilePath)
(Get-Content $versionDetailsXmlPath) -replace "`n", "`r`n" | Set-Content -Force $versionDetailsXmlPath
(Get-Content $versionPropsPath) -replace "`n", "`r`n" | Set-Content -Force $versionPropsPath
(Get-Content $PackagesPropsFilePath) -replace "`n", "`r`n" | Set-Content -Force $PackagesPropsFilePath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment