//writes new version of the package to packages.config file
        public void writeToConfig(string fileName, string packageId, SemanticVersion newversion)
        {
            var file = new PackageReferenceFile(fileName);

            file.DeleteEntry(packageId, null);
            file.AddEntry(packageId, newversion);
        }
        /// <summary>
        /// Executes the command.
        /// </summary>
        public override void ExecuteCommand()
        {
            string inputPackagesConfigPath = GetLatestPackagesConfigPath();
            string outputPackagesConfigPath = GetPackagesConfigPath();

            if (String.IsNullOrEmpty(inputPackagesConfigPath) || String.IsNullOrEmpty(outputPackagesConfigPath))
            {
                throw new CommandLineException();
            }

            if (!File.Exists(inputPackagesConfigPath))
            {
                throw new CommandLineException();
            }

            TryCreateAllDirectories(Path.GetDirectoryName(outputPackagesConfigPath));
            if(!File.Exists(outputPackagesConfigPath))
            {
                File.WriteAllText(outputPackagesConfigPath, @"<?xml version=""1.0"" encoding=""utf-8""?>
<packages>
</packages>");
            }

            PhysicalFileSystem outputFileSystem = new PhysicalFileSystem(Path.GetDirectoryName(outputPackagesConfigPath));
            PackageReferenceFile outputFile = new PackageReferenceFile(outputFileSystem, Path.GetFileName(outputPackagesConfigPath));

            // Remove all existing references from output file
            Dictionary<string, SemanticVersion> existingReferences = new Dictionary<string,SemanticVersion>();
            foreach (PackageReference packageReference in outputFile.GetPackageReferences())
            {
                existingReferences.Add(packageReference.Id, packageReference.Version);
            }
            foreach (KeyValuePair<string, SemanticVersion> pair in existingReferences)
            {
                outputFile.DeleteEntry(pair.Key, pair.Value);
            }

            PhysicalFileSystem inputFileSystem = new PhysicalFileSystem(Path.GetDirectoryName(inputPackagesConfigPath));
            PackageReferenceFile inputFile = new PackageReferenceFile(inputFileSystem, Path.GetFileName(inputPackagesConfigPath));
            foreach (PackageReference packageReference in inputFile.GetPackageReferences())
            {
                IPackage package = GetLatestPackage(packageReference.Id);
                outputFile.AddEntry(packageReference.Id, package.Version, false, packageReference.TargetFramework);
            }
        }
Exemple #3
0
        /// <summary>
        /// Installs the packages from config file.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="file">The file.</param>
        /// <param name="target"> </param>
        private IEnumerable <PackageReference> InstallPackagesFromConfigFile(string packagesDirectory, PackageReferenceFile file, string target)
        {
            var packageReferences = file.GetPackageReferences().ToList();
            var installedPackages = new List <PackageReference>();
            var allInstalled      = new List <PackageReference>();

            //We need to create a damn filesystem at the packages directory, so that the ROOT is correct.  Ahuh...
            var fileSystem = CreateFileSystem(packagesDirectory);

            if (!NoCache)
            {
                Console.WriteLine("Using cache....");
            }
            PackageManager packageManager = CreatePackageManager(fileSystem, useMachineCache: !NoCache);

            if (Clean)
            {
                packageManager.CleanPackageFolders();
            }

            bool installedAny = false;

            foreach (var packageReference in packageReferences)
            {
                if (String.IsNullOrEmpty(packageReference.Id))
                {
                    // GetPackageReferences returns all records without validating values. We'll throw if we encounter packages
                    // with malformed ids / Versions.
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, GetResources.GetCommandInvalidPackageReference, target));
                }
                else if (packageReference.Version == null)
                {
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, GetResources.GetCommandPackageReferenceInvalidVersion, packageReference.Id));
                }

                packageManager.PackageInstalled += (sender, e) =>
                {
                    var installedPackage = new PackageReference(e.Package.Id, e.Package.Version, null, _frameworkName, false);
                    if (!allInstalled.Contains(installedPackage))
                    {
                        allInstalled.Add(installedPackage);
                    }
                };
                IPackage package = _packageResolutionManager.ResolveLatestInstallablePackage(_repository, packageReference);
                if (package == null)
                {
                    SemanticVersion version = _packageResolutionManager.ResolveInstallableVersion(_repository, packageReference);
                    installedAny |= InstallPackage(packageManager, fileSystem, packageReference.Id, version ?? packageReference.Version);
                    installedPackages.Add(new PackageReference(packageReference.Id, version ?? packageReference.Version, null, _frameworkName, false));
                }
                else
                {
                    //We got it straight from the server, check whether we get a cache hit, else just install
                    var resolvedPackage = _packageResolutionManager.FindPackageInAllLocalSources(packageManager.LocalRepository, packageManager.SourceRepository, package);
                    packageManager.InstallPackage(resolvedPackage ?? package, !IncludeDependencies, false);
                    installedPackages.Add(new PackageReference(package.Id, resolvedPackage != null ? resolvedPackage.Version : package.Version, null, _frameworkName, false));
                }
                // Note that we ignore dependencies here because packages.config already contains the full closure
            }

            if (!installedAny && packageReferences.Any())
            {
                Console.WriteLine(GetResources.GetCommandNothingToInstall, Constants.PackageReferenceFile);
            }

            if (packageReferences != installedPackages)
            {
                foreach (var reference in file.GetPackageReferences())
                {
                    file.DeleteEntry(reference.Id, reference.Version);
                }
                foreach (var installedPackage in installedPackages)
                {
                    file.AddEntry(installedPackage.Id, installedPackage.Version);
                }
            }

            return(allInstalled);
        }
Exemple #4
0
        public static void ManagePackage(PackageReference packageReference, IEnumerable<Project> checkedProjects, IEnumerable<LibraryReference> libraries)
        {
            if (!IsSolutionOpen)
                return;

            foreach (var project in DTE.Solution.Projects.OfType<Project>().Where(n => n.IsSupported()))
            {
                var resultLibraries = Enumerable.Empty<LibraryReference>();

                var projectLibraries = libraries.Where(n => n.ProjectName == project.GetName());

                switch (packageReference.Type)
                {
                    case DeveloperLibraryType.VcInclude:
                        project.ManageIncludeDirectories(packageReference, checkedProjects);
                        break;
                    case DeveloperLibraryType.VcLibrary:
                        project.ManageLinkerDefinitions(packageReference, checkedProjects, projectLibraries);
                        resultLibraries = projectLibraries.Where(n => n.IsChecked);
                        break;
                    case DeveloperLibraryType.Net:
                        project.ManageReferences(packageReference, projectLibraries);
                        resultLibraries = projectLibraries.Where(n => n.IsChecked);
                        break;
                }

                var packageReferenceFile = new PackageReferenceFile(project.GetDirectory() + "/coapp.packages.config");

                if (checkedProjects.Any(n => n.FullName == project.FullName))
                {
                    packageReferenceFile.AddEntry(packageReference.CanonicalName, resultLibraries, packageReference.Type);
                }
                else
                {
                    packageReferenceFile.DeleteEntry(packageReference.CanonicalName);
                }

                project.Save(project.FullName);
            }
        }
        public override void ExecuteCommand()
        {
            if (string.IsNullOrEmpty(Directory))
            {
                Directory = Environment.CurrentDirectory;
            }

            if (_repository == null)
            {
                _repository = AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source);
            }

            var packageFiles = _fileSystem.GetFiles(Directory, "packages.config", true).ToList();
            Console.WriteLine("Processing : {0} files.",packageFiles.Count);
            Parallel.ForEach(packageFiles, packageFile =>
                {
                    Console.WriteLine("\t{0}", packageFile);
                    var newReferences = new List<PackageReference>();
                    var packageFileDetails = new PackageReferenceFile(_fileSystem, packageFile);
                    foreach (var packageReference in packageFileDetails.GetPackageReferences())
                    {
                        Console.WriteLine("\t\t{0}",packageReference.ToString());
                        var exists = _repository.FindPackage(packageReference.Id, packageReference.Version);
                        if (exists != null)
                        {
                            Console.WriteLine("\t\t\tExists, not changing.");
                            newReferences.Add(packageReference);
                        }
                        else
                        {
                            var package = _repository.FindPackagesById(packageReference.Id).FirstOrDefault();
                            if (package == null)
                            {
                                Console.WriteLine("\t\t\tNot found, no change.");
                                newReferences.Add(packageReference);
                            }
                            else
                            {
                                var newReference = new PackageReference(package.Id, package.Version, new VersionSpec(), new FrameworkName(".NET Framework, Version=4.0"), false);
                                Console.WriteLine("\t\t\tUpdated to {0}", newReference.ToString());
                                newReferences.Add(newReference);
                            }
                        }
                    }

                    //TODO Clear the file (must be an easier way).
                    Console.WriteLine("Clearing existing file...");
                    foreach (var packageReference in packageFileDetails.GetPackageReferences())
                    {
                        packageFileDetails.DeleteEntry(packageReference.Id, packageReference.Version);
                    }

                    //Add the new references.
                    Console.WriteLine("Adding new references...");
                    foreach (var packageReference in newReferences)
                    {
                        packageFileDetails.AddEntry(packageReference.Id, packageReference.Version);
                    }
                });
        }
Exemple #6
0
        public override void ExecuteCommand()
        {
            if (string.IsNullOrEmpty(Directory))
            {
                Directory = Environment.CurrentDirectory;
            }

            if (_repository == null)
            {
                _repository = AggregateRepositoryHelper.CreateAggregateRepositoryFromSources(RepositoryFactory, SourceProvider, Source);
            }

            var packageFiles = _fileSystem.GetFiles(Directory, "packages.config", true).ToList();

            Console.WriteLine("Processing : {0} files.", packageFiles.Count);
            Parallel.ForEach(packageFiles, packageFile =>
            {
                Console.WriteLine("\t{0}", packageFile);
                var newReferences      = new List <PackageReference>();
                var packageFileDetails = new PackageReferenceFile(_fileSystem, packageFile);
                foreach (var packageReference in packageFileDetails.GetPackageReferences())
                {
                    Console.WriteLine("\t\t{0}", packageReference.ToString());
                    var exists = _repository.FindPackage(packageReference.Id, packageReference.Version);
                    if (exists != null)
                    {
                        Console.WriteLine("\t\t\tExists, not changing.");
                        newReferences.Add(packageReference);
                    }
                    else
                    {
                        var package = _repository.FindPackagesById(packageReference.Id).FirstOrDefault();
                        if (package == null)
                        {
                            Console.WriteLine("\t\t\tNot found, no change.");
                            newReferences.Add(packageReference);
                        }
                        else
                        {
                            var newReference = new PackageReference(package.Id, package.Version, new VersionSpec(), new FrameworkName(".NET Framework, Version=4.0"), false);
                            Console.WriteLine("\t\t\tUpdated to {0}", newReference.ToString());
                            newReferences.Add(newReference);
                        }
                    }
                }

                //TODO Clear the file (must be an easier way).
                Console.WriteLine("Clearing existing file...");
                foreach (var packageReference in packageFileDetails.GetPackageReferences())
                {
                    packageFileDetails.DeleteEntry(packageReference.Id, packageReference.Version);
                }

                //Add the new references.
                Console.WriteLine("Adding new references...");
                foreach (var packageReference in newReferences)
                {
                    packageFileDetails.AddEntry(packageReference.Id, packageReference.Version);
                }
            });
        }
Exemple #7
0
        /// <summary>
        /// Installs the packages from config file.
        /// </summary>
        /// <param name="fileSystem">The file system.</param>
        /// <param name="file">The file.</param>
        /// <param name="target"> </param>
        private IEnumerable<PackageReference> InstallPackagesFromConfigFile(string packagesDirectory, PackageReferenceFile file, string target)
        {
            var packageReferences = file.GetPackageReferences().ToList();
            var installedPackages = new List<PackageReference>();
            var allInstalled = new List<PackageReference>();

            //We need to create a damn filesystem at the packages directory, so that the ROOT is correct.  Ahuh...
            var fileSystem = CreateFileSystem(packagesDirectory);

            if (!NoCache)
                Console.WriteLine("Using cache....");
            PackageManager packageManager = CreatePackageManager(fileSystem, useMachineCache: !NoCache);

            if (Clean)
                packageManager.CleanPackageFolders();

            bool installedAny = false;
            foreach (var packageReference in packageReferences)
            {
                if (String.IsNullOrEmpty(packageReference.Id))
                {
                    // GetPackageReferences returns all records without validating values. We'll throw if we encounter packages
                    // with malformed ids / Versions.
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, GetResources.GetCommandInvalidPackageReference, target));
                }
                else if (packageReference.Version == null)
                {
                    throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, GetResources.GetCommandPackageReferenceInvalidVersion, packageReference.Id));
                }

                packageManager.PackageInstalled += (sender, e) => 
                    { 
                        var installedPackage = new PackageReference(e.Package.Id, e.Package.Version, null, _frameworkName, false);
                        if (!allInstalled.Contains(installedPackage))
                            allInstalled.Add(installedPackage);
                    };
                IPackage package = _packageResolutionManager.ResolveLatestInstallablePackage(_repository, packageReference);
                if (package == null)
                {
                    SemanticVersion version = _packageResolutionManager.ResolveInstallableVersion(_repository, packageReference);
                    installedAny |= InstallPackage(packageManager, fileSystem, packageReference.Id, version ?? packageReference.Version);
                    installedPackages.Add(new PackageReference(packageReference.Id, version ?? packageReference.Version, null, _frameworkName, false));
                }
                else
                {
                    //We got it straight from the server, check whether we get a cache hit, else just install
                    var resolvedPackage = _packageResolutionManager.FindPackageInAllLocalSources(packageManager.LocalRepository, packageManager.SourceRepository, package);
                    packageManager.InstallPackage(resolvedPackage ?? package, !IncludeDependencies, false);
                    installedPackages.Add(new PackageReference(package.Id, resolvedPackage != null ? resolvedPackage.Version : package.Version, null, _frameworkName, false));
                }
                // Note that we ignore dependencies here because packages.config already contains the full closure
            }

            if (!installedAny && packageReferences.Any())
            {
                Console.WriteLine(GetResources.GetCommandNothingToInstall, Constants.PackageReferenceFile);
            }
            
            if (packageReferences != installedPackages)
            {
                foreach (var reference in file.GetPackageReferences())
                    file.DeleteEntry(reference.Id, reference.Version);
                foreach (var installedPackage in installedPackages)
                {
                    file.AddEntry(installedPackage.Id,installedPackage.Version);
                }
            }

            return allInstalled;
        }