예제 #1
0
 public PackageFolder(
     string physicalPath,
     bool ignoreFailure,
     Reports reports)
 {
     // We need to help restore operation to ensure case-sensitivity here
     // Turn on the flag to get package ids in accurate casing
     _repository = new PackageRepository(physicalPath, caseSensitivePackagesName: true);
     _fileSystem = new PhysicalFileSystem(physicalPath);
     _pathResolver = new DefaultPackagePathResolver(_fileSystem);
     _reports = reports;
     Source = physicalPath;
     _ignored = false;
     _ignoreFailure = ignoreFailure;
 }
예제 #2
0
        private void PurgeOrphanPackages()
        {
            _reports.Verbose.WriteLine("Removing orphaned packages...");

            // Find the packages still used by the command scripts
            var applicationPackagesStillUsed = _commandsRepo.Commands
                .Select(cmd => _commandsRepo.FindCommandOwner(cmd))
                .Distinct();

            // Get all the installed packages
            var packagesRepo = new PackageRepository(
                _commandsRepo.PackagesRoot,
                caseSensitivePackagesName: false);

            // Key = package<id, version>, Value = bool (true if used, false otherwise)
            var usedPackages = packagesRepo
                    .GetAllPackages()
                    .SelectMany(pack => pack.Value)
                    .ToDictionary(
                        pack => pack,
                        _ => false);

            var lockFileFormat = new LockFileFormat();

            // Mark all the packages still in used by using the dependencies in the lock file
            foreach(var applicationPackage in applicationPackagesStillUsed)
            {
                var appLockFileFullPath = Path.Combine(
                    _commandsRepo.PackagesRoot.Root,
                    _commandsRepo.PathResolver.GetPackageDirectory(applicationPackage.Id, applicationPackage.Version),
                    InstallBuilder.CommandsFolderName,
                    LockFileFormat.LockFileName);

                if (!File.Exists(appLockFileFullPath))
                {
                    _reports.Verbose.WriteLine("Lock file {0} not found. This package will be removed if it is not used by another application", appLockFileFullPath);
                    // The lock file is missing, probably the app is not installed correctly
                    // unless someone else is using it, we'll remove it
                    continue;
                }

                var lockFile = lockFileFormat.Read(appLockFileFullPath);
                foreach(var dependency in lockFile.PackageLibraries)
                {
                    var dependencyPackage = new NuGet.PackageInfo(
                        _commandsRepo.PackagesRoot,
                        dependency.Name,
                        dependency.Version,
                        null,
                        null);

                    if (usedPackages.ContainsKey(dependencyPackage))
                    {
                        // Mark the dependency as used
                        usedPackages[dependencyPackage] = true;
                    }
                }
            }

            // Now it's time to remove those unused packages
            var unusedPackages = usedPackages
                .Where(pack => !pack.Value)
                .Select(pack => pack.Key);

            foreach (var package in unusedPackages)
            {
                packagesRepo.RemovePackage(package);
                _reports.Verbose.WriteLine("Removed orphaned package: {0} {1}", package.Id, package.Version);
            }
        }
예제 #3
0
 public NuGetDependencyResolver(PackageRepository repository)
 {
     _repository = repository;
     Dependencies = Enumerable.Empty<LibraryDescription>();
 }
        public void LibrariesAreResolvedCorrectly(string packageName, string framework, bool resolved)
        {
            var repo = new PackageRepository("path/to/packages");
            var resolver = new NuGetDependencyResolver(repo);

            var net45Target = new LockFileTarget
            {
                TargetFramework = new FrameworkName(".NETFramework,Version=v4.5"),
                Libraries = new List<LockFileTargetLibrary>
                {
                    new LockFileTargetLibrary
                    {
                        Name = "MetaPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "Net451LibPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "Net451RefPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "AssemblyPlaceholderPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                        CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("ref", "net45", "_._") },
                        RuntimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net45", "_._") }
                    }
                }
            };

            var dnx451Target = new LockFileTarget
            {
                TargetFramework = new FrameworkName("DNX,Version=v4.5.1"),
                Libraries = new List<LockFileTargetLibrary>
                {
                    new LockFileTargetLibrary
                    {
                        Name = "MetaPackage",
                        Version = SemanticVersion.Parse("1.0.0")
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "Net451LibPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                        CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net451", "Net451LibPackage.dll") },
                        RuntimeAssemblies = new List<LockFileItem> { Path.Combine("lib", "net451", "Net451LibPackage.dll") }
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "Net451RefPackage",
                        Version = SemanticVersion.Parse("1.0.0"),
                        CompileTimeAssemblies = new List<LockFileItem> { Path.Combine("ref", "net451", "Net451LibPackage.dll") }
                    },
                    new LockFileTargetLibrary
                    {
                        Name = "AssemblyPlaceholderPackage",
                        Version = SemanticVersion.Parse("1.0.0")
                    }
                }
            };

            var metaPackageLibrary = new LockFilePackageLibrary
            {
                Name = "MetaPackage",
                Version = SemanticVersion.Parse("1.0.0")
            };

            var net451LibPackageLibrary = new LockFilePackageLibrary
            {
                Name = "Net451LibPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files = new List<string>
                {
                    Path.Combine("lib", "net451", "Net451LibPackage.dll"),
                    Path.Combine("lib", "net451", "Net451LibPackage.xml")
                }
            };

            var net451RefPackageLibrary = new LockFilePackageLibrary
            {
                Name = "Net451RefPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files = new List<string>
                {
                    Path.Combine("ref", "net451", "Net451LibPackage.dll")
                }
            };

            var assemblyPlaceholderPackageLibrary = new LockFilePackageLibrary
            {
                Name = "AssemblyPlaceholderPackage",
                Version = SemanticVersion.Parse("1.0.0"),
                Files = new List<string>
                {
                    Path.Combine("lib", "net45", "_._"),
                    Path.Combine("ref", "net45", "_._"),
                }
            };

            var lockFile = new LockFile()
            {
                Targets = new List<LockFileTarget> { net45Target, dnx451Target },
                PackageLibraries = new List<LockFilePackageLibrary>
                {
                    metaPackageLibrary,
                    net451LibPackageLibrary,
                    net451RefPackageLibrary,
                    assemblyPlaceholderPackageLibrary
                }
            };

            resolver.ApplyLockFile(lockFile);

            var libToLookup = new LibraryRange(packageName, frameworkReference: false);
            Assert.Equal(resolved, resolver.GetDescription(libToLookup, new FrameworkName(framework)).Resolved);
        }
예제 #5
0
 public NuGetDependencyResolver(PackageRepository repository)
 {
     _repository = repository;
 }
예제 #6
0
 public NuGetDependencyResolver(string packagesPath, IFrameworkReferenceResolver frameworkReferenceResolver)
 {
     _repository = new PackageRepository(packagesPath);
     _frameworkReferenceResolver = frameworkReferenceResolver;
     Dependencies = Enumerable.Empty<LibraryDescription>();
 }
예제 #7
0
        private void UpdateLockFile(PublishRoot root)
        {
            var lockFileFormat = new LockFileFormat();
            string lockFilePath;
            if (root.NoSource)
            {
                lockFilePath = Path.Combine(TargetPath, "root", LockFileFormat.LockFileName);
            }
            else
            {
                lockFilePath = Path.Combine(TargetPath, LockFileFormat.LockFileName);
            }

            LockFile lockFile;
            if (File.Exists(lockFilePath))
            {
                lockFile = lockFileFormat.Read(lockFilePath);
            }
            else
            {
                lockFile = new LockFile
                {
                    Islocked = false
                };

                var project = GetCurrentProject();

                // Restore dependency groups for future lockfile validation
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                    string.Empty,
                    project.Dependencies.Select(x => x.LibraryRange.ToString())));
                foreach (var frameworkInfo in project.GetTargetFrameworks())
                {
                    lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                        frameworkInfo.FrameworkName.ToString(),
                        frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString())));
                }
            }

            if (root.NoSource)
            {
                // The dependency group shared by all frameworks should only contain the main nupkg (i.e. entrypoint)
                lockFile.ProjectFileDependencyGroups.RemoveAll(g => string.IsNullOrEmpty(g.FrameworkName));
                lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup(
                    string.Empty,
                    new[] { _libraryDescription.LibraryRange.ToString() }));
            }

            var repository = new PackageRepository(root.TargetPackagesPath);
            var resolver = new DefaultPackagePathResolver(root.TargetPackagesPath);

            // For dependency projects that were published to nupkgs
            // Add them to lockfile to ensure the contents of lockfile are still valid
            using (var sha512 = SHA512.Create())
            {
                foreach (var publishProject in root.Projects.Where(p => p.IsPackage))
                {
                    var packageInfo = repository.FindPackagesById(publishProject.Name)
                        .SingleOrDefault();
                    if (packageInfo == null)
                    {
                        root.Reports.Information.WriteLine("Unable to locate published package {0} in {1}",
                            publishProject.Name.Yellow(),
                            repository.RepositoryRoot);
                        continue;
                    }

                    var package = packageInfo.Package;
                    var nupkgPath = resolver.GetPackageFilePath(package.Id, package.Version);

                    var project = publishProject.GetCurrentProject();
                    lockFile.Libraries.Add(LockFileUtils.CreateLockFileLibraryForProject(
                        project,
                        package,
                        sha512,
                        project.GetTargetFrameworks().Select(f => f.FrameworkName),
                        resolver));
                }
            }

            lockFileFormat.Write(lockFilePath, lockFile);
        }