NuGet.Program.Initialize C# (CSharp) Метод

Initialize() приватный Метод

private Initialize ( IFileSystem fileSystem, IConsole console ) : void
fileSystem IFileSystem
console IConsole
Результат void
        private void Initialize(IFileSystem fileSystem, IConsole console)
        {
            using (var catalog = new AggregateCatalog(new AssemblyCatalog(GetType().Assembly)))
            {
                if (!IgnoreExtensions)
                {
                    AddExtensionsToCatalog(catalog);
                }
                using (var container = new CompositionContainer(catalog))
                {
                    var settings = GetCommandLineSettings(fileSystem);
                    var defaultPackageSource = new PackageSource(NuGetConstants.DefaultFeedUrl);

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

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

                    // Register an additional provider for the console specific application so that the user
                    // will be prompted if a proxy is set and credentials are required
                    var credentialProvider = new SettingsCredentialProvider(new ConsoleCredentialProvider(), packageSourceProvider, console);
                    HttpClient.DefaultCredentialProvider = credentialProvider;

                    container.ComposeExportedValue<IConsole>(console);
                    container.ComposeExportedValue<ISettings>(settings);
                    container.ComposeExportedValue<IPackageRepositoryFactory>(new NuGet.Common.CommandLineRepositoryFactory());
                    container.ComposeExportedValue<IPackageSourceProvider>(packageSourceProvider);
                    container.ComposeExportedValue<ICredentialProvider>(credentialProvider);
                    container.ComposeParts(this);
                }
            }
        }

Usage Example

Пример #1
0
        public static int Main(string[] args)
        {
            var console = new Common.Console();
            var fileSystem = new PhysicalFileSystem(Directory.GetCurrentDirectory());
            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(NuGetResources.InvalidArguments, commandName);

                    p.HelpCommand.ViewHelpForCommand(commandName);
                }
                else
                {
                    command.Execute();
                }
            }
            catch (AggregateException exception)
            {
                if (ExceptionUtility.Unwrap(exception) == exception)
                {
                    // If the AggregateException contains more than one InnerException, it cannot be unwrapped. In which case, simply print out individual error messages
                    var messages = exception.InnerExceptions.Select(ex => ex.Message)
                                                            .Distinct(StringComparer.CurrentCulture);
                    console.WriteError(String.Join("\n", messages));
                }
            }
            catch (Exception e)
            {
                console.WriteError(ExceptionUtility.Unwrap(e).Message);
                return 1;
            }
            return 0;
        }
All Usage Examples Of NuGet.Program::Initialize