nuget-manager

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

NuGet Manager

NuGet包管理器

Overview

概述

This skill ensures consistent and safe management of NuGet packages across .NET projects. It prioritizes using the
dotnet
CLI to maintain project integrity and enforces a strict verification and restoration workflow for version updates.
此技能确保在.NET项目中一致且安全地管理NuGet包。它优先使用
dotnet
CLI来维护项目完整性,并为版本更新强制执行严格的验证和还原工作流。

Prerequisites

前提条件

  • .NET SDK installed (typically .NET 8.0 SDK or later, or a version compatible with the target solution).
  • dotnet
    CLI available on your
    PATH
    .
  • jq
    (JSON processor) OR PowerShell (for version verification using
    dotnet package search
    ).
  • 已安装.NET SDK(通常为.NET 8.0 SDK或更高版本,或与目标解决方案兼容的版本)。
  • dotnet
    CLI已添加到您的
    PATH
    中。
  • jq
    (JSON处理器)或PowerShell(用于通过
    dotnet package search
    进行版本验证)。

Core Rules

核心规则

  1. NEVER directly edit
    .csproj
    ,
    .props
    , or
    Directory.Packages.props
    files to add or remove packages. Always use
    dotnet add package
    and
    dotnet remove package
    commands.
  2. DIRECT EDITING is ONLY permitted for changing versions of existing packages.
  3. VERSION UPDATES must follow the mandatory workflow:
    • Verify the target version exists on NuGet.
    • Determine if versions are managed per-project (
      .csproj
      ) or centrally (
      Directory.Packages.props
      ).
    • Update the version string in the appropriate file.
    • Immediately run
      dotnet restore
      to verify compatibility.
  1. 绝不直接编辑
    .csproj
    .props
    Directory.Packages.props
    文件来添加移除包。始终使用
    dotnet add package
    dotnet remove package
    命令。
  2. 直接编辑仅允许用于更改现有包的版本
  3. 版本更新必须遵循强制工作流:
    • 验证目标版本在NuGet上是否存在。
    • 确定版本是按项目(
      .csproj
      )管理还是集中管理(
      Directory.Packages.props
      )。
    • 在相应文件中更新版本字符串。
    • 立即运行
      dotnet restore
      以验证兼容性。

Workflows

工作流

Adding a Package

添加包

Use
dotnet add [<PROJECT>] package <PACKAGE_NAME> [--version <VERSION>]
. Example:
dotnet add src/MyProject/MyProject.csproj package Newtonsoft.Json
使用
dotnet add [<PROJECT>] package <PACKAGE_NAME> [--version <VERSION>]
。 示例:
dotnet add src/MyProject/MyProject.csproj package Newtonsoft.Json

Removing a Package

移除包

Use
dotnet remove [<PROJECT>] package <PACKAGE_NAME>
. Example:
dotnet remove src/MyProject/MyProject.csproj package Newtonsoft.Json
使用
dotnet remove [<PROJECT>] package <PACKAGE_NAME>
。 示例:
dotnet remove src/MyProject/MyProject.csproj package Newtonsoft.Json

Updating Package Versions

更新包版本

When updating a version, follow these steps:
  1. Verify Version Existence: Check if the version exists using the
    dotnet package search
    command with exact match and JSON formatting. Using
    jq
    :
    dotnet package search <PACKAGE_NAME> --exact-match --format json | jq -e '.searchResult[].packages[] | select(.version == "<VERSION>")'
    Using PowerShell:
    (dotnet package search <PACKAGE_NAME> --exact-match --format json | ConvertFrom-Json).searchResult.packages | Where-Object { $_.version -eq "<VERSION>" }
  2. Determine Version Management:
    • Search for
      Directory.Packages.props
      in the solution root. If present, versions should be managed there via
      <PackageVersion Include="Package.Name" Version="1.2.3" />
      .
    • If absent, check individual
      .csproj
      files for
      <PackageReference Include="Package.Name" Version="1.2.3" />
      .
  3. Apply Changes: Modify the identified file with the new version string.
  4. Verify Stability: Run
    dotnet restore
    on the project or solution. If errors occur, revert the change and investigate.
更新版本时,请遵循以下步骤:
  1. 验证版本存在性: 使用带精确匹配和JSON格式的
    dotnet package search
    命令检查版本是否存在。 使用
    jq
    dotnet package search <PACKAGE_NAME> --exact-match --format json | jq -e '.searchResult[].packages[] | select(.version == "<VERSION>")'
    使用PowerShell:
    (dotnet package search <PACKAGE_NAME> --exact-match --format json | ConvertFrom-Json).searchResult.packages | Where-Object { $_.version -eq "<VERSION>" }
  2. 确定版本管理方式
    • 在解决方案根目录中搜索
      Directory.Packages.props
      。如果存在,版本应通过
      <PackageVersion Include="Package.Name" Version="1.2.3" />
      在其中管理。
    • 如果不存在,检查各个
      .csproj
      文件中的
      <PackageReference Include="Package.Name" Version="1.2.3" />
  3. 应用更改: 修改已识别的文件,更新版本字符串。
  4. 验证稳定性: 在项目或解决方案上运行
    dotnet restore
    。如果出现错误,还原更改并进行调查。

Examples

示例

User: "Add Serilog to the WebApi project"

用户:"将Serilog添加到WebApi项目"

Action: Execute
dotnet add src/WebApi/WebApi.csproj package Serilog
.
操作:执行
dotnet add src/WebApi/WebApi.csproj package Serilog

User: "Update Newtonsoft.Json to 13.0.3 in the whole solution"

用户:"将整个解决方案中的Newtonsoft.Json更新到13.0.3"

Action:
  1. Verify 13.0.3 exists:
    dotnet package search Newtonsoft.Json --exact-match --format json
    (and parse output to confirm "13.0.3" is present).
  2. Find where it's defined (e.g.,
    Directory.Packages.props
    ).
  3. Edit the file to update the version.
  4. Run
    dotnet restore
    .
操作
  1. 验证13.0.3是否存在:
    dotnet package search Newtonsoft.Json --exact-match --format json
    (并解析输出以确认"13.0.3"存在)。
  2. 查找其定义位置(例如
    Directory.Packages.props
    )。
  3. 编辑文件以更新版本。
  4. 运行
    dotnet restore