Exemplo n.º 1
0
        /// <summary>
        ///   The (non-static) startup method
        /// </summary>
        /// <param name = "args">
        ///   The command line arguments.
        /// </param>
        /// <returns>
        ///   Process return code.
        /// </returns>
        protected override int Main(IEnumerable<string> args)
        {
            try {
                _pkgManager = new PackageManager();

                bool waitforbreak = false;

                #region commane line parsing

                // default:
                _pkgManager.SessionFeedLocations = new[] {Environment.CurrentDirectory};

                var options = args.Switches();
                var parameters = args.Parameters();

                foreach (var arg in options.Keys) {
                    var argumentParameters = options[arg];

                    switch (arg) {
                            /* options  */
                        case "pretend":
                            _pkgManager.Pretend = true;
                            break;

                        case "wait-for-break":
                            waitforbreak = true;
                            break;

                        case "maximum":
                            _pkgManager.MaximumPackagesToProcess = argumentParameters.Last().ToInt32(10);
                            break;

                        case "as-specified":
                            _pkgManager.PackagesAsSpecified = string.IsNullOrEmpty(argumentParameters.FirstOrDefault())
                                ? new[] {"*"}
                                : argumentParameters;
                            break;

                        case "upgrade":
                            _pkgManager.PackagesAreUpgradable = string.IsNullOrEmpty(argumentParameters.FirstOrDefault())
                                ? new[] {"*"}
                                : argumentParameters;
                            break;

                        case "no-scan":
                            _pkgManager.DoNotScanLocations = string.IsNullOrEmpty(argumentParameters.FirstOrDefault())
                                ? new[] {"*"}
                                : argumentParameters;
                            break;

                        case "no-network":
                            _pkgManager.DoNotScanLocations = new[] {"*://*"};
                            break;

                        case "scan":
                            if (string.IsNullOrEmpty(argumentParameters.FirstOrDefault())) {
                                throw new ConsoleException(Resources.OptionRequiresLocation.format("--scan"));
                            }
                            _pkgManager.SessionFeedLocations = argumentParameters;
                            break;

                        case "flush-cache":
                            _pkgManager.FlushCache();
                            break;

                            /* global switches */
                        case "load-config":
                            // all ready done, but don't get too picky.
                            break;

                        case "nologo":
                            this.Assembly().SetLogo(string.Empty);
                            break;

                        case "feed-output-file":
                            _feedOutputFile = argumentParameters.LastOrDefault();
                            break;
                        case "feed-root-url":
                            _feedRootUrl = argumentParameters.LastOrDefault();
                            break;
                        case "feed-actual-url":
                            _feedActualUrl = argumentParameters.LastOrDefault();
                            break;
                        case "feed-package-source":
                            _feedPackageSource = argumentParameters.LastOrDefault();
                            break;
                        case "feed-recursive":
                            _feedRecursive = true;
                            break;
                        case "feed-package-url":
                            _feedPackageUrl = argumentParameters.LastOrDefault();
                            break;
                        case "feed-title":
                            _feedTitle = argumentParameters.LastOrDefault();
                            break;

                        case "help":
                            return Help();

                        default:
                            throw new ConsoleException(Resources.UnknownParameter, arg);
                    }
                }

                Logo();

                if (parameters.Count() < 1) {
                    throw new ConsoleException(Resources.MissingCommand);
                }

                #endregion

                // GS01: I'm putting this in here so that feed resoltion happens before we actually get around to doing something.
                // Look into the necessity later.
                Tasklet.WaitforCurrentChildTasks();

                var command = parameters.FirstOrDefault().ToLower();
                parameters = parameters.Skip(1);

                if (File.Exists(command)) {
                    // assume install if the only thing given is a filename.
                    Install(command.SingleItemAsEnumerable());
                }
                else {
                    switch (command) {
                        case "download":
                            var remoteFileUri = new Uri(parameters.First());
                            // create the remote file reference
                            var remoteFile = new RemoteFile(remoteFileUri, CancellationTokenSource.Token);
                            var previewTask = remoteFile.Preview();
                            previewTask.Wait();
                            // Tell it to download it
                            var getTask = remoteFile.Get();

                            // monitor the progress.
                            remoteFile.DownloadProgress.Notification += progress => {
                                // this executes when the download progress value changes.
                                Console.Write(progress <= 100 ? "..{0}% " : "bytes: [{0}]", progress);
                            };

                            // when it's done, do this:
                            getTask.ContinueWithParent(t => {
                                // this executes when the Get() operation completes.
                                Console.WriteLine("File {0} has finished downloading", remoteFile.LocalFullPath);
                            });
                            break;

                        case "verify-package":
                            var r = Verifier.HasValidSignature(parameters.First());
                            Console.WriteLine("Has Valid Signature: {0}", r);
                            Console.WriteLine("Name: {0}", Verifier.GetPublisherInformation(parameters.First())["PublisherName"]);
                            break;

                        case "install":
                            if (parameters.Count() < 1) {
                                throw new ConsoleException(Resources.InstallRequiresPackageName);
                            }

                            Tasklet.WaitforCurrentChildTasks(); // HACK HACK HACK ???

                            Install(parameters);
                            break;

                        case "remove":
                            if (parameters.Count() < 1) {
                                throw new ConsoleException(Resources.RemoveRequiresPackageName);
                            }
                            Remove(parameters);
                            break;

                        case "list":
                            if (parameters.Count() != 1) {
                                throw new ConsoleException(Resources.MissingParameterForList);
                            }
                            switch (parameters.FirstOrDefault().ToLower()) {
                                case "packages":
                                case "package":
                                    ListPackages(parameters);
                                    break;

                                case "feed":
                                case "feeds":
                                case "repo":
                                case "repos":
                                case "repositories":
                                case "repository":
                                    ListFeeds(parameters);
                                    break;
                            }
                            break;

                        case "upgrade":
                            if (parameters.Count() != 1) {
                                throw new ConsoleException(Resources.MissingParameterForUpgrade);
                            }

                            Upgrade(parameters);
                            break;

                        case "add":
                            if (parameters.Count() < 1) {
                                throw new ConsoleException(Resources.AddFeedRequiresLocation);
                            }
                            CoTask.Factory.StartNew(() => AddFeed(parameters));
                            break;

                        case "delete":
                            if (parameters.Count() < 1) {
                                throw new ConsoleException(Resources.DeleteFeedRequiresLocation);
                            }
                            CoTask.Factory.StartNew(() => DeleteFeed(parameters));
                            break;

                        case "trim":
                            if (parameters.Count() != 0) {
                                throw new ConsoleException(Resources.TrimErrorMessage);
                            }

                            CoTask.Factory.StartNew(() => Trim(parameters));
                            break;

                        case "generate-feed":
                            CoTask.Factory.StartNew(() => GenerateFeed(parameters));
                            break;

                        default:
                            throw new ConsoleException(Resources.UnknownCommand, command);
                    }
                }

                while (waitforbreak && !CancellationTokenSource.IsCancellationRequested) {
                    Thread.Sleep(100);
                }
            }
            catch (ConsoleException failure) {
                CancellationTokenSource.Cancel();
                Fail("{0}\r\n\r\n    {1}", failure.Message, Resources.ForCommandLineHelp);
            }
            return 0;
        }