示例#1
0
文件: Program.cs 项目: widistef/nuget
        public static int Main(string[] args)
        {
            DebugHelper.WaitForAttach(ref args);

            // This is to avoid applying weak event pattern usage, which breaks under Mono or restricted environments, e.g. Windows Azure Web Sites.
            EnvironmentUtility.SetRunningFromCommandLine();

            // Set output encoding to UTF8 if running on Unices. This is not needed on Windows.
            if (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                System.Console.OutputEncoding = System.Text.Encoding.UTF8;
            }
            var console    = new Common.Console();
            var fileSystem = new PhysicalFileSystem(Directory.GetCurrentDirectory());

            Func <Exception, string> getErrorMessage = e => e.Message;

            try
            {
                // Remove NuGet.exe.old
                RemoveOldFile(fileSystem);

                // Import Dependencies
                var p = new Program();
                p.Initialize(fileSystem, console);

                // Add commands to the manager
                foreach (ICommand cmd in p.Commands)
                {
                    p.Manager.RegisterCommand(cmd);
                }

                CommandLineParser parser = new CommandLineParser(p.Manager);

                // Parse the command
                ICommand command = parser.ParseCommandLine(args) ?? p.HelpCommand;

                // Fallback on the help command if we failed to parse a valid command
                if (!ArgumentCountValid(command))
                {
                    // Get the command name and add it to the argument list of the help command
                    string commandName = command.CommandAttribute.CommandName;

                    // Print invalid command then show help
                    console.WriteLine(LocalizedResourceManager.GetString("InvalidArguments"), commandName);

                    p.HelpCommand.ViewHelpForCommand(commandName);
                }
                else
                {
                    SetConsoleInteractivity(console, command as Command);

                    // When we're detailed, get the whole exception including the stack
                    // This is useful for debugging errors.
                    if (console.Verbosity == Verbosity.Detailed)
                    {
                        getErrorMessage = e => e.ToString();
                    }

                    command.Execute();
                }
            }
            catch (AggregateException exception)
            {
                string    message;
                Exception unwrappedEx = ExceptionUtility.Unwrap(exception);
                if (unwrappedEx == exception)
                {
                    // If the AggregateException contains more than one InnerException, it cannot be unwrapped. In which case, simply print out individual error messages
                    message = String.Join(Environment.NewLine, exception.InnerExceptions.Select(getErrorMessage).Distinct(StringComparer.CurrentCulture));
                }
                else
                {
                    message = getErrorMessage(ExceptionUtility.Unwrap(exception));
                }
                console.WriteError(message);
                return(1);
            }
            catch (Exception e)
            {
                console.WriteError(getErrorMessage(ExceptionUtility.Unwrap(e)));
                return(1);
            }
            finally
            {
                OptimizedZipPackage.PurgeCache();
            }

            return(0);
        }
示例#2
0
        internal static PackageSourceProvider CreateSourceProvider(ISettings settings)
        {
            var defaultPackageSource = new PackageSource(NuGetConstants.DefaultFeedUrl);

            var officialPackageSource = new PackageSource(NuGetConstants.DefaultFeedUrl, LocalizedResourceManager.GetString("OfficialPackageSourceName"));
            var v1PackageSource       = new PackageSource(NuGetConstants.V1FeedUrl, LocalizedResourceManager.GetString("OfficialPackageSourceName"));
            var legacyV2PackageSource = new PackageSource(NuGetConstants.V2LegacyFeedUrl, LocalizedResourceManager.GetString("OfficialPackageSourceName"));

            var packageSourceProvider = new PackageSourceProvider(
                settings,
                new[] { defaultPackageSource },
                new Dictionary <PackageSource, PackageSource> {
                { v1PackageSource, officialPackageSource },
                { legacyV2PackageSource, officialPackageSource }
            }
                );

            return(packageSourceProvider);
        }
        public static ICollection <PackageReference> GetPackageReferences(PackageReferenceFile configFile, bool requireVersion)
        {
            if (configFile == null)
            {
                throw new ArgumentNullException("configFile");
            }

            var packageReferences = configFile.GetPackageReferences(requireVersion).ToList();

            foreach (var package in packageReferences)
            {
                // GetPackageReferences returns all records without validating values. We'll throw if we encounter packages
                // with malformed ids / Versions.
                if (String.IsNullOrEmpty(package.Id))
                {
                    throw new InvalidDataException(
                              String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandInvalidPackageReference"),
                                            configFile.FullPath));
                }
                if (requireVersion && (package.Version == null))
                {
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("InstallCommandPackageReferenceInvalidVersion"), package.Id));
                }
            }

            return(packageReferences);
        }
        public ICommand GetCommand(string commandName)
        {
            IEnumerable <ICommand> results = from command in _commands
                                             where command.CommandAttribute.CommandName.StartsWith(commandName, StringComparison.OrdinalIgnoreCase) ||
                                             (command.CommandAttribute.AltName ?? String.Empty).StartsWith(commandName, StringComparison.OrdinalIgnoreCase)
                                             select command;

            if (!results.Any())
            {
                throw new CommandLineException(LocalizedResourceManager.GetString("UnknowCommandError"), commandName);
            }

            var matchedCommand = results.First();

            if (results.Skip(1).Any())
            {
                // Were there more than one results found?
                matchedCommand = results.FirstOrDefault(c => c.CommandAttribute.CommandName.Equals(commandName, StringComparison.OrdinalIgnoreCase) ||
                                                        commandName.Equals(c.CommandAttribute.AltName, StringComparison.OrdinalIgnoreCase));

                if (matchedCommand == null)
                {
                    // No exact match was found and the result returned multiple prefixes.
                    throw new CommandLineException(String.Format(CultureInfo.CurrentCulture, LocalizedResourceManager.GetString("AmbiguousCommand"), commandName,
                                                                 String.Join(" ", from c in results select c.CommandAttribute.CommandName)));
                }
            }
            return(matchedCommand);
        }