
Intro
This will automate a Nuget update to your personal/company NuGet library for any projects you build as “Release” This is useful for any personal/private libraries you maintain. This basically automates the process here:
Consider using with a version automation script such as HERE
Directions around how to get this set up are at the top of the script.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
::Error Thrown by PushToNuget script @echo off setlocal ENABLEDELAYEDEXPANSION :: NuGet Auto-publish on Build :: by stigzler, MagoArcade.org :: For use with .net Framework, .net and .net core projects :: FOR .NET AND .NET CORE architecture: you will need to ensure the following: :: Project for packaging has the file [Project]\Properties\AssemblyInfo.cs :: Above can be auto-created: Create Properties folder. Right Click>Add New Item>Assembly Information File :: in the .csproj file, ensure following is extant: :: <GenerateAssemblyInfo>false</GenerateAssemblyInfo> :: Your AssemblyInfo.cs file should contain the two lines below (with your desired starting version in AssemblyVersion): :: [assembly: AssemblyVersion("1.4.14.0")] :: [assembly: AssemblyFileVersion("1.4.14.0")] :: USAGE: First, install nuget.exe and add it to your Environment Path variable (guide here: https://docs.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-studio-net-framework) :: Save the Script below somewhere in your solution (I save it in a dir in the Solution folder called "BuildScripts") :: Add the below script to your projects Post-Build Events, amending the path as necessary (Project Properties>Build>Events): :: call $(ProjectDir)..\BuildScripts\PushToNuget.bat $(ProjectDir) $(AssemblyName) "$(ConfigurationName)" :: Enter your nuget API key and URL in user variables below :: If you wish to skip deployment for certain build configs, add them to the SkipConfigurationsCSV variable :: If you wish to clear the previous versions of the package, set ClearPreviousVersions to true :: ** Packages are built to the following location: [projectDir]\bin\[buildConfigName]\nupkg ** :: Whenever you want to publish to NuGet, just build the Release version. :: DO NOT FORGET TO INCREASE THE ASSEMBLY VERSION ON ANY NEW PUBLISHING, else you will get a conflict error. :: You must increase "AssemblyVersion" in the AssemblyInfo.cs file to publish a new version. Increasing AssemblyFileVersion will not be effective :: Consider implementing a Version number auto-increase script in the pre-build events. Try this here: :: https://magoarcade.org/automated-version-control-and-deployment-pre-and-post-build-scripts-for-visual-studio/ :: ================================================================================ :: USER VARIABLES: :: ================================================================================ set ApiKey=cc60155c645641dc801294bd7342c66e set NugetUrl=https://nuget.yoursite.org/nuget set SkipConfigurationsCSV=Debug,DeploymentDebug set ClearPreviousVersions=true set AdditionalPackArgs= :: ================================================================================ :: !!!!!!!!!!!!!!!!!!!! DO NOT EDIT BELOW THIS LINE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :: ================================================================================ echo ================================================================================================================ echo PUSH TO NUGET SCRIPT echo ================================================================================================================ set ProjectDir=%~1% set TargetName=%~2% set ConfigurationName=%~3 echo User Set Options: echo NugetUrl: [%NugetUrl%] echo Skip for Configurations: [%SkipConfigurationsCSV%] echo Clear Previous Versions: [%ClearPreviousVersions%] echo. echo Recieved parameters: echo Project Dir: [%ProjectDir%] echo Target Name: [%TargetName%] echo ConfigurationName: [%ConfigurationName%] echo. :: Skip for this Build Configuration? echo Considering whether this build configuration should be skipped: for %%a in ("%SkipConfigurationsCSV:,=" "%") do ( if [%%~a]==[%ConfigurationName%] ( echo This configuration set to skip in SkipConfigurationsCSV. Exiting.. goto EndScript ) ) echo This configuration OK to deploy. Attempting Deployment cd %ProjectDir% :: Get the Assembly Version set version=0.0.0.0 FOR /F delims^=^"^ tokens^=2 %%i in ('findstr /b /c:"[assembly: AssemblyVersion(" !ProjectDir!Properties\AssemblyInfo.cs') do ( set version=%%i ) echo AssemblyVersion in AssemblyInfo.cs: [%version%] :: Check if the version is 0.0.0 - if so - likely error - do not continue if [0.0.0.0]==[!version!] ( echo Present Assembly version at zero - likely error - exiting script goto EndScript ) :: Figure out the shortened version number in 0.0.0 format. If 0.0.0 - likely error - terminate. set concatVersion=0.0.0 for /F "tokens=1,2,3,4 delims=." %%a in ("!version!") do ( set concatVersion=%%a.%%b.%%c ) if [0.0.0.0]==[!concatVersion!] ( echo Package Path is 0.0.0 - likely error - exiting script goto EndScript ) :: Construct the directory path for the package: set packageDir=%ProjectDir%bin\%ConfigurationName%\nupkg echo Package Path set to: [%packageDir%] :: Clear previous versions if user set this if [%ClearPreviousVersions%]==[true] ( echo Clearing previous versions from Package Directory as per user setting RmDir /S /Q %packageDir% MkDir %packageDir% ) :: Attempt to Pack the project echo Attempting to Pack project in Dir: [%packageDir%] dotnet pack --no-build --output %packageDir% %AdditionalPackArgs% If %ERRORLEVEL% NEQ 0 ( Echo ERROR whilst Packing. Cannot continue. Ending. Goto EndScript ) set packageFile=%packageDir%\%TargetName%.%concatVersion%.nupkg echo Package File Made: [%packageFile%] echo Attempting to Push package to nuget nuget push %packageFile% -ApiKey %ApiKey% -Source %NugetUrl% If %ERRORLEVEL% NEQ 0 ( echo ERROR whilst Pushing package. A 409 is likely your forgetting to update the Version Number. ) else ( echo ---------------------------------------------------------------------------------------------------------------- echo *** NUGET PACKAGE UPLOADED *** \o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/ \o/ echo ---------------------------------------------------------------------------------------------------------------- ) :EndScript echo ================================================================================================================ echo PUSH TO NUGET SCRIPT FINISHED. echo ================================================================================================================ exit 0 |
Leave a Reply