Skip to content

Instantly share code, notes, and snippets.

@lkaczanowski
Last active August 29, 2015 14:22
Show Gist options
  • Save lkaczanowski/308296339d502c29aab6 to your computer and use it in GitHub Desktop.
Save lkaczanowski/308296339d502c29aab6 to your computer and use it in GitHub Desktop.
Nuget Powershell Helpers
Register-TabExpansion 'Get-SolutionPackages' @{
'PackageName' = { Get-Package | Sort-Object -Property Id -Unique | foreach { $_.Id } }
}
Register-TabExpansion 'Refresh-Package' @{
'PackageName' = { Get-Package | Sort-Object -Property Id -Unique | foreach { $_.Id } }
}
function Refresh-Package($PackageName) {
$solutionFile = Get-ChildItem $dte.Solution.FullName
$projectMainPath = Split-Path -Path $solutionFile.DirectoryName -Parent
$packagesPath = Join-Path $solutionFile.DirectoryName "\packages"
### 1. Remove packages
if($PackageName) {
Write-Host "Removing package from packages folder."
## this regex will find all folders with different versions for given package
$packageFolderRegex = $PackageName + ".\d"
$packageFoldersToRemove = Get-ChildItem $packagePath | Where-Object { $_.Name -match $packageFolderRegex }
$packageFoldersToRemove | ForEach-Object { Remove-Item $_.FullName -Force -Recurse }
}
else {
Write-Host "Removing all packages from packages folder."
Remove-Item ($packagesPath + "\*") -Force -Recurse
}
### 2. Restore packages
$nugetExeFullPath = Join-Path $projectMainPath "libs/nuget/nuget.exe"
$nugetExeExists = Test-Path ($nugetExeFullPath)
## setting this to 1 means that we assume running nuget.exe failed
$nugetExeExitCode = 1
## lets try to run nuget.exe first to restore packages
if($nugetExeExists) {
Write-Host "Trying to restore package using 'nuget restore' command."
& $nugetExeFullPath restore -NoCache
$nugetExeExitCode = $LastExitCode
}
## if running nuget.exe was not successfull lets try to restore packages by clearing cache and building solution
if ($nugetExeExitCode -ne 0) {
Write-Host "Restoring packages by clearing cache and building solution."
Clear-NugetCache
$dte.Solution.SolutionBuild.Build($true)
}
Write-Host "Done!"
}
function Get-SolutionPackages($PackageName) {
$nl = [Environment]::NewLine
$packageSummaryList = @()
Get-Project -All | ForEach-Object {
$projectName = $_.ProjectName
Get-Package -ProjectName $projectName | ForEach-Object {
$packageSummary = New-Object System.Object
$packageSummary | Add-Member -type NoteProperty -name Id -value $_.Id
$packageSummary | Add-Member -type NoteProperty -name Version -value $_.Version
$packageSummary | Add-Member -type NoteProperty -name Project -value $projectName
$packageSummaryList += $packageSummary
}
}
if(!$packageName) {
$packageSummaryList | Sort-Object -Property Id, Version
}
else {
$packageSummaryList | Where-Object { $_.Id -eq $PackageName } | Sort-Object -Property Id, Version
}
}
function Get-DesynchronizedPackages {
$nl = [Environment]::NewLine
$packageSummaryList = @()
Get-Project -All | ForEach-Object {
$projectName = $_.ProjectName
Get-Package -ProjectName $projectName | ForEach-Object {
$packageSummary = New-Object System.Object
$packageSummary | Add-Member -type NoteProperty -name Id -value $_.Id
$packageSummary | Add-Member -type NoteProperty -name Version -value $_.Version
$packageSummary | Add-Member -type NoteProperty -name Project -value $projectName
$packageSummaryList += $packageSummary
}
}
$packageSummaryList | Sort-Object -Property Id, Version -Unique | Group-Object Id | Where-Object { $_.Count -gt 1 } | Select-Object -ExpandProperty Group
}
function Add-SolutionBindingRedirect{
Add-BindingRedirect *
foreach ($proj in get-project -all) {
##write-host “Project $($proj.Name) is in this solution.”
$dir = split-path $proj.FullName -parent
##write-host "Path: $($dir)"
$foundConfigs = Get-childitem -path $dir -Include *.config -Exclude packages.config -Recurse
if ($foundConfigs.count -gt 0) {
foreach ($configPath in $foundConfigs){
if ($configPath.Name.EndsWith(".template.config")) {
continue
}
#write-host "Found config: $($configPath)"
$configFileName = [io.path]::GetFileNameWithoutExtension("$($configPath)")
$configPathParent = Split-Path $configPath -parent
$configTemplatePath = "$($configPathParent)\$($configFileName).template.config"
#write-host "Searching for template like: [$($configTemplatePath)]"
$templateExists = Test-Path -Path $configTemplatePath -pathtype leaf
if ($templateExists){
[Xml]$configFileContent = Get-Content -Path $configPath
$runtimeXpath = "//configuration/runtime"
$configRuntimeNode = $configFileContent.SelectSingleNode($runtimeXpath)
if ($configRuntimeNode -and $configRuntimeNode.assemblyBinding){
write-host "Found config ["$configPath"] with binding redirects. Will rewrite binding redirect node to file ["$configTemplatePath"]"
[Xml]$templateConfigFileContent = Get-Content -Path $configTemplatePath
$templateConfigRuntimeNode = $templateConfigFileContent.SelectSingleNode($runtimeXpath)
if (-not $templateConfigRuntimeNode){
$templateConfigRuntimeNode = $templateConfigFileContent.CreateNode("element","runtime", "")
[void]($templateConfigFileContent.configuration.AppendChild($templateConfigRuntimeNode))
}
if (-not $templateConfigRuntimeNode.assemblyBinding){
write-host "Adding assemblybinding in template."
$importedNode = $templateConfigRuntimeNode.OwnerDocument.ImportNode($configRuntimeNode.assemblyBinding, $true)
[void]($templateConfigRuntimeNode.AppendChild($importedNode))
}
else{
write-host "Replacing assemblybinding in template."
$importedNode2 = $templateConfigRuntimeNode.OwnerDocument.ImportNode($configRuntimeNode.assemblyBinding, $true)
[void]($templateConfigRuntimeNode.RemoveChild($templateConfigRuntimeNode.assemblyBinding))
[void]($templateConfigRuntimeNode.AppendChild($importedNode2))
}
[void]($templateConfigFileContent.Save($configTemplatePath))
}
}
}
}
}
}
function Clear-NugetCache {
$nugetCachePath = $env:LOCALAPPDATA + "\Nuget\Cache"
$nugetCacheFiles = $nugetCachePath + "\*"
Write-Host "Removing cached packages from:" $nugetCachePath
Remove-Item $nugetCacheFiles
}
function Sync-References([string]$PackageId) {
$nl = [Environment]::NewLine
Get-Project -All | %{
$proj = $_ ;
Write-Host $nl "============================== " $proj.Name " ==============================" $nl
Get-Package -Project $proj.Name | ? { $_.Id -eq $PackageId } | % {
Uninstall-Package -ProjectName $proj.Name -Id $_.Id -Version $_.Version -RemoveDependencies -Force;
Install-Package -Projectname $proj.name -Id $_.id -version $_.version
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment