OSDN Git Service

all-get,サブコマンドの一部修正および追加
[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.Net;\r
6 using NaGet.Packages;\r
7 using NaGet.Packages.Install;\r
8 using NaGet.SubCommands;\r
9 \r
10 namespace AllGet\r
11 {\r
12 \r
13         class DownloadListener\r
14         {\r
15                 private string line = null;\r
16                 \r
17                 /// <summary>\r
18                 /// lineのコンソール上における長さ\r
19                 /// </summary>\r
20                 private int lineWidth = 0;\r
21         \r
22                 public void OnDownload(object sender, DownloadEventArgs a) {\r
23                         if (line != null) {\r
24                                 lock (line) {\r
25                                         int newpos = (lineWidth <= 0)? 0 : Console.CursorLeft - lineWidth;\r
26                                         if (newpos < 0) newpos = 0;\r
27                                         \r
28                                         // 空白で埋める\r
29                                         Console.CursorLeft = newpos;\r
30                                         for (int i = 0; i < lineWidth && Console.CursorLeft < Console.BufferWidth; i++) {\r
31                                                 Console.Write(' ');\r
32                                         }\r
33                                         Console.CursorLeft = newpos;\r
34                                         \r
35                                         line = null; lineWidth = 0;\r
36                                 }\r
37                         }\r
38                         \r
39                         switch (a.Type) {\r
40                         case DownloadEventType.INITED:\r
41                                 line = "starting...";\r
42                                 break;\r
43                         case DownloadEventType.CONNECTED:\r
44                         case DownloadEventType.DOWNLOADING:\r
45                                 line = a.TaskMessage;\r
46                                 break;\r
47                         case DownloadEventType.COMPLETED:\r
48                                 //\r
49                                 line = null;\r
50                                 break;\r
51                         case DownloadEventType.ERROR:\r
52                                 Console.Write("interrupted! ERROR!");\r
53                                 line = null;\r
54                                 break;\r
55                         }\r
56                         \r
57                         if (line != null) {\r
58                                 int posOld = Console.CursorLeft;\r
59                                 Console.Write(line);\r
60                                 lineWidth = Console.CursorLeft - posOld;\r
61                         }\r
62                 }\r
63         }\r
64 \r
65         class MainClass\r
66         {\r
67                 private Downloader downloader;\r
68                 \r
69                 private PackageListsManager pkgListMan;\r
70                 \r
71                 public MainClass()\r
72                 {\r
73                         downloader = new Downloader();\r
74                         DownloadListener dl = new DownloadListener();\r
75                         downloader.DownloadEventRaised += new EventHandler<DownloadEventArgs>(dl.OnDownload);\r
76                         \r
77                         pkgListMan = new PackageListsManager();\r
78                 }\r
79                 \r
80                 public void Update()\r
81                 {\r
82                         update(true);\r
83                 }\r
84                 \r
85                 public void LocalUpdate()\r
86                 {\r
87                         update(false);\r
88                 }\r
89                 \r
90                 public void update(bool downloadPackageListFlag)\r
91                 {\r
92                         NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListMan, downloadPackageListFlag);\r
93                         tasks.Downloader = this.downloader;\r
94                         tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) { \r
95                                 switch (e.Type) {\r
96 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
97 //                                              break;\r
98                                         case NaGetTaskSetEventType.COMPLETED:\r
99                                                 Console.WriteLine("Done.");\r
100                                                 break;\r
101                                         case NaGetTaskSetEventType.STARTED_TASKSET:\r
102                                                 Console.Write("  " + e.TaskMessage);\r
103                                                 break;\r
104                                         case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
105                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);\r
106                                                 break;\r
107                                         case NaGetTaskSetEventType.INFO:\r
108                                                 Console.WriteLine("  " + e.TaskMessage);\r
109                                                 break;\r
110                                         case NaGetTaskSetEventType.ERROR:\r
111                                         case NaGetTaskSetEventType.WARNING:\r
112                                                 Console.WriteLine("  [Error]" + e.TaskMessage);\r
113                                                 break;\r
114                                 }\r
115                         };\r
116                         \r
117                         tasks.Run();\r
118                 }\r
119                 \r
120                 public void CheckUpgrade()\r
121                 {\r
122                         PackageList<Package> packageList = pkgListMan.AvailablePkgList;\r
123                         \r
124                         VersionComparetor verComp = new VersionComparetor();\r
125                         \r
126                         foreach (InstalledPackage pkg in pkgListMan.InstalledPkgList.Packages) {\r
127                                 Package avaiablePkg = packageList.GetPackageForName(pkg.Name);\r
128                                 \r
129                                 if (avaiablePkg != null) {\r
130                                         if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
131                                             pkgListMan.InstalledPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
132                                                 Console.ForegroundColor = ConsoleColor.Green;\r
133                                         }\r
134                                         \r
135                                         Console.WriteLine("{0} ({1}) => ({2})", pkg.Name, pkg.Version, avaiablePkg.Version);\r
136                                         Console.ResetColor();\r
137                                 }\r
138                         }\r
139                         foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList.Packages) {\r
140                                 Package avaiablePkg = packageList.GetPackageForName(pkg.Name);\r
141                                 \r
142                                 if (avaiablePkg != null) {\r
143                                         if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&\r
144                                             pkgListMan.SystemInstalledPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {\r
145                                                 Console.ForegroundColor = ConsoleColor.Green;\r
146                                         }\r
147                                         \r
148                                         Console.WriteLine("{0} ({1})@sys => ({2})", pkg.Name, pkg.Version, avaiablePkg.Version);\r
149                                         Console.ResetColor();\r
150                                 }\r
151                         }\r
152                 }\r
153                 \r
154                 public void Search(string keys)\r
155                 {\r
156                         foreach(Package package in pkgListMan.AvailablePkgList.Search(keys)) {\r
157                                 Console.WriteLine("{0} ({1}) - {2}", package.Name, package.Version, package.Summary);\r
158                         }\r
159                         \r
160                         foreach(InstalledPackage package in pkgListMan.InstalledPkgList.Search(keys)) {\r
161                                 Console.WriteLine("{0} ({1})[@install] - {2}", package.Name, package.Version, package.Summary);\r
162                         }\r
163                         \r
164                         foreach(InstalledPackage package in pkgListMan.SystemInstalledPkgList.Search(keys)) {\r
165                                 Console.WriteLine("{0} ({1})[@sys] - {2}", package.Name, package.Version, package.Summary);\r
166                         }\r
167                 }\r
168                 \r
169                 public void Show(string packagename)\r
170                 {\r
171                         PackageList<Package> allPkgs = new PackageList<Package>();\r
172                         allPkgs.AddPackages(pkgListMan.GetAllPackages());\r
173                         \r
174                         foreach (Package pkg in allPkgs.GetPackagesForName(packagename)) {\r
175                                 Console.WriteLine("Package: {0}", pkg.Name);\r
176                                 Console.WriteLine("Version: {0}", pkg.Version);\r
177                                 if (pkg.Url != null) Console.WriteLine("Website: {0}", pkg.Url.Href);\r
178                                 if (pkg.Tags != null) Console.WriteLine("Tag: {0}", pkg.Tags.ToLower());\r
179                                 Console.WriteLine("Type: {0}", pkg.Type);\r
180                                 if (pkg.License != null) Console.WriteLine("License: {0}", pkg.License);\r
181                                 if (pkg is InstalledPackage) Console.WriteLine("State: Installed");\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                 public void Download(string[] packagenames)\r
192                 {\r
193                         Installation[] installations = null;\r
194                         {\r
195                                 List<Package> downloadList = new List<Package>();\r
196                         \r
197                                 foreach(string packagename in packagenames) {\r
198                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
199                                         if (foundPackage == null) {\r
200                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
201                                                 Environment.Exit(1);\r
202                                         }\r
203                                         if (! downloadList.Contains(foundPackage)) {\r
204                                                 downloadList.Add(foundPackage);\r
205                                         }\r
206                                 }\r
207                                 \r
208                                 installations = new Installation[downloadList.Count];\r
209                                 for (int i = 0; i < installations.Length; i++) {\r
210                                         installations[i] = new Installation((Package) downloadList[i]);\r
211                                 }\r
212                         }\r
213                         \r
214                         Console.WriteLine("The following packages will be downloaded:");\r
215                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
216                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
217                                 Console.WriteLine("Abort.");\r
218                                 Environment.Exit(0);\r
219                         }\r
220                         \r
221                         {\r
222                                 int i = 0;\r
223                                 foreach (Installation inst in installations) {\r
224                                         i++;\r
225                                         \r
226                                         if (! inst.IsInstallablePackage()) {\r
227                                                 Console.WriteLine("E:{0} {1} can not be installed.", i, inst.ToString());\r
228                                                 continue;\r
229                                         }\r
230                                         \r
231                                         try {\r
232                                                 Console.Write("Get:{0} {1}", i, inst.ToString());\r
233                                                 inst.Download(downloader);\r
234                                                 Console.WriteLine(" ...Done");\r
235                                         } catch (Exception e) {\r
236                                                 Console.WriteLine("E: "+e.Message);\r
237                                                 throw new ApplicationException(string.Format("Failed to download {0}", inst.ToString()), e);\r
238                                         }\r
239                                 }\r
240                                 \r
241                                 i = 0;\r
242                                 foreach (Installation inst in installations) {\r
243                                         i++;\r
244                                         \r
245                                         if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {\r
246                                                 Console.WriteLine("Verify:{0} {1} does not match hash value!", i, inst.ToString());\r
247                                         }\r
248                                 }\r
249                         }\r
250                 }\r
251                 \r
252                 public void Install(string[] packagenames)\r
253                 {\r
254                         if (! NaGet.Utils.IsAdministrators()) {\r
255                                 Console.WriteLine("W: you are not administrators!");\r
256                         }\r
257                         \r
258                         Installation[] installations = null;\r
259                         {\r
260                                 List<Package> downloadList = new List<Package>();\r
261                         \r
262                                 foreach(string packagename in packagenames) {\r
263                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
264                                         if (foundPackage == null) {\r
265                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
266                                                 Environment.Exit(1);\r
267                                         }\r
268                                         if (! downloadList.Contains(foundPackage)) {\r
269                                                 downloadList.Add(foundPackage);\r
270                                         }\r
271                                 }\r
272                                 \r
273                                 installations = new Installation[downloadList.Count];\r
274                                 for (int i = 0; i < installations.Length; i++) {\r
275                                         installations[i] = new Installation((Package) downloadList[i]);\r
276                                         \r
277                                         if (! installations[i].IsInstallablePackage()) {\r
278                                                 Console.WriteLine("E: Can not install package {0} to your PC.", installations[i].ToString());\r
279                                                 Environment.Exit(1);\r
280                                         }\r
281                                 }\r
282                         }\r
283                         \r
284                         Console.WriteLine("The following packages will be downloaded:");\r
285                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
286                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
287                                 Console.WriteLine("Abort.");\r
288                                 Environment.Exit(0);\r
289                         }\r
290                         \r
291                         NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListMan, installations);\r
292                         tasks.Downloader = this.downloader;\r
293                         tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) { \r
294                                 switch (e.Type) {\r
295 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
296 //                                              break;\r
297                                         case NaGetTaskSetEventType.COMPLETED:\r
298                                                 Console.WriteLine("Done.");\r
299                                                 break;\r
300                                         case NaGetTaskSetEventType.STARTED_TASKSET:\r
301                                                 Console.Write("  " + e.TaskMessage);\r
302                                                 break;\r
303                                         case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
304                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);\r
305                                                 break;\r
306                                         case NaGetTaskSetEventType.INFO:\r
307                                                 Console.WriteLine("  " + e.TaskMessage);\r
308                                                 break;\r
309                                         case NaGetTaskSetEventType.ERROR:\r
310                                         case NaGetTaskSetEventType.WARNING:\r
311                                                 Console.WriteLine("  [Error] " + e.TaskMessage);\r
312                                                 break;\r
313                                 }\r
314                         };\r
315                         \r
316                         tasks.Run();\r
317                 }\r
318                 \r
319                 public void Remove(string[] packagenames)\r
320                 {\r
321                         if (! NaGet.Utils.IsAdministrators()) {\r
322                                 Console.WriteLine("W: you are not administrators!");\r
323                         }\r
324                         \r
325                         this.LocalUpdate();\r
326                         \r
327                         PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
328                         installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());\r
329                         \r
330                         Uninstallation[] uninstallations = new Uninstallation[packagenames.Length];\r
331                         for (int i = 0; i < packagenames.Length; i++) {\r
332                                 InstalledPackage pkg = installedPkgList.GetPackageForName(packagenames[i]);\r
333                                 \r
334                                 if (pkg == null) {\r
335                                         Console.WriteLine("E: could not found package " + packagenames[i]);\r
336                                         Environment.Exit(1);\r
337                                 }\r
338                                 \r
339                                 uninstallations[i] = new Uninstallation(pkg);\r
340                         }\r
341                         \r
342 //                      if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
343 //                              Console.WriteLine("Abort.");\r
344 //                              Environment.Exit(0);\r
345 //                      }\r
346                         \r
347                         NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListMan, uninstallations);\r
348                         tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) { \r
349                                 switch (e.Type) {\r
350 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
351 //                                              break;\r
352                                         case NaGetTaskSetEventType.COMPLETED:\r
353                                                 Console.WriteLine("Done.");\r
354                                                 break;\r
355                                         case NaGetTaskSetEventType.STARTED_TASKSET:\r
356                                                 Console.Write("  " + e.TaskMessage);\r
357                                                 break;\r
358                                         case NaGetTaskSetEventType.COMPLETED_TASKSET:\r
359                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);\r
360                                                 break;\r
361                                         case NaGetTaskSetEventType.INFO:\r
362                                                 Console.WriteLine("  " + e.TaskMessage);\r
363                                                 break;\r
364                                         case NaGetTaskSetEventType.ERROR:\r
365                                         case NaGetTaskSetEventType.WARNING:\r
366                                                 Console.WriteLine("  [Error] " + e.TaskMessage);\r
367                                                 break;\r
368                                 }\r
369                         };\r
370                         \r
371                         tasks.Run();\r
372                 }\r
373                 \r
374                 public void CleanCache(string[] packages)\r
375                 {\r
376                         if (! Directory.Exists(NaGet.Env.ArchiveFolderPath)) {\r
377                                 return; \r
378                         }\r
379                         \r
380                         int i = 0;\r
381                         if (packages.Length == 0) {\r
382                                 foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath)) {\r
383                                         Directory.Delete(folder, true);\r
384                                         i ++;\r
385                                 }\r
386                         } else {\r
387                                 foreach (string package in packages) {\r
388                                         foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath, package+"*", SearchOption.TopDirectoryOnly)) {\r
389                                                 Directory.Delete(folder, true);\r
390                                                 i ++;\r
391                                         }\r
392                                 }\r
393                         }\r
394                         if (i > 0) {\r
395                                 Console.WriteLine("... Done.");\r
396                         }\r
397                 }\r
398                 \r
399                 public void Help()\r
400                 {\r
401                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
402                         Console.Write("Usage:");\r
403                         \r
404                         Console.WriteLine("\t{0} update|localupdate", executeFileName);\r
405                         Console.WriteLine("\t{0} search|show pkg1 [pkg2 ...]", executeFileName);\r
406                         Console.WriteLine("\t{0} cleancache [pkg ...]", executeFileName);\r
407                         Console.WriteLine("\t{0} download pkg1 [pkg2 ...]", executeFileName);\r
408                         Console.WriteLine("\t{0} install|uninstall pkg1 [pkg2 ...]", executeFileName);\r
409                         Console.WriteLine();\r
410                         Console.WriteLine("{0} is a simple command line interface for downloading and "+\r
411                                           "installing packages. The most frequently used commands are update "+\r
412                                           "and install.", executeFileName);\r
413                         Console.WriteLine();\r
414                         Console.WriteLine("Commands:");\r
415                         Console.WriteLine("   update - Retrieve new lists of packages");\r
416                         Console.WriteLine("   localupdate - Update installed-soft-list only");\r
417                         Console.WriteLine("   checkupgrade - Show upgraded-soft list");\r
418                         Console.WriteLine("   search - Search the package list for not a regex pattern");\r
419                         Console.WriteLine("   show - Show package detail");\r
420                         Console.WriteLine("   cleancache - Clear cached archived file(s)");\r
421                         Console.WriteLine("   download - Download only - do NOT install or unpack archives");\r
422                         Console.WriteLine("   install - Install new packages");\r
423                         Console.WriteLine("   remove - Uninstall packages");\r
424                         Console.WriteLine();\r
425                 }\r
426                 \r
427                 public void FooBar()\r
428                 {\r
429                         foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList.Packages) {\r
430                                 Console.WriteLine("{0} : {1}", pkg.Name, pkg.UninstallInfo.UninstallString);\r
431                         }\r
432                 }\r
433                 \r
434                 public void Hoge()\r
435                 {\r
436                         foreach (UninstallInformation uInfo in RegistriedUninstallers.Uninstallers) {\r
437                                 if (! uInfo.IsOSPatch && ! uInfo.IsSystemComponent) {\r
438                                         Console.WriteLine("{0}", uInfo.DisplayName);\r
439                                 }\r
440                         }\r
441                 }\r
442                 \r
443                 public void Moo()\r
444                 {\r
445                         // TODO スーパー牛さんパワー化\r
446                         \r
447                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
448                         Console.WriteLine("誰か {0} をスーパー牛さんパワー化してくれ", executeFileName);\r
449                 }\r
450                 \r
451                 public static void Main(string[] args)\r
452                 {\r
453                         // アーカイブSYSTEM32をパスに足す\r
454                         NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
455                         \r
456                         MainClass mc = new MainClass();\r
457                         \r
458                         if (args.Length == 0) {\r
459                                 mc.Help();\r
460                         } else if (args[0] == "update") {\r
461                                 if (args.Length != 1) {\r
462                                         Console.WriteLine("E: The update command takes no arguments");\r
463                                         Environment.Exit(1);\r
464                                 }\r
465                                 mc.Update();\r
466                         } else if (args[0] == "localupdate") {\r
467                                 if (args.Length != 1) {\r
468                                         Console.WriteLine("E: The update command takes no arguments");\r
469                                         Environment.Exit(1);\r
470                                 }\r
471                                 mc.LocalUpdate();\r
472                         } else if (args[0] == "search") {\r
473                                 if (args.Length <= 1) {\r
474                                         Console.WriteLine("E: You must give exactly one pattern");\r
475                                         Environment.Exit(1);\r
476                                 }\r
477                                 \r
478                                 mc.Search(string.Join(" ", args, 1, args.Length - 1));\r
479                         } else if (args[0] == "show") {\r
480                                 if (args.Length <= 1) {\r
481                                         Console.WriteLine("E: You must give exactly one pattern");\r
482                                         Environment.Exit(1);\r
483                                 }\r
484                                 \r
485                                 for (int i = 1; i < args.Length; i++) {\r
486                                         mc.Show(args[i]);\r
487                                 }\r
488                         } else if (args[0] == "download") {\r
489                                 if (args.Length <= 1) {\r
490                                         Console.WriteLine("E: Invalid operation download");\r
491                                         Environment.Exit(1);\r
492                                 }\r
493                                 \r
494                                 string[] packages = new string[args.Length - 1];\r
495                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
496                                 \r
497                                 mc.Download(packages);\r
498                         } else if (args[0] == "install") {\r
499                                 if (args.Length <= 1) {\r
500                                         Console.WriteLine("E: Invalid operation install");\r
501                                         Environment.Exit(1);\r
502                                 }\r
503                                 \r
504                                 string[] packages = new string[args.Length - 1];\r
505                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
506                                 \r
507                                 mc.Install(packages);\r
508                         } else if (args[0] == "checkupgrade") {\r
509                                 mc.CheckUpgrade();\r
510                         } else if (args[0] == "remove") {\r
511                                 if (args.Length <= 1) {\r
512                                         Console.WriteLine("E: Invalid operation remove");\r
513                                         Environment.Exit(1);\r
514                                 }\r
515                                 \r
516                                 string[] packages = new string[args.Length - 1];\r
517                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
518                                 \r
519                                 mc.Remove(packages);\r
520                         } else if (args[0] == "cleancache") {\r
521                                 string[] packages = new string[args.Length - 1];\r
522                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
523                                 \r
524                                 mc.CleanCache(packages);\r
525                         } else if (args[0] == "foobar") {\r
526                                 mc.FooBar();\r
527                         } else if (args[0] == "hoge") {\r
528                                 mc.Hoge();\r
529                         } else if (args[0] == "moo") {\r
530                                 mc.Moo();\r
531                         } else {\r
532                                 mc.Help();\r
533                                 \r
534                                 Environment.Exit(1);\r
535                         }\r
536                 }\r
537         }\r
538 }\r