OSDN Git Service

na-get-lib,all-getについてアップデート処理を新方式(NaGetUpdate2)に変更。
[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 using NaGet.Tasks;\r
10 \r
11 namespace AllGet\r
12 {\r
13 \r
14         class DownloadListener\r
15         {\r
16                 private string line = null;\r
17                 \r
18                 /// <summary>\r
19                 /// lineのコンソール上における長さ\r
20                 /// </summary>\r
21                 private int lineWidth = 0;\r
22         \r
23                 public void OnDownload(object sender, DownloadEventArgs a) {\r
24                         if (line != null) {\r
25                                 lock (line) {\r
26                                         int newpos = (lineWidth <= 0)? 0 : Console.CursorLeft - lineWidth;\r
27                                         if (newpos < 0) newpos = 0;\r
28                                         \r
29                                         // 空白で埋める\r
30                                         Console.CursorLeft = newpos;\r
31                                         for (int i = 0; i < lineWidth && Console.CursorLeft < Console.BufferWidth; i++) {\r
32                                                 Console.Write(' ');\r
33                                         }\r
34                                         Console.CursorLeft = newpos;\r
35                                         \r
36                                         line = null; lineWidth = 0;\r
37                                 }\r
38                         }\r
39                         \r
40                         switch (a.DownloadTaskType) {\r
41                         case DownloadEventType.INITED:\r
42                                 line = "starting...";\r
43                                 break;\r
44                         case DownloadEventType.CONNECTED:\r
45                         case DownloadEventType.DOWNLOADING:\r
46                                 line = a.TaskMessage;\r
47                                 break;\r
48                         case DownloadEventType.COMPLETED:\r
49                                 //\r
50                                 line = null;\r
51                                 break;\r
52                         case DownloadEventType.ERROR:\r
53                                 Console.Write("interrupted! ERROR!");\r
54                                 line = null;\r
55                                 break;\r
56                         }\r
57                         \r
58                         if (line != null) {\r
59                                 int posOld = Console.CursorLeft;\r
60                                 Console.Write(line);\r
61                                 lineWidth = Console.CursorLeft - posOld;\r
62                         }\r
63                 }\r
64         }\r
65 \r
66         class MainClass\r
67         {\r
68                 private Downloader downloader;\r
69                 \r
70                 private PackageListsManager pkgListMan;\r
71                 \r
72                 /// <summary>\r
73                 /// サイレントインストールをするか否か\r
74                 /// </summary>\r
75                 private bool silent = false;\r
76                 \r
77                 public MainClass()\r
78                 {\r
79                         downloader = new Downloader();\r
80                         DownloadListener dl = new DownloadListener();\r
81                         downloader.DownloadEventRaised += new EventHandler<DownloadEventArgs>(dl.OnDownload);\r
82                         \r
83                         pkgListMan = new PackageListsManager();\r
84                         pkgListMan.LoadPackageLists();\r
85                 }\r
86                 \r
87                 public void Update()\r
88                 {\r
89                         update(true);\r
90                 }\r
91                 \r
92                 public void LocalUpdate()\r
93                 {\r
94                         update(false);\r
95                 }\r
96                 \r
97                 public void update(bool downloadPackageListFlag)\r
98                 {\r
99                         NaGet.SubCommands.NaGetUpdate2 tasks = new NaGet.SubCommands.NaGetUpdate2(pkgListMan, downloadPackageListFlag);\r
100                         tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
101                                 switch (e.Type) {\r
102 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
103 //                                              break;\r
104                                         case TaskEventType.COMPLETED:\r
105                                                 Console.WriteLine("Done.");\r
106                                                 break;\r
107                                         case TaskEventType.STARTED_SUBTASK:\r
108                                                 Console.Write("  " + e.TaskMessage);\r
109                                                 break;\r
110                                         case TaskEventType.COMPLETED_SUBTASK:\r
111                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
112                                                 break;\r
113                                         case TaskEventType.INFO:\r
114                                                 Console.WriteLine("  " + e.TaskMessage);\r
115                                                 break;\r
116                                         case TaskEventType.ERROR:\r
117                                         case TaskEventType.WARNING:\r
118                                                 Console.WriteLine("  [Error]" + e.TaskMessage);\r
119                                                 break;\r
120                                 }\r
121                         };\r
122                         tasks.SubTaskEventRaised += delegate (object sender, TaskEventArgs e) {\r
123                                 if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
124                                         if (e.Type == TaskEventType.STARTED) {\r
125                                                 Console.WriteLine();\r
126                                         }\r
127                                         \r
128                                         int origPosX = Console.CursorLeft;\r
129                                         for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
130                                                 Console.Write(' ');\r
131                                         }\r
132                                         Console.CursorLeft = origPosX;\r
133                                         \r
134                                         if (e.Type == TaskEventType.PING) {\r
135                                                 int origPos = Console.CursorLeft;\r
136                                                 Console.Write(e.TaskMessage);\r
137                                                 Console.CursorLeft = origPos;\r
138                                         } else {\r
139                                                 Console.WriteLine(e.TaskMessage);\r
140                                         }\r
141                                 }\r
142                         };\r
143                         \r
144                         tasks.Run();\r
145                 }\r
146                 \r
147                 public void CheckUpgrade()\r
148                 {\r
149                         foreach (Package pkg in UpgradeFinder.GetUpgradePackages(pkgListMan)) {\r
150                                 bool isSystem = false;\r
151                                 Package instPkg = null;\r
152                                 \r
153                                 instPkg = pkgListMan.SystemInstalledPkgList.GetPackageForName(pkg.Name);\r
154                                 if (instPkg != null) {\r
155                                         isSystem = true;\r
156                                 } else {\r
157                                         instPkg = pkgListMan.InstalledPkgList.GetPackageForName(pkg.Name);\r
158                                         if (instPkg != null) {\r
159                                                 isSystem = false;\r
160                                         } else {\r
161                                                 System.Diagnostics.Debug.Fail("internal error.");\r
162                                         }\r
163                                 }\r
164                                 \r
165                                 Console.WriteLine("{0} ({1}){3} => ({2})", pkg.Name, instPkg.Version, pkg.Version, (isSystem)? "@sys" : "");\r
166                         }\r
167                 }\r
168                 \r
169                 public void Search(string keys)\r
170                 {\r
171                         foreach(Package package in pkgListMan.AvailablePkgList.Search(keys)) {\r
172                                 Console.WriteLine("{0} ({1}) - {2}", package.Name, package.Version, package.Summary);\r
173                         }\r
174                         \r
175                         foreach(InstalledPackage package in pkgListMan.InstalledPkgList.Search(keys)) {\r
176                                 Console.WriteLine("{0} ({1})[@install] - {2}", package.Name, package.Version, package.Summary);\r
177                         }\r
178                         \r
179                         foreach(InstalledPackage package in pkgListMan.SystemInstalledPkgList.Search(keys)) {\r
180                                 Console.WriteLine("{0} ({1})[@sys] - {2}", package.Name, package.Version, package.Summary);\r
181                         }\r
182                 }\r
183                 \r
184                 public void Show(string packagename)\r
185                 {\r
186                         PackageList<Package> allPkgs = new PackageList<Package>();\r
187                         allPkgs.AddPackages(pkgListMan.GetAllPackages());\r
188                         \r
189                         foreach (Package pkg in allPkgs.GetPackagesForName(packagename)) {\r
190                                 Console.WriteLine("Package: {0}", pkg.Name);\r
191                                 Console.WriteLine("Version: {0}", pkg.Version);\r
192                                 Console.WriteLine("Summary: {0}", pkg.Summary);\r
193                                 if (pkg.Url != null) Console.WriteLine("Website: {0}", pkg.Url.Href);\r
194                                 if (pkg.Tags != null) Console.WriteLine("Tag: {0}", pkg.Tags.ToLower());\r
195                                 Console.WriteLine("Type: {0}", pkg.Type);\r
196                                 if (pkg.License != null) Console.WriteLine("License: {0}", pkg.License);\r
197                                 if (pkg is InstalledPackage) Console.WriteLine("State: Installed");\r
198                                 Console.WriteLine("Repository: {0}", pkg.PackageListName);\r
199                                 \r
200                                 if (pkg.Description != null) {\r
201                                         Console.WriteLine("Description:");\r
202                                         Console.WriteLine(pkg.Description);\r
203                                 }\r
204                                 Console.WriteLine();\r
205                         }\r
206                 }\r
207                 \r
208                 public void Download(string[] packagenames)\r
209                 {\r
210                         Installation[] installations = null;\r
211                         {\r
212                                 List<Package> downloadList = new List<Package>();\r
213                         \r
214                                 foreach(string packagename in packagenames) {\r
215                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
216                                         if (foundPackage == null) {\r
217                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
218                                                 Environment.Exit(1);\r
219                                         }\r
220                                         if (! downloadList.Contains(foundPackage)) {\r
221                                                 downloadList.Add(foundPackage);\r
222                                         }\r
223                                 }\r
224                                 \r
225                                 installations = new Installation[downloadList.Count];\r
226                                 for (int i = 0; i < installations.Length; i++) {\r
227                                         installations[i] = new Installation(downloadList[i]);\r
228                                 }\r
229                         }\r
230                         \r
231                         Console.WriteLine("The following packages will be downloaded:");\r
232                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
233                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
234                                 Console.WriteLine("Abort.");\r
235                                 Environment.Exit(0);\r
236                         }\r
237                         \r
238                         NaGet.SubCommands.NaGetDownloadToCache tasks = new NaGet.SubCommands.NaGetDownloadToCache(pkgListMan, installations);\r
239                         tasks.TaskQueryRaised += delegate(object sender, NaGetTaskQueryArgs e) {\r
240                                 char result = '\u0000';\r
241                                 if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
242                                         result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
243                                 } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
244                                         result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
245                                 } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
246                                         result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
247                                 } else {\r
248                                         result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
249                                 }\r
250                                 \r
251                                 switch (result) {\r
252                                         case 'y':\r
253                                                 return NaGetTaskQueryResult.CONTINUE;\r
254                                         case 'n':\r
255                                                 return NaGetTaskQueryResult.CANCEL;\r
256                                         case 'r':\r
257                                                 return NaGetTaskQueryResult.RETRY;\r
258                                         default:\r
259                                                 return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
260                                 }\r
261                         };\r
262                         tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
263                                 switch (e.Type) {\r
264 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
265 //                                              break;\r
266                                         case TaskEventType.COMPLETED:\r
267                                                 Console.WriteLine("Done.");\r
268                                                 break;\r
269                                         case TaskEventType.STARTED_SUBTASK:\r
270                                                 Console.Write("  " + e.TaskMessage);\r
271                                                 break;\r
272                                         case TaskEventType.COMPLETED_SUBTASK:\r
273                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
274                                                 break;\r
275                                         case TaskEventType.INFO:\r
276                                                 Console.WriteLine("  " + e.TaskMessage);\r
277                                                 break;\r
278                                         case TaskEventType.ERROR:\r
279                                         case TaskEventType.WARNING:\r
280                                                 Console.WriteLine("  [Error] " + e.TaskMessage);\r
281                                                 break;\r
282                                 }\r
283                         };\r
284                         DownloadListener dl = new DownloadListener();\r
285                         tasks.Downloader.DownloadEventRaised += dl.OnDownload;\r
286                         \r
287                         tasks.Run();\r
288                 }\r
289                 \r
290                 public void Install(string[] packagenames)\r
291                 {\r
292                         if (! NaGet.Utils.IsAdministrators()) {\r
293                                 Console.WriteLine("W: you are not administrators!");\r
294                         }\r
295                         \r
296                         Installation[] installations = null;\r
297                         {\r
298                                 List<Package> downloadList = new List<Package>();\r
299                         \r
300                                 foreach(string packagename in packagenames) {\r
301                                         Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);\r
302                                         if (foundPackage == null) {\r
303                                                 Console.WriteLine("E: Couldn't find package "+packagename);\r
304                                                 Environment.Exit(1);\r
305                                         }\r
306                                         if (! downloadList.Contains(foundPackage)) {\r
307                                                 downloadList.Add(foundPackage);\r
308                                         }\r
309                                 }\r
310                                 \r
311                                 installations = new Installation[downloadList.Count];\r
312                                 for (int i = 0; i < installations.Length; i++) {\r
313                                         installations[i] = new Installation(downloadList[i]);\r
314                                         \r
315                                         installations[i].Silent = silent; // サイレントインストールのフラグの設定\r
316                                         \r
317                                         if (! installations[i].IsInstallablePackage()) {\r
318                                                 Console.WriteLine("E: Can not install package {0} to your PC.", installations[i].ToString());\r
319                                                 Environment.Exit(1);\r
320                                         }\r
321                                 }\r
322                         }\r
323                         \r
324                         Installation[] depInstallations;\r
325                         {\r
326                                 Installation[] resolved;\r
327                                 DependeciesResolver.ResolveInstallations(installations, pkgListMan, out resolved, out depInstallations);\r
328                                 installations = resolved;\r
329                         }\r
330                         \r
331                         Console.WriteLine("The following packages will be downloaded:");\r
332                         Console.WriteLine("  {0}", Installation.ToString(installations));\r
333                         if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
334                                 Console.WriteLine("Abort.");\r
335                                 Environment.Exit(0);\r
336                         }\r
337                         \r
338                         NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListMan, installations);\r
339                         tasks.TaskQueryRaised += delegate(object sender, NaGetTaskQueryArgs e) {\r
340                                 char result = '\u0000';\r
341                                 if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
342                                         result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
343                                 } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
344                                         result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
345                                 } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
346                                         result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
347                                 } else {\r
348                                         result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
349                                 }\r
350                                 \r
351                                 switch (result) {\r
352                                         case 'y':\r
353                                                 return NaGetTaskQueryResult.CONTINUE;\r
354                                         case 'n':\r
355                                                 return NaGetTaskQueryResult.CANCEL;\r
356                                         case 'r':\r
357                                                 return NaGetTaskQueryResult.RETRY;\r
358                                         default:\r
359                                                 return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
360                                 }\r
361                         };\r
362                         tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
363                                 switch (e.Type) {\r
364 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
365 //                                              break;\r
366                                         case TaskEventType.COMPLETED:\r
367                                                 Console.WriteLine("Done.");\r
368                                                 break;\r
369                                         case TaskEventType.STARTED_SUBTASK:\r
370                                                 Console.Write("  " + e.TaskMessage);\r
371                                                 break;\r
372                                         case TaskEventType.COMPLETED_SUBTASK:\r
373                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
374                                                 break;\r
375                                         case TaskEventType.INFO:\r
376                                                 Console.WriteLine("  " + e.TaskMessage);\r
377                                                 break;\r
378                                         case TaskEventType.ERROR:\r
379                                         case TaskEventType.WARNING:\r
380                                                 Console.WriteLine("  [Error] " + e.TaskMessage);\r
381                                                 break;\r
382                                 }\r
383                         };\r
384                         DownloadListener dl = new DownloadListener();\r
385                         tasks.Downloader.DownloadEventRaised += dl.OnDownload;\r
386                         \r
387                         tasks.Run();\r
388                 }\r
389                 \r
390                 public void Remove(string[] packagenames)\r
391                 {\r
392                         if (! NaGet.Utils.IsAdministrators()) {\r
393                                 Console.WriteLine("W: you are not administrators!");\r
394                         }\r
395                         \r
396                         this.LocalUpdate();\r
397                         \r
398                         PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
399                         installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());\r
400                         \r
401                         Uninstallation[] uninstallations = new Uninstallation[packagenames.Length];\r
402                         for (int i = 0; i < packagenames.Length; i++) {\r
403                                 InstalledPackage pkg = installedPkgList.GetPackageForName(packagenames[i]);\r
404                                 \r
405                                 if (pkg == null) {\r
406                                         Console.WriteLine("E: could not found package " + packagenames[i]);\r
407                                         Environment.Exit(1);\r
408                                 }\r
409                                 \r
410                                 uninstallations[i] = new Uninstallation(pkg);\r
411                         }\r
412                         \r
413 //                      if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
414 //                              Console.WriteLine("Abort.");\r
415 //                              Environment.Exit(0);\r
416 //                      }\r
417                         \r
418                         NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListMan, uninstallations);\r
419                         tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
420                                 switch (e.Type) {\r
421 //                                      case NaGetTaskSetEventType.COMPLETED_TASKSET\r
422 //                                              break;\r
423                                         case TaskEventType.COMPLETED:\r
424                                                 Console.WriteLine("Done.");\r
425                                                 break;\r
426                                         case TaskEventType.STARTED_SUBTASK:\r
427                                                 Console.Write("  " + e.TaskMessage);\r
428                                                 break;\r
429                                         case TaskEventType.COMPLETED_SUBTASK:\r
430                                                 Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
431                                                 break;\r
432                                         case TaskEventType.INFO:\r
433                                                 Console.WriteLine("  " + e.TaskMessage);\r
434                                                 break;\r
435                                         case TaskEventType.ERROR:\r
436                                         case TaskEventType.WARNING:\r
437                                                 Console.WriteLine("  [Error] " + e.TaskMessage);\r
438                                                 break;\r
439                                 }\r
440                         };\r
441                         \r
442                         tasks.Run();\r
443                 }\r
444                 \r
445                 public void CleanCache(string[] packages)\r
446                 {\r
447                         if (! Directory.Exists(NaGet.Env.ArchiveFolderPath)) {\r
448                                 return; \r
449                         }\r
450                         \r
451                         int i = 0;\r
452                         if (packages.Length == 0) {\r
453                                 foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath)) {\r
454                                         Directory.Delete(folder, true);\r
455                                         i ++;\r
456                                 }\r
457                         } else {\r
458                                 foreach (string package in packages) {\r
459                                         foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath, package+"*", SearchOption.TopDirectoryOnly)) {\r
460                                                 Directory.Delete(folder, true);\r
461                                                 i ++;\r
462                                         }\r
463                                 }\r
464                         }\r
465                         if (i > 0) {\r
466                                 Console.WriteLine("... Done.");\r
467                         }\r
468                 }\r
469                 \r
470                 public void Help()\r
471                 {\r
472                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
473                         Console.Write("Usage:");\r
474                         \r
475                         Console.WriteLine("\t{0} update|localupdate", executeFileName);\r
476                         Console.WriteLine("\t{0} search|show pkg1 [pkg2 ...]", executeFileName);\r
477                         Console.WriteLine("\t{0} cleancache [pkg ...]", executeFileName);\r
478                         Console.WriteLine("\t{0} download pkg1 [pkg2 ...]", executeFileName);\r
479                         Console.WriteLine("\t{0} [--quiet] install|uninstall pkg1 [pkg2 ...]", executeFileName);\r
480                         Console.WriteLine();\r
481                         Console.WriteLine("{0} is a simple command line interface for downloading and "+\r
482                                           "installing packages. The most frequently used commands are update "+\r
483                                           "and install.", executeFileName);\r
484                         Console.WriteLine();\r
485                         Console.WriteLine("Commands:");\r
486                         Console.WriteLine("   update - Retrieve new lists of packages");\r
487                         Console.WriteLine("   localupdate - Update installed-soft-list only");\r
488                         Console.WriteLine("   checkupgrade - Show upgraded-soft list");\r
489                         Console.WriteLine("   search - Search the package list for not a regex pattern");\r
490                         Console.WriteLine("   show - Show package detail");\r
491                         Console.WriteLine("   cleancache - Clear cached archived file(s)");\r
492                         Console.WriteLine("   download - Download only - do NOT install or unpack archives");\r
493                         Console.WriteLine("   install - Install new packages");\r
494                         Console.WriteLine("   remove - Uninstall packages");\r
495                         Console.WriteLine();\r
496                 }\r
497                 \r
498                 public void FooBar()\r
499                 {\r
500                         foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList) {\r
501                                 Console.WriteLine("{0} : {1}", pkg.Name, pkg.UninstallInfo.UninstallString);\r
502                         }\r
503                 }\r
504                 \r
505                 public void Hoge()\r
506                 {\r
507                         foreach (UninstallInformation uInfo in RegistriedUninstallers.Uninstallers) {\r
508                                 if (! uInfo.IsOSPatch && ! uInfo.IsSystemComponent) {\r
509                                         Console.WriteLine("{0}", uInfo.DisplayName);\r
510                                 }\r
511                         }\r
512                 }\r
513                 \r
514                 public void Moo()\r
515                 {\r
516                         // TODO スーパー牛さんパワー化\r
517                         \r
518                         string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;\r
519                         Console.WriteLine("誰か {0} をスーパー牛さんパワー化してくれ", executeFileName);\r
520                 }\r
521                 \r
522                 [STAThread]\r
523                 public static void Main(string[] args)\r
524                 {\r
525                         // アーカイブSYSTEM32をパスに足す\r
526                         NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
527                         \r
528                         MainClass mc = new MainClass();\r
529                         \r
530                         if (args.Length == 0) {\r
531                                 mc.Help();\r
532                                 \r
533                                 Environment.Exit(1);\r
534                         } else {\r
535                                 try {\r
536                                         NaGet.ArgParser argParser = new NaGet.ArgParser();\r
537                                         argParser["quiet"] = false;\r
538                                         args = argParser.Parse(args);\r
539                                         mc.silent = (bool) argParser["quiet"];\r
540                                 } catch (ApplicationException e) {\r
541                                         Console.WriteLine(e.Message);\r
542                                         Environment.Exit(1);\r
543                                 }\r
544                         }\r
545                         \r
546                         // 引数ごとに操作を決定\r
547                         switch (args[0])\r
548                         {\r
549                                 case "update":\r
550                                         if (args.Length != 1) {\r
551                                                 Console.WriteLine("E: The update command takes no arguments");\r
552                                                 Environment.Exit(1);\r
553                                         }\r
554                                         mc.Update();\r
555                                         break;\r
556                                 case "localupate":\r
557                                         if (args.Length != 1) {\r
558                                                 Console.WriteLine("E: The update command takes no arguments");\r
559                                                 Environment.Exit(1);\r
560                                         }\r
561                                         mc.LocalUpdate();\r
562                                         break;\r
563                                 case "search":\r
564                                         if (args.Length <= 1) {\r
565                                                 Console.WriteLine("E: You must give exactly one pattern");\r
566                                                 Environment.Exit(1);\r
567                                         }\r
568                                         \r
569                                         mc.Search(string.Join(" ", args, 1, args.Length - 1));\r
570                                         break;\r
571                                 case "show":\r
572                                         if (args.Length <= 1) {\r
573                                                 Console.WriteLine("E: You must give exactly one pattern");\r
574                                                 Environment.Exit(1);\r
575                                         }\r
576                                         \r
577                                         for (int i = 1; i < args.Length; i++) {\r
578                                                 mc.Show(args[i]);\r
579                                         }\r
580                                         break;\r
581                                 case "download":\r
582                                         if (args.Length <= 1) {\r
583                                                 Console.WriteLine("E: Invalid operation download");\r
584                                                 Environment.Exit(1);\r
585                                         } else {\r
586                                                 string[] packages = new string[args.Length - 1];\r
587                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
588                                                 \r
589                                                 mc.Download(packages);\r
590                                         }\r
591                                         break;\r
592                                 case "install":\r
593                                         if (args.Length <= 1) {\r
594                                                 Console.WriteLine("E: Invalid operation install");\r
595                                                 Environment.Exit(1);\r
596                                         } else {\r
597                                                 string[] packages = new string[args.Length - 1];\r
598                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
599                                                 \r
600                                                 mc.Install(packages);\r
601                                         }\r
602                                         break;\r
603                                 case "checkupgrade":\r
604                                         mc.CheckUpgrade();\r
605                                         break;\r
606                                 case "remove":\r
607                                         if (args.Length <= 1) {\r
608                                                 Console.WriteLine("E: Invalid operation remove");\r
609                                                 Environment.Exit(1);\r
610                                         } else {\r
611                                                 string[] packages = new string[args.Length - 1];\r
612                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
613                                                 \r
614                                                 mc.Remove(packages);\r
615                                         }\r
616                                         break;\r
617                                 case "cleancache":\r
618                                         {\r
619                                                 string[] packages = new string[args.Length - 1];\r
620                                                 Array.Copy(args, 1, packages, 0, packages.Length);\r
621                                                 \r
622                                                 mc.CleanCache(packages);\r
623                                         }\r
624                                         break;\r
625                                 case "foobar":\r
626                                         mc.FooBar();\r
627                                         break;\r
628                                 case "hoge":\r
629                                         mc.Hoge();\r
630                                         break;\r
631                                 case "moo":\r
632                                         mc.Moo();\r
633                                         break;\r
634                                 default:\r
635                                         mc.Help();\r
636                                         \r
637                                         Environment.Exit(1);\r
638                                         break;\r
639                         }\r
640                 }\r
641         }\r
642 }\r