LoginSignup
1
0

More than 1 year has passed since last update.

特定フォルダー以下にあるプロジェクトで参照しているライブラリの一覧を取得する(.NET)

Posted at

はじめに

特定ディレクトリ以下にある複数のプロジェクトで使っているライブラリの一覧とそのバージョンが知りたいといわれた。

プロジェクトファイルの構成

C#で利用しているライブラリの一覧は*.csprojファイルに記載されているので、この子を探してXMLをパースすればよいはず。
プロジェクトファイルはこんな感じで、ItemGroup/PackageReferenceを探して列挙すればよさそう。

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.DataProtection.EntityFrameworkCore" Version="6.0.14" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Identity.Web" Version="2.5.0" />
    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
    <PackageReference Include="NSwag.AspNetCore" Version="13.18.2" />
  </ItemGroup>

  以下略
</Project>

取得スクリプト

特定フォルダー以下のプロジェクトファイルを再帰的に探して、Referenceの一覧を取得する。

$csprojList = ls *.csproj -Recurse
$referenceList = $csprojList | % { 
   $xml = [xml](Get-Content($_.FullName))  
   @{ 
     File = $_.FullName
     Reference = $xml.Project.ItemGroup.PackageReference | select Include, Version
   }
 }
$referenceList | ConvertTo-Json

適当なディレクトリで実行したらこんな感じの結果になった

[
  {
    "Reference": [
      "@{Include=AngleSharp; Version=0.14.0}",
      "@{Include=Microsoft.VisualStudio.Azure.Containers.Tools.Targets; Version=1.10.9}"
    ],
    "File": "C:\\localrepo\\SandBox\\ConsoleApp5\\ConsoleApp5\\ConsoleApp5.csproj"
  },
  {
    "Reference": [
      "@{Include=Microsoft.NET.Test.Sdk; Version=16.7.1}",
      "@{Include=xunit; Version=2.4.1}",
      "@{Include=xunit.runner.visualstudio; Version=2.4.3}",
      "@{Include=coverlet.collector; Version=1.3.0}"
    ],
    "File": "C:\\localrepo\\SandBox\\ConsoleApp5\\XUnitTestProject1\\XUnitTestProject1.csproj"
  },
  以下略
]

おわりに

本当はNugetAPIを使って依存ライブラリやライセンスまで取りたかったんだけれど、使い方が悪いのかことごとく空のデータが返ってくるので今回はここまでで断念。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0