Skip to content

Instantly share code, notes, and snippets.

@hallojoe
Last active October 7, 2019 14:32
Show Gist options
  • Save hallojoe/316fb003cc1d0bcff329d475ebfb3a8f to your computer and use it in GitHub Desktop.
Save hallojoe/316fb003cc1d0bcff329d475ebfb3a8f to your computer and use it in GitHub Desktop.
Powershell script that will pack and push .nupkg
# Nuget packaging thing
#
# Use this guy to pack and push .nupkg from your .csproj
#
# Note: nuget.exe pack will assume a .nuspec file already exist
# if not then create one yourself: nuget.exe spec.
# This is not done automatically, as you probably wan't to
# customize this your self, before running this.
#
# This will check if the assembly version on given project
# has changed, and if so it will re-pack the project and push
# it to given gallery
#
# To use it, make sure nuget.exe is in your csproj root and
# add this post build event to your csproj:
#
# powershell -ExecutionPolicy Unrestricted $(ProjectDir)nuget.ps1 -ProjectDir $(ProjectDir) -TargetDir $(TargetDir) -ApiKey YOUR_SECRET_API_KEY -Server http://YOUR_NUGET_GALLERY.com
# enjoy!
Param(
[Parameter()][string]$ProjectDir,
[Parameter()][string]$TargetDir,
[Parameter()][string]$ApiKey,
[Parameter()][string]$Server
)
if($TargetDir -match "debug") {
Write-Host "debug build - exit"
exit
}
Set-Location($ProjectDir)
$csproj = Get-ChildItem $ProjectDir -Filter "*.csproj" | sort LastWriteTime | Select -last 1
if($csproj -eq $null) {
Write-Host "no .csproj file found in $($ProjectDir)"
exit
}
$csprojPath = "$($ProjectDir)$($csproj.Name)"
$dll = Get-ChildItem $TargetDir -Filter "*.dll" | sort LastWriteTime | Select -last 1
if($dll -eq $null) {
Write-Host "no .dll file found in $($ProjectDir)"
exit
}
$dllPath = "$($TargetDir)\$($dll.Name)"
$dllVersion = [Reflection.Assembly]::Loadfile($dllPath).GetName().Version
$nupkg = Get-ChildItem $ProjectDir -Filter "*.nupkg" | sort LastWriteTime | Select -last 1
$nupkgPath = "$($ProjectDir)$($nupkg.Name)"
if($nupkg -and $nupkgPath) {
Write-Host "Found $($nupkg)"
if($dllVersion -ne $null -and $dllVersion -ne $nupkgVersion) {
Write-Host "Version changed dll: $($dllVersion) nupkg: $($nupkgVersion)"
Write-Host "Re-packing $($csprojPath)"
& "$($ProjectDir)nuget.exe" pack $csprojPath -p "configuration=release"
$nupkg = Get-ChildItem $ProjectDir -Filter "*.nupkg" | sort LastWriteTime | Select -last 1
$nupkgPath = "$($ProjectDir)$($nupkg.Name)"
Write-Host "Pushing $($nupkg.Name) to $($Server)"
& "$($ProjectDir)nuget.exe" push $nupkgPath -ApiKey $ApiKey -Source $server
}
Write-Host "Finished"
exit
}
Write-Host "No nupkg found"
Write-Host "Packing $($csprojPath)"
& "$($ProjectDir)nuget.exe" pack $csprojPath -p "configuration=release"
$nupkg = Get-ChildItem $ProjectDir -Filter "*.nupkg" | sort LastWriteTime | Select -last 1
$nupkgPath = "$($ProjectDir)$($nupkg.Name)"
Write-Host "Pushing $($nupkg.Name) to $($Server)"
Write-Host $nupkg.Name
& "$($ProjectDir)nuget.exe" push $nupkgPath -ApiKey $ApiKey -Source $server
Write-Host "Finished"
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment