Пример #1
0
        /// <summary>
        /// Add a file to the repository.
        /// </summary>
        public override void AddPackage(IPackage package)
        {
            string fileName = _pathResolver.GetPackageFileName(package);

            if (_fileSystem.FileExists(fileName) && !AllowOverrideExistingPackageOnPush)
            {
                throw new InvalidOperationException(String.Format(NuGetResources.Error_PackageAlreadyExists, package));
            }

            lock (_lockObj)
            {
                using (Stream stream = package.GetStream())
                {
                    _fileSystem.AddFile(fileName, stream);
                }

                //InvalidatePackages();

                ParallelOptions opts = new ParallelOptions();
                opts.MaxDegreeOfParallelism = 4;

                ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > absoluteLatest =
                    new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >();
                ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > latest =
                    new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >();

                // get settings
                bool checkFrameworks = EnableFrameworkFiltering;
                // we need to save the current context because it's stored in TLS and we're computing hashes on different threads.
                var context = HttpContext.Current;

                // load and cache all packages.
                // Note that we can't pass GetPackageFiles() to Parallel.ForEach() because
                // the file could be added/deleted from _fileSystem, and if this happens,
                // we'll get error "Collection was modified; enumeration operation may not execute."
                // So we have to materialize the IEnumerable into a list first.

                var path = _pathResolver.GetInstallPath(package) + ".nupkg";
                Debug.Assert(package != null, "Unable to open " + path);
                if (package == null)
                {
                    return;
                }
                AddPackageToCache(PackageCache, absoluteLatest, latest, checkFrameworks, context, path, package);

                // Set additional attributes after visiting all packages
                foreach (var entry in absoluteLatest.Values)
                {
                    entry.Item2.IsAbsoluteLatestVersion = true;
                }

                foreach (var entry in latest.Values)
                {
                    entry.Item2.IsLatestVersion = true;
                }
            }
        }
        public IEnumerable <ScaffolderInfo> GetScaffolders(Project project, string name, bool resolveDefaultNames)
        {
            var customScaffoldersFolder = project != null?project.GetProjectItem(ScaffoldingConstants.CustomScaffoldersFolderPath) : null;

            var customScaffoldersFolders = customScaffoldersFolder != null ? new[] { customScaffoldersFolder } : Enumerable.Empty <ProjectItem>();

            var allPackages           = _packageManager.LocalRepository.GetPackages();
            var packagesWithToolsPath = from p in allPackages
                                        let installPath = _pathResolver.GetInstallPath(p)
                                                          where !string.IsNullOrEmpty(installPath)
                                                          select new {
                Package   = p,
                ToolsPath = Path.Combine(installPath, "tools")
            };

            // Only consider the single latest version of any given package
            var latestPackagesWithToolsPath = from packageGroup in packagesWithToolsPath.GroupBy(x => x.Package.Id)
                                              select packageGroup.OrderByDescending(x => x.Package.Version).First();

            // Note that we prefer to match actual scaffolder names (rather than resolving default names first)
            // so that you can't "override" a scaffolder by creating a default with the same name. Allowing that
            // could lead to unexpected behavior, especially if such a default was created by mistake.
            var actualMatches = latestPackagesWithToolsPath.SelectMany(x => FindScaffolders(x.Package, x.ToolsPath, name))
                                .Concat(customScaffoldersFolders.SelectMany(x => FindScaffolders(x, name)));

            if (actualMatches.Any())
            {
                return(actualMatches);
            }

            // Since no scaffolders actually match "name", try resolving that as a default name and go again
            if (resolveDefaultNames)
            {
                // First look for a project-specific setting, and if not found, fall back on solution-wide settings
                IQueryable <DefaultScaffolderConfigEntry> resolvedNames = null;
                if (project != null)
                {
                    resolvedNames = _configStore.GetProjectDefaultScaffolders(project).Where(x => x.DefaultName.Equals(name, StringComparison.OrdinalIgnoreCase));
                }
                if ((resolvedNames == null) || (!resolvedNames.Any()))
                {
                    resolvedNames = _configStore.GetSolutionDefaultScaffolders().Where(x => x.DefaultName.Equals(name, StringComparison.OrdinalIgnoreCase));
                }

                if (resolvedNames.Count() == 1)
                {
                    var resolvedName = resolvedNames.Single().ScaffolderName;
                    return(latestPackagesWithToolsPath.SelectMany(x => FindScaffolders(x.Package, x.ToolsPath, resolvedName))
                           .Concat(customScaffoldersFolders.SelectMany(x => FindScaffolders(x, resolvedName))));
                }
            }

            return(Enumerable.Empty <ScaffolderInfo>());
        }
        private string ResolvePackagePath(IPackagePathResolver defaultResolver,
                                          IEnumerable <IPackagePathResolver> cacheResolvers,
                                          IPackage package)
        {
            var    defaultHashPath = defaultResolver.GetHashPath(package.Id, package.Version);
            string expectedHash    = null;

            if (File.Exists(defaultHashPath))
            {
                expectedHash = File.ReadAllText(defaultHashPath);
            }
            else if (_globalSettings != null)
            {
                var library = new Library()
                {
                    Name    = package.Id,
                    Version = package.Version
                };

                _globalSettings.PackageHashes.TryGetValue(library, out expectedHash);
            }

            if (string.IsNullOrEmpty(expectedHash))
            {
                return(defaultResolver.GetInstallPath(package.Id, package.Version));
            }

            foreach (var resolver in cacheResolvers)
            {
                var cacheHashFile = resolver.GetHashPath(package.Id, package.Version);

                // REVIEW: More efficient compare?
                if (File.Exists(cacheHashFile) &&
                    File.ReadAllText(cacheHashFile) == expectedHash)
                {
                    return(resolver.GetInstallPath(package.Id, package.Version));
                }
            }

            return(defaultResolver.GetInstallPath(package.Id, package.Version));
        }
        public void GetPackagesByDependencyOrder_OnePackageInSharedRepository_SharedRepositoryCreatedWithPathResolverForSolutionPackagesFolder()
        {
            CreateSolution(@"d:\projects\myproject\myproject.sln");
            CreateRepository(solution);
            FakePackage package = AddPackageToSharedRepository("Test", "1.0");

            repository.GetPackagesByDependencyOrder().ToList();

            IPackagePathResolver pathResolver = fakeRepositoryFactory.PathResolverPassedToCreateSharedRepository;
            string installPath = pathResolver.GetInstallPath(package);

            string expectedInstallPath = @"d:\projects\myproject\packages\Test.1.0".ToNativePath();

            Assert.AreEqual(expectedInstallPath, installPath);
        }
Пример #5
0
        public static LockFileLibrary CreateLockFileLibrary(LockFileLibrary previousLibrary, IPackagePathResolver resolver, IPackage package, string correctedPackageName = null)
        {
            var lockFileLib = new LockFileLibrary();

            // package.Id is read from nuspec and it might be in wrong casing.
            // correctedPackageName should be the package name used by dependency graph and
            // it has the correct casing that runtime needs during dependency resolution.
            lockFileLib.Name = correctedPackageName ?? package.Id;
            lockFileLib.Version = package.Version;
            lockFileLib.Sha512 = File.ReadAllText(resolver.GetHashPath(package.Id, package.Version));

            // If the shas are equal then do nothing
            if (previousLibrary?.Sha512 == lockFileLib.Sha512)
            {
                lockFileLib.Files = previousLibrary.Files;
                lockFileLib.IsServiceable = previousLibrary.IsServiceable;
            }
            else
            {
                lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList();
                var installPath = resolver.GetInstallPath(package.Id, package.Version);
                foreach (var filePath in lockFileLib.Files)
                {
                    if (!string.Equals(Path.GetExtension(filePath), ".dll", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var assemblyPath = Path.Combine(installPath, filePath);
                    try
                    {
                        if (IsAssemblyServiceable(assemblyPath))
                        {
                            lockFileLib.IsServiceable = true;
                            break;
                        }
                    }
                    catch
                    {
                        // Just move on to the next file
                    }
                }
            }

            return lockFileLib;
        }
Пример #6
0
        public static LockFileLibrary CreateLockFileLibrary(LockFileLibrary previousLibrary, IPackagePathResolver resolver, IPackage package, string correctedPackageName = null)
        {
            var lockFileLib = new LockFileLibrary();

            // package.Id is read from nuspec and it might be in wrong casing.
            // correctedPackageName should be the package name used by dependency graph and
            // it has the correct casing that runtime needs during dependency resolution.
            lockFileLib.Name    = correctedPackageName ?? package.Id;
            lockFileLib.Version = package.Version;
            lockFileLib.Sha512  = File.ReadAllText(resolver.GetHashPath(package.Id, package.Version));

            // If the shas are equal then do nothing
            if (previousLibrary?.Sha512 == lockFileLib.Sha512)
            {
                lockFileLib.Files         = previousLibrary.Files;
                lockFileLib.IsServiceable = previousLibrary.IsServiceable;
            }
            else
            {
                lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList();
                var installPath = resolver.GetInstallPath(package.Id, package.Version);
                foreach (var filePath in lockFileLib.Files)
                {
                    if (!string.Equals(Path.GetExtension(filePath), ".dll", StringComparison.OrdinalIgnoreCase))
                    {
                        continue;
                    }

                    var assemblyPath = Path.Combine(installPath, filePath);
                    try
                    {
                        if (IsAssemblyServiceable(assemblyPath))
                        {
                            lockFileLib.IsServiceable = true;
                            break;
                        }
                    }
                    catch
                    {
                        // Just move on to the next file
                    }
                }
            }

            return(lockFileLib);
        }
        private void EnsureMainAssemblyLoadedInAppDomain(IPackagePathResolver pathResolver, IPackage package, PackageMetadata packageInfo, Guid correlationId)
        {
            var mainAssemblyFileName    = $"{PackageMainAssemblySubFolderPath}{packageInfo.PackageKey}{DefaultAssemblyExtensionLowerCase}";
            var mainAssemblyPackageFile =
                package
                .GetLibFiles()
                .FirstOrDefault(libFile => libFile.Path.Equals(mainAssemblyFileName, StringComparison.InvariantCultureIgnoreCase));

            if (mainAssemblyPackageFile == null)
            {
                this.LogPackageOperation(packageInfo.PackageKey, package.Version, $"No assembly {mainAssemblyFileName} found in {package.GetFullName()}", correlationId);
                return;
            }

            var mainAssemblyFullPath = Path.Combine(pathResolver.GetInstallPath(package), mainAssemblyPackageFile.Path);

            this.LogPackageOperation(packageInfo.PackageKey, package.Version, $"AppDomain.LoadAssembly:'{mainAssemblyFullPath}'", correlationId);
            AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(mainAssemblyFullPath));
        }
Пример #8
0
        private string ResolvePackagePath(IPackagePathResolver defaultResolver,
                                          IEnumerable <IPackagePathResolver> cacheResolvers,
                                          PackageInfo packageInfo)
        {
            string expectedHash = packageInfo.LockFileLibrary.Sha512;

            foreach (var resolver in cacheResolvers)
            {
                var cacheHashFile = resolver.GetHashPath(packageInfo.Id, packageInfo.Version);

                // REVIEW: More efficient compare?
                if (File.Exists(cacheHashFile) &&
                    File.ReadAllText(cacheHashFile) == expectedHash)
                {
                    return(resolver.GetInstallPath(packageInfo.Id, packageInfo.Version));
                }
            }

            return(defaultResolver.GetInstallPath(packageInfo.Id, packageInfo.Version));
        }
Пример #9
0
        private static string ResolvePackagePath(IPackagePathResolver defaultResolver,
                                                 IEnumerable <IPackagePathResolver> cacheResolvers,
                                                 IPackage package)
        {
            var defaultHashPath = defaultResolver.GetHashPath(package.Id, package.Version);

            foreach (var resolver in cacheResolvers)
            {
                var cacheHashFile = resolver.GetHashPath(package.Id, package.Version);

                // REVIEW: More efficient compare?
                if (File.Exists(defaultHashPath) &&
                    File.Exists(cacheHashFile) &&
                    File.ReadAllText(defaultHashPath) == File.ReadAllText(cacheHashFile))
                {
                    return(resolver.GetInstallPath(package.Id, package.Version));
                }
            }

            return(defaultResolver.GetInstallPath(package.Id, package.Version));
        }
 public string GetPath(IPackage package, IPackageAssemblyReference packageAssemblyReference)
 {
     return(Path.Combine(_pathResolver.GetInstallPath(package), packageAssemblyReference.Path));
 }
Пример #11
0
        private string ResolvePackagePath(IPackagePathResolver defaultResolver,
                                          IEnumerable<IPackagePathResolver> cacheResolvers,
                                          PackageInfo packageInfo)
        {
            string expectedHash = packageInfo.LockFileLibrary.Sha;

            foreach (var resolver in cacheResolvers)
            {
                var cacheHashFile = resolver.GetHashPath(packageInfo.Id, packageInfo.Version);

                // REVIEW: More efficient compare?
                if (File.Exists(cacheHashFile) &&
                    File.ReadAllText(cacheHashFile) == expectedHash)
                {
                    return resolver.GetInstallPath(packageInfo.Id, packageInfo.Version);
                }
            }

            return defaultResolver.GetInstallPath(packageInfo.Id, packageInfo.Version);
        }
Пример #12
0
        private static string ResolvePackagePath(IPackagePathResolver defaultResolver,
                                                 IEnumerable<IPackagePathResolver> cacheResolvers,
                                                 IPackage package)
        {
            var defaultHashPath = defaultResolver.GetHashPath(package.Id, package.Version);

            foreach (var resolver in cacheResolvers)
            {
                var cacheHashFile = resolver.GetHashPath(package.Id, package.Version);

                // REVIEW: More efficient compare?
                if (File.Exists(defaultHashPath) &&
                    File.Exists(cacheHashFile) &&
                    File.ReadAllText(defaultHashPath) == File.ReadAllText(cacheHashFile))
                {
                    return resolver.GetInstallPath(package.Id, package.Version);
                }
            }

            return defaultResolver.GetInstallPath(package.Id, package.Version);
        }
Пример #13
0
        public static LockFileLibrary CreateLockFileLibraryForProject(
            Runtime.Project project,
            IPackage package,
            SHA512 sha512,
            IEnumerable <FrameworkName> frameworks,
            IPackagePathResolver resolver,
            string correctedPackageName = null)
        {
            var lockFileLib = new LockFileLibrary();

            // package.Id is read from nuspec and it might be in wrong casing.
            // correctedPackageName should be the package name used by dependency graph and
            // it has the correct casing that runtime needs during dependency resolution.
            lockFileLib.Name    = correctedPackageName ?? package.Id;
            lockFileLib.Version = package.Version;

            using (var nupkgStream = package.GetStream())
            {
                lockFileLib.Sha = Convert.ToBase64String(sha512.ComputeHash(nupkgStream));
            }
            lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList();

            foreach (var framework in frameworks)
            {
                var group = new LockFileFrameworkGroup();
                group.TargetFramework = framework;

                IEnumerable <PackageDependencySet> dependencySet;
                if (VersionUtility.TryGetCompatibleItems(framework, package.DependencySets, out dependencySet))
                {
                    var set = dependencySet.FirstOrDefault()?.Dependencies?.ToList();

                    if (set != null)
                    {
                        group.Dependencies = set;
                    }
                }

                // TODO: Remove this when we do #596
                // ASP.NET Core isn't compatible with generic PCL profiles
                if (!string.Equals(framework.Identifier, VersionUtility.AspNetCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase) &&
                    !string.Equals(framework.Identifier, VersionUtility.DnxCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase))
                {
                    IEnumerable <FrameworkAssemblyReference> frameworkAssemblies;
                    if (VersionUtility.TryGetCompatibleItems(framework, package.FrameworkAssemblies, out frameworkAssemblies))
                    {
                        foreach (var assemblyReference in frameworkAssemblies)
                        {
                            if (!assemblyReference.SupportedFrameworks.Any() &&
                                !VersionUtility.IsDesktop(framework))
                            {
                                // REVIEW: This isn't 100% correct since none *can* mean
                                // any in theory, but in practice it means .NET full reference assembly
                                // If there's no supported target frameworks and we're not targeting
                                // the desktop framework then skip it.

                                // To do this properly we'll need all reference assemblies supported
                                // by each supported target framework which isn't always available.
                                continue;
                            }

                            group.FrameworkAssemblies.Add(assemblyReference);
                        }
                    }
                }

                group.RuntimeAssemblies = GetPackageAssemblies(package, framework);

                string contractPath = Path.Combine("lib", "contract", package.Id + ".dll");
                var    hasContract  = lockFileLib.Files.Any(path => path == contractPath);
                var    hasLib       = group.RuntimeAssemblies.Any();

                if (hasContract && hasLib && !VersionUtility.IsDesktop(framework))
                {
                    group.CompileTimeAssemblies.Add(contractPath);
                }
                else if (hasLib)
                {
                    group.CompileTimeAssemblies.AddRange(group.RuntimeAssemblies);
                }

                lockFileLib.FrameworkGroups.Add(group);
            }

            var installPath = resolver.GetInstallPath(package.Id, package.Version);

            foreach (var assembly in lockFileLib.FrameworkGroups.SelectMany(f => f.RuntimeAssemblies))
            {
                var assemblyPath = Path.Combine(installPath, assembly);
                if (IsAssemblyServiceable(assemblyPath))
                {
                    lockFileLib.IsServiceable = true;
                    break;
                }
            }

            return(lockFileLib);
        }
Пример #14
0
        public static LockFileLibrary CreateLockFileLibraryForProject(
            Runtime.Project project,
            IPackage package,
            SHA512 sha512,
            IEnumerable<FrameworkName> frameworks,
            IPackagePathResolver resolver,
            string correctedPackageName = null)
        {
            var lockFileLib = new LockFileLibrary();

            // package.Id is read from nuspec and it might be in wrong casing.
            // correctedPackageName should be the package name used by dependency graph and
            // it has the correct casing that runtime needs during dependency resolution.
            lockFileLib.Name = correctedPackageName ?? package.Id;
            lockFileLib.Version = package.Version;

            using (var nupkgStream = package.GetStream())
            {
                lockFileLib.Sha = Convert.ToBase64String(sha512.ComputeHash(nupkgStream));
            }
            lockFileLib.Files = package.GetFiles().Select(p => p.Path).ToList();

            foreach (var framework in frameworks)
            {
                var group = new LockFileFrameworkGroup();
                group.TargetFramework = framework;

                IEnumerable<PackageDependencySet> dependencySet;
                if (VersionUtility.TryGetCompatibleItems(framework, package.DependencySets, out dependencySet))
                {
                    var set = dependencySet.FirstOrDefault()?.Dependencies?.ToList();

                    if (set != null)
                    {
                        group.Dependencies = set;
                    }
                }

                // TODO: Remove this when we do #596
                // ASP.NET Core isn't compatible with generic PCL profiles
                if (!string.Equals(framework.Identifier, VersionUtility.AspNetCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase) &&
                    !string.Equals(framework.Identifier, VersionUtility.DnxCoreFrameworkIdentifier, StringComparison.OrdinalIgnoreCase))
                {
                    IEnumerable<FrameworkAssemblyReference> frameworkAssemblies;
                    if (VersionUtility.TryGetCompatibleItems(framework, package.FrameworkAssemblies, out frameworkAssemblies))
                    {
                        foreach (var assemblyReference in frameworkAssemblies)
                        {
                            if (!assemblyReference.SupportedFrameworks.Any() &&
                                !VersionUtility.IsDesktop(framework))
                            {
                                // REVIEW: This isn't 100% correct since none *can* mean
                                // any in theory, but in practice it means .NET full reference assembly
                                // If there's no supported target frameworks and we're not targeting
                                // the desktop framework then skip it.

                                // To do this properly we'll need all reference assemblies supported
                                // by each supported target framework which isn't always available.
                                continue;
                            }

                            group.FrameworkAssemblies.Add(assemblyReference);
                        }
                    }
                }

                group.RuntimeAssemblies = GetPackageAssemblies(package, framework);

                string contractPath = Path.Combine("lib", "contract", package.Id + ".dll");
                var hasContract = lockFileLib.Files.Any(path => path == contractPath);
                var hasLib = group.RuntimeAssemblies.Any();

                if (hasContract && hasLib && !VersionUtility.IsDesktop(framework))
                {
                    group.CompileTimeAssemblies.Add(contractPath);
                }
                else if (hasLib)
                {
                    group.CompileTimeAssemblies.AddRange(group.RuntimeAssemblies);
                }

                lockFileLib.FrameworkGroups.Add(group);
            }

            var installPath = resolver.GetInstallPath(package.Id, package.Version);
            foreach (var assembly in lockFileLib.FrameworkGroups.SelectMany(f => f.RuntimeAssemblies))
            {
                var assemblyPath = Path.Combine(installPath, assembly);
                if (IsAssemblyServiceable(assemblyPath))
                {
                    lockFileLib.IsServiceable = true;
                    break;
                }
            }

            return lockFileLib;
        }