2017-02-21 56 views
0

我建立一個小的C#程序來收集所使用的NuGet包C#構建一個工具來收集項目的NuGet信息

信息這是每一塊信息,我想:

public class Package 
{ 
    public string Version { get; set; } 
    public string Name { get; set; } 
    public string Description { get; set; } 
    public int Count { get; set; } 
    public List<string> Versions { get; set; } 
    } 

這是我如何收集所有信息:

public partial class MainWindow : Window 
{ 
    public Dictionary<string, Package> Packages; 
    public string Folder; 
    public SaveFileDialog saveFileDialog = new SaveFileDialog(); 
    public FolderBrowserDialog folderBrowseDialog = new FolderBrowserDialog(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Packages = new Dictionary<string, Package>(); 
    } 

    private void SearchBtn_Click(object sender, RoutedEventArgs e) 
    { 
     DialogResult result = folderBrowseDialog.ShowDialog(); 
     if (result == System.Windows.Forms.DialogResult.OK) 
     { 
      DoneLbl.Content = "Started"; 
      Folder = folderBrowseDialog.SelectedPath; 
      foreach (string fileName in Directory.EnumerateFiles(Folder, "packages.config", SearchOption.AllDirectories)) 
      { 
       var file = new PackageReferenceFile(fileName); 
       foreach (PackageReference packageReference in file.GetPackageReferences()) 
       { 
        if (Packages.ContainsKey(packageReference.Id)) 
        { 
         Packages[packageReference.Id].Count++; 
         if (Packages[packageReference.Id].Versions.Contains(packageReference.Version.ToString())) 
         { 

         } 
         else 
         { 
          var oldVersion = new Version(Packages[packageReference.Id].Version); 
          var newVersion = new Version(packageReference.Version.ToString()); 
          if (newVersion > oldVersion) 
          { 
           Packages[packageReference.Id].Version = packageReference.Version.ToString(); 
          } 
          Packages[packageReference.Id].Versions.Add(packageReference.Version.ToString()); 
         } 
        } 
        else 
        { 
         var package = new Package(); 
         package.Name = packageReference.Id; 
         package.Count = 1; 
         package.Version = packageReference.Version.ToString(); 
         package.Versions = new List<string>(); 
         package.Versions.Add(packageReference.Version.ToString()); 
         Packages.Add(packageReference.Id, package); 
        } 
       } 
       DoneLbl.Content = "Done"; 
      } 
     } 
    } 

    private void ExportBtn_Click(object sender, RoutedEventArgs e) 
    { 
     saveFileDialog.Filter = "Text File | *.txt"; 
     DialogResult result = saveFileDialog.ShowDialog(); 

     if (result == System.Windows.Forms.DialogResult.OK) 
     { 
      string lines = "Nuget Packages for \r\n" + Folder + "\r\n"; 

      foreach (var Package in Packages) 
      { 
       lines += "********************\r\n"; 
       lines += "Name: " + Package.Value.Name + "\r\n"; 
       lines += "Version: " + Package.Value.Version + "\r\n"; 
       lines += "Different Versions: " + Package.Value.Versions.Count + "\r\n"; 
       lines += "Description: " + Package.Value.Description + "\r\n"; 
       lines += "Count: " + Package.Value.Count + "\r\n"; 
      } 

      StreamWriter file = new StreamWriter(saveFileDialog.FileName); 
      file.WriteLine(lines); 

      file.Close(); 
     } 
    } 
} 

這通過「Nuget.Core」nuget包獲取所有內容。 但我仍然缺乏說明。有沒有辦法通過這個nuget軟件包找到軟件包描述?或者我可以用其他方式找到包裝描述?

回答

0

我看着的NuGet V3包如下所述: https://docs.microsoft.com/en-us/nuget/api/nuget-api-v3

使用此我犯了一個GetDescription方法

public async void GetDescription(string name, Package package) 
{ 
    List<Lazy<INuGetResourceProvider>> providers = new List<Lazy<INuGetResourceProvider>>(); 
    providers.AddRange(Repository.Provider.GetCoreV3()); // Add v3 API support 
    PackageSource packageSource = new PackageSource("https://api.nuget.org/v3/index.json"); 
    SourceRepository sourceRepository = new SourceRepository(packageSource, providers); 
    PackageMetadataResource packageMetadataResource = await sourceRepository.GetResourceAsync<PackageMetadataResource>(); 
    IEnumerable<IPackageSearchMetadata> searchMetadata = await packageMetadataResource.GetMetadataAsync(name, true, true, new Logger(), CancellationToken.None); 
    package.Description = searchMetadata.First().Description; 
} 

並把它稱爲在增加了新的軟件包列表中的代碼。

   { 
        var package = new Package(); 
        package.Name = packageReference.Id; 
        package.Count = 1; 
        GetDescription(packageReference.Id, package); 
        package.Version = packageReference.Version.ToString(); 
        package.Versions = new List<string>(); 
        package.Versions.Add(packageReference.Version.ToString()); 
        Packages.Add(packageReference.Id, package); 
       } 

我稍後會看到讓所有的事情都通過Nuget V3,但現在一切正常。