【问题标题】:Is there an easy way to switch from local project references to nuget packages?有没有一种简单的方法可以从本地项目引用切换到 nuget 包?
【发布时间】:2014-04-26 10:09:33
【问题描述】:

在我当前的项目中,我们有几个独立的库,被 +20 个不同的系统使用。

最近我们决定开始使用 NuGet 管理我们的库...我们已经将所有库作为 nuget 包发布在内部 NuGet 服务器中,现在我们将开始迁移我们的 +20 应用程序以将这些库用作 nuget 包而不是本地项目参考文献。

是否有一种简单/自动化的方式来进行这些更改?

【问题讨论】:

    标签: .net nuget package-managers


    【解决方案1】:

    为您的项目中的包运行Install-Package 命令,引用将更新到 nuget 包目录(或您配置的任何目录)。

    如果您的包不仅仅是添加一个简单的引用(例如,ASP.NET MVC 包修改了 Web.config 文件),您将需要做一些撤消操作,但它通常是更新 .csproj 引用的一种相对轻松的方式.

    【讨论】:

      【解决方案2】:

      我曾经写过一个 PowerShell 脚本来做你想做的事(我想)。可能是你开始的地方。这必须从 Visual Studio 中的 PowerShell 控制台执行。两个参数分别是项目名和对应的包名。

      param (
          [parameter(Mandatory = $true)] [string] $referencedProjectName,
          [parameter(Mandatory = $true)] [string] $packageName
      )
      
      
      
      function Backup-File
      {
          param ([parameter(Mandatory = $true)][string] $file)
      
          try
          {
              if (!(Test-Path $file))
                  { throw "Could not find file $file" }
      
              $backup = $file + ".bak"
      
              if (Test-Path $backup)
                  { Remove-Item $backup -Force }
      
              Write-Host "Backing-up $file"
      
              Copy-Item -Path $file -Destination $backup
          }
      
          catch
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }
      }
      
      
      
      function Solution-Strip-Project-Reference
      {
          param (
              [parameter(Mandatory = $true)] [string] $solutionFile,
              [parameter(Mandatory = $true)] [string] $referencedProjectName
          )
      
          try
          {
              $regEx = '^Project\("\{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC\}"\)\s*=\s*"' + $referencedProjectName + '"'
      
              $content = Get-Content $solutionFile
      
              $newContent = @()
      
              for ($i = 0; $i -lt $content.Count; ++$i)
              {
                  $line = $content[$i]
      
                  if ($line -notmatch $regEx)
                      { $newContent += $line; continue }
      
                  do { ++$i; $line = $content[$i] } while ($line -notmatch "^EndProject$")
              }
      
              if ($newContent.Count -ne $content.Count)
              {
                  Write-Host "Stripping project $referencedProjectName from solution $solutionFile"
                  $newContent | Set-Content $solutionFile
              }
          }
      
          catch
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }
      }
      
      
      
      
      function Solution-Get-Project-List
      {
          param (
              [parameter(Mandatory = $true)][string] $solutionFile
          )
      
          $projects = @()
      
          try
          {
              if (!(Test-Path $solutionFile))
                  { throw "Could not find file $solutionFile" }
      
              $regEx = '^Project\("\{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC\}"\)\s*=\s*"(?<name>[^"]*)",\s*"(?<path>[^"]*)",\s*"(?<guid>[^"]*)"'
      
              $content = Get-Content $solutionFile
      
              for ($i = 0; $i -lt $content.Count; ++$i)
              {
                  $line = $content[$i]
      
                  if ($line -match $regEx)
                  { 
                      $name = $Matches['name']
                      $projectPath = Resolve-Path $Matches['path'] -ErrorAction SilentlyContinue
      
                      if ($projectPath -eq $null -or !(Test-Path $projectPath))
                          { continue }
      
                      $guid = $Matches['guid']
                      $xml = [XML](Get-Content $projectPath)
      
      
                      $project = New-Object Object |
                          Add-Member -MemberType NoteProperty -Name "Name" -Value $name -PassThru |
                          Add-Member -MemberType NoteProperty -Name "Path" -Value $projectPath -PassThru |
                          Add-Member -MemberType NoteProperty -Name "Guid" -Value $guid -PassThru |
                          Add-Member -MemberType NoteProperty -Name "XML"  -Value $xml -PassThru
      
                      $projects += $project
                  }
              }
          }
      
          catch
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }
      
          return $projects
      }
      
      
      
      function Project-Remove-Reference
      {
          param (
              [parameter(Mandatory = $true)][object] $project,
              [parameter(Mandatory = $true)][object] $referencedProject
          )
      
          try
          {
              $nm = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $project.XML.NameTable
              $nm.AddNamespace('x', 'http://schemas.microsoft.com/developer/msbuild/2003')
              $projectNode = $project.XML.SelectSingleNode("/x:Project/x:ItemGroup/x:ProjectReference/x:Project[.='" + $referencedProject.Guid + "']", $nm)
      
              if ($projectNode)
              {
                  $projectReferenceNode = $projectNode.ParentNode
                  $itemGroupNode = $projectReferenceNode.ParentNode
                  $itemGroupNode.RemoveChild($projectReferenceNode) | Out-Null
                  $project.XML.Save($project.Path) | Out-Null
              }
          }
      
          catch
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }
      }
      
      
      
      function Project-Install-Nuget-Package
      {
          param (
              [parameter(Mandatory = $true)][object] $project,
              [parameter(Mandatory = $true)][string] $packageName
          )
      
          try
          {
              Install-Package -Id $packageName -ProjectName $project.Path
          }
      
          catch 
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }   
      }
      
      
      
      function Project-Contains-Reference
      {
          param (
              [parameter(Mandatory = $true)][object] $project,
              [parameter(Mandatory = $true)][object] $referencedProject
          )
      
          $retVal = $null
      
          try
          {
              $nm = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $project.XML.NameTable
              $nm.AddNamespace('x', 'http://schemas.microsoft.com/developer/msbuild/2003')
              $node = $project.XML.SelectSingleNode("/x:Project/x:ItemGroup/x:ProjectReference/x:Name[.='" + $referencedProject.Name + "']", $nm)
              $retVal = $node -ne $null
          }
      
          catch
          {
              Write-Host "$($MyInvocation.InvocationName): $_"
          }
      
          return $retVal
      }
      
      
      
      try
      {
          $Error.Clear()
      
          Push-Location
      
          if (!(Get-Package $packageName -ListAvailable))
              { throw "Could not find package $packageName" }
      
          $path = Split-Path (Resolve-Path $MyInvocation.InvocationName) -Parent
      
          $solutions = @(Get-ChildItem $path -Filter "*.sln" -Recurse)
      
          $solutions | foreach { 
              $solution = $_
      
              cd (Split-Path $solution.FullName -parent)
      
              $projectList = Solution-Get-Project-List $solution
              $referencedProject = $projectList | where { $_.Name -eq $referencedProjectName }
      
              if ($referencedProject -ne $null)
              {
                  Backup-File $solution
                  Solution-Strip-Project-Reference $solution $referencedProjectName
      
                  foreach ($project in $projectList | where { Project-Contains-Reference $_ $referencedProject } )
                  {
                      Backup-File $project.Path
                      Project-Remove-Reference $project $referencedProject
                      Project-Install-Nuget-Package $project $packageName
                  }
              }
          }
      }
      
      catch
      {
          Write-Host "$($MyInvocation.InvocationName): $_"
      }
      
      finally
      {
          Pop-Location
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-03
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 2013-11-30
        • 1970-01-01
        • 2021-12-25
        相关资源
        最近更新 更多