Exemple #1
0
        private async Task <IVsPathContext> GetPathContextFromPackagesConfigAsync(
            NuGetProject nuGetProject, CancellationToken token)
        {
            var msbuildNuGetProject = nuGetProject as MSBuildNuGetProject;

            if (msbuildNuGetProject == null)
            {
                return(null);
            }

            var trie = new PathLookupTrie <string>();

            var packageReferences = await msbuildNuGetProject.GetInstalledPackagesAsync(token);

            foreach (var pr in packageReferences)
            {
                var packageInstallPath = msbuildNuGetProject.FolderNuGetProject.GetInstalledPath(
                    pr.PackageIdentity);
                if (packageInstallPath != null)
                {
                    trie[packageInstallPath] = packageInstallPath;
                }
            }

            var pathContext = GetSolutionPathContext();

            return(new VsIndexedPathContext(
                       pathContext.UserPackageFolder,
                       pathContext.FallbackPackageFolders.Cast <string>(),
                       trie));
        }
        private async Task <IVsPathContext> GetPathContextFromPackagesConfigAsync(
            NuGetProject nuGetProject, CancellationToken token)
        {
            var msbuildNuGetProject = nuGetProject as MSBuildNuGetProject;

            if (msbuildNuGetProject == null)
            {
                return(null);
            }

            var packageReferences = await msbuildNuGetProject.GetInstalledPackagesAsync(token);

            // switch to a background thread to process packages data
            await TaskScheduler.Default;

            var trie = new PathLookupTrie <string>();

            foreach (var pid in packageReferences.Select(pr => pr.PackageIdentity))
            {
                var packageInstallPath = msbuildNuGetProject.FolderNuGetProject.GetInstalledPath(pid);
                if (string.IsNullOrEmpty(packageInstallPath))
                {
                    throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
                }

                trie[packageInstallPath] = packageInstallPath;
            }

            var pathContext = GetSolutionPathContext();

            return(new VsIndexedPathContext(
                       pathContext.UserPackageFolder,
                       pathContext.FallbackPackageFolders.Cast <string>(),
                       trie));
        }
Exemple #3
0
        private async Task <IVsPathContext> GetPathContextFromAssetsFileAsync(
            NuGetProject nuGetProject, CancellationToken token)
        {
            // It's possible that this project isn't a build integrated NuGet project at all. That is, this project may
            // be a packages.config project.
            var buildIntegratedProject = nuGetProject as BuildIntegratedNuGetProject;

            if (buildIntegratedProject == null)
            {
                return(null);
            }

            // It's possible the lock file doesn't exist or it's an older format that doesn't have the package folders
            // property persisted.
            var lockFile = await _getLockFileOrNullAsync(buildIntegratedProject);

            if (lockFile == null ||
                lockFile.PackageFolders == null ||
                lockFile.PackageFolders.Count == 0)
            {
                return(null);
            }

            // The user packages folder is always the first package folder. Subsequent package folders are always
            // fallback package folders.
            var packageFolders = lockFile
                                 .PackageFolders
                                 .Select(lockFileItem => lockFileItem.Path)
                                 .ToList();

            var userPackageFolder      = packageFolders[0];
            var fallbackPackageFolders = packageFolders.Skip(1);

            if (lockFile.Libraries == null ||
                lockFile.Libraries.Count == 0)
            {
                return(new VsPathContext(userPackageFolder, fallbackPackageFolders));
            }

            var fppr = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders);

            var trie = new PathLookupTrie <string>();

            foreach (var pid in lockFile
                     .Libraries
                     .Where(l => l.Type == LibraryType.Package)
                     .Select(l => new PackageIdentity(l.Name, l.Version)))
            {
                var packageInstallPath = fppr.GetPackageDirectory(pid.Id, pid.Version);
                if (packageInstallPath != null)
                {
                    trie[packageInstallPath] = packageInstallPath;
                }
            }

            return(new VsIndexedPathContext(
                       userPackageFolder,
                       fallbackPackageFolders,
                       trie));
        }
        private async Task <IVsPathContext> GetPathContextFromAssetsFileAsync(
            NuGetProject nuGetProject, CancellationToken token)
        {
            // It's possible that this project isn't a build integrated NuGet project at all. That is, this project may
            // be a packages.config project.
            var buildIntegratedProject = nuGetProject as BuildIntegratedNuGetProject;

            if (buildIntegratedProject == null)
            {
                return(null);
            }

            var lockFile = await _getLockFileOrNullAsync(buildIntegratedProject);

            if ((lockFile?.PackageFolders?.Count ?? 0) == 0)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError));
            }

            // The user packages folder is always the first package folder. Subsequent package folders are always
            // fallback package folders.
            var packageFolders = lockFile
                                 .PackageFolders
                                 .Select(lockFileItem => lockFileItem.Path)
                                 .ToList();

            var userPackageFolder      = packageFolders[0];
            var fallbackPackageFolders = packageFolders.Skip(1);

            if (lockFile.Libraries == null ||
                lockFile.Libraries.Count == 0)
            {
                return(new VsPathContext(userPackageFolder, fallbackPackageFolders));
            }

            var fppr = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders);

            var trie = new PathLookupTrie <string>();

            foreach (var pid in lockFile
                     .Libraries
                     .Where(l => l.Type == LibraryType.Package)
                     .Select(l => new PackageIdentity(l.Name, l.Version)))
            {
                var packageInstallPath = fppr.GetPackageDirectory(pid.Id, pid.Version);
                if (string.IsNullOrEmpty(packageInstallPath))
                {
                    throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
                }

                trie[packageInstallPath] = packageInstallPath;
            }

            return(new VsIndexedPathContext(
                       userPackageFolder,
                       fallbackPackageFolders,
                       trie));
        }
Exemple #5
0
        private async Task <IVsPathContext> GetPathContextFromProjectLockFileAsync(
            string lockFilePath, CancellationToken token)
        {
            var lockFile = _getLockFileOrNull(lockFilePath);

            if ((lockFile?.PackageFolders?.Count ?? 0) == 0)
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_LockFileError));
            }

            // switch to a background thread to process packages data
            await TaskScheduler.Default;

            // The user packages folder is always the first package folder. Subsequent package folders are always
            // fallback package folders.
            var packageFolders = lockFile
                                 .PackageFolders
                                 .Select(lockFileItem => lockFileItem.Path)
                                 .ToList();

            var userPackageFolder      = packageFolders[0];
            var fallbackPackageFolders = packageFolders.Skip(1);

            if (lockFile.Libraries == null ||
                lockFile.Libraries.Count == 0)
            {
                return(new VsPathContext(userPackageFolder, fallbackPackageFolders));
            }

            var fppr = new FallbackPackagePathResolver(userPackageFolder, fallbackPackageFolders);

            var trie = new PathLookupTrie <string>();

            foreach (var pid in lockFile
                     .Libraries
                     .Where(l => l.Type == LibraryType.Package)
                     .Select(l => new PackageIdentity(l.Name, l.Version)))
            {
                var packageInstallPath = fppr.GetPackageDirectory(pid.Id, pid.Version);
                if (string.IsNullOrEmpty(packageInstallPath))
                {
                    throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
                }

                trie[packageInstallPath] = packageInstallPath;
            }

            return(new VsIndexedPathContext(
                       userPackageFolder,
                       fallbackPackageFolders,
                       trie));
        }
        public VsIndexedPathContext(
            string userPackageFolder,
            IEnumerable <string> fallbackPackageFolders,
            PathLookupTrie <string> index,
            INuGetTelemetryProvider telemetryProvider)
        {
            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            UserPackageFolder      = userPackageFolder ?? throw new ArgumentNullException(nameof(userPackageFolder));
            FallbackPackageFolders = fallbackPackageFolders?.ToList() ?? throw new ArgumentNullException(nameof(fallbackPackageFolders));
            _referenceLookupIndex  = index ?? throw new ArgumentNullException(nameof(index));
            _telemetryProvider     = telemetryProvider ?? throw new ArgumentNullException(nameof(telemetryProvider));
        }
Exemple #7
0
        private async Task <IVsPathContext> GetPathContextForPackagesConfigAsync(
            IVsProjectAdapter vsProjectAdapter, CancellationToken token)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var props = new Dictionary <string, object>();

            props.Add(NuGetProjectMetadataKeys.Name, Path.GetFileNameWithoutExtension(vsProjectAdapter.FullProjectPath));
            props.Add(NuGetProjectMetadataKeys.TargetFramework, await vsProjectAdapter.GetTargetFrameworkAsync());

            var packagesProject = new PackagesConfigNuGetProject(vsProjectAdapter.ProjectDirectory, props);

            var packagesFolderPath = PackagesFolderPathUtility.GetPackagesFolderPath(_solutionManager.Value, _settings.Value);
            var folderProject      = new FolderNuGetProject(packagesFolderPath);

            // switch to a background thread to process packages data
            await TaskScheduler.Default;

            var packageReferences = await packagesProject.GetInstalledPackagesAsync(token);

            var trie = new PathLookupTrie <string>();

            foreach (var pid in packageReferences.Select(pr => pr.PackageIdentity))
            {
                var packageInstallPath = folderProject.GetInstalledPath(pid);
                if (string.IsNullOrEmpty(packageInstallPath))
                {
                    throw new KeyNotFoundException(string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_PackageDirectoryNotFound, pid));
                }

                trie[packageInstallPath] = packageInstallPath;
            }

            var pathContext = GetSolutionPathContext();

            return(new VsIndexedPathContext(
                       pathContext.UserPackageFolder,
                       pathContext.FallbackPackageFolders.Cast <string>(),
                       trie));
        }
        public VsIndexedPathContext(
            string userPackageFolder,
            IEnumerable <string> fallbackPackageFolders,
            PathLookupTrie <string> index)
        {
            if (userPackageFolder == null)
            {
                throw new ArgumentNullException(nameof(userPackageFolder));
            }

            if (fallbackPackageFolders == null)
            {
                throw new ArgumentNullException(nameof(fallbackPackageFolders));
            }

            if (index == null)
            {
                throw new ArgumentNullException(nameof(index));
            }

            UserPackageFolder      = userPackageFolder;
            FallbackPackageFolders = fallbackPackageFolders.ToList();
            _referenceLookupIndex  = index;
        }