public async Task <IVsPathContext> CreatePathContextAsync(
            EnvDTE.Project dteProject,
            string projectUniqueName,
            CancellationToken token)
        {
            IVsPathContext context = null;

            try
            {
                // get the VSProjectAdapter instance which will be used to retrieve MSBuild properties
                var projectAdapter = await _vsProjectAdapterProvider.Value.CreateAdapterForFullyLoadedProjectAsync(dteProject);

                // First check for project.assets.json file and generate VsPathContext from there.
                context = await GetPathContextForPackageRefAsync(projectAdapter, CancellationToken.None);

                // if no project.assets.json file, then check for project.lock.json file.
                context = context ?? await GetPathContextForProjectJsonAsync(projectAdapter, CancellationToken.None);

                // if no project.lock.json file, then look for packages.config file.
                context = context ?? await GetPathContextForPackagesConfigAsync(projectAdapter, CancellationToken.None);

                // Fallback to reading the path context from the solution's settings. Note that project level settings in
                // VS are not currently supported.
                context = context ?? GetSolutionPathContext();
            }
            catch (Exception e) when(e is KeyNotFoundException || e is InvalidOperationException)
            {
                var errorMessage = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_CreateContextError, projectUniqueName, e.Message);

                _logger.Value.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage, e);
            }

            return(context);
        }
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                async() =>
            {
                var dte    = await _dte.GetValueAsync();
                var lookup = await GetPathToDTEProjectLookupAsync(dte);

                if (!lookup.TryGetValue(projectUniqueName, out var dteProject))
                {
                    return(null);
                }

                var nuGetProject = await _solutionManager.Value.GetOrCreateProjectAsync(dteProject, _projectContext.Value);

                // It's possible the project isn't a NuGet-compatible project at all.
                if (nuGetProject == null)
                {
                    return(null);
                }

                return(await CreatePathContextAsync(nuGetProject, CancellationToken.None));
            });

            return(outputPathContext != null);
        }
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            try
            {
                // invoke async operation from within synchronous method
                outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                    async() =>
                {
                    var nuGetProject = await CreateNuGetProjectAsync(projectUniqueName);

                    // It's possible the project isn't a NuGet-compatible project at all.
                    if (nuGetProject == null)
                    {
                        return(null);
                    }

                    return(await CreatePathContextAsync(nuGetProject, CancellationToken.None));
                });

                return(outputPathContext != null);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
                throw;
            }
        }
示例#4
0
        public bool TryCreateNoSolutionContext(out IVsPathContext vsPathContext)
        {
            // invoke async operation from within synchronous method
            vsPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(() => TryCreateUserWideContextAsync());

            return(vsPathContext != null);
        }
        public bool TryCreateNoSolutionContext(out IVsPathContext vsPathContext)
        {
            try
            {
                // invoke async operation from within synchronous method
                vsPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(() => TryCreateUserWideContextAsync());

                return(vsPathContext != null);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
                throw;
            }
        }
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var dte = await _dte.GetValueAsync();

                EnvDTE.Project dteProject = null;
                var lookup = await EnvDTESolutionUtility.GetPathToDTEProjectLookupAsync(dte);

                if (lookup.ContainsKey(projectUniqueName))
                {
                    dteProject = lookup[projectUniqueName];
                }

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

                var nuGetProject = await _solutionManager.Value.GetOrCreateProjectAsync(dteProject, _projectContext.Value);

                // It's possible the project isn't a NuGet-compatible project at all.
                if (nuGetProject == null)
                {
                    return(null);
                }

                await TaskScheduler.Default;

                return(await CreatePathContextAsync(nuGetProject, CancellationToken.None));
            });

            return(outputPathContext != null);
        }
示例#7
0
        public bool TryCreateNoSolutionContext(out IVsPathContext vsPathContext)
        {
            const string eventName = nameof(IVsPathContextProvider2) + "." + nameof(TryCreateNoSolutionContext);

            using var _ = NuGetETW.ExtensibilityEventSource.StartStopEvent(eventName);

            try
            {
                // invoke async operation from within synchronous method
                vsPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(() => TryCreateUserWideContextAsync());

                return(vsPathContext != null);
            }
            catch (Exception exception)
            {
                _telemetryProvider.PostFault(exception, typeof(VsPathContextProvider).FullName);
                throw;
            }
        }
示例#8
0
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                async() =>
            {
                // result.item1 is IVsProjectAdapter instance
                // result.item2 is ProjectAssetsFile path if exists
                var result = await CreateProjectAdapterAsync(projectUniqueName);

                return(result == null ? null
                        : await CreatePathContextAsync(result.Item1, result.Item2, projectUniqueName, CancellationToken.None));
            });

            return(outputPathContext != null);
        }
示例#9
0
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            var nuGetProject = _solutionManager.Value.GetNuGetProject(projectUniqueName);

            // It's possible the project isn't a NuGet-compatible project at all.
            if (nuGetProject == null)
            {
                outputPathContext = null;
                return(false);
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                () => CreatePathContextAsync(nuGetProject));

            return(outputPathContext != null);
        }
示例#10
0
        public bool TryCreateContext(string projectUniqueName, out IVsPathContext outputPathContext)
        {
            if (projectUniqueName == null)
            {
                throw new ArgumentNullException(nameof(projectUniqueName));
            }

            // invoke async operation from within synchronous method
            outputPathContext = NuGetUIThreadHelper.JoinableTaskFactory.Run(
                async() =>
            {
                var dte    = await _dte.GetValueAsync();
                var lookup = await GetPathToDTEProjectLookupAsync(dte);

                if (!lookup.TryGetValue(projectUniqueName, out var dteProject))
                {
                    return(null);
                }

                return(await CreatePathContextAsync(dteProject, projectUniqueName, CancellationToken.None));
            });

            return(outputPathContext != null);
        }
示例#11
0
        public async Task <IVsPathContext> CreatePathContextAsync(
            IVsProjectAdapter vsProjectAdapter,
            string projectAssetsFile,
            string projectUniqueName,
            CancellationToken token)
        {
            IVsPathContext context = null;

            try
            {
                // First check for project.assets.json file and generate VsPathContext from there.
                if (!string.IsNullOrEmpty(projectAssetsFile))
                {
                    context = GetPathContextFromProjectLockFile(projectAssetsFile);
                }

                // if no project.assets.json file, then check for project.lock.json file.
                context = context ?? GetPathContextForProjectJson(vsProjectAdapter);

                // if no project.lock.json file, then look for packages.config file.
                context = context ?? await GetPathContextForPackagesConfigAsync(vsProjectAdapter, token);

                // Fallback to reading the path context from the solution's settings. Note that project level settings in
                // VS are not currently supported.
                context = context ?? GetSolutionPathContext();
            }
            catch (Exception e) when(e is KeyNotFoundException || e is InvalidOperationException)
            {
                var errorMessage = string.Format(CultureInfo.CurrentCulture, VsResources.PathContext_CreateContextError, projectUniqueName, e.Message);

                _logger.Value.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage, e);
            }

            return(context);
        }
        public bool TryCreateSolutionContext(out IVsPathContext outputPathContext)
        {
            outputPathContext = GetSolutionPathContext();

            return(outputPathContext != null);
        }