即使已存在相同版本,也创建新的 Nuget 包
Create new Nuget package even if same version already exist
我正在处理一个 C# 库项目,我想从中创建一个 Nuget 包。我为此使用此命令
dotnet pack /p:Version=2.0.1 --include-symbols
只要版本处理得当就可以正常工作
但有时我必须创建一个与以前版本相同的 nuget 包 [以替换 2.0.1],而最后一个是错误的。
现在当我尝试上面的命令时,如果相同版本的包已经存在,它没有做任何事情?
有什么方法可以在不手动删除的情况下替换现有的包吗?
我尝试了很多东西,最后做了如下。
编写了一个控制台应用程序,它将更新库项目的 .csproj 版本部分,然后在 powershell 脚本的帮助下构建库项目。
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.");
}
}
}
}
我正在处理一个 C# 库项目,我想从中创建一个 Nuget 包。我为此使用此命令
dotnet pack /p:Version=2.0.1 --include-symbols
只要版本处理得当就可以正常工作
但有时我必须创建一个与以前版本相同的 nuget 包 [以替换 2.0.1],而最后一个是错误的。
现在当我尝试上面的命令时,如果相同版本的包已经存在,它没有做任何事情?
有什么方法可以在不手动删除的情况下替换现有的包吗?
我尝试了很多东西,最后做了如下。
编写了一个控制台应用程序,它将更新库项目的 .csproj 版本部分,然后在 powershell 脚本的帮助下构建库项目。
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.");
}
}
}
}