Esempio n. 1
0
        private async Task EnsureNuGetAndVsProjectAdapterCacheAsync()
        {
            await _initLock.ExecuteNuGetOperationAsync(async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                if (!_cacheInitialized && IsSolutionOpen)
                {
                    try
                    {
                        var deferredProjects = GetDeferredProjects();

                        foreach (var project in deferredProjects)
                        {
                            try
                            {
                                var vsProjectAdapter = await _vsProjectAdapterProvider.CreateAdapterForDeferredProjectAsync(project);
                                await AddVsProjectAdapterToCacheAsync(vsProjectAdapter);
                            }
                            catch (Exception e)
                            {
                                // Ignore failed projects.
                                _logger.LogWarning($"The project {project} failed to initialize as a NuGet project.");
                                _logger.LogError(e.ToString());
                            }

                            // Consider that the cache is initialized only when there are any projects to add.
                            _cacheInitialized = true;
                        }

                        var dte = _serviceProvider.GetDTE();

                        var supportedProjects = EnvDTESolutionUtility
                                                .GetAllEnvDTEProjects(dte)
                                                .Where(EnvDTEProjectUtility.IsSupported);

                        foreach (var project in supportedProjects)
                        {
                            try
                            {
                                var vsProjectAdapter = await _vsProjectAdapterProvider.CreateAdapterForFullyLoadedProjectAsync(project);
                                await AddVsProjectAdapterToCacheAsync(vsProjectAdapter);
                            }
                            catch (Exception e)
                            {
                                // Ignore failed projects.
                                _logger.LogWarning($"The project {project.Name} failed to initialize as a NuGet project.");
                                _logger.LogError(e.ToString());
                            }

                            // Consider that the cache is initialized only when there are any projects to add.
                            _cacheInitialized = true;
                        }

                        SetDefaultProjectName();
                    }
                    catch
                    {
                        _projectSystemCache.Clear();
                        _cacheInitialized       = false;
                        DefaultNuGetProjectName = null;

                        throw;
                    }
                }
            }, CancellationToken.None);
        }
Esempio n. 2
0
        public async Task <KeyValuePair <bool, bool> > CheckSolutionUIPreConditionsAsync()
        {
            // Do NOT initialize VSSolutionManager through this API (by calling EnsureInitializeAsync)
            // This checks all the pre-conditions before showing NuGet manager UI at solution level
            // without initializing full VSSolutionManager, otherwise it will create hang issues.
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            bool?isSolutionAvailable         = null;
            var  DoesNuGetSupportsAnyProject = false;

            if (!IsSolutionOpen)
            {
                // Solution is not open. return immediately with false
                return(new KeyValuePair <bool, bool>(false, false));
            }

            if (!DoesSolutionRequireAnInitialSaveAs())
            {
                // Solution is open and 'Save As' is not required. So solution is available to operate but
                // we also need to check if solution has any supported NuGet project.
                isSolutionAvailable = true;
            }

            // read all deferred projects and check for packages.config based projects
            var deferredProjects = await GetDeferredProjectsAsync();

            foreach (var project in deferredProjects)
            {
                var vsProjectAdapter = await _vsProjectAdapterProvider.CreateAdapterForDeferredProjectAsync(project);

                // project is supported in NuGet, then set DoesNuGetSupportsAnyProject
                if (vsProjectAdapter.IsSupported)
                {
                    DoesNuGetSupportsAnyProject = true;

                    if (isSolutionAvailable == true)
                    {
                        // solution is available and also has NuGet supported project, so we don't need to go further.
                        break;
                    }

                    // construct packages.config file path and check if it exists
                    var packagesConfigFilePath      = Path.Combine(Path.GetDirectoryName(vsProjectAdapter.FullProjectPath), "packages.config");
                    var DoesPackagesConfigFileExist = await _vsProjectAdapterProvider.EntityExistsAsync(packagesConfigFilePath);

                    if (DoesPackagesConfigFileExist)
                    {
                        if (isSolutionAvailable != true)
                        {
                            // if there is any project with packages.config and solution is also required to be saved then solution is not available.
                            isSolutionAvailable = false;

                            // we got the final result for both the conditions so we can break now.
                            break;
                        }
                    }
                }
            }

            // Finally, solution is open and not saved. And, only contains project.json based projects.
            // Check if globalPackagesFolder is a full path. If so, solution is available.
            if (isSolutionAvailable == null)
            {
                var globalPackagesFolder = SettingsUtility.GetGlobalPackagesFolder(_settings.Value);
                isSolutionAvailable = Path.IsPathRooted(globalPackagesFolder);
            }

            return(new KeyValuePair <bool, bool>(isSolutionAvailable == true, DoesNuGetSupportsAnyProject));
        }