예제 #1
0
        /// <summary>
        /// This returns all the installed package files (does not include satellite files)
        /// </summary>
        /// <param name="packageIdentity"></param>
        /// <param name="packagePathResolver"></param>
        /// <param name="packageDirectory"></param>
        /// <param name="packageSaveMode"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task <IEnumerable <ZipFilePair> > GetInstalledPackageFiles(Stream packageStream,
                                                                                       PackageIdentity packageIdentity,
                                                                                       PackagePathResolver packagePathResolver,
                                                                                       PackageSaveModes packageSaveMode,
                                                                                       CancellationToken token)
        {
            List <ZipFilePair> installedPackageFiles = new List <ZipFilePair>();
            string             packageDirectory      = packagePathResolver.GetInstalledPath(packageIdentity);

            if (!String.IsNullOrEmpty(packageDirectory))
            {
                var zipArchive   = new ZipArchive(packageStream);
                var packageFiles = await GetPackageFiles(zipArchive.Entries, packageDirectory, packageSaveMode, token);

                installedPackageFiles.AddRange(GetInstalledPackageFiles(packageFiles));
            }

            return(installedPackageFiles);
        }
        /// <summary>
        /// This returns all the installed package files (does not include satellite files)
        /// </summary>
        public static IEnumerable <ZipFilePair> GetInstalledPackageFiles(
            PackageArchiveReader packageReader,
            PackageIdentity packageIdentity,
            PackagePathResolver packagePathResolver,
            PackageSaveMode packageSaveMode)
        {
            var installedPackageFiles = Enumerable.Empty <ZipFilePair>();

            var packageDirectory = packagePathResolver.GetInstalledPath(packageIdentity);

            if (!string.IsNullOrEmpty(packageDirectory))
            {
                var packageFiles = packageReader.GetPackageFiles(packageSaveMode);
                var entries      = packageReader.EnumeratePackageEntries(packageFiles, packageDirectory);
                installedPackageFiles = entries.Where(e => e.IsInstalled());
            }

            return(installedPackageFiles.ToList());
        }
        /// <summary>
        /// This returns all the installed package files (does not include satellite files)
        /// </summary>
        public static async Task <IEnumerable <ZipFilePair> > GetInstalledPackageFilesAsync(
            PackageArchiveReader packageReader,
            PackageIdentity packageIdentity,
            PackagePathResolver packagePathResolver,
            PackageSaveMode packageSaveMode,
            CancellationToken cancellationToken)
        {
            var installedPackageFiles = Enumerable.Empty <ZipFilePair>();

            var packageDirectory = packagePathResolver.GetInstalledPath(packageIdentity);

            if (!string.IsNullOrEmpty(packageDirectory))
            {
                var packageFiles = await packageReader.GetPackageFilesAsync(packageSaveMode, cancellationToken);

                var entries = packageReader.EnumeratePackageEntries(packageFiles, packageDirectory);
                installedPackageFiles = entries.Where(e => e.IsInstalled());
            }

            return(installedPackageFiles.ToList());
        }
        private void ExecuteInitScripts()
        {
            // Fix for Bug 1426 Disallow ExecuteInitScripts from being executed concurrently by multiple threads.
            lock (_initScriptsLock)
            {
                if (!_solutionManager.IsSolutionOpen)
                {
                    return;
                }

                Debug.Assert(_settings != null);
                if (_settings == null)
                {
                    return;
                }

                try
                {
                    // invoke init.ps1 files in the order of package dependency.
                    // if A -> B, we invoke B's init.ps1 before A's.
                    IEnumerable<NuGetProject> projects = _solutionManager.GetNuGetProjects();
                    NuGetPackageManager packageManager = new NuGetPackageManager(_sourceRepositoryProvider, _settings, _solutionManager);
                    List<PackageIdentity> sortedPackages = new List<PackageIdentity>();
                    foreach (NuGetProject project in projects)
                    {
                        // Skip project K projects.
                        if (project is NuGet.ProjectManagement.Projects.ProjectKNuGetProjectBase)
                        {
                            continue;
                        }

                        IEnumerable<PackageReference> installedRefs = project.GetInstalledPackagesAsync(CancellationToken.None).Result;
                        if (installedRefs != null && installedRefs.Any())
                        {
                            IEnumerable<PackageIdentity> installedPackages = packageManager.GetInstalledPackagesInDependencyOrder(project, new EmptyNuGetProjectContext(), CancellationToken.None).Result;
                            sortedPackages.AddRange(installedPackages);
                        }
                    }

                    // Get the path to the Packages folder.
                    string packagesFolderPath = packageManager.PackagesFolderSourceRepository.PackageSource.Source;
                    foreach (var package in sortedPackages)
                    {
                        PackagePathResolver packagePathResolver = new PackagePathResolver(packagesFolderPath);
                        string pathToPackage = packagePathResolver.GetInstalledPath(package);
                        string toolsPath = Path.Combine(pathToPackage, "tools");
                        AddPathToEnvironment(toolsPath);
                        Runspace.ExecuteScript(toolsPath, PowerShellScripts.Init, package);
                    }
                }
                catch (Exception ex)
                {
                    // When Packages folder is not present, NuGetResolverInputException will be thrown
                    // as resolving DependencyInfo requires the presence of Packages folder.
                    if (ex.InnerException is NuGetResolverInputException)
                    {
                        // Silently fail.
                    }
                    else
                    {
                        // if execution of Init scripts fails, do not let it crash our console
                        ReportError(ex);
                    }

                    ExceptionHelper.WriteToActivityLog(ex);
                }
            }
        }
예제 #5
0
        /// <summary>
        /// This returns all the installed package files (does not include satellite files)
        /// </summary>
        /// <param name="packageIdentity"></param>
        /// <param name="packagePathResolver"></param>
        /// <param name="packageDirectory"></param>
        /// <param name="packageSaveMode"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task<IEnumerable<ZipFilePair>> GetInstalledPackageFiles(Stream packageStream,
            PackageIdentity packageIdentity,
            PackagePathResolver packagePathResolver,
            PackageSaveModes packageSaveMode,
            CancellationToken token)
        {
            var installedPackageFiles = new List<ZipFilePair>();
            var packageDirectory = packagePathResolver.GetInstalledPath(packageIdentity);
            if (!String.IsNullOrEmpty(packageDirectory))
            {
                var zipArchive = new ZipArchive(packageStream);
                var packageFiles = await GetPackageFiles(zipArchive.Entries, packageDirectory, packageSaveMode, token);
                installedPackageFiles.AddRange(GetInstalledPackageFiles(packageFiles));
            }

            return installedPackageFiles;
        }
        public async Task UninstallPackage(PackageIdentity identity)
        {
            var packagePathResolver = new NuGet.Packaging.PackagePathResolver(Packagesfolder);
            var installedPath       = packagePathResolver.GetInstalledPath(identity);
            PackageReaderBase packageReader;

            packageReader = new PackageFolderReader(installedPath);
            var  clientPolicyContext      = NuGet.Packaging.Signing.ClientPolicyContext.GetClientPolicy(Settings, Logger);
            var  packageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3, XmlDocFileSaveMode.None, clientPolicyContext, Logger);
            bool failed = true;

            try
            {
                await PackageExtractor.ExtractPackageAsync(installedPath,
                                                           packageReader,
                                                           packagePathResolver,
                                                           packageExtractionContext,
                                                           CancellationToken.None);

                failed = false;
            }
            catch (Exception)
            {
            }
            if (failed)
            {
                try
                {
                    await Download(identity);

                    installedPath = packagePathResolver.GetInstalledPath(identity);
                    packageReader = new PackageFolderReader(installedPath);
                    failed        = false;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            var libItems         = packageReader.GetLibItems();
            var frameworkReducer = new FrameworkReducer();
            var nearest          = frameworkReducer.GetNearest(NuGetFramework, libItems.Select(x => x.TargetFramework));
            var files            = libItems
                                   .Where(x => x.TargetFramework.Equals(nearest))
                                   .SelectMany(x => x.Items).ToList();

            foreach (var f in files)
            {
                RemoveFile(installedPath, f);
            }

            var cont = packageReader.GetContentItems();

            nearest = frameworkReducer.GetNearest(NuGetFramework, cont.Select(x => x.TargetFramework));
            files   = cont
                      .Where(x => x.TargetFramework.Equals(nearest))
                      .SelectMany(x => x.Items).ToList();
            foreach (var f in files)
            {
                RemoveFile(installedPath, f);
            }

            if (System.IO.Directory.Exists(installedPath + @"\build"))
            {
                if (System.IO.Directory.Exists(installedPath + @"\build\x64"))
                {
                    foreach (var f in System.IO.Directory.GetFiles(installedPath + @"\build\x64"))
                    {
                        var filename = System.IO.Path.GetFileName(f);
                        var target   = System.IO.Path.Combine(Destinationfolder, filename);
                        if (System.IO.File.Exists(target))
                        {
                            try
                            {
                                System.IO.File.Delete(target);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
        }
예제 #7
0
        public async Task <bool> IsPackageInstalled(LocalPackageInfo package)
        {
            try
            {
                await Download(package.Identity);
            }
            catch (Exception ex)
            {
                OpenRPAPackageManagerLogger.Instance.LogError(ex.ToString());
            }
            try
            {
                var packagePathResolver = new NuGet.Packaging.PackagePathResolver(Packagesfolder);
                var installedPath       = packagePathResolver.GetInstalledPath(package.Identity);

                PackageReaderBase packageReader;
                packageReader = new PackageFolderReader(installedPath);
                var libItems = packageReader.GetLibItems();
                if (libItems.Count() == 0)
                {
                    Console.WriteLine("Booom!");
                }
                var frameworkReducer = new FrameworkReducer();
                var nearest          = frameworkReducer.GetNearest(NuGetFramework, libItems.Select(x => x.TargetFramework));
                var files            = libItems
                                       .Where(x => x.TargetFramework.Equals(nearest))
                                       .SelectMany(x => x.Items).ToList();


                foreach (var f in files)
                {
                    string source   = "";
                    string f2       = "";
                    string filename = "";
                    string dir      = "";
                    string target   = "";
                    try
                    {
                        source   = System.IO.Path.Combine(installedPath, f);
                        f2       = f.Substring(f.IndexOf("/", 4) + 1);
                        filename = System.IO.Path.GetFileName(f2);
                        dir      = System.IO.Path.GetDirectoryName(f2);
                        target   = System.IO.Path.Combine(Destinationfolder, dir, filename);
                        if (!System.IO.Directory.Exists(System.IO.Path.Combine(Destinationfolder, dir)))
                        {
                            System.IO.Directory.CreateDirectory(System.IO.Path.Combine(Destinationfolder, dir));
                        }
                        if (!System.IO.File.Exists(source))
                        {
                            return(false);
                        }
                        if (!System.IO.File.Exists(target))
                        {
                            return(false);
                        }
                        var infoOld = new System.IO.FileInfo(source);
                        var infoNew = new System.IO.FileInfo(target);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                return(false);

                OpenRPAPackageManagerLogger.Instance.LogError(ex.ToString());
            }
        }
예제 #8
0
        public async Task Download(PackageIdentity identity)
        {
            var packagePathResolver = new NuGet.Packaging.PackagePathResolver(Packagesfolder);
            var installedPath       = packagePathResolver.GetInstalledPath(identity);

            if (identity.HasVersion && !string.IsNullOrEmpty(installedPath))
            {
                var idstring = identity.Id + "." + identity.Version;
                if (installedPath.Contains(idstring))
                {
                    return;
                }
            }
            var result = new List <IPackageSearchMetadata>();

            using (var cacheContext = new SourceCacheContext())
            {
                var repositories      = SourceRepositoryProvider.GetRepositories();
                var availablePackages = new HashSet <SourcePackageDependencyInfo>(PackageIdentityComparer.Default);

                await GetPackageWithoutDependencies(identity, cacheContext, availablePackages);

                var resolverContext = new PackageResolverContext(
                    DependencyBehavior.Lowest,
                    new[] { identity.Id },
                    Enumerable.Empty <string>(),
                    Enumerable.Empty <NuGet.Packaging.PackageReference>(),
                    Enumerable.Empty <PackageIdentity>(),
                    availablePackages,
                    SourceRepositoryProvider.GetRepositories().Select(s => s.PackageSource),
                    Logger);

                var packageToInstall = availablePackages.Where(p => p.Id == identity.Id).FirstOrDefault();
                if (packageToInstall == null)
                {
                    throw new Exception("Failed finding package " + identitystring(identity));
                }

                // var packagePathResolver = new NuGet.Packaging.PackagePathResolver(Packagesfolder);
                var clientPolicyContext      = NuGet.Packaging.Signing.ClientPolicyContext.GetClientPolicy(Settings, Logger);
                var packageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3, XmlDocFileSaveMode.None, clientPolicyContext, Logger);
                var frameworkReducer         = new FrameworkReducer();

                // PackageReaderBase packageReader;
                installedPath = packagePathResolver.GetInstalledPath(packageToInstall);
                //if (installedPath == null)
                //{
                var downloadResource = await packageToInstall.Source.GetResourceAsync <DownloadResource>(CancellationToken.None);

                var downloadResult = await downloadResource.GetDownloadResourceResultAsync(
                    packageToInstall,
                    new PackageDownloadContext(cacheContext),
                    NuGet.Configuration.SettingsUtility.GetGlobalPackagesFolder(Settings),
                    Logger, CancellationToken.None);

                await PackageExtractor.ExtractPackageAsync(
                    downloadResult.PackageSource,
                    downloadResult.PackageStream,
                    packagePathResolver,
                    packageExtractionContext,
                    CancellationToken.None);

                // packageReader = downloadResult.PackageReader;
                // }
                //else
                //{
                //    packageReader = new PackageFolderReader(installedPath);
                //}
            }
            return;
        }
        public bool InstallPackage(PackageIdentity identity)
        {
            bool ret = true;

            var packagePathResolver = new NuGet.Packaging.PackagePathResolver(PackagesInstallFolder);
            var installedPath       = packagePathResolver.GetInstalledPath(identity);

            PackageReaderBase packageReader;

            packageReader = new PackageFolderReader(installedPath);
            var libItems         = packageReader.GetLibItems();
            var frameworkReducer = new FrameworkReducer();
            var nearest          = frameworkReducer.GetNearest(NuGetFramework, libItems.Select(x => x.TargetFramework));
            var files            = libItems
                                   .Where(x => x.TargetFramework.Equals(nearest))
                                   .SelectMany(x => x.Items).ToList();

            foreach (var f in files)
            {
                InstallFile(installedPath, f);
            }

            var cont = packageReader.GetContentItems();

            nearest = frameworkReducer.GetNearest(NuGetFramework, cont.Select(x => x.TargetFramework));
            files   = cont
                      .Where(x => x.TargetFramework.Equals(nearest))
                      .SelectMany(x => x.Items).ToList();
            foreach (var f in files)
            {
                InstallFile(installedPath, f);
            }

            try
            {
                var dependencies = packageReader.GetPackageDependencies();
                nearest = frameworkReducer.GetNearest(NuGetFramework, dependencies.Select(x => x.TargetFramework));
                foreach (var dep in dependencies.Where(x => x.TargetFramework.Equals(nearest)))
                {
                    foreach (var p in dep.Packages)
                    {
                        var local = getLocal(p.Id);
                        InstallPackage(local.Identity);
                    }
                }
            }
            catch (Exception ex)
            {
                ret = false;
                SharedObject.Instance.Output(SharedObject.enOutputType.Error, "安装nupkg包出错", ex);
            }

            if (System.IO.Directory.Exists(installedPath + @"\build"))
            {
                if (System.IO.Directory.Exists(installedPath + @"\build\x64"))
                {
                    foreach (var f in System.IO.Directory.GetFiles(installedPath + @"\build\x64"))
                    {
                        var filename = System.IO.Path.GetFileName(f);
                        var target   = System.IO.Path.Combine(TargetFolder, filename);
                        CopyIfNewer(f, target);
                    }
                }
            }

            return(ret);
        }
예제 #10
0
        public async Task <List <IPackageSearchMetadata> > DownloadAndInstall(PackageIdentity identity)
        {
            var result = new List <IPackageSearchMetadata>();

            using (var cacheContext = new SourceCacheContext())
            {
                var repositories      = SourceRepositoryProvider.GetRepositories();
                var availablePackages = new HashSet <SourcePackageDependencyInfo>(PackageIdentityComparer.Default);
                await GetPackageDependencies(identity, cacheContext, availablePackages);

                var resolverContext = new PackageResolverContext(
                    DependencyBehavior.Lowest,
                    new[] { identity.Id },
                    Enumerable.Empty <string>(),
                    Enumerable.Empty <NuGet.Packaging.PackageReference>(),
                    Enumerable.Empty <PackageIdentity>(),
                    availablePackages,
                    SourceRepositoryProvider.GetRepositories().Select(s => s.PackageSource),
                    NullLogger.Instance);

                var resolver = new PackageResolver();
                // resolverContext.IncludeUnlisted = true;
                var packagesToInstall = resolver.Resolve(resolverContext, CancellationToken.None)
                                        .Select(p => availablePackages.Single(x => PackageIdentityComparer.Default.Equals(x, p)));
                var packagePathResolver      = new NuGet.Packaging.PackagePathResolver(Packagesfolder);
                var clientPolicyContext      = NuGet.Packaging.Signing.ClientPolicyContext.GetClientPolicy(Settings, OpenRPAPackageManagerLogger.Instance);
                var packageExtractionContext = new PackageExtractionContext(PackageSaveMode.Defaultv3, XmlDocFileSaveMode.None, clientPolicyContext, OpenRPAPackageManagerLogger.Instance);
                var frameworkReducer         = new FrameworkReducer();

                foreach (var packageToInstall in packagesToInstall)
                {
                    // PackageReaderBase packageReader;
                    var installedPath = packagePathResolver.GetInstalledPath(packageToInstall);
                    if (installedPath == null)
                    {
                        var downloadResource = await packageToInstall.Source.GetResourceAsync <DownloadResource>(CancellationToken.None);

                        var downloadResult = await downloadResource.GetDownloadResourceResultAsync(
                            packageToInstall,
                            new PackageDownloadContext(cacheContext),
                            NuGet.Configuration.SettingsUtility.GetGlobalPackagesFolder(Settings),
                            NullLogger.Instance, CancellationToken.None);

                        await PackageExtractor.ExtractPackageAsync(
                            downloadResult.PackageSource,
                            downloadResult.PackageStream,
                            packagePathResolver,
                            packageExtractionContext,
                            CancellationToken.None);

                        // packageReader = downloadResult.PackageReader;
                    }
                    //else
                    //{
                    //    packageReader = new PackageFolderReader(installedPath);
                    //}

                    InstallPackage(packageToInstall);
                }
            }
            return(result);
        }