Beispiel #1
0
 private ToolsService <Framework, Version, VersionRange> GetToolsService()
 {
     return(new ToolsService <Framework, Version, VersionRange>(
                ClientVersionUtility.GetNuGetAssemblyVersion(),
                new FrameworkLogic(),
                new VersionLogic(),
                new VersionRangeLogic()));
 }
Beispiel #2
0
        /// <summary>
        /// Read the NuGet client version from the assembly info as a NuGetVersion.
        /// </summary>
        public static NuGetVersion GetNuGetClientVersion()
        {
            if (_clientVersion == null)
            {
                var versionString = ClientVersionUtility.GetNuGetAssemblyVersion();

                NuGetVersion clientVersion;
                if (!NuGetVersion.TryParse(versionString, out clientVersion))
                {
                    throw new InvalidOperationException(Strings.UnableToParseClientVersion);
                }

                _clientVersion = clientVersion;
            }

            return(_clientVersion);
        }
Beispiel #3
0
        /// <summary>
        /// Read the NuGet client version from the assembly info as a NuGetVersion.
        /// </summary>
        public static NuGetVersion GetNuGetClientVersion()
        {
            if (_clientVersion == null)
            {
                var versionString = ClientVersionUtility.GetNuGetAssemblyVersion();

                NuGetVersion clientVersion;
                if (!NuGetVersion.TryParse(versionString, out clientVersion))
                {
                    throw new InvalidOperationException(Strings.UnableToParseClientVersion);
                }

                // Remove pre-release info from the version and return a stable version.
                _clientVersion = new NuGetVersion(
                    major: clientVersion.Major,
                    minor: clientVersion.Minor,
                    patch: clientVersion.Patch,
                    revision: clientVersion.Revision);
            }

            return(_clientVersion);
        }
Beispiel #4
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(serviceProvider =>
            {
                var hostingEnvironment = serviceProvider.GetService <IHostingEnvironment>();

                var settings      = new InMemorySettings();
                var nuGetSettings = new NuGetSettings(settings);
                nuGetSettings.GlobalPackagesFolder = Path.Combine(hostingEnvironment.ContentRootPath, "packages");

                return(nuGetSettings);
            });

            services.AddLogging(builder =>
            {
                builder.AddConsole();
                builder.AddDebug();
            });

            services.AddApplicationInsightsTelemetry();

            services.AddTransient <IAlignedVersionsDownloader, AlignedVersionsDownloader>();
            services.AddTransient <IAssemblyLoader, AssemblyLoader>();
            services.AddTransient <IFrameworkEnumerator <Framework>, FrameworkEnumerator>();
            services.AddTransient <IFrameworkList, FrameworkList <Framework> >();
            services.AddTransient <IPackageLoader, PackageLoader>();
            services.AddTransient <IPackageRangeDownloader, PackageRangeDownloader>();

            services.AddSingleton <IToolsFactory, ToolsFactory>();

            try
            {
                // Try to construct the reflection-based tools factory.
                var serviceProvider = services.BuildServiceProvider();
                var toolsFactory    = serviceProvider.GetRequiredService <IToolsFactory>();

                var versions = toolsFactory.GetAvailableVersionsAsync(CancellationToken.None).Result;

                if (!versions.Any())
                {
                    throw new InvalidOperationException("At least one version is required.");
                }
            }
            catch
            {
                // Fallback to using the NuGet version directly referenced by this project.
                var serviceDescriptor = services.First(x => x.ImplementationType == typeof(ToolsFactory));
                services.Remove(serviceDescriptor);

                services.AddTransient <IFrameworkLogic <Framework>, FrameworkLogic>();
                services.AddTransient <IVersionLogic <Version>, VersionLogic>();
                services.AddTransient <IVersionRangeLogic <Version, VersionRange>, VersionRangeLogic>();

                var clientVersion = NuGet.Versioning.NuGetVersion.Parse(ClientVersionUtility.GetNuGetAssemblyVersion()).ToNormalizedString();

                services.AddTransient <IToolsService>(serviceProvider =>
                {
                    return(new ToolsService <Framework, Version, VersionRange>(
                               clientVersion,
                               serviceProvider.GetRequiredService <IFrameworkLogic <Framework> >(),
                               serviceProvider.GetRequiredService <IVersionLogic <Version> >(),
                               serviceProvider.GetRequiredService <IVersionRangeLogic <Version, VersionRange> >()));
                });

                services.AddTransient <IFrameworkPrecedenceService>(serviceProvider =>
                {
                    return(new FrameworkPrecedenceService <Framework>(
                               clientVersion,
                               serviceProvider.GetRequiredService <IFrameworkList>(),
                               serviceProvider.GetRequiredService <IFrameworkLogic <Framework> >()));
                });

                services.AddSingleton <IToolsFactory, SingletonToolsFactory>();
            }

            services.AddMvc();
        }