Reputation: 2627
I was working on a C# library project and i want to create a Nuget package out of it. I'm using this command for that
dotnet pack /p:Version=2.0.1 --include-symbols
It is working fine when the version is handled properly.
But there can be situation where i have to create a nuget package with same version as before [ to replace the 2.0.1 ] when last one was a mistake.
Right now when i try above command it is not doing anything if package with same version already exists?
Is there a way where i can replace the existing package without deleting it manually?
Upvotes: 0
Views: 234
Reputation: 2627
I tried a lot of things, and ended up doing as below.
Wrote a console application which will update the .csproj version part of the library project then build the library project with the help of powershell script.
class Program
{
private static string csProjectPath => ConfigurationManager.AppSettings["projectPath"] + ConfigurationManager.AppSettings["projectName"];
private static string projectPath => ConfigurationManager.AppSettings["projectPath"];
static void Main(string[] args)
{
try
{
var allowedTypes = new List<string> { "1", "2", "3", "4" };
Console.WriteLine($"Build Type: {Environment.NewLine} 1. Major {Environment.NewLine} 2. Minor {Environment.NewLine} 3. Build Number {Environment.NewLine} 4. Revision ");
var buildType = Console.ReadLine();
while (!allowedTypes.Contains(buildType))
{
Console.WriteLine("Please enter a valid entry");
Console.WriteLine($"Build Type: {Environment.NewLine} 1. Major {Environment.NewLine} 2. Minor {Environment.NewLine} 3. Build Number {Environment.NewLine} 4. Revision ");
buildType = Console.ReadLine();
}
RunPowershellScript(buildType);
}
catch (Exception ex)
{
throw ex;
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
public static string GetCSProjectUpdateCommand(string buildType)
{
return @"
$csprojPath = $args[0]
$buildType = $args[1]
$filePath = $csprojPath
$xml = New-Object XML
$xml.Load($filePath)
$versionNode = $xml.Project.PropertyGroup.Version
$currentSplit = $versionNode.Split('.')
$currentRevisionNumber = 0
$myBuildNumber = ''
$splitNumber = $versionNode.Split('.')
$majorNumber = $splitNumber[0]
$minorNumber = $splitNumber[1]
$buildNumber = $splitNumber[2]
$revisNumber = $splitNumber[3]
Switch($buildType)
{
'1' { $myBuildNumber = ([int]$majorNumber + 1) + '.0.0.0'; break }
'2' { $myBuildNumber = $majorNumber + '.' + ([int]$minorNumber + 1) + '.0.0'; break }
'3' { $myBuildNumber = $majorNumber + '.' + $minorNumber + '.' + ([int]$buildNumber + 1) +'.0'; break }
'4' { $myBuildNumber = $majorNumber + '.' + $minorNumber + '.' + $buildNumber + '.' + ([int]$updatedRevisionNumber + 1); break}
default { Write - Host 'Invalid data' }
}
if ($versionNode -eq $null) {
$versionNode = $xml.CreateElement('Version')
$xml.Project.PropertyGroup.AppendChild($versionNode)
}
$xml.Project.PropertyGroup.Version = $myBuildNumber
$xml.Save($filePath)";
}
private static void RunPowershellScript(string buildType)
{
UpdateCSProject(buildType);
BuildProject();
}
private static void UpdateCSProject(string buildType)
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript(GetCSProjectUpdateCommand(buildType));
PowerShellInstance.AddArgument(csProjectPath);
PowerShellInstance.AddArgument(buildType);
IAsyncResult result = PowerShellInstance.BeginInvoke();
while (result.IsCompleted == false)
{
Console.WriteLine("Updating csproj...");
Thread.Sleep(1000);
}
Console.WriteLine("Update completed!");
}
}
private static void BuildProject()
{
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript($"dotnet pack {projectPath} --include-symbols");
PowerShellInstance.AddArgument(projectPath);
IAsyncResult result = PowerShellInstance.BeginInvoke();
while (result.IsCompleted == false)
{
Console.WriteLine("Please wait, we are building the project...");
Thread.Sleep(1000);
}
Console.WriteLine("Build completed!. Nuget package created.");
}
}
}
}
Upvotes: 0