OSDN Git Service

na-get-lib,チケット #36130 に関連して、更新パッケージから非対応パッケージを除外するようにした
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages.Install / UpgradeFinder.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Reflection;
4 using NaGet.Packages;
5
6 namespace NaGet.Packages.Install
7 {
8         /// <summary>
9         /// \8dX\90V\83\\83t\83g\82ð\92T\82·
10         /// </summary>
11         public sealed class UpgradeFinder
12         {
13                 private UpgradeFinder()
14                 {
15                 }
16                 
17                 private static Package getUpgradePkg(Package pkg, PackageList<Package> pkgList, PackageList<InstalledPackage> instPkgList, string[] exclude, IComparer<string> verComp)
18                 {
19                         Package avaiablePkg = pkgList.GetPackageForName(pkg.Name);
20                         bool canUpgrade = false;
21                         
22                         if (avaiablePkg != null) {
23                                 
24                                 if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&
25                                     instPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {
26                                         canUpgrade = true;
27                                         
28                                         if (Array.IndexOf(exclude, pkg.Name) >= 0) {
29                                                 canUpgrade = false;
30                                         } else if (Installation.GetPreferInstallerIndex(pkg) == -1) {
31                                                 canUpgrade = false;
32                                         }
33                                 }
34                         }
35                         
36                         return (canUpgrade)? avaiablePkg : null;
37                 }
38                 
39                 public static IEnumerable<Package> GetUpgradePackages(PackageListsManager pkgListMan)
40                 {
41                         PackageList<Package> packageList = pkgListMan.AvailablePkgList;
42                         string[] exclude = NaGet.Env.ExcludeUpdatePackageNames ?? new string[0];
43                         
44                         VersionComparetor verComp = new VersionComparetor();
45                         
46                         foreach (InstalledPackage pkg in pkgListMan.InstalledPkgList) {
47                                 Package availablePkg = getUpgradePkg(pkg, packageList, pkgListMan.InstalledPkgList, exclude, verComp);
48                                 if (availablePkg != null) { // if it has an upgrade, yield
49                                         yield return availablePkg;
50                                 }
51                         }
52                         foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList) {
53                                 Package availablePkg = getUpgradePkg(pkg, packageList, pkgListMan.SystemInstalledPkgList, exclude, verComp);
54                                 if (availablePkg != null) { // if it has an upgrade, yield
55                                         yield return availablePkg;
56                                 }
57                         }
58                 }
59         }
60 }