Skip to content

Instantly share code, notes, and snippets.

@Fireforge
Last active December 16, 2016 22:17
Show Gist options
  • Save Fireforge/dc295f23d35731bb95f0bff52fd15699 to your computer and use it in GitHub Desktop.
Save Fireforge/dc295f23d35731bb95f0bff52fd15699 to your computer and use it in GitHub Desktop.
Nuget push script, more advanced
#
# push.ps1
#
# Push all Nuget packages to Artifactory if the package version has been updated. Checks to make sure that there was not a
# previous push of the same version using Git tags.
##### USER PARAMETERS
param (
[string[]]$csproj_list = $(gci *.csproj),
[string]$source = "artifactory-vt",
[string]$nugetconfig = "../nuget.config",
[switch]$force = $false
)
Set-Location -Path $PSScriptRoot
$ErrorActionPreference = "Stop"
##### CHECK DEPENDENCIES AND VERIFY CONFIGURATION
function EnsureCommand($cmdname)
{
if(-not (Get-Command -Name $cmdname -ErrorAction SilentlyContinue))
{
write-host "ERROR: cannot find command ""$cmdName"". Make sure this utility is installed and it's location is in the PATH." -foregroundcolor Red
exit
}
}
EnsureCommand "nuget" # we need nuget to pack and push the library
EnsureCommand "git" # we need git to check and add tags for the version
$msbuild = "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
if(!(test-path $msbuild))
{
write-host "ERROR: cannot find MSBuild 14.0 ($msbuild). Make sure you have Visual Studio installed." -foregroundcolor Red
exit
}
if(!(test-path $nugetconfig))
{
write-host "ERROR: Nuget Config File '$nugetconfig' does not exist." -foregroundcolor Red
exit
}
# TODO Check that nuget config contians the given $source
##### BEGIN PUSHING
# Get the name and apikey information to connect to the Nuget package server
$name = Read-Host 'username'
$pass = Read-Host 'password' -AsSecureString
$apikey = "$($name):$([Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)))"
# TODO Do an early check to make sure that the username and password given actually work. Otherwise you won't find out until a project attempts a push
# Loop through and push all .csproj files found
foreach ($proj in $csproj_list)
{
write-host "#####################################"
$projname = [io.path]::GetFileNameWithoutExtension($proj)
write-host "Pushing $projname..."
# Check for a nuspec file
$projdir = [io.path]::GetDirectoryName($proj)
$nuspec = "$projdir/$projname.nuspec"
if(![System.IO.File]::Exists($nuspec))
{
write-host "Skipping $($projname): Nuspec file $nuspec is missing" -foregroundcolor Red
continue
}
# Get version information from the nuspec file and create the tag from it
[xml]$XmlDocument = Get-Content -Path $nuspec
$version = $XmlDocument.package.metadata.version
$tag = "releases/$($projname)/$version"
$tag_message = "'$projname Nuget Version $version. Pushed to: $source'"
# make a git call to tag this commit with a version number. Skip project if it already exists
# TODO add a --force parameter to override this behavior
$prevtags = git tag -l $tag
if (!($force) -and !([string]::IsNullOrEmpty($prevtags)))
{
write-host "Skipping $($projname): The tag $tag already exists! To push out a new build, please change the version number in $nuspec" -foregroundcolor Red
continue
}
# Build, Package, Push, and Tag
rm *.nupkg # Ensure no previous nuget packages are still around
.$msbuild $proj /p:Configuration=Release /v:q
nuget pack $proj -Properties Configuration=Release
$nupkg = ls *.nupkg # This will match any version we produce
# TODO nuget.config needs to be a setting that is confirmed to exist
nuget push $nupkg -source $source -config $nugetconfig -apikey $apikey
git tag -a $tag -m $tag_message -f
rm $nupkg
write-host "Done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment