OSDN Git Service

na-get-lib,新構造タスクのキャンセル処理を修正。
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages / PackageCollectionFileData.cs
1 using System;\r
2 using System.Text;\r
3 using System.IO;\r
4 using System.Collections.Generic;\r
5 \r
6 namespace NaGet.Packages\r
7 {\r
8         /// <summary>\r
9         /// 環境構築/移行を便利にするためのパッケージ名リストの表現クラス\r
10         /// </summary>\r
11         public class PackageCollectionFileData : System.Collections.ObjectModel.Collection<string>\r
12         {\r
13                 public PackageCollectionFileData()\r
14                 {\r
15                 }\r
16                 \r
17                 /// <summary>\r
18                 /// パッケージ名をリストに入れる\r
19                 /// </summary>\r
20                 /// <param name="pkgs">パッケージ情報</param>\r
21                 public void loadPackages<T>(IEnumerable<T> pkgs) where T : Package\r
22                 {\r
23                         foreach (Package pkg in pkgs) {\r
24                                 if (this.IndexOf(pkg.Name) < 0) {\r
25                                         this.Add(pkg.Name);\r
26                                 }\r
27                         }\r
28                 }\r
29                 \r
30                 /// <summary>\r
31                 /// パッケージ名をリストに入れる\r
32                 /// </summary>\r
33                 /// <param name="pkgs">パッケージ情報</param>\r
34                 public void loadPackages<T>(IEnumerator<T> e) where T : Package\r
35                 {\r
36                         while(e.MoveNext()) {\r
37                                 T pkg = e.Current;\r
38                                 if (this.IndexOf(pkg.Name) < 0) {\r
39                                         this.Add(pkg.Name);\r
40                                 }\r
41                         }\r
42                 }\r
43                 \r
44                 /// <summary>\r
45                 /// このリストからパッケージのリストを得る\r
46                 /// </summary>\r
47                 /// <returns>パッケージのリスト</returns>\r
48                 public void generatePackages(PackageListsManager pkgMan, out IList<Package> pkgs, out IList<string> invalidPkgNames)\r
49                 {\r
50                         pkgs = new List<Package>();\r
51                         invalidPkgNames = new List<string>();\r
52                         \r
53                         foreach (string pkgName in this) {\r
54                                 Package pkg = pkgMan.availablePkgList.GetPackageForName(pkgName);\r
55                                 if (pkg != null) {\r
56                                         if ((pkgMan.installedPkgList.GetPackageForName(pkgName) == null)\r
57                                            && (pkgMan.systemInstalledPkgList.GetPackageForName(pkgName) == null)) {\r
58                                                 pkgs.Add(pkg);\r
59                                         } else {\r
60                                                 // すでにインストールされている場合は何もしない\r
61                                         }\r
62                                 } else {\r
63                                         invalidPkgNames.Add(pkgName);\r
64                                 }\r
65                         }\r
66                 }\r
67                 \r
68                 #region ファイル入出力\r
69                 \r
70                 /// <summary>\r
71                 /// リストをストリームに保存する\r
72                 /// </summary>\r
73                 /// <param name="stream">保存先</param>\r
74                 public void saveAs(StreamWriter stream)\r
75                 {\r
76                         foreach (string name in this) {\r
77                                 stream.WriteLine("{0}", name);\r
78                         }\r
79                 }\r
80                 \r
81                 /// <summary>\r
82                 /// リストをファイルに保存する\r
83                 /// </summary>\r
84                 /// <param name="filepath">保存先</param>\r
85                 public void saveAs(String filepath)\r
86                 {\r
87                         using (FileStream stream = new FileStream(filepath, FileMode.Create)) {\r
88                                 using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8)) {\r
89                                         saveAs(sw);\r
90                                 }\r
91                         }\r
92                 }\r
93                 \r
94                 /// <summary>\r
95                 /// ストリームからリストを読み込む\r
96                 /// </summary>\r
97                 /// <param name="stream">読み取り元</param>\r
98                 public void load(StreamReader stream)\r
99                 {\r
100                         string line = null;\r
101                         \r
102                         while ((line = stream.ReadLine()) != null) {\r
103                                 this.Add(line.Trim());\r
104                         }\r
105                 }\r
106 \r
107                 /// <summary>\r
108                 /// ファイルからリストを読み込む\r
109                 /// </summary>\r
110                 /// <param name="stream">読み取り元</param>\r
111                 public void load(string filepath)\r
112                 {\r
113                         using (FileStream fs = new FileStream(filepath, FileMode.Open)) {\r
114                                 using (StreamReader sr = new StreamReader(fs, Encoding.UTF8)) {\r
115                                         load(sr);\r
116                                 }\r
117                         }\r
118                 }\r
119                 \r
120                 #endregion\r
121         }\r
122 }\r