OSDN Git Service

initial build for opensource
[applistation/AppliStation.git] / na-get-lib / NaGet.SubCommands / NaGetInstall.cs
1 using System;\r
2 using NaGet.Packages.Install;\r
3 using NaGet.Packages;\r
4 using NaGet.Net;\r
5 \r
6 namespace NaGet.SubCommands\r
7 {\r
8         public class NaGetInstall : NaGetTaskSet\r
9         {\r
10                 private bool done = false;\r
11                 \r
12                 private int currentTaskSetIndex = -1;\r
13                 \r
14                 private PackageListsManager pkgListMan;\r
15                 \r
16                 public Downloader Downloader; // TODO \8eQ\8fÆ\82ð\82µ\82Ä\83Z\83b\83g\82·\82é\82æ\82¤\82É\r
17                 \r
18                 private bool packageInstallerDownloaded = false;\r
19                 \r
20                 public override bool Cancelable {\r
21                         get { return ! done; }\r
22                 }\r
23                 \r
24                 /// <summary>\r
25                 /// \83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W\r
26                 /// </summary>\r
27                 public Installation[] Installations;\r
28                 \r
29                 /// <summary>\r
30                 /// \83R\83\93\83X\83g\83\89\83N\83^\r
31                 /// </summary>\r
32                 /// <param name="pkgs">\83C\83\93\83X\83g\81[\83\8b\82·\82é\83p\83b\83P\81[\83W</param>\r
33                 public NaGetInstall(PackageListsManager pkgListMan, Package[] pkgs)\r
34                         : this(pkgListMan, Installation.ConvertInstallations(pkgs))\r
35                 {\r
36                 }\r
37                 \r
38                 /// <summary>\r
39                 /// \83R\83\93\83X\83g\83\89\83N\83^\r
40                 /// </summary>\r
41                 /// <param name="installations">\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82Ì\94z\97ñ</param>\r
42                 public NaGetInstall(PackageListsManager pkgMan, Installation[] installations)\r
43                 {\r
44                         pkgListMan = pkgMan;\r
45                         \r
46                         Installations = installations;\r
47                         initializeMainTaskSetNames();\r
48                 }\r
49                 \r
50                 private void initializeMainTaskSetNames()\r
51                 {\r
52                         System.Collections.Generic.List<string> taskSetNames = new System.Collections.Generic.List<string>();\r
53                         \r
54                         for (int i =0; i < Installations.Length; i++) {\r
55                                 taskSetNames.Add(string.Format("\8eæ\93¾: {0}", Installations[i].ToString()));\r
56                         }\r
57                         taskSetNames.Add("\83C\83\93\83X\83g\81[\83\89\82Ì\8c\9f\8fØ");\r
58                         for (int i =0; i < Installations.Length; i++) {\r
59                                 taskSetNames.Add(string.Format("\83C\83\93\83X\83g\81[\83\8b: {0}", Installations[i].ToString()));\r
60                         }\r
61                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.ArchiveInstalledPackageListFile));\r
62                         taskSetNames.Add(string.Format("\83\8a\83X\83g\8dX\90V: {0}", NaGet.Env.SystemInstalledPackageListFile));\r
63                         \r
64                         TaskSetNames = taskSetNames.ToArray();\r
65                 }\r
66                 \r
67                 public override void Run()\r
68                 {\r
69                         currentTaskSetIndex ++;\r
70                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED, "\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\8aJ\8en");\r
71                         \r
72                         foreach (Installation inst in Installations) {\r
73                                 if (! inst.IsInstallablePackage()) {\r
74                                         string msg = string.Format("{0}\82Í\83C\83\93\83X\83g\81[\83\8b\82·\82é\82±\82Æ\82ª\82Å\82«\82Ü\82¹\82ñ", inst.ToString());\r
75                                         \r
76                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, msg);\r
77                                         done = true;\r
78                                         return;\r
79                                 }\r
80                                 \r
81                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, inst.ToString());\r
82                                 \r
83                                 if (! inst.Downloaded) {\r
84                                         try {\r
85                                                 inst.Download(Downloader);\r
86                                         } catch (NaGetTaskCanceledException) {\r
87                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "\83C\83\93\83X\83g\81[\83\89\82Ì\83_\83E\83\93\83\8d\81[\83h\8f\88\97\9d\82ª\83L\83\83\83\93\83Z\83\8b\82³\82ê\82Ü\82µ\82½");\r
88                                                 done = true;\r
89                                                 return;\r
90                                         } catch (System.Net.WebException e) {\r
91                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, e.Message);\r
92                                                 if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) {\r
93                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "\83l\83b\83g\83\8f\81[\83N\82É\90Ú\91±\82³\82ê\82Ä\82¢\82Ü\82¹\82ñ\81B");\r
94                                                 } else {\r
95                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, "\83l\83b\83g\83\8f\81[\83N\82É\90Ú\91±\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½\81B\83l\83b\83g\83\8f\81[\83N\82ª\90Ø\92f\82³\82ê\82Ä\82¢\82é\82©\81A\83t\83@\83C\83A\83E\83H\81[\83\8b\82É\82æ\82Á\82Ä\8eÕ\92f\82³\82ê\82½\89Â\94\\90«\82ª\82 \82è\82Ü\82·\81B");\r
96                                                 }\r
97                                                 done = true;\r
98                                                 return;\r
99                                         } catch (Exception e) {\r
100                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
101                                                 done = true;\r
102                                                 return;\r
103                                         }\r
104                                 }\r
105                                 currentTaskSetIndex ++;\r
106                                 \r
107                                 if (inst.Downloaded) { // \90³\8fí\8fI\97¹\r
108                                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, inst.ToString());\r
109                                 } else { // \83C\83\93\83X\83g\81[\83\8b\82ª\8a®\97¹\82¹\82¸\82É\8fI\82í\82Á\82½=\8e¸\94s=\83G\83\89\81[\r
110                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, string.Format("{0}\82Ì\83C\83\93\83X\83g\81[\83\89\82ð\90³\8fí\82É\83_\83E\83\93\83\8d\81[\83h\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½", inst.ToString()));\r
111                                 }\r
112                         }\r
113                         \r
114                         packageInstallerDownloaded = true;\r
115                         \r
116                         {\r
117                                 bool invalid = false;\r
118                                 int i = 0;\r
119                                 foreach (Installation inst in Installations) {\r
120                                         float percent = (CurrentTaskSetIndex+((float)i / Installations.Length))*100f/TaskSetNames.Length;\r
121                                         \r
122                                         if (inst.GetRegisteredHashCount() > 0) {\r
123                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "\8c\9f\8fØ: "+inst.ToString(), percent);\r
124                                                 \r
125                                                 if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
126                                                         invalid = true;\r
127                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "\8c\9f\8fØ: "+inst.ToString() + " \94ñ\90®\8d\87", percent);\r
128                                                 } else {\r
129                                                         RaiseTaskSetEvent(NaGetTaskSetEventType.INFO, "\8c\9f\8fØ: "+inst.ToString() + " OK", percent);\r
130                                                 }\r
131                                         }\r
132                                         i++;\r
133                                 }\r
134                                 \r
135                                 // TODO \83n\83b\83V\83\85\82ª\89ó\82ê\82Ä\82¢\82é\82Æ\82«\82Ì\91Î\8dô\r
136 //                              if (invalid && (! AllGet.Util.Confirm("Some packages do not match hash value. Are you really sure to install them [yN]?", false) )) {\r
137 //                                      Console.WriteLine("Abort.");\r
138 //                                      Environment.Exit(0);\r
139 //                              }\r
140                                 currentTaskSetIndex ++;\r
141                         }\r
142                         \r
143                         foreach (Installation inst in Installations) {\r
144                                 RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, inst.ToString());\r
145                                 \r
146                                 try {\r
147                                         inst.ErrorDataReceived += this.ReceivedErrorData;\r
148                                         inst.OutputDataReceived += this.ReceivedOutputData;\r
149                                         int exitCode = inst.Install();\r
150                                         if (exitCode != 0) {\r
151                                                 RaiseTaskSetEvent(NaGetTaskSetEventType.WARNING, "\83C\83\93\83X\83g\81[\83\8b\82ª\90³\8fí\82É\8fI\82¦\82Ä\82¢\82È\82¢\89Â\94\\90«\82ª\82 \82è\82Ü\82·\81B\83C\83\93\83X\83g\81[\83\89\82Ì\8fI\97¹\83R\81[\83h:"+exitCode);\r
152                                         }\r
153                                         \r
154                                         pkgListMan.WriteInstallationLog(inst);\r
155                                 } catch (Exception e) {\r
156                                         RaiseTaskSetEvent(NaGetTaskSetEventType.ERROR, e.Message);\r
157                                         done = true;\r
158                                         return;\r
159                                 }\r
160                                 currentTaskSetIndex ++;\r
161                                 \r
162                                 RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, inst.ToString());\r
163                                 \r
164                                 if (cancelCalled) {\r
165                                         RaiseTaskSetEvent(NaGetTaskSetEventType.CANCELED, "\83p\83b\83P\81[\83W\82Ì\83C\83\93\83X\83g\81[\83\8b\8f\88\97\9d\82ª\83L\83\83\83\93\83Z\83\8b\82³\82ê\82Ü\82µ\82½");\r
166                                         done = true;\r
167                                         return;\r
168                                 }\r
169                         }\r
170                         pkgListMan.SaveSystemInstalledLogList(); // \83\8d\83O\82Ì\83R\83~\83b\83g\r
171                         \r
172                         runLocalUpdate();\r
173                         \r
174                         done = true;\r
175                         \r
176                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED, "\8fI\97¹", 100);\r
177                 }\r
178                 \r
179                 private void runLocalUpdate()\r
180                 {\r
181                         // \83C\83\93\83X\83g\81[\83\8b\83g\83\8a\83X\83g\82Ì\8dX\90V\r
182                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
183                         pkgListMan.DetectInstalledPkgs();\r
184                         pkgListMan.SaveInstalledPackageList();\r
185                         currentTaskSetIndex++;\r
186                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
187                 \r
188                         // \83V\83X\83e\83\80\82É\83C\83\93\83X\83g\81[\83\8b\82³\82ê\82Ä\82¢\82é\83\8a\83X\83g\82Ì\8dX\90V\r
189                         RaiseTaskSetEvent(NaGetTaskSetEventType.STARTED_TASKSET, TaskSetNames[currentTaskSetIndex]);\r
190                         pkgListMan.DetectSystemInstalledPkgs();\r
191                         pkgListMan.SaveSystemInstalledPackageList();\r
192                         currentTaskSetIndex++;\r
193                         RaiseTaskSetEvent(NaGetTaskSetEventType.COMPLETED_TASKSET, TaskSetNames[currentTaskSetIndex-1]);\r
194                 }\r
195                 \r
196                 public override bool Done {\r
197                         get { return done; }\r
198                 }\r
199                 \r
200                 public override int CurrentTaskSetIndex {\r
201                         get { return currentTaskSetIndex; }\r
202                 }\r
203                 \r
204                 private bool cancelCalled = false;\r
205                 \r
206                 public override bool Cancel()\r
207                 {\r
208                         cancelCalled = true;\r
209                         if (! packageInstallerDownloaded) {\r
210                                 return Downloader.Cancel();\r
211                         } else return true;\r
212                 }\r
213         }\r
214 }\r