/// <summary>
        /// Get all versions for a specific package Id.
        /// </summary>
        /// <param name="sourceRepository"></param>
        /// <param name="packageId"></param>
        /// <param name="project"></param>
        /// <param name="includePrerelease"></param>
        /// <returns></returns>
        public static IEnumerable<NuGetVersion> GetAllVersionsForPackageId(SourceRepository sourceRepository, string packageId, NuGetProject project, bool includePrerelease)
        {
            IEnumerable<string> targetFrameworks = GetProjectTargetFrameworks(project);
            SearchFilter searchfilter = new SearchFilter();
            searchfilter.IncludePrerelease = includePrerelease;
            searchfilter.SupportedFrameworks = targetFrameworks;
            searchfilter.IncludeDelisted = false;
            PSSearchResource resource = sourceRepository.GetResource<PSSearchResource>();
            PSSearchMetadata result = null;
            IEnumerable<NuGetVersion> allVersions = Enumerable.Empty<NuGetVersion>();

            try
            {
                Task<IEnumerable<PSSearchMetadata>> task = resource.Search(packageId, searchfilter, 0, 30, CancellationToken.None);
                result = task.Result
                    .Where(p => string.Equals(p.Identity.Id, packageId, StringComparison.OrdinalIgnoreCase))
                    .FirstOrDefault();
                allVersions = result.Versions;
            }
            catch (Exception)
            {
                if (result == null || !allVersions.Any())
                {
                    throw new InvalidOperationException(
                        String.Format(CultureInfo.CurrentCulture,
                        Resources.UnknownPackage, packageId));
                }

            }
            return result.Versions;
        }
Beispiel #2
0
        public async Task TestPowerShellAutocompleteResourceForPackageIds(string SourceUrl)
        {
            SourceRepository       repo     = GetSourceRepository(SourceUrl);
            PSAutoCompleteResource resource = repo.GetResource <PSAutoCompleteResource>();

            //Check if we are able to obtain a resource
            Assert.True(resource != null);
            IEnumerable <string> searchResults = await resource.IdStartsWith("Elmah", true, CancellationToken.None);

            // Check if non empty search result is returned.
            Assert.True(searchResults.Count() > 0);
            //Make sure that the results starts with the given prefix.
            Assert.True(searchResults.All(p => p.StartsWith("Elmah", StringComparison.OrdinalIgnoreCase)));
        }
        public async Task TestDependencyInfoResourceForPackageWithAnyFramework(string SourceUrl)
        {
            SourceRepository      repo     = GetSourceRepository(SourceUrl);
            DepedencyInfoResource resource = await repo.GetResource <DepedencyInfoResource>();

            //Check if we are able to obtain a resource
            Assert.True(resource != null);
            List <PackageIdentity> packageIdentities = new List <PackageIdentity>();

            //Check the dependency tree depth for a known package. Since the same test executes for both V2 and V3 source, we cna also ensure that the pre-resolver data is same for both V2 and V3.
            packageIdentities.Add(new PackageIdentity("WebGrease", new NuGetVersion("1.6.0")));
            IEnumerable <PackageDependencyInfo> packages = await resource.ResolvePackages(packageIdentities, NuGet.Frameworks.NuGetFramework.AnyFramework, true, new CancellationToken());

            Assert.True(packages.Count() >= 16);
        }
Beispiel #4
0
        public async Task TestAllSearchTitle(string SourceUrl)
        {
            SourceRepository repo     = GetSourceRepository(SourceUrl);
            UISearchResource resource = repo.GetResource <UISearchResource>();

            SearchFilter filter     = new SearchFilter();
            string       searchTerm = "Json";

            IEnumerable <UISearchMetadata> uiSearchResults = await resource.Search(searchTerm, filter, 0, 100, new CancellationToken());

            UISearchMetadata metadata = uiSearchResults.Where(e => StringComparer.OrdinalIgnoreCase.Equals("newtonsoft.json", e.Identity.Id)).Single();

            // TODO: check the title value once the server is updated
            Assert.True(!String.IsNullOrEmpty(metadata.Title));
            Assert.True(!String.IsNullOrEmpty(metadata.LatestPackageMetadata.Title));
        }
Beispiel #5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="packageSource">package source</param>
 /// <param name="destinationPath">destination path where the packages should be downloaded</param>
 internal NuGetPackageDownloader(string packageSource, string destinationPath)
 {
     _logger    = new Logger();
     _providers = new List <Lazy <INuGetResourceProvider> >();
     _providers.AddRange(Repository.Provider.GetCoreV3());
     _packageSource     = new PackageSource(packageSource);
     _sourceRepository  = new SourceRepository(_packageSource, _providers);
     _searchResource    = _sourceRepository.GetResource <PackageSearchResource>();
     _settings          = Settings.LoadDefaultSettings(destinationPath);
     _project           = new FolderNuGetProject(destinationPath);
     _packageManager    = new NuGetPackageManager(new SourceRepositoryProvider(new PackageSourceProvider(_settings), _providers), _settings, destinationPath);
     _projectContext    = new ProjectContext(_logger);
     _searchFilter      = new SearchFilter(true, SearchFilterType.IsAbsoluteLatestVersion);
     _resolutionContext = new ResolutionContext(NuGet.Resolver.DependencyBehavior.Ignore, false, false, VersionConstraints.None);
     _secondarySources  = new List <SourceRepository>();
 }
Beispiel #6
0
        internal async Task SelfUpdate(string exePath, NuGetVersion version)
        {
            Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandCheckingForUpdates"), NuGetConstants.DefaultFeedUrl);
            var metadataResource = _source.GetResource <MetadataResource>();

            if (metadataResource != null)
            {
                ResolutionContext resolutionContext = new ResolutionContext();
                var latestVersionKeyPair            = await metadataResource.GetLatestVersions(new List <string>() { NuGetCommandLinePackageId },
                                                                                               resolutionContext.IncludePrerelease, resolutionContext.IncludeUnlisted, CancellationToken.None);

                var lastetVersion = latestVersionKeyPair.FirstOrDefault().Value;

                if (version >= lastetVersion)
                {
                    Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandNuGetUpToDate"));
                }
                else
                {
                    Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandUpdatingNuGet"), lastetVersion.Version.ToString());

                    // Get NuGet.exe file
                    using (var targetPackageStream = new MemoryStream())
                    {
                        await PackageDownloader.GetPackageStream(_source, new PackageIdentity(NuGetCommandLinePackageId, lastetVersion), targetPackageStream, CancellationToken.None);

                        // If for some reason this package doesn't have NuGet.exe then we don't want to use it
                        if (targetPackageStream == null)
                        {
                            throw new CommandLineException(LocalizedResourceManager.GetString("UpdateCommandUnableToLocateNuGetExe"));
                        }

                        // Get the exe path and move it to a temp file (NuGet.exe.old) so we can replace the running exe with the bits we got
                        // from the package repository
                        string renamedPath = exePath + ".old";
                        Move(exePath, renamedPath);

                        // Update the file
                        UpdateFile(exePath, targetPackageStream);
                        Console.WriteLine(LocalizedResourceManager.GetString("UpdateCommandUpdateSuccessful"));
                    }
                }
            }
        }
Beispiel #7
0
        public void SourceRepository_Counts()
        {
            PackageSource source = new PackageSource("http://source");

            var A = new TestProvider(null);
            var B = new TestProvider(null);
            var C = new TestProvider(null);

            var A2 = new TestProvider(null);
            var B2 = new TestProvider(null);
            var C2 = new TestProvider(null);

            List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> > providers = new List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> >()
            {
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "A", NuGetResourceProviderPositions.First), A),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "B"), B),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "C", NuGetResourceProviderPositions.Last), C),

                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "A2", "A"), A2),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "B2", "B"), B2),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "C2", "C"), C2),
            };

            SourceRepository repo = new SourceRepository(source, providers);

            // verify order - work backwards
            Assert.Null(repo.GetResource <TestResource>().Result);

            C.Data = "C";
            Assert.Equal("C", repo.GetResource <TestResource>().Result.Data);

            C2.Data = "C2";
            Assert.Equal("C2", repo.GetResource <TestResource>().Result.Data);

            B.Data = "B";
            Assert.Equal("B", repo.GetResource <TestResource>().Result.Data);

            B.Data = "B2";
            Assert.Equal("B2", repo.GetResource <TestResource>().Result.Data);

            A.Data = "A";
            Assert.Equal("A", repo.GetResource <TestResource>().Result.Data);

            A2.Data = "A2";
            Assert.Equal("A2", repo.GetResource <TestResource>().Result.Data);
        }
        public void EnsurePackageSourceClientCertificatesForwardedToV3HttpClientHandler()
        {
            // Arrange
            var certificate   = GetCertificate();
            var packageSource = new PackageSource("https://contoso.com/v3/index.json")
            {
                ClientCertificates = new List <X509Certificate> {
                    certificate
                }
            };
            var sourceRepository = new SourceRepository(packageSource, new[] { new HttpHandlerResourceV3Provider() });

            // Act
            var httpHandlerResourceV3 = sourceRepository.GetResource <HttpHandlerResource>();

            // Assert
            Assert.NotNull(httpHandlerResourceV3);
            Assert.True(httpHandlerResourceV3.ClientHandler.ClientCertificates.Contains(certificate));
        }
Beispiel #9
0
        public async Task TestVisualStudioUIMetadataResource(string SourceUrl)
        {
            SourceRepository   repo     = GetSourceRepository(SourceUrl);
            UIMetadataResource resource = repo.GetResource <UIMetadataResource>();

            Assert.True(resource != null);
            var result = await resource.GetMetadata("Microsoft.AspNet.Razor", true, true, CancellationToken.None);

            UIPackageMetadata packageMetadata = result.FirstOrDefault(
                p => p.Identity.Version == new NuGetVersion("4.0.0-beta1"));

            Assert.True(packageMetadata.DependencySets.Count() == 1);
            Assert.True(packageMetadata.DependencySets.First().Packages.Count().Equals(12));

            IEnumerable <UIPackageMetadata> packageMetadataList = resource.GetMetadata("Nuget.core", true, true, CancellationToken.None).Result;

            Assert.True(packageMetadataList != null);
            Assert.Equal(47, packageMetadataList.Count());
        }
Beispiel #10
0
        public void SourceRepository_SortTest2()
        {
            PackageSource source = new PackageSource("http://source");

            var empty = new TestProvider(null);
            var A     = new TestProvider(null);
            var B     = new TestProvider(null);
            var C     = new TestProvider(null);
            var D     = new TestProvider(null);
            var E     = new TestProvider(null);

            List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> > providers = new List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> >()
            {
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource)), empty),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "B", NuGetResourceProviderPositions.First), B),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "C"), C),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "A", "B"), A),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "E", NuGetResourceProviderPositions.Last), E),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "D", "E"), D),
            };

            SourceRepository repo = new SourceRepository(source, providers);

            // verify order - work backwards
            Assert.Null(repo.GetResource <TestResource>().Result);

            empty.Data = "EMPTY";
            Assert.Equal("EMPTY", repo.GetResource <TestResource>().Result.Data);

            E.Data = "E";
            Assert.Equal("E", repo.GetResource <TestResource>().Result.Data);

            D.Data = "D";
            Assert.Equal("D", repo.GetResource <TestResource>().Result.Data);

            C.Data = "C";
            Assert.Equal("C", repo.GetResource <TestResource>().Result.Data);

            B.Data = "B";
            Assert.Equal("B", repo.GetResource <TestResource>().Result.Data);

            A.Data = "A";
            Assert.Equal("A", repo.GetResource <TestResource>().Result.Data);
        }
Beispiel #11
0
        public void SourceRepository_EmptyType()
        {
            PackageSource source = new PackageSource("http://source");

            var A = new TestProvider(null);
            var B = new TestProvider(null);
            var C = new TestProvider(null);

            List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> > providers = new List <KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider> >()
            {
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "A", NuGetResourceProviderPositions.First), A),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "B"), B),
                new KeyValuePair <INuGetResourceProviderMetadata, INuGetResourceProvider>(new NuGetResourceProviderMetadata(typeof(TestResource), "C", NuGetResourceProviderPositions.Last), C),
            };

            SourceRepository repo = new SourceRepository(source, providers);

            // verify order - work backwards
            Assert.Null(repo.GetResource <TestResource2>().Result);
        }
        private List <PackageMetadataResource> CreateMetaDataResourceList(List <Lazy <INuGetResourceProvider> > providers)
        {
            List <PackageMetadataResource> list = new List <PackageMetadataResource>();

            string[] splitRepoUrls = PackagesRepoUrl.Split(new char[] { ',' });

            foreach (string repoUrl in splitRepoUrls)
            {
                string url = repoUrl.Trim();
                if (!String.IsNullOrWhiteSpace(url))
                {
                    PackageSource           packageSource           = new PackageSource(url);
                    SourceRepository        sourceRepository        = new SourceRepository(packageSource, providers);
                    PackageMetadataResource packageMetadataResource = sourceRepository.GetResource <PackageMetadataResource>();
                    list.Add(packageMetadataResource);
                }
            }

            return(list);
        }
Beispiel #13
0
        public BdioContent BuildBOM()
        {
            // Load the packages.config file into a list of Packages
            NuGet.PackageReferenceFile configFile = new NuGet.PackageReferenceFile(PackagesConfigPath);

            // Setup NuGet API
            // Snippets taken from https://daveaglick.com/posts/exploring-the-nuget-v3-libraries-part-2 with modifications
            List <Lazy <INuGetResourceProvider> > providers = new List <Lazy <INuGetResourceProvider> >();

            providers.AddRange(Repository.Provider.GetCoreV3());  // Add v3 API support
            providers.AddRange(Repository.Provider.GetCoreV2());  // Add v2 API support
            // we may need more code here around handling package sources.
            PackageSource           packageSource           = new PackageSource(PackagesRepoUrl);
            SourceRepository        sourceRepository        = new SourceRepository(packageSource, providers);
            PackageMetadataResource packageMetadataResource = sourceRepository.GetResource <PackageMetadataResource>();

            // Create BDIO
            BdioContent bdioContent = BuildBOMFromMetadata(new List <NuGet.PackageReference>(configFile.GetPackageReferences()), packageMetadataResource);

            return(bdioContent);
        }
Beispiel #14
0
        private static void PackNuget(Version nextVersion, string path)
        {
            var providers = new List <Lazy <INuGetResourceProvider> >();

            providers.AddRange(Repository.Provider.GetCoreV3());
            var packageSource           = new PackageSource("https://api.nuget.org/v3/index.json");
            var sourceRepository        = new SourceRepository(packageSource, providers);
            var packageMetadataResource = sourceRepository.GetResource <PackageMetadataResource>();
            var version = packageMetadataResource.GetMetadataAsync("AzurePipelineAsCode.NET", true, true, new NullLogger(), CancellationToken.None).Result
                          .OfType <PackageSearchMetadata>()
                          .Max(m => m.Version.Version);

            if (nextVersion <= version)
            {
                Console.WriteLine($"Skipping nuget pack, because the target version {nextVersion} is not greater than the latest published version {version}");
                Execute.Command("dotnet", $"build {path} -c Release");
                return;
            }

            Console.WriteLine($"Packing nuget version {nextVersion}");
            Execute.Command("dotnet", $"pack {Path.Combine(path, "AzurePipelineAsCode.NET.csproj")} -c Release");
        }
Beispiel #15
0
        public NuGetClientIntegrationTest(ITestOutputHelper helper)
        {
            Helper = helper ?? throw new ArgumentNullException(nameof(helper));
            server = TestServerBuilder.Create().TraceToTestOutputHelper(Helper, LogLevel.Error).Build();
            var providers = new List <Lazy <INuGetResourceProvider> >();

            providers.AddRange(Repository.Provider.GetCoreV3());
            providers.Add(new Lazy <INuGetResourceProvider>(() => new PackageMetadataResourceV3Provider()));
            _httpClient = server.CreateClient();
            providers.Insert(0, new Lazy <INuGetResourceProvider>(() => new HttpSourceResourceProviderTestHost(_httpClient)));

            indexUrl = new Uri(server.BaseAddress, IndexUrlString).AbsoluteUri;
            var packageSource = new PackageSource(indexUrl);

            _sourceRepository = new SourceRepository(packageSource, providers);
            _cacheContext     = new SourceCacheContext()
            {
                NoCache = true, MaxAge = new DateTimeOffset(), DirectDownload = true
            };
            _httpSource = _sourceRepository.GetResource <HttpSourceResource>();
            Assert.IsType <HttpSourceTestHost>(_httpSource.HttpSource);
        }
Beispiel #16
0
    private static NuGetVersion GetLatestNugetVersion(string package, bool includePrerelease, string sourceUrl)
    {
        var providers = new List <Lazy <INuGetResourceProvider> >();

        providers.AddRange(Repository.Provider.GetCoreV3());

        var packageSource = new PackageSource(sourceUrl);

        var sourceRepository = new SourceRepository(packageSource, providers);

        var metadataResource = sourceRepository.GetResource <PackageMetadataResource>();

        var versions = metadataResource.GetMetadataAsync(package, includePrerelease, false, new SourceCacheContext(),
                                                         new NullLogger(), CancellationToken.None)
                       .GetAwaiter()
                       .GetResult()
                       .Where(data => data.Identity.Id == package)
                       .Select(data => data.Identity.Version)
                       .ToArray();

        return(versions.Any()
            ? versions.Max()
            : null);
    }
        private async IAsyncEnumerable <IPackageSearchMetadata> SearchForPackages(SourceRepository nugetRepository, string searchTerm, bool includePrerelease, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        {
            // The max value allowed by the NuGet search API is 1000.
            // This large value is important to ensure results fit in a single page therefore avoiding the problem with duplicates.
            // For more details, see: https://github.com/NuGet/NuGetGallery/issues/7494
            const int take = 1000;

            var skip    = 0;
            var filters = new SearchFilter(includePrerelease)
            {
                IncludeDelisted = false,
                OrderBy         = SearchOrderBy.Id
            };

            var nugetSearchClient = nugetRepository.GetResource <PackageSearchResource>();

            while (true)
            {
                var searchResult = await nugetSearchClient.SearchAsync(searchTerm, filters, skip, take, NullLogger.Instance, cancellationToken).ConfigureAwait(false);

                skip += take;

                if (!searchResult.Any())
                {
                    break;
                }

                foreach (var packageMetadata in searchResult)
                {
                    if (packageMetadata.Identity.Id.StartsWith($"{searchTerm}.", StringComparison.OrdinalIgnoreCase))
                    {
                        yield return(packageMetadata);
                    }
                }
            }
        }
        public static LocalPackageInfo GetLocalPackage(this SourceRepository repository, PackageIdentity identity, CancellationToken token = default)
        {
            var findPackageResource = repository.GetResource <FindLocalPackagesResource>(token);

            return(findPackageResource.GetPackage(identity, NullLogger.Instance, token));
        }
Beispiel #19
0
        public static ScriptExtensions CompileAssembly(
            NuGetFramework projectFramework,
            PackageConfiguration configuration,
            string editorRepositoryPath,
            bool includeDebugInformation)
        {
            var path = Environment.CurrentDirectory;
            var configurationRoot = ConfigurationHelper.GetConfigurationRoot(configuration);
            var scriptProjectFile = Path.Combine(path, Path.ChangeExtension(OutputAssemblyName, ProjectExtension));

            if (!File.Exists(scriptProjectFile))
            {
                return(new ScriptExtensions(configuration, null));
            }

            var extensionsPath = Path.Combine(path, OutputAssemblyName);

            if (!Directory.Exists(extensionsPath))
            {
                return(new ScriptExtensions(configuration, null));
            }

            var scriptFiles = Directory.GetFiles(extensionsPath, ScriptExtension, SearchOption.AllDirectories);

            if (scriptFiles.Length == 0)
            {
                return(new ScriptExtensions(configuration, null));
            }

            var assemblyNames        = new HashSet <string>();
            var assemblyDirectory    = Path.GetTempPath() + OutputAssemblyName + "." + Guid.NewGuid().ToString();
            var scriptEnvironment    = new ScriptExtensions(configuration, assemblyDirectory);
            var packageSource        = new PackageSource(editorRepositoryPath);
            var packageRepository    = new SourceRepository(packageSource, Repository.Provider.GetCoreV3());
            var dependencyResource   = packageRepository.GetResource <DependencyInfoResource>();
            var localPackageResource = packageRepository.GetResource <FindLocalPackagesResource>();

            using (var cacheContext = new SourceCacheContext())
            {
                var projectReferences = from id in scriptEnvironment.GetPackageReferences()
                                        from assemblyReference in FindAssemblyReferences(
                    projectFramework,
                    dependencyResource,
                    localPackageResource,
                    cacheContext,
                    packageId: id)
                                        select assemblyReference;
                assemblyNames.AddRange(scriptEnvironment.GetAssemblyReferences());
                assemblyNames.AddRange(projectReferences);
            }

            var assemblyFile       = Path.Combine(assemblyDirectory, Path.ChangeExtension(OutputAssemblyName, DllExtension));
            var assemblyReferences = (from fileName in assemblyNames
                                      let assemblyName = Path.GetFileNameWithoutExtension(fileName)
                                                         let assemblyLocation = ConfigurationHelper.GetAssemblyLocation(configuration, assemblyName)
                                                                                select assemblyLocation == null ? fileName :
                                                                                Path.IsPathRooted(assemblyLocation) ? assemblyLocation :
                                                                                Path.Combine(configurationRoot, assemblyLocation))
                                     .ToArray();
            var compilerParameters = new CompilerParameters(assemblyReferences, assemblyFile);

            compilerParameters.GenerateExecutable      = false;
            compilerParameters.GenerateInMemory        = false;
            compilerParameters.IncludeDebugInformation = includeDebugInformation;
            if (!includeDebugInformation)
            {
                compilerParameters.CompilerOptions = "/optimize";
            }

            using (var codeProvider = new CSharpCodeProvider())
            {
                var results = codeProvider.CompileAssemblyFromFile(compilerParameters, scriptFiles);
                if (results.Errors.HasErrors)
                {
                    try
                    {
                        Console.Error.WriteLine("--- Error building script extensions ---");
                        foreach (var error in results.Errors)
                        {
                            Console.Error.WriteLine(error);
                        }
                    }
                    finally { scriptEnvironment.Dispose(); }
                    return(new ScriptExtensions(configuration, null));
                }
                else
                {
                    var assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
                    configuration.AssemblyReferences.Add(assemblyName.Name);
                    configuration.AssemblyLocations.Add(assemblyName.Name, ProcessorArchitecture.MSIL, assemblyName.CodeBase);
                    scriptEnvironment.AssemblyName = assemblyName;
                }
                return(scriptEnvironment);
            }
        }
Beispiel #20
0
        /// <summary>
        /// For each package in our registry.json, query NuGet, extract package metadata, and convert them to unity packages.
        /// </summary>
        private async Task BuildInternal()
        {
            var packageMetadataResource = _sourceRepository.GetResource <PackageMetadataResource>();

            foreach (var packageDesc in _registry)
            {
                var packageName  = packageDesc.Key;
                var packageEntry = packageDesc.Value;
                // A package entry is ignored but allowed in the registry (case of Microsoft.CSharp)
                if (packageEntry.Ignored)
                {
                    continue;
                }

                var packageMetaIt = await packageMetadataResource.GetMetadataAsync(packageName, false, false, _sourceCacheContext, Logger, CancellationToken.None);

                var packageMetas = packageMetaIt.ToList();
                foreach (var packageMeta in packageMetas)
                {
                    var packageIdentity = packageMeta.Identity;
                    var packageId       = packageIdentity.Id.ToLowerInvariant();
                    var npmPackageId    = $"{UnityScope}.{packageId}";

                    if (!packageEntry.Version.Satisfies(packageMeta.Identity.Version))
                    {
                        continue;
                    }

                    PackageDependencyGroup netstd20Dependency = null;

                    foreach (var dependencySet in packageMeta.DependencySets)
                    {
                        if (dependencySet.TargetFramework == NuGetFrameworkNetStandard20)
                        {
                            netstd20Dependency = dependencySet;
                            break;
                        }
                    }

                    if (netstd20Dependency == null)
                    {
                        Logger.LogWarning($"The package `{packageIdentity}` doesn't support `netstandard2.0`");
                        continue;
                    }

                    if (!_npmPackageRegistry.Packages.TryGetValue(npmPackageId, out var npmPackage))
                    {
                        npmPackage = new NpmPackage();
                        _npmPackageRegistry.Packages.Add(npmPackageId, npmPackage);
                    }

                    // One NpmPackage (for package request)

                    var packageInfoList = packageEntry.Listed ? _npmPackageRegistry.ListedPackageInfos : _npmPackageRegistry.UnlistedPackageInfos;

                    if (!packageInfoList.Packages.TryGetValue(npmPackageId, out var npmPackageInfo))
                    {
                        npmPackageInfo = new NpmPackageInfo();
                        packageInfoList.Packages.Add(npmPackageId, npmPackageInfo);
                    }

                    // Update latest version
                    var currentVersion = packageIdentity.Version;

                    var update = !npmPackage.DistTags.TryGetValue("latest", out var latestVersion) ||
                                 (currentVersion > NuGetVersion.Parse(latestVersion));

                    string npmCurrentVersion = $"{currentVersion.Major}.{currentVersion.Minor}.{currentVersion.Patch}";

                    if (currentVersion.Revision != 0)
                    {
                        npmCurrentVersion += $"-{currentVersion.Revision}";
                    }

                    if (update)
                    {
                        npmPackage.DistTags["latest"] = npmCurrentVersion;

                        npmPackageInfo.Versions.Clear();
                        npmPackageInfo.Versions[npmCurrentVersion] = "latest";

                        npmPackage.Id      = npmPackageId;
                        npmPackage.License = packageMeta.LicenseMetadata?.License ?? packageMeta.LicenseUrl?.ToString();

                        npmPackage.Name     = npmPackageId;
                        npmPackageInfo.Name = npmPackageId;

                        npmPackage.Description     = packageMeta.Description;
                        npmPackageInfo.Description = packageMeta.Description;

                        npmPackageInfo.Author = packageMeta.Authors;
                        if (packageMeta.Owners != null)
                        {
                            npmPackageInfo.Maintainers.Clear();
                            npmPackageInfo.Maintainers.AddRange(SplitCommaSeparatedString(packageMeta.Owners));
                        }

                        if (packageMeta.Tags != null)
                        {
                            npmPackageInfo.Keywords.Clear();
                            npmPackageInfo.Keywords.Add("nuget");
                            npmPackageInfo.Keywords.AddRange(SplitCommaSeparatedString(packageMeta.Tags));
                        }
                    }

                    var npmVersion = new NpmPackageVersion
                    {
                        Id          = $"{npmPackageId}@{npmCurrentVersion}",
                        Version     = npmCurrentVersion,
                        Name        = npmPackageId,
                        Description = packageMeta.Description,
                        Author      = AuthorNameUnityGroup,
                        DisplayName = $"{packageMeta.Title} ({npmPackageInfo.Author})"
                    };
                    npmVersion.Distribution.Tarball = new Uri($"{RootHttpUrl}/{npmPackage.Id}/-/{GetUnityPackageFileName(packageIdentity, npmVersion)}");
                    npmVersion.Unity = MinimumUnityVersion;
                    npmPackage.Versions[npmVersion.Version] = npmVersion;

                    bool hasDependencyErrors = false;
                    foreach (var deps in netstd20Dependency.Packages)
                    {
                        var depsId = deps.Id.ToLowerInvariant();

                        if (!_registry.TryGetValue(deps.Id, out var packageEntryDep))
                        {
                            LogError($"The package `{packageIdentity}` has a dependency on `{deps.Id}` which is not in the registry. You must add this dependency to the registry.json file.");
                            hasDependencyErrors = true;
                        }
                        else if (packageEntryDep.Ignored)
                        {
                            // A package that is ignored is not declared as an explicit dependency
                            continue;
                        }
                        else if (!deps.VersionRange.IsSubSetOrEqualTo(packageEntryDep.Version))
                        {
                            LogError($"The version range `{deps.VersionRange}` for the dependency `{deps.Id}` for the package `{packageIdentity}` doesn't match the range allowed from the registry.json: `{packageEntryDep.Version}`");
                            hasDependencyErrors = true;
                        }

                        // Otherwise add the package as a dependency
                        npmVersion.Dependencies.Add($"{UnityScope}.{depsId}", deps.VersionRange.MinVersion.ToString());
                    }

                    // If we don't have any dependencies error, generate the package
                    if (!hasDependencyErrors)
                    {
                        await ConvertNuGetToUnityPackageIfDoesNotExist(packageIdentity, npmPackageInfo, npmVersion, packageMeta);

                        npmPackage.Time[npmCurrentVersion] = packageMeta.Published?.UtcDateTime ?? GetUnityPackageFileInfo(packageIdentity, npmVersion).CreationTimeUtc;

                        // Copy repository info if necessary
                        if (update)
                        {
                            npmPackage.Repository = npmVersion.Repository?.Clone();
                        }
                    }
                }
            }
        }
 /// <summary>
 /// Get resource from repository, alternative for extension method
 /// </summary>
 /// <typeparam name="TResource"></typeparam>
 /// <returns></returns>
 public TResource GetResource <TResource>() where TResource : class, INuGetResource
 {
     return(sourceRepository.GetResource <TResource>());
 }
Beispiel #22
0
        /// <summary>
        /// For each package in our registry.json, query NuGet, extract package metadata, and convert them to unity packages.
        /// </summary>
        private async Task BuildInternal()
        {
            var packageMetadataResource = _sourceRepository.GetResource <PackageMetadataResource>();

            var versionPath = Path.Combine(_rootPersistentFolder, "version.txt");
            var forceUpdate = !File.Exists(versionPath) || await File.ReadAllTextAsync(versionPath) != CurrentRegistryVersion;

            if (forceUpdate)
            {
                _logger.LogInformation($"Registry version changed to {CurrentRegistryVersion} - Regenerating all packages");
            }


            var regexFilter = Filter != null ? new Regex(Filter, RegexOptions.IgnoreCase) : null;

            if (Filter != null)
            {
                _logger.LogInformation($"Filtering with regex: {Filter}");
            }

            var onProgress = OnProgress;

            var progressCount = 0;

            foreach (var packageDesc in _registry)
            {
                var packageName  = packageDesc.Key;
                var packageEntry = packageDesc.Value;

                // Log progress count
                onProgress?.Invoke(++progressCount, _registry.Count);

                // A package entry is ignored but allowed in the registry (case of Microsoft.CSharp)
                if (packageEntry.Ignored || (regexFilter != null && !regexFilter.IsMatch(packageName)))
                {
                    continue;
                }

                var packageMetaIt = await packageMetadataResource.GetMetadataAsync(packageName, false, false, _sourceCacheContext, _logger, CancellationToken.None);

                var packageMetas = packageMetaIt.ToList();
                foreach (var packageMeta in packageMetas)
                {
                    var packageIdentity = packageMeta.Identity;
                    var packageId       = packageIdentity.Id.ToLowerInvariant();
                    var npmPackageId    = $"{_unityScope}.{packageId}";

                    if (!packageEntry.Version.Satisfies(packageMeta.Identity.Version))
                    {
                        continue;
                    }

                    var resolvedDependencyGroups = packageMeta.DependencySets.Where(dependencySet => dependencySet.TargetFramework.IsAny || _targetFrameworks.Any(targetFramework => dependencySet.TargetFramework == targetFramework.Framework)).ToList();

                    if (!packageEntry.Analyzer && resolvedDependencyGroups.Count == 0)
                    {
                        _logger.LogWarning($"The package `{packageIdentity}` doesn't support `{string.Join(",", _targetFrameworks.Select(x => x.Name))}`");
                        continue;
                    }

                    if (!_npmPackageRegistry.Packages.TryGetValue(npmPackageId, out var npmPackage))
                    {
                        npmPackage = new NpmPackage();
                        _npmPackageRegistry.Packages.Add(npmPackageId, npmPackage);
                    }

                    // One NpmPackage (for package request)

                    var packageInfoList = packageEntry.Listed ? _npmPackageRegistry.ListedPackageInfos : _npmPackageRegistry.UnlistedPackageInfos;

                    if (!packageInfoList.Packages.TryGetValue(npmPackageId, out var npmPackageInfo))
                    {
                        npmPackageInfo = new NpmPackageInfo();
                        packageInfoList.Packages.Add(npmPackageId, npmPackageInfo);
                    }

                    // Update latest version
                    var currentVersion = packageIdentity.Version;

                    var update = !npmPackage.DistTags.TryGetValue("latest", out var latestVersion) ||
                                 (currentVersion > NuGetVersion.Parse(latestVersion)) ||
                                 forceUpdate;

                    string npmCurrentVersion = GetNpmVersion(currentVersion);

                    if (update)
                    {
                        npmPackage.DistTags["latest"] = npmCurrentVersion;

                        npmPackageInfo.Versions.Clear();
                        npmPackageInfo.Versions[npmCurrentVersion] = "latest";

                        npmPackage.Id      = npmPackageId;
                        npmPackage.License = packageMeta.LicenseMetadata?.License ?? packageMeta.LicenseUrl?.ToString();

                        npmPackage.Name     = npmPackageId;
                        npmPackageInfo.Name = npmPackageId;

                        npmPackage.Description     = packageMeta.Description;
                        npmPackageInfo.Description = packageMeta.Description;

                        npmPackageInfo.Author = packageMeta.Authors;
                        if (packageMeta.Owners != null)
                        {
                            npmPackageInfo.Maintainers.Clear();
                            npmPackageInfo.Maintainers.AddRange(SplitCommaSeparatedString(packageMeta.Owners));
                        }

                        if (packageMeta.Tags != null)
                        {
                            npmPackageInfo.Keywords.Clear();
                            npmPackageInfo.Keywords.Add("nuget");
                            npmPackageInfo.Keywords.AddRange(SplitCommaSeparatedString(packageMeta.Tags));
                        }
                    }

                    var npmVersion = new NpmPackageVersion
                    {
                        Id          = $"{npmPackageId}@{npmCurrentVersion}",
                        Version     = npmCurrentVersion,
                        Name        = npmPackageId,
                        Description = packageMeta.Description,
                        Author      = npmPackageInfo.Author,
                        DisplayName = packageMeta.Title + _packageNameNuGetPostFix
                    };
                    npmVersion.Distribution.Tarball = new Uri(_rootHttpUri, $"{npmPackage.Id}/-/{GetUnityPackageFileName(packageIdentity, npmVersion)}");
                    npmVersion.Unity = _minimumUnityVersion;
                    npmPackage.Versions[npmVersion.Version] = npmVersion;

                    bool hasDependencyErrors = false;
                    foreach (var resolvedDependencyGroup in resolvedDependencyGroups)
                    {
                        foreach (var deps in resolvedDependencyGroup.Packages)
                        {
                            if (!_registry.TryGetValue(deps.Id, out var packageEntryDep))
                            {
                                LogError($"The package `{packageIdentity}` has a dependency on `{deps.Id}` which is not in the registry. You must add this dependency to the registry.json file.");
                                hasDependencyErrors = true;
                            }
                            else if (packageEntryDep.Ignored)
                            {
                                // A package that is ignored is not declared as an explicit dependency
                                continue;
                            }
                            else if (!deps.VersionRange.IsSubSetOrEqualTo(packageEntryDep.Version))
                            {
                                LogError($"The version range `{deps.VersionRange}` for the dependency `{deps.Id}` for the package `{packageIdentity}` doesn't match the range allowed from the registry.json: `{packageEntryDep.Version}`");
                                hasDependencyErrors = true;
                                continue;
                            }

                            // Otherwise add the package as a dependency
                            var depsId = deps.Id.ToLowerInvariant();
                            var key    = $"{_unityScope}.{depsId}";
                            if (!npmVersion.Dependencies.ContainsKey(key))
                            {
                                npmVersion.Dependencies.Add(key, GetNpmVersion(deps.VersionRange.MinVersion));
                            }
                        }
                    }

                    // If we don't have any dependencies error, generate the package
                    if (!hasDependencyErrors)
                    {
                        await ConvertNuGetToUnityPackageIfDoesNotExist(packageIdentity, npmPackageInfo, npmVersion, packageMeta, forceUpdate, packageEntry);

                        npmPackage.Time[npmCurrentVersion] = packageMeta.Published?.UtcDateTime ?? GetUnityPackageFileInfo(packageIdentity, npmVersion).CreationTimeUtc;

                        // Copy repository info if necessary
                        if (update)
                        {
                            npmPackage.Repository = npmVersion.Repository?.Clone();
                        }
                    }
                }
            }

            if (forceUpdate)
            {
                await File.WriteAllTextAsync(versionPath, CurrentRegistryVersion);
            }
        }
Beispiel #23
0
        private static PackageMetadataResource BuildPackageResource(string repositoryUrl)
        {
            SourceRepository repository = BuildSourceRepository(repositoryUrl);

            return(repository.GetResource <PackageMetadataResource>());
        }
        public static IEnumerable <LocalPackageInfo> GetLocalPackages(this SourceRepository repository, CancellationToken token = default)
        {
            var findPackageResource = repository.GetResource <FindLocalPackagesResource>(token);

            return(findPackageResource.GetPackages(NullLogger.Instance, token));
        }
Beispiel #25
0
        /// <summary>
        ///     Initializes this instance.
        /// </summary>
        private void Initialize()
        {
            _logger.LogInformation("Using Sources:");

            var settings = Settings.LoadDefaultSettings(null);

            if (!string.IsNullOrEmpty(_options.CustomNuGetFile))
            {
                if (File.Exists(_options.CustomNuGetFile))
                {
                    var additionalSettings = Settings.LoadSpecificSettings(null, _options.CustomNuGetFile);
                    var section            = additionalSettings.GetSection("packageSources");
                    foreach (var item in section.Items)
                    {
                        settings.AddOrUpdate("packageSources", item);
                    }
                }
                else
                {
                    _logger.LogWarning($"Additional NuGet Config \"{_options.CustomNuGetFile}\" not found");
                }
            }

            var sourceRepositoryProvider = new SourceRepositoryProvider(settings, Repository.Provider.GetCoreV3());
            var sourceRepository         = sourceRepositoryProvider.GetRepositories();

            foreach (var repository in sourceRepository)
            {
                var resource = repository.GetResource <PackageMetadataResource>();
                _packageMetadataResources.Add(resource);
                _logger.LogInformation("  " + repository + " \t - \t" + repository.PackageSource.Source);
                Sources.Add(repository.PackageSource.Source);
            }


            if (!string.IsNullOrEmpty(_options.AzureArtifactsFeedUri))
            {
                _logger.LogInformation($"Adding Azure Feed: {_options.AzureArtifactsFeedUri}");
                var username = Environment.GetEnvironmentVariable("BUILD_REQUESTEDFOREMAIL");
                var token    = Environment.GetEnvironmentVariable("SYSTEM_ACCESSTOKEN");

                if (string.IsNullOrEmpty(username))
                {
                    throw new Exception("Username not provided");
                }

                if (string.IsNullOrEmpty(token))
                {
                    throw new Exception("This features needs access to the OAuth token to query DevOps Artifacts. Please activate OAuth Access for this stage. See https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops#system-variables");
                }

                _logger.LogInformation("Adding DevOps Feed with the provided credentials...");
                var ps = new PackageSource(_options.AzureArtifactsFeedUri)
                {
                    Credentials = new PackageSourceCredential(_options.AzureArtifactsFeedUri, username, token, true,
                                                              "basic,negotiate")
                };

                var sr = new SourceRepository(ps, Repository.Provider.GetCoreV3());
                var metadataResource = sr.GetResource <PackageMetadataResource>();
                _packageMetadataResources.Add(metadataResource);
                Sources.Add(sr.PackageSource.Source);
            }

            _logger.LogInformation(string.Empty); // Blank line
        }