OSDN Git Service

na-get-lib,ダウンロード後にファイル名が引き継がれずインストールなどが動作していなかったのを修正
[applistation/AppliStation.git] / all-get / Main.cs
1 // project created on 2007/09/08 at 20:20\r
2 using System;\r
3 using System.IO;\r
4 using System.Collections.Generic;\r
5 using NaGet.Packages;\r
6 using NaGet.Packages.Install;\r
7 using NaGet.SubCommands;\r
8 using NaGet.Tasks;\r
9 \r
10 namespace AllGet\r
11 {\r
12         sealed class TaskSetEventHandlers\r
13         {\r
14                 public static void OnTaskSetEvent(object sender, TaskEventArgs e) { \r
15                         switch (e.Type) {\r
16 //                              case NaGetTaskSetEventType.COMPLETED_TASKSET\r
17 //                                      break;\r
18                                 case TaskEventType.COMPLETED:\r
19                                         Console.WriteLine("Done.");\r
20                                         break;\r
21                                 case TaskEventType.STARTED_SUBTASK:\r
22                                         Console.Write("  " + e.TaskMessage);\r
23                                         break;\r
24                                 case TaskEventType.COMPLETED_SUBTASK:\r
25                                         Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
26                                         break;\r
27                                 case TaskEventType.INFO:\r
28                                         Console.WriteLine("  " + e.TaskMessage);\r
29                                         break;\r
30                                 case TaskEventType.ERROR:\r
31                                 case TaskEventType.WARNING:\r
32                                         Console.WriteLine("  [Error]" + e.TaskMessage);\r
33                                         break;\r
34                         }\r
35                 }\r
36                 \r
37                 public static void OnDownloadSubTaskEvent(object sender, TaskEventArgs e)\r
38                 {\r
39                         if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
40                                 if (e.Type == TaskEventType.STARTED) {\r
41                                         Console.WriteLine();\r
42                                 }\r
43                                 \r
44                                 int origPosX = Console.CursorLeft;\r
45                                 for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
46                                         Console.Write(' ');\r
47                                 }\r
48                                 Console.CursorLeft = origPosX;\r
49                                 \r
50                                 switch (e.Type) {\r
51                                         case TaskEventType.PING:\r
52                                                 {\r
53                                                         int origPos = Console.CursorLeft;\r
54                                                         Console.Write(e.TaskMessage);\r
55                                                         Console.CursorLeft = origPos;\r
56                                                 }\r
57                                                 break;\r
58                                         case TaskEventType.WARNING:\r
59                                         case TaskEventType.ERROR:\r
60                                                 Console.WriteLine("  [Error]" + e.TaskMessage);\r
61                                                 break;\r
62                                         default:\r
63                                                 Console.WriteLine(e.TaskMessage);\r
64                                                 break;\r
65                                 }\r
66                         }\r
67                 }\r
68                 \r
69                 public static NaGetTaskQueryResult OnTaskQueryEvent(object sender, NaGetTaskQueryArgs e)\r
70                 {\r
71                         char result = '\u0000';\r
72                         if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
73                                 result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
74                         } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
75                                 result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
76                         } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
77                                 result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
78                         } else {\r
79                                 result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
80                         }\r
81                         \r
82                         switch (result) {\r
83                                 case 'y':\r
84                                         return NaGetTaskQueryResult.CONTINUE;\r
85                                 case 'n':\r
86                                         return NaGetTaskQueryResult.CANCEL;\r
87                                 case 'r':\r
88                                         return NaGetTaskQueryResult.RETRY;\r
89                                 default:\r
90                                         return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
91                         }\r
92                 }\r
93         }\r
94         \r
95         \r
96         class MainClass\r
97         {\r
98                 private PackageListsManager pkgListMan;\r
99                 \r
100                 /// <summary>\r
101                 /// サイレントインストールをするか否か\r
102                 /// </summary>\r
103                 private bool silent = false;\r
104                 \r
105                 public MainClass()\r
106                 {\r
107                         pkgListMan = new PackageListsManager();\r
108                         pkgListMan.LoadPackageLists();\r
109                 }\r
110                 \r
111                 public void Update()\r
112                 {\r
113                         update(true);\r
114                 }\r
115                 \r
116                 public void LocalUpdate()\r
117                 {\r
118                         update(false);\r
119                 }\r
120                 \r
121                 public void update(bool downloadPackageListFlag)\r
122                 {\r
123                         NaGet.SubCommands.NaGetUpdate2 tasks = new NaGet.SubCommands.NaGetUpdate2(pkgListMan, downloadPackageListFlag);\r
124                         tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
125                         tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
126                         \r
127                         tasks.Run();\r
128                 }\r
129                 \r
130                 public void CheckUpgrade()\r
131                 {\r
132                         foreach (Package pkg in UpgradeFinder.GetUpgradePackages(pkgListMan)) {\r
133                                 bool isSystem = false;\r
134                                 Package instPkg = null;\r
135                                 \r
136                                 instPkg = pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
137                                 if (instPkg != null) {\r
138                                         isSystem = true;\r
139                                 } else {\r
140                                         instPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name);\r
141                                         if (instPkg != null) {\r
142                                                 isSystem = false;\r
143                                         } else {\r
144                                                 System.Diagnostics.Debug.Fail("internal error.");\r
145                                         }\r
146                                 }\r
147                                 \r
148                                 Console.WriteLine("{0} ({1}){3} => ({2})", pkg.Name, instPkg.Version, pkg.Version, (isSystem)? "@sys" : "");\r
149                         }\r
150                 }\r
151                 \r
152                 public void Search(string keys)\r
153                 {\r
154                         foreach(Package package in pkgListMan.AvailablePkgList.Search(keys)) {\r
155                                 Console.WriteLine("{0} ({1}) - {2}", package.Name, package.Version, package.Summary);\r
156                         }\r
157                         \r
158                         foreach(InstalledPackage package in pkgListMan.InstalledPkgList.Search(keys)) {\r
159                                 Console.WriteLine("{0} ({1})[@install] - {2}", package.Name, package.Version, package.Summary);\r
160                         }\r
161                         \r
162                         foreach(InstalledPackage package in pkgListMan.SystemInstalledPkgList.Search(keys)) {\r
163                                 Console.WriteLine("{0} ({1})[@sys] - {2}", package.Name, package.Version, package.Summary);\r
164                         }\r
165                 }\r
166                 \r
167                 public void Show(string packagename)\r
168                 {\r
169                         PackageList<Package> allPkgs = new PackageList<Package>();\r
170                         allPkgs.AddPackages(pkgListMan.GetAllPackages());\r
171                         \r
172                         foreach (Package pkg in allPkgs.GetPackagesForName(packagename)) {\r
173                                 Console.WriteLine("Package: {0}", pkg.Name);\r
174                                 Console.WriteLine("Version: {0}", pkg.Version);\r
175                                 Console.WriteLine("Summary: {0}", pkg.Summary);\r
176                                 if (pkg.Url != null) Console.WriteLine("Website: {0}", pkg.Url.Href);\r
177                                 if (pkg.Tags != null) Console.WriteLine("Tag: {0}", pkg.Tags.ToLower());\r
178                                 Console.WriteLine("Type: {0}", pkg.Type);\r
179                                 if (pkg.License != null) Console.WriteLine("License: {0}", pkg.License);\r
180                                 if (pkg is InstalledPackage) Console.WriteLine("State: Installed");\r
181                                 Console.WriteLine("Repository: {0}", pkg.PackageListName);\r
182                                 \r
183                                 if (pkg.Description != null) {\r
184                                         Console.WriteLine("Description:");\r
185                                         Console.WriteLine(pkg.Description);\r
186                                 }\r
187                                 Console.WriteLine();\r
188                         }\r
189                 }\r
190                 \r
191                 \r
192                 public void Download(string[] packagenames)\r
193                 {\r
194                         Installation[] installations = null;\r
195                         {\r
196                                 List<Package> downloadList = new List<Package>();\r
197                         \r
198                                 foreach(string packagename in packagenames) {\r
199                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
200                                         if (foundPackage == null) {\r
201                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
202                                                 Environment.Exit(1);\r
203                                         }\r
204                                         if (! downloadList.Contains(foundPackage)) {\r
205                                                 downloadList.Add(foundPackage);\r
206                                         }\r
207                                 }\r
208                                 \r
209                                 installations = new Installation[downloadList.Count];\r
210                                 for (int i = 0; i < installations.Length; i++) {\r
211                                         installations[i] = new Installation(downloadList[i]);\r
212                                 }\r
213                         }\r
214                         \r
215                         Console.WriteLine("The following packages will be downloaded:");\r
216                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
217                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
218                                 Console.WriteLine("Abort.");\r
219                                 Environment.Exit(0);\r
220                         }\r
221                         \r
222                         NaGet.SubCommands.NaGetDownloadToCache2 tasks = new NaGet.SubCommands.NaGetDownloadToCache2(pkgListMan, installations);\r
223                         tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
224                         tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
225                         tasks.TaskQueryRaised += TaskSetEventHandlers.OnTaskQueryEvent;\r
226                         \r
227                         tasks.Run();\r
228                 }\r
229                 \r
230                 public void Install(string[] packagenames)\r
231                 {\r
232                         if (! NaGet.Utils.IsAdministrators()) {\r
233                                 Console.WriteLine("W: you are not administrators!");\r
234                         }\r
235                         \r
236                         Installation[] installations = null;\r
237                         {\r
238                                 List<Package> downloadList = new List<Package>();\r
239                         \r
240                                 foreach(string packagename in packagenames) {\r
241                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
242                                         if (foundPackage == null) {\r
243                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
244                                                 Environment.Exit(1);\r
245                                         }\r
246                                         if (! downloadList.Contains(foundPackage)) {\r
247                                                 downloadList.Add(foundPackage);\r
248                                         }\r
249                                 }\r
250                                 \r
251                                 installations = new Installation[downloadList.Count];\r
252                                 for (int i = 0; i < installations.Length; i++) {\r
253                                         installations[i] = new Installation(downloadList[i]);\r
254                                 }\r
255                         }\r
256                         \r
257                         Installation[] depInstallations;\r
258                         {\r
259                                 Installation[] resolved;\r
260                                 DependeciesResolver.ResolveInstallations(installations, pkgListMan, out resolved, out depInstallations);\r
261                                 installations = resolved;\r
262                         }\r
263                         \r
264                         Console.WriteLine("The following packages will be downloaded:");\r
265                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
266                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
267                                 Console.WriteLine("Abort.");\r
268                                 Environment.Exit(0);\r
269                         }\r
270                         \r
271                         NaGet.SubCommands.NaGetInstall2 tasks = new NaGet.SubCommands.NaGetInstall2(pkgListMan, installations);\r
272                         tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
273                         tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
274                         tasks.TaskQueryRaised += TaskSetEventHandlers.OnTaskQueryEvent;\r
275                         \r
276                         tasks.Run();\r
277                 }\r
278                 \r
279                 public void Remove(string[] packagenames)\r
280                 {\r
281                         if (! NaGet.Utils.IsAdministrators()) {\r
282                                 Console.WriteLine("W: you are not administrators!");\r
283                         }\r
284                         \r
285                         this.LocalUpdate();\r
286                         \r
287                         PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
288                         installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());\r
289                         \r
290                         InstalledPackage[] insPkgs = new InstalledPackage[packagenames.Length];\r
291                         for (int i = 0; i < packagenames.Length; i++) {\r
292                                 insPkgs[i] = installedPkgList.GetPackageForName(packagenames[i]);\r
293                                 \r
294                                 if (insPkgs[i] == null) {\r
295                                         Console.WriteLine("E: could not found package " + packagenames[i]);\r
296                                         Environment.Exit(1);\r
297                                 }\r
298                         }\r
299                         \r
300 //                      if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
301 //                              Console.WriteLine("Abort.");\r
302 //                              Environment.Exit(0);\r
303 //                      }\r
304                         \r
305                         NaGet.SubCommands.NaGetUninstall2 tasks = new NaGet.SubCommands.NaGetUninstall2(pkgListMan, insPkgs);\r
306                         tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
307                         \r
308                         tasks.Run();\r
309                 }\r
310                 \r
311                 public void CleanCache(string[] packages)\r
312                 {\r
313                         if (! Directory.Exists(NaGet.Env.ArchiveFolderPath)) {\r
314                                 return; \r
315                         }\r
316                         \r
317                         int i = 0;\r
318                         if (packages.Length == 0) {\r
319                                 foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath)) {\r
320                                         Directory.Delete(folder, true);\r
321                                         i ++;\r
322                                 }\r
323                         } else {\r
324                                 foreach (string package in packages) {\r
325                                         foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath, package+"*", SearchOption.TopDirectoryOnly)) {\r
326                                                 Directory.Delete(folder, true);\r
327                                                 i ++;\r
328                                         }\r
329                                 }\r
330                         }\r
331                         if (i > 0) {\r
332                                 Console.WriteLine("... Done.");\r
333                         }\r
334                 }\r
335                 \r
336                 public void Help()\r
337                 {\r
338                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
339                         Console.Write("Usage:");\r
340                         \r
341                         Console.WriteLine("\t{0} update|localupdate", executeFileName);\r
342                         Console.WriteLine("\t{0} search|show pkg1 [pkg2 ...]", executeFileName);\r
343                         Console.WriteLine("\t{0} cleancache [pkg ...]", executeFileName);\r
344                         Console.WriteLine("\t{0} download pkg1 [pkg2 ...]", executeFileName);\r
345                         Console.WriteLine("\t{0} [--quiet] install|uninstall pkg1 [pkg2 ...]", executeFileName);\r
346                         Console.WriteLine();\r
347                         Console.WriteLine("{0} is a simple command line interface for downloading and "+\r
348                                           "installing packages. The most frequently used commands are update "+\r
349                                           "and install.", executeFileName);\r
350                         Console.WriteLine();\r
351                         Console.WriteLine("Commands:");\r
352                         Console.WriteLine("   update - Retrieve new lists of packages");\r
353                         Console.WriteLine("   localupdate - Update installed-soft-list only");\r
354                         Console.WriteLine("   checkupgrade - Show upgraded-soft list");\r
355                         Console.WriteLine("   search - Search the package list for not a regex pattern");\r
356                         Console.WriteLine("   show - Show package detail");\r
357                         Console.WriteLine("   cleancache - Clear cached archived file(s)");\r
358                         Console.WriteLine("   download - Download only - do NOT install or unpack archives");\r
359                         Console.WriteLine("   install - Install new packages");\r
360                         Console.WriteLine("   remove - Uninstall packages");\r
361                         Console.WriteLine();\r
362                 }\r
363                 \r
364                 public void FooBar()\r
365                 {\r
366                         foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList) {\r
367                                 Console.WriteLine("{0} : {1}", pkg.Name, pkg.UninstallInfo.UninstallString);\r
368                         }\r
369                 }\r
370                 \r
371                 public void Hoge()\r
372                 {\r
373                         foreach (UninstallInformation uInfo in RegistriedUninstallers.Uninstallers) {\r
374                                 if (! uInfo.IsOSPatch && ! uInfo.IsSystemComponent) {\r
375                                         Console.WriteLine("{0}", uInfo.DisplayName);\r
376                                 }\r
377                         }\r
378                 }\r
379                 \r
380                 public void Moo()\r
381                 {\r
382                         // TODO スーパー牛さんパワー化\r
383                         \r
384                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
385                         Console.WriteLine("誰か {0} をスーパー牛さんパワー化してくれ", executeFileName);\r
386                 }\r
387                 \r
388                 [STAThread]\r
389                 public static void Main(string[] args)\r
390                 {\r
391                         // アーカイブSYSTEM32をパスに足す\r
392                         NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
393                         \r
394                         MainClass mc = new MainClass();\r
395                         \r
396                         if (args.Length == 0) {\r
397                                 mc.Help();\r
398                                 \r
399                                 Environment.Exit(1);\r
400                         } else {\r
401                                 try {\r
402                                         NaGet.ArgParser argParser = new NaGet.ArgParser();\r
403                                         argParser["quiet"] = false;\r
404                                         args = argParser.Parse(args);\r
405                                         mc.silent = (bool) argParser["quiet"];\r
406                                 } catch (ApplicationException e) {\r
407                                         Console.WriteLine(e.Message);\r
408                                         Environment.Exit(1);\r
409                                 }\r
410                         }\r
411                         \r
412                         // 引数ごとに操作を決定\r
413                         switch (args[0])\r
414                         {\r
415                                 case "update":\r
416                                         if (args.Length != 1) {\r
417                                                 Console.WriteLine("E: The update command takes no arguments");\r
418                                                 Environment.Exit(1);\r
419                                         }\r
420                                         mc.Update();\r
421                                         break;\r
422                                 case "localupate":\r
423                                         if (args.Length != 1) {\r
424                                                 Console.WriteLine("E: The update command takes no arguments");\r
425                                                 Environment.Exit(1);\r
426                                         }\r
427                                         mc.LocalUpdate();\r
428                                         break;\r
429                                 case "search":\r
430                                         if (args.Length <= 1) {\r
431                                                 Console.WriteLine("E: You must give exactly one pattern");\r
432                                                 Environment.Exit(1);\r
433                                         }\r
434                                         \r
435                                         mc.Search(string.Join(" ", args, 1, args.Length - 1));\r
436                                         break;\r
437                                 case "show":\r
438                                         if (args.Length <= 1) {\r
439                                                 Console.WriteLine("E: You must give exactly one pattern");\r
440                                                 Environment.Exit(1);\r
441                                         }\r
442                                         \r
443                                         for (int i = 1; i < args.Length; i++) {\r
444                                                 mc.Show(args[i]);\r
445                                         }\r
446                                         break;\r
447                                 case "download":\r
448                                         if (args.Length <= 1) {\r
449                                                 Console.WriteLine("E: Invalid operation download");\r
450                                                 Environment.Exit(1);\r
451                                         } else {\r
452                                                 string[] packages = new string[args.Length - 1];\r
453                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
454                                                 \r
455                                                 mc.Download(packages);\r
456                                         }\r
457                                         break;\r
458                                 case "install":\r
459                                         if (args.Length <= 1) {\r
460                                                 Console.WriteLine("E: Invalid operation install");\r
461                                                 Environment.Exit(1);\r
462                                         } else {\r
463                                                 string[] packages = new string[args.Length - 1];\r
464                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
465                                                 \r
466                                                 mc.Install(packages);\r
467                                         }\r
468                                         break;\r
469                                 case "checkupgrade":\r
470                                         mc.CheckUpgrade();\r
471                                         break;\r
472                                 case "remove":\r
473                                         if (args.Length <= 1) {\r
474                                                 Console.WriteLine("E: Invalid operation remove");\r
475                                                 Environment.Exit(1);\r
476                                         } else {\r
477                                                 string[] packages = new string[args.Length - 1];\r
478                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
479                                                 \r
480                                                 mc.Remove(packages);\r
481                                         }\r
482                                         break;\r
483                                 case "cleancache":\r
484                                         {\r
485                                                 string[] packages = new string[args.Length - 1];\r
486                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
487                                                 \r
488                                                 mc.CleanCache(packages);\r
489                                         }\r
490                                         break;\r
491                                 case "foobar":\r
492                                         mc.FooBar();\r
493                                         break;\r
494                                 case "hoge":\r
495                                         mc.Hoge();\r
496                                         break;\r
497                                 case "moo":\r
498                                         mc.Moo();\r
499                                         break;\r
500                                 default:\r
501                                         mc.Help();\r
502                                         \r
503                                         Environment.Exit(1);\r
504                                         break;\r
505                         }\r
506                 }\r
507         }\r
508 }\r