所以,我有一个版本列表,如下所示:
v1.1.0
v1.2.0
v1.3.0
v1.4.0
v1.5.0
v1.7.0
v1.8.0
v1.9.0
v2.0.0
v2.1.0
v2.10.0
v2.11.0
v2.12.0
v2.2.0
v2.3.0
v2.4.0
v2.5.0
v2.6.0
v2.7.0
v2.8.0
v2.9.0
问题是,它们的顺序不正确。
我是 Powershell 新手,因此在尝试对它们进行排序时遇到了一些问题。
我尝试这样做:
$tags = git tag
$versions = $tags | %{ new-object System.Version ($_) } | sort
但我收到这个错误:
new-object :使用“1”个参数调用“.ctor”时出现异常:“版本字符串部分太短或太长。”
行:1 字符:24
+ $版本= $标签| %{ 新对象 System.Version ($_) } |种类
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object],MethodInitationException
+FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
有人可以帮忙吗?
Update
我使用了如下解决方案之一:
$location = Get-Location
$path = $location.tostring() + "\CHANGELOG.md"
$tags = git tag
$i = 0
Clear-Content $path
Add-Content $path "Change Log"
Add-Content $path "=========="
Add-Content $path " "
$ToNatural = { [regex]::Replace($_, '\d+', { $args[0].Value.PadLeft(20) }) }
$tags | Sort-Object $ToNatural
foreach($tag in $tags)
{
if (-NOT ($tag -match "v(\d+\.)(\d+\.)(\*|\d+)")) { continue }
$i = $i + 1
if ($i -eq 0) { continue }
$tag
If ($i -gt 0) {
$previous = $tags[$i - 1]
Add-Content $path " "
}
}
这种方法可行,但所有标签似乎都已控制台记录,并且显示如下:
1.6.0
changeDeliveryFieldAccess
orders/autoComplete
returns/autoComplete
save-lines-dates
services/serviceDetails
tile-colours
users/confirmation
v0.1
v1.1.0
v1.2.0
v1.3.0
v1.4.0
v1.5.0
v1.7.0
v1.8.0
v1.9.0
v2.0.0
v2.1.0
v2.2.0
v2.3.0
v2.4.0
v2.5.0
v2.6.0
v2.7.0
v2.8.0
v2.9.0
v2.10.0
v2.11.0
v2.12.0
v.2.7.1
正如你所看到的,其中有一些我不想要的。具体来说:
1.6.0
changeDeliveryFieldAccess
orders/autoComplete
returns/autoComplete
save-lines-dates
services/serviceDetails
tile-colours
users/confirmation
v.2.7.1
一旦这些从我的列表中清除,那么顺序就会正确:)
Update 2
所以我尝试了另一种希望更好的解决方案:
$location = 获取位置
$path = $location.tostring() + "\CHANGELOG.md"
$tags = git 标签
$i = 0
Clear-Content $path
Add-Content $path "#Change Log"
Add-Content $path "=========="
Add-Content $path " "
$tags |
Where-Object { $_.Substring(1) -as [version] } |
Sort-Object { [version] $_.Substring(1) }
foreach($tag in $tags) {
write-host "$($tag) is ok"
}
我不确定我这样做是否正确,但这是上面代码的输出:
1.6.0 is ok
changeDeliveryFieldAccess is ok
orders/autoComplete is ok
returns/autoComplete is ok
save-lines-dates is ok
services/serviceDetails is ok
tile-colours is ok
users/confirmation is ok
v.2.7.1 is ok
v0.1 is ok
v1.1.0 is ok
v1.2.0 is ok
v1.3.0 is ok
v1.4.0 is ok
v1.5.0 is ok
v1.7.0 is ok
v1.8.0 is ok
v1.9.0 is ok
v2.0.0 is ok
v2.1.0 is ok
v2.10.0 is ok
v2.11.0 is ok
v2.12.0 is ok
v2.2.0 is ok
v2.3.0 is ok
v2.4.0 is ok
v2.5.0 is ok
v2.6.0 is ok
v2.7.0 is ok
v2.8.0 is ok
v2.9.0 is ok