OSDN Git Service

na-get-lib,UserAgentをアセンブリバージョンなどから取得するよう変更
[applistation/AppliStation.git] / all-get / Main.cs
index 1091025..a4be970 100644 (file)
@@ -2,7 +2,6 @@
 using System;\r
 using System.IO;\r
 using System.Collections.Generic;\r
-using NaGet.Net;\r
 using NaGet.Packages;\r
 using NaGet.Packages.Install;\r
 using NaGet.SubCommands;\r
@@ -10,63 +9,92 @@ using NaGet.Tasks;
 \r
 namespace AllGet\r
 {\r
-\r
-       class DownloadListener\r
+       sealed class TaskSetEventHandlers\r
        {\r
-               private string line = null;\r
+               public static void OnTaskSetEvent(object sender, TaskEventArgs e) { \r
+                       switch (e.Type) {\r
+//                             case NaGetTaskSetEventType.COMPLETED_TASKSET\r
+//                                     break;\r
+                               case TaskEventType.COMPLETED:\r
+                                       Console.WriteLine("Done.");\r
+                                       break;\r
+                               case TaskEventType.STARTED_SUBTASK:\r
+                                       Console.Write("  " + e.TaskMessage);\r
+                                       break;\r
+                               case TaskEventType.COMPLETED_SUBTASK:\r
+                                       Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
+                                       break;\r
+                               case TaskEventType.INFO:\r
+                                       Console.WriteLine("  " + e.TaskMessage);\r
+                                       break;\r
+                               case TaskEventType.ERROR:\r
+                               case TaskEventType.WARNING:\r
+                                       Console.WriteLine("  [Error]" + e.TaskMessage);\r
+                                       break;\r
+                       }\r
+               }\r
                \r
-               /// <summary>\r
-               /// lineのコンソール上における長さ\r
-               /// </summary>\r
-               private int lineWidth = 0;\r
-       \r
-               public void OnDownload(object sender, DownloadEventArgs a) {\r
-                       if (line != null) {\r
-                               lock (line) {\r
-                                       int newpos = (lineWidth <= 0)? 0 : Console.CursorLeft - lineWidth;\r
-                                       if (newpos < 0) newpos = 0;\r
-                                       \r
-                                       // 空白で埋める\r
-                                       Console.CursorLeft = newpos;\r
-                                       for (int i = 0; i < lineWidth && Console.CursorLeft < Console.BufferWidth; i++) {\r
-                                               Console.Write(' ');\r
-                                       }\r
-                                       Console.CursorLeft = newpos;\r
-                                       \r
-                                       line = null; lineWidth = 0;\r
+               public static void OnDownloadSubTaskEvent(object sender, TaskEventArgs e)\r
+               {\r
+                       if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
+                               if (e.Type == TaskEventType.STARTED) {\r
+                                       Console.WriteLine();\r
+                               }\r
+                               \r
+                               int origPosX = Console.CursorLeft;\r
+                               for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
+                                       Console.Write(' ');\r
+                               }\r
+                               Console.CursorLeft = origPosX;\r
+                               \r
+                               switch (e.Type) {\r
+                                       case TaskEventType.PING:\r
+                                               {\r
+                                                       int origPos = Console.CursorLeft;\r
+                                                       Console.Write(e.TaskMessage);\r
+                                                       Console.CursorLeft = origPos;\r
+                                               }\r
+                                               break;\r
+                                       case TaskEventType.WARNING:\r
+                                       case TaskEventType.ERROR:\r
+                                               Console.WriteLine("  [Error]" + e.TaskMessage);\r
+                                               break;\r
+                                       default:\r
+                                               Console.WriteLine(e.TaskMessage);\r
+                                               break;\r
                                }\r
                        }\r
-                       \r
-                       switch (a.DownloadTaskType) {\r
-                       case DownloadEventType.INITED:\r
-                               line = "starting...";\r
-                               break;\r
-                       case DownloadEventType.CONNECTED:\r
-                       case DownloadEventType.DOWNLOADING:\r
-                               line = a.TaskMessage;\r
-                               break;\r
-                       case DownloadEventType.COMPLETED:\r
-                               //\r
-                               line = null;\r
-                               break;\r
-                       case DownloadEventType.ERROR:\r
-                               Console.Write("interrupted! ERROR!");\r
-                               line = null;\r
-                               break;\r
+               }\r
+               \r
+               public static NaGetTaskQueryResult OnTaskQueryEvent(object sender, NaGetTaskQueryArgs e)\r
+               {\r
+                       char result = '\u0000';\r
+                       if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
+                               result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
+                       } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
+                               result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
+                       } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
+                               result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
+                       } else {\r
+                               result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
                        }\r
                        \r
-                       if (line != null) {\r
-                               int posOld = Console.CursorLeft;\r
-                               Console.Write(line);\r
-                               lineWidth = Console.CursorLeft - posOld;\r
+                       switch (result) {\r
+                               case 'y':\r
+                                       return NaGetTaskQueryResult.CONTINUE;\r
+                               case 'n':\r
+                                       return NaGetTaskQueryResult.CANCEL;\r
+                               case 'r':\r
+                                       return NaGetTaskQueryResult.RETRY;\r
+                               default:\r
+                                       return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
                        }\r
                }\r
        }\r
-\r
+       \r
+       \r
        class MainClass\r
        {\r
-               private Downloader downloader;\r
-               \r
                private PackageListsManager pkgListMan;\r
                \r
                /// <summary>\r
@@ -76,10 +104,6 @@ namespace AllGet
                \r
                public MainClass()\r
                {\r
-                       downloader = new Downloader();\r
-                       DownloadListener dl = new DownloadListener();\r
-                       downloader.DownloadEventRaised += new EventHandler<DownloadEventArgs>(dl.OnDownload);\r
-                       \r
                        pkgListMan = new PackageListsManager();\r
                        pkgListMan.LoadPackageLists();\r
                }\r
@@ -97,49 +121,8 @@ namespace AllGet
                public void update(bool downloadPackageListFlag)\r
                {\r
                        NaGet.SubCommands.NaGetUpdate2 tasks = new NaGet.SubCommands.NaGetUpdate2(pkgListMan, downloadPackageListFlag);\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error]" + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
-                       tasks.SubTaskEventRaised += delegate (object sender, TaskEventArgs e) {\r
-                               if (sender is NaGet.SubCommands.SubTask.DownloadSubTask) {\r
-                                       if (e.Type == TaskEventType.STARTED) {\r
-                                               Console.WriteLine();\r
-                                       }\r
-                                       \r
-                                       int origPosX = Console.CursorLeft;\r
-                                       for (int i = 0; i < Console.WindowWidth - 1; i++) {\r
-                                               Console.Write(' ');\r
-                                       }\r
-                                       Console.CursorLeft = origPosX;\r
-                                       \r
-                                       if (e.Type == TaskEventType.PING) {\r
-                                               int origPos = Console.CursorLeft;\r
-                                               Console.Write(e.TaskMessage);\r
-                                               Console.CursorLeft = origPos;\r
-                                       } else {\r
-                                               Console.WriteLine(e.TaskMessage);\r
-                                       }\r
-                               }\r
-                       };\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
+                       tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
                        \r
                        tasks.Run();\r
                }\r
@@ -205,6 +188,7 @@ namespace AllGet
                        }\r
                }\r
                \r
+               \r
                public void Download(string[] packagenames)\r
                {\r
                        Installation[] installations = null;\r
@@ -235,54 +219,10 @@ namespace AllGet
                                Environment.Exit(0);\r
                        }\r
                        \r
-                       NaGet.SubCommands.NaGetDownloadToCache tasks = new NaGet.SubCommands.NaGetDownloadToCache(pkgListMan, installations);\r
-                       tasks.TaskQueryRaised += delegate(object sender, NaGetTaskQueryArgs e) {\r
-                               char result = '\u0000';\r
-                               if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
-                                       result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
-                               } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
-                                       result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
-                               } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
-                                       result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
-                               } else {\r
-                                       result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
-                               }\r
-                               \r
-                               switch (result) {\r
-                                       case 'y':\r
-                                               return NaGetTaskQueryResult.CONTINUE;\r
-                                       case 'n':\r
-                                               return NaGetTaskQueryResult.CANCEL;\r
-                                       case 'r':\r
-                                               return NaGetTaskQueryResult.RETRY;\r
-                                       default:\r
-                                               return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
-                               }\r
-                       };\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error] " + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
-                       DownloadListener dl = new DownloadListener();\r
-                       tasks.Downloader.DownloadEventRaised += dl.OnDownload;\r
+                       NaGet.SubCommands.NaGetDownloadToCache2 tasks = new NaGet.SubCommands.NaGetDownloadToCache2(pkgListMan, installations);\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
+                       tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
+                       tasks.TaskQueryRaised += TaskSetEventHandlers.OnTaskQueryEvent;\r
                        \r
                        tasks.Run();\r
                }\r
@@ -311,13 +251,6 @@ namespace AllGet
                                installations = new Installation[downloadList.Count];\r
                                for (int i = 0; i < installations.Length; i++) {\r
                                        installations[i] = new Installation(downloadList[i]);\r
-                                       \r
-                                       installations[i].Silent = silent; // サイレントインストールのフラグの設定\r
-                                       \r
-                                       if (! installations[i].IsInstallablePackage()) {\r
-                                               Console.WriteLine("E: Can not install package {0} to your PC.", installations[i].ToString());\r
-                                               Environment.Exit(1);\r
-                                       }\r
                                }\r
                        }\r
                        \r
@@ -335,54 +268,10 @@ namespace AllGet
                                Environment.Exit(0);\r
                        }\r
                        \r
-                       NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListMan, installations);\r
-                       tasks.TaskQueryRaised += delegate(object sender, NaGetTaskQueryArgs e) {\r
-                               char result = '\u0000';\r
-                               if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
-                                       result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');\r
-                               } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {\r
-                                       result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');\r
-                               } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {\r
-                                       result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');\r
-                               } else {\r
-                                       result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');\r
-                               }\r
-                               \r
-                               switch (result) {\r
-                                       case 'y':\r
-                                               return NaGetTaskQueryResult.CONTINUE;\r
-                                       case 'n':\r
-                                               return NaGetTaskQueryResult.CANCEL;\r
-                                       case 'r':\r
-                                               return NaGetTaskQueryResult.RETRY;\r
-                                       default:\r
-                                               return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;\r
-                               }\r
-                       };\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error] " + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
-                       DownloadListener dl = new DownloadListener();\r
-                       tasks.Downloader.DownloadEventRaised += dl.OnDownload;\r
+                       NaGet.SubCommands.NaGetInstall2 tasks = new NaGet.SubCommands.NaGetInstall2(pkgListMan, installations);\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
+                       tasks.SubTaskEventRaised += TaskSetEventHandlers.OnDownloadSubTaskEvent;\r
+                       tasks.TaskQueryRaised += TaskSetEventHandlers.OnTaskQueryEvent;\r
                        \r
                        tasks.Run();\r
                }\r
@@ -398,16 +287,14 @@ namespace AllGet
                        PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();\r
                        installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());\r
                        \r
-                       Uninstallation[] uninstallations = new Uninstallation[packagenames.Length];\r
+                       InstalledPackage[] insPkgs = new InstalledPackage[packagenames.Length];\r
                        for (int i = 0; i < packagenames.Length; i++) {\r
-                               InstalledPackage pkg = installedPkgList.GetPackageForName(packagenames[i]);\r
+                               insPkgs[i] = installedPkgList.GetPackageForName(packagenames[i]);\r
                                \r
-                               if (pkg == null) {\r
+                               if (insPkgs[i] == null) {\r
                                        Console.WriteLine("E: could not found package " + packagenames[i]);\r
                                        Environment.Exit(1);\r
                                }\r
-                               \r
-                               uninstallations[i] = new Uninstallation(pkg);\r
                        }\r
                        \r
 //                     if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {\r
@@ -415,29 +302,8 @@ namespace AllGet
 //                             Environment.Exit(0);\r
 //                     }\r
                        \r
-                       NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListMan, uninstallations);\r
-                       tasks.TaskEventRaised += delegate(object sender, TaskEventArgs e) { \r
-                               switch (e.Type) {\r
-//                                     case NaGetTaskSetEventType.COMPLETED_TASKSET\r
-//                                             break;\r
-                                       case TaskEventType.COMPLETED:\r
-                                               Console.WriteLine("Done.");\r
-                                               break;\r
-                                       case TaskEventType.STARTED_SUBTASK:\r
-                                               Console.Write("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.COMPLETED_SUBTASK:\r
-                                               Console.WriteLine(" ... Done. [{0}%]", (int) e.ProgressPercent);\r
-                                               break;\r
-                                       case TaskEventType.INFO:\r
-                                               Console.WriteLine("  " + e.TaskMessage);\r
-                                               break;\r
-                                       case TaskEventType.ERROR:\r
-                                       case TaskEventType.WARNING:\r
-                                               Console.WriteLine("  [Error] " + e.TaskMessage);\r
-                                               break;\r
-                               }\r
-                       };\r
+                       NaGet.SubCommands.NaGetUninstall2 tasks = new NaGet.SubCommands.NaGetUninstall2(pkgListMan, insPkgs);\r
+                       tasks.TaskEventRaised += TaskSetEventHandlers.OnTaskSetEvent;\r
                        \r
                        tasks.Run();\r
                }\r