我正在尝试更新所有NuGet包以获取VS Code中的解决方案(使用Mac)。有没有办法在VS代码或特定的project.json文件中实现?目前我一个接一个地去,但我原本以为有一个扩展或功能为你做这个?
这是一个shell脚本和一个PowerShell脚本,可以执行此操作
#!/bin/bash
regex='PackageReference Include="([^"]*)" Version="([^"]*)"'
find . -name "*.*proj" | while read proj
do
while read line
do
if [[ $line =~ $regex ]]
then
name="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
if [[ $version != *-* ]]
then
dotnet add $proj package $name
fi
fi
done < $proj
done
$regex = [regex] 'PackageReference Include="([^"]*)" Version="([^"]*)"'
ForEach ($file in get-childitem . -recurse | where {$_.extension -like "*proj"})
{
$proj = $file.fullname
$content = Get-Content $proj
$match = $regex.Match($content)
if ($match.Success) {
$name = $match.Groups[1].Value
$version = $match.Groups[2].Value
if ($version -notin "-") {
iex "dotnet add $proj package $name"
}
}
}
还应该提到Paket是一个支持更新的出色的替代包管理器:
https://fsprojects.github.io/Paket/index.html
dotnet tool install paket --tool-path .paket
基于Jon Canning的powershell解决方案。我修复了一个小错误,其中只更新了第一个依赖项,而不是项目文件的所有依赖项。
$regex = 'PackageReference Include="([^"]*)" Version="([^"]*)"'
ForEach ($file in get-childitem . -recurse | where {$_.extension -like "*proj"})
{
$packages = Get-Content $file.FullName |
select-string -pattern $regex -AllMatches |
ForEach-Object {$_.Matches} |
ForEach-Object {$_.Groups[1].Value.ToString()}|
sort -Unique
ForEach ($package in $packages)
{
write-host "Update $file package :$package" -foreground 'magenta'
$fullName = $file.FullName
iex "dotnet add $fullName package $package"
}
}
基于Jon Caning的回答,我写了这个小的bash脚本来添加.bashrc(或者只是改变一下以将其保存在bash文件中)
function read_solution() {
echo "Parsing solution $1"
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ \"([^\"]*.csproj)\" ]]; then
project="${BASH_REMATCH[1]}"
read_project "$(echo "$project"|tr '\\' '/')"
fi
done < "$1"
}
function read_project() {
echo "Parsing project $1"
package_regex='PackageReference Include="([^"]*)" Version="([^"]*)"'
while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ $line =~ $package_regex ]]; then
name="${BASH_REMATCH[1]}"
version="${BASH_REMATCH[2]}"
if [[ $version != *-* ]]; then
dotnet add "$1" package "$name"
fi
fi
done < $1
}
function dotnet_update_packages() {
has_read=0
if [[ $1 =~ \.sln$ ]]; then
read_solution "$1"
return 0
elif [[ $1 =~ \.csproj$ ]]; then
read_project "$1"
return 0
elif [[ $1 != "" ]]; then
echo "Invalid file $1"
return 1
fi
for solution in ./*.sln; do
if [ ! -f ${solution} ]; then
continue
fi
read_solution "${solution}"
has_read=1
done
if [[ $has_read -eq 1 ]]; then
return 0
fi
for project in ./*.csproj; do
if [ ! -f ${project} ]; then
continue
fi
read_project "${project}"
done
}
export -f dotnet_update_packages
要使用它,要么在没有参数的情况下在带有解决方案的文件夹中运行它,它将首先查找当前文件夹中的所有解决方案文件并运行那些引用的所有csproj文件(如果您使用其他内容,可能需要更改它们比c#)。
如果找不到解决方案,它会查找当前目录中的所有csproj文件并运行这些文件。
您还可以传递.sln或.csproj文件作为参数:
dotnet_update_packages mysolution.sln
dotnet_update_packages myproject.csproj
我不是bash专家所以我相信它可以改进
我创建了一个蛋糕构建任务来做同样的事情。见下文:
Task("Nuget-Update")
.Does(() =>
{
var files = GetFiles("./**/*.csproj");
foreach(var file in files)
{
var content = System.IO.File.ReadAllText(file.FullPath);
var matches = System.Text.RegularExpressions.Regex.Matches(content, @"PackageReference Include=""([^""]*)"" Version=""([^""]*)""");
Information($"Updating {matches.Count} reference(s) from {file.GetFilename()}");
foreach (System.Text.RegularExpressions.Match match in matches) {
var packageName = match.Groups[1].Value;
Information($" Updating package {packageName}");
var exitCode = StartProcess("cmd.exe",
new ProcessSettings {
Arguments = new ProcessArgumentBuilder()
.Append("/C")
.Append("dotnet")
.Append("add")
.Append(file.FullPath)
.Append("package")
.Append(packageName)
}
);
}
}
});
您可以尝试NuGet Package Manager for VS Code:
https://marketplace.visualstudio.com/items?itemName=jmrog.vscode-nuget-package-manager