1 // project created on 2007/09/08 at 20:20
\r
4 using System.Collections.Generic;
\r
6 using NaGet.Packages;
\r
7 using NaGet.Packages.Install;
\r
8 using NaGet.SubCommands;
\r
13 class DownloadListener
\r
15 private string line = null;
\r
18 /// lineのコンソール上における長さ
\r
20 private int lineWidth = 0;
\r
22 public void OnDownload(object sender, DownloadEventArgs a) {
\r
25 int newpos = (lineWidth <= 0)? 0 : Console.CursorLeft - lineWidth;
\r
26 if (newpos < 0) newpos = 0;
\r
29 Console.CursorLeft = newpos;
\r
30 for (int i = 0; i < lineWidth && Console.CursorLeft < Console.BufferWidth; i++) {
\r
33 Console.CursorLeft = newpos;
\r
35 line = null; lineWidth = 0;
\r
40 case DownloadEventType.INITED:
\r
41 line = "starting...";
\r
43 case DownloadEventType.CONNECTED:
\r
44 case DownloadEventType.DOWNLOADING:
\r
45 line = a.TaskMessage;
\r
47 case DownloadEventType.COMPLETED:
\r
51 case DownloadEventType.ERROR:
\r
52 Console.Write("interrupted! ERROR!");
\r
58 int posOld = Console.CursorLeft;
\r
59 Console.Write(line);
\r
60 lineWidth = Console.CursorLeft - posOld;
\r
67 private Downloader downloader;
\r
69 private PackageListsManager pkgListMan;
\r
72 /// サイレントインストールをするか否か
\r
74 private bool silent = false;
\r
78 downloader = new Downloader();
\r
79 DownloadListener dl = new DownloadListener();
\r
80 downloader.DownloadEventRaised += new EventHandler<DownloadEventArgs>(dl.OnDownload);
\r
82 pkgListMan = new PackageListsManager();
\r
85 public void Update()
\r
90 public void LocalUpdate()
\r
95 public void update(bool downloadPackageListFlag)
\r
97 NaGet.SubCommands.NaGetUpdate tasks = new NaGet.SubCommands.NaGetUpdate(pkgListMan, downloadPackageListFlag);
\r
98 tasks.Downloader = this.downloader;
\r
99 tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {
\r
101 // case NaGetTaskSetEventType.COMPLETED_TASKSET
\r
103 case NaGetTaskSetEventType.COMPLETED:
\r
104 Console.WriteLine("Done.");
\r
106 case NaGetTaskSetEventType.STARTED_TASKSET:
\r
107 Console.Write(" " + e.TaskMessage);
\r
109 case NaGetTaskSetEventType.COMPLETED_TASKSET:
\r
110 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);
\r
112 case NaGetTaskSetEventType.INFO:
\r
113 Console.WriteLine(" " + e.TaskMessage);
\r
115 case NaGetTaskSetEventType.ERROR:
\r
116 case NaGetTaskSetEventType.WARNING:
\r
117 Console.WriteLine(" [Error]" + e.TaskMessage);
\r
125 public void CheckUpgrade()
\r
127 PackageList<Package> packageList = pkgListMan.AvailablePkgList;
\r
129 VersionComparetor verComp = new VersionComparetor();
\r
131 foreach (InstalledPackage pkg in pkgListMan.InstalledPkgList.Packages) {
\r
132 Package avaiablePkg = packageList.GetPackageForName(pkg.Name);
\r
134 if (avaiablePkg != null) {
\r
135 if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&
\r
136 pkgListMan.InstalledPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {
\r
137 Console.ForegroundColor = ConsoleColor.Green;
\r
140 Console.WriteLine("{0} ({1}) => ({2})", pkg.Name, pkg.Version, avaiablePkg.Version);
\r
141 Console.ResetColor();
\r
144 foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList.Packages) {
\r
145 Package avaiablePkg = packageList.GetPackageForName(pkg.Name);
\r
147 if (avaiablePkg != null) {
\r
148 if (verComp.Compare(pkg.Version, avaiablePkg.Version) < 0 &&
\r
149 pkgListMan.SystemInstalledPkgList.GetPackageForPackage(pkg.Name, avaiablePkg.Version) == null) {
\r
150 Console.ForegroundColor = ConsoleColor.Green;
\r
153 Console.WriteLine("{0} ({1})@sys => ({2})", pkg.Name, pkg.Version, avaiablePkg.Version);
\r
154 Console.ResetColor();
\r
159 public void Search(string keys)
\r
161 foreach(Package package in pkgListMan.AvailablePkgList.Search(keys)) {
\r
162 Console.WriteLine("{0} ({1}) - {2}", package.Name, package.Version, package.Summary);
\r
165 foreach(InstalledPackage package in pkgListMan.InstalledPkgList.Search(keys)) {
\r
166 Console.WriteLine("{0} ({1})[@install] - {2}", package.Name, package.Version, package.Summary);
\r
169 foreach(InstalledPackage package in pkgListMan.SystemInstalledPkgList.Search(keys)) {
\r
170 Console.WriteLine("{0} ({1})[@sys] - {2}", package.Name, package.Version, package.Summary);
\r
174 public void Show(string packagename)
\r
176 PackageList<Package> allPkgs = new PackageList<Package>();
\r
177 allPkgs.AddPackages(pkgListMan.GetAllPackages());
\r
179 foreach (Package pkg in allPkgs.GetPackagesForName(packagename)) {
\r
180 Console.WriteLine("Package: {0}", pkg.Name);
\r
181 Console.WriteLine("Version: {0}", pkg.Version);
\r
182 if (pkg.Url != null) Console.WriteLine("Website: {0}", pkg.Url.Href);
\r
183 if (pkg.Tags != null) Console.WriteLine("Tag: {0}", pkg.Tags.ToLower());
\r
184 Console.WriteLine("Type: {0}", pkg.Type);
\r
185 if (pkg.License != null) Console.WriteLine("License: {0}", pkg.License);
\r
186 if (pkg is InstalledPackage) Console.WriteLine("State: Installed");
\r
188 if (pkg.Description != null) {
\r
189 Console.WriteLine("Description:");
\r
190 Console.WriteLine(pkg.Description);
\r
192 Console.WriteLine();
\r
196 public void Download(string[] packagenames)
\r
198 Installation[] installations = null;
\r
200 List<Package> downloadList = new List<Package>();
\r
202 foreach(string packagename in packagenames) {
\r
203 Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);
\r
204 if (foundPackage == null) {
\r
205 Console.WriteLine("E: Couldn't find package "+packagename);
\r
206 Environment.Exit(1);
\r
208 if (! downloadList.Contains(foundPackage)) {
\r
209 downloadList.Add(foundPackage);
\r
213 installations = new Installation[downloadList.Count];
\r
214 for (int i = 0; i < installations.Length; i++) {
\r
215 installations[i] = new Installation((Package) downloadList[i]);
\r
219 Console.WriteLine("The following packages will be downloaded:");
\r
220 Console.WriteLine(" {0}", Installation.ToString(installations));
\r
221 if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {
\r
222 Console.WriteLine("Abort.");
\r
223 Environment.Exit(0);
\r
228 foreach (Installation inst in installations) {
\r
231 if (! inst.IsInstallablePackage()) {
\r
232 Console.WriteLine("E:{0} {1} can not be installed.", i, inst.ToString());
\r
237 Console.Write("Get:{0} {1}", i, inst.ToString());
\r
238 inst.Download(downloader);
\r
239 Console.WriteLine(" ...Done");
\r
240 } catch (Exception e) {
\r
241 Console.WriteLine("E: "+e.Message);
\r
242 throw new ApplicationException(string.Format("Failed to download {0}", inst.ToString()), e);
\r
247 foreach (Installation inst in installations) {
\r
250 if (inst.IsInstallablePackage() && inst.VerifyHashValues() == false) {
\r
251 Console.WriteLine("Verify:{0} {1} does not match hash value!", i, inst.ToString());
\r
257 public void Install(string[] packagenames)
\r
259 if (! NaGet.Utils.IsAdministrators()) {
\r
260 Console.WriteLine("W: you are not administrators!");
\r
263 Installation[] installations = null;
\r
265 List<Package> downloadList = new List<Package>();
\r
267 foreach(string packagename in packagenames) {
\r
268 Package foundPackage = pkgListMan.AvailablePkgList.GetPackageForName(packagename);
\r
269 if (foundPackage == null) {
\r
270 Console.WriteLine("E: Couldn't find package "+packagename);
\r
271 Environment.Exit(1);
\r
273 if (! downloadList.Contains(foundPackage)) {
\r
274 downloadList.Add(foundPackage);
\r
278 installations = new Installation[downloadList.Count];
\r
279 for (int i = 0; i < installations.Length; i++) {
\r
280 installations[i] = new Installation((Package) downloadList[i]);
\r
282 installations[i].Silent = silent; // サイレントインストールのフラグの設定
\r
284 if (! installations[i].IsInstallablePackage()) {
\r
285 Console.WriteLine("E: Can not install package {0} to your PC.", installations[i].ToString());
\r
286 Environment.Exit(1);
\r
291 Console.WriteLine("The following packages will be downloaded:");
\r
292 Console.WriteLine(" {0}", Installation.ToString(installations));
\r
293 if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {
\r
294 Console.WriteLine("Abort.");
\r
295 Environment.Exit(0);
\r
298 NaGet.SubCommands.NaGetInstall tasks = new NaGet.SubCommands.NaGetInstall(pkgListMan, installations);
\r
299 tasks.Downloader = this.downloader;
\r
300 tasks.TaskQueryRaised += delegate(object sender, NaGetTaskQueryArgs e) {
\r
301 char result = '\u0000';
\r
302 if (e.SelectionFlag == (NaGetTaskQueryResult.CONTINUE | NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {
\r
303 result = AllGet.Util.Query(e.Message + " [y/R/n]?", "yrn", 'r');
\r
304 } else if (e.SelectionFlag == (NaGetTaskQueryResult.RETRY | NaGetTaskQueryResult.CANCEL)) {
\r
305 result = AllGet.Util.Query(e.Message + " [R/n]?", "rn", 'r');
\r
306 } else if (e.SelectionFlag == NaGetTaskQueryResult.CONTINUE) {
\r
307 result = AllGet.Util.Query(e.Message + " [Y]?", "y", 'y');
\r
309 result = AllGet.Util.Query(e.Message + " [Y/n]?", "yn", 'y');
\r
314 return NaGetTaskQueryResult.CONTINUE;
\r
316 return NaGetTaskQueryResult.CANCEL;
\r
318 return NaGetTaskQueryResult.RETRY;
\r
320 return NaGetTaskQueryResult.CANCELED_AUTOMATICALLY;
\r
323 tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {
\r
325 // case NaGetTaskSetEventType.COMPLETED_TASKSET
\r
327 case NaGetTaskSetEventType.COMPLETED:
\r
328 Console.WriteLine("Done.");
\r
330 case NaGetTaskSetEventType.STARTED_TASKSET:
\r
331 Console.Write(" " + e.TaskMessage);
\r
333 case NaGetTaskSetEventType.COMPLETED_TASKSET:
\r
334 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);
\r
336 case NaGetTaskSetEventType.INFO:
\r
337 Console.WriteLine(" " + e.TaskMessage);
\r
339 case NaGetTaskSetEventType.ERROR:
\r
340 case NaGetTaskSetEventType.WARNING:
\r
341 Console.WriteLine(" [Error] " + e.TaskMessage);
\r
349 public void Remove(string[] packagenames)
\r
351 if (! NaGet.Utils.IsAdministrators()) {
\r
352 Console.WriteLine("W: you are not administrators!");
\r
355 this.LocalUpdate();
\r
357 PackageList<InstalledPackage> installedPkgList = new PackageList<InstalledPackage>();
\r
358 installedPkgList.AddPackages(pkgListMan.GetAllInstalledPackages());
\r
360 Uninstallation[] uninstallations = new Uninstallation[packagenames.Length];
\r
361 for (int i = 0; i < packagenames.Length; i++) {
\r
362 InstalledPackage pkg = installedPkgList.GetPackageForName(packagenames[i]);
\r
365 Console.WriteLine("E: could not found package " + packagenames[i]);
\r
366 Environment.Exit(1);
\r
369 uninstallations[i] = new Uninstallation(pkg);
\r
372 // if (AllGet.Util.Confirm("Do you want to continue [Y/n]?", true) == false) {
\r
373 // Console.WriteLine("Abort.");
\r
374 // Environment.Exit(0);
\r
377 NaGet.SubCommands.NaGetUninstall tasks = new NaGet.SubCommands.NaGetUninstall(pkgListMan, uninstallations);
\r
378 tasks.TaskSetRaised += delegate(object sender, NaGetTaskSetEventArgs e) {
\r
380 // case NaGetTaskSetEventType.COMPLETED_TASKSET
\r
382 case NaGetTaskSetEventType.COMPLETED:
\r
383 Console.WriteLine("Done.");
\r
385 case NaGetTaskSetEventType.STARTED_TASKSET:
\r
386 Console.Write(" " + e.TaskMessage);
\r
388 case NaGetTaskSetEventType.COMPLETED_TASKSET:
\r
389 Console.WriteLine(" ... Done. [{0}%]", (int) e.TaskProgressPercent);
\r
391 case NaGetTaskSetEventType.INFO:
\r
392 Console.WriteLine(" " + e.TaskMessage);
\r
394 case NaGetTaskSetEventType.ERROR:
\r
395 case NaGetTaskSetEventType.WARNING:
\r
396 Console.WriteLine(" [Error] " + e.TaskMessage);
\r
404 public void CleanCache(string[] packages)
\r
406 if (! Directory.Exists(NaGet.Env.ArchiveFolderPath)) {
\r
411 if (packages.Length == 0) {
\r
412 foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath)) {
\r
413 Directory.Delete(folder, true);
\r
417 foreach (string package in packages) {
\r
418 foreach (string folder in Directory.GetDirectories(NaGet.Env.ArchiveFolderPath, package+"*", SearchOption.TopDirectoryOnly)) {
\r
419 Directory.Delete(folder, true);
\r
425 Console.WriteLine("... Done.");
\r
431 string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;
\r
432 Console.Write("Usage:");
\r
434 Console.WriteLine("\t{0} update|localupdate", executeFileName);
\r
435 Console.WriteLine("\t{0} search|show pkg1 [pkg2 ...]", executeFileName);
\r
436 Console.WriteLine("\t{0} cleancache [pkg ...]", executeFileName);
\r
437 Console.WriteLine("\t{0} download pkg1 [pkg2 ...]", executeFileName);
\r
438 Console.WriteLine("\t{0} install|uninstall pkg1 [pkg2 ...]", executeFileName);
\r
439 Console.WriteLine();
\r
440 Console.WriteLine("{0} is a simple command line interface for downloading and "+
\r
441 "installing packages. The most frequently used commands are update "+
\r
442 "and install.", executeFileName);
\r
443 Console.WriteLine();
\r
444 Console.WriteLine("Commands:");
\r
445 Console.WriteLine(" update - Retrieve new lists of packages");
\r
446 Console.WriteLine(" localupdate - Update installed-soft-list only");
\r
447 Console.WriteLine(" checkupgrade - Show upgraded-soft list");
\r
448 Console.WriteLine(" search - Search the package list for not a regex pattern");
\r
449 Console.WriteLine(" show - Show package detail");
\r
450 Console.WriteLine(" cleancache - Clear cached archived file(s)");
\r
451 Console.WriteLine(" download - Download only - do NOT install or unpack archives");
\r
452 Console.WriteLine(" install - Install new packages");
\r
453 Console.WriteLine(" remove - Uninstall packages");
\r
454 Console.WriteLine();
\r
457 public void FooBar()
\r
459 foreach (InstalledPackage pkg in pkgListMan.SystemInstalledPkgList.Packages) {
\r
460 Console.WriteLine("{0} : {1}", pkg.Name, pkg.UninstallInfo.UninstallString);
\r
466 foreach (UninstallInformation uInfo in RegistriedUninstallers.Uninstallers) {
\r
467 if (! uInfo.IsOSPatch && ! uInfo.IsSystemComponent) {
\r
468 Console.WriteLine("{0}", uInfo.DisplayName);
\r
475 // TODO スーパー牛さんパワー化
\r
477 string executeFileName = System.AppDomain.CurrentDomain.FriendlyName;
\r
478 Console.WriteLine("誰か {0} をスーパー牛さんパワー化してくれ", executeFileName);
\r
481 public static void Main(string[] args)
\r
483 // アーカイブSYSTEM32をパスに足す
\r
484 NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);
\r
486 MainClass mc = new MainClass();
\r
488 if (args.Length == 0) {
\r
491 Environment.Exit(1);
\r
494 NaGet.ArgParser argParser = new NaGet.ArgParser();
\r
495 argParser["quiet"] = false;
\r
496 args = argParser.Parse(args);
\r
497 mc.silent = (bool) argParser["quiet"];
\r
498 } catch (ApplicationException e) {
\r
499 Console.WriteLine(e.Message);
\r
500 Environment.Exit(1);
\r
504 if (args[0] == "update") {
\r
505 if (args.Length != 1) {
\r
506 Console.WriteLine("E: The update command takes no arguments");
\r
507 Environment.Exit(1);
\r
510 } else if (args[0] == "localupdate") {
\r
511 if (args.Length != 1) {
\r
512 Console.WriteLine("E: The update command takes no arguments");
\r
513 Environment.Exit(1);
\r
516 } else if (args[0] == "search") {
\r
517 if (args.Length <= 1) {
\r
518 Console.WriteLine("E: You must give exactly one pattern");
\r
519 Environment.Exit(1);
\r
522 mc.Search(string.Join(" ", args, 1, args.Length - 1));
\r
523 } else if (args[0] == "show") {
\r
524 if (args.Length <= 1) {
\r
525 Console.WriteLine("E: You must give exactly one pattern");
\r
526 Environment.Exit(1);
\r
529 for (int i = 1; i < args.Length; i++) {
\r
532 } else if (args[0] == "download") {
\r
533 if (args.Length <= 1) {
\r
534 Console.WriteLine("E: Invalid operation download");
\r
535 Environment.Exit(1);
\r
538 string[] packages = new string[args.Length - 1];
\r
539 Array.Copy(args, 1, packages, 0, packages.Length);
\r
541 mc.Download(packages);
\r
542 } else if (args[0] == "install") {
\r
543 if (args.Length <= 1) {
\r
544 Console.WriteLine("E: Invalid operation install");
\r
545 Environment.Exit(1);
\r
548 string[] packages = new string[args.Length - 1];
\r
549 Array.Copy(args, 1, packages, 0, packages.Length);
\r
551 mc.Install(packages);
\r
552 } else if (args[0] == "checkupgrade") {
\r
554 } else if (args[0] == "remove") {
\r
555 if (args.Length <= 1) {
\r
556 Console.WriteLine("E: Invalid operation remove");
\r
557 Environment.Exit(1);
\r
560 string[] packages = new string[args.Length - 1];
\r
561 Array.Copy(args, 1, packages, 0, packages.Length);
\r
563 mc.Remove(packages);
\r
564 } else if (args[0] == "cleancache") {
\r
565 string[] packages = new string[args.Length - 1];
\r
566 Array.Copy(args, 1, packages, 0, packages.Length);
\r
568 mc.CleanCache(packages);
\r
569 } else if (args[0] == "foobar") {
\r
571 } else if (args[0] == "hoge") {
\r
573 } else if (args[0] == "moo") {
\r
578 Environment.Exit(1);
\r