Exemple #1
0
        public async Task <NuGetProject> TryCreateNuGetProjectAsync(
            IVsProjectAdapter vsProject,
            ProjectProviderContext context,
            bool forceProjectType)
        {
            Assumes.Present(vsProject);
            Assumes.Present(context);

            ThreadHelper.ThrowIfNotOnUIThread();

            // The project must be an IVsHierarchy.
            var hierarchy = vsProject.VsHierarchy;

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

            // Check if the project is not CPS capable or if it is CPS capable that its opt'd in PackageReferences
            if (!VsHierarchyUtility.IsCPSCapabilityCompliant(hierarchy) ||
                !VsHierarchyUtility.IsProjectCapabilityCompliant(hierarchy))
            {
                return(null);
            }

            var buildProperties = vsProject.BuildProperties;

            // read MSBuild property RestoreProjectStyle
            var restoreProjectStyle = await buildProperties.GetPropertyValueAsync(ProjectBuildProperties.RestoreProjectStyle);

            // check for RestoreProjectStyle property is set and if not set to PackageReference then return false
            if (!(string.IsNullOrEmpty(restoreProjectStyle) ||
                  restoreProjectStyle.Equals(PackageReference, StringComparison.OrdinalIgnoreCase)))
            {
                return(null);
            }

            var fullProjectPath     = vsProject.FullProjectPath;
            var unconfiguredProject = GetUnconfiguredProject(vsProject.Project);

            var projectServices = new NetCoreProjectSystemServices(vsProject, await _componentModel.GetValueAsync());

            return(new CpsPackageReferenceProject(
                       vsProject.ProjectName,
                       vsProject.CustomUniqueName,
                       fullProjectPath,
                       _projectSystemCache,
                       unconfiguredProject,
                       projectServices,
                       vsProject.ProjectId));
        }
Exemple #2
0
        /// <summary>
        /// Tries to create an instance of <see cref="NuGetProject"/> by calling registered
        /// providers in predefined order.
        /// </summary>
        /// <param name="vsProjectAdapter">A project adapter.</param>
        /// <param name="context">Project context</param>
        /// <param name="result">New project instance when <code>true</code> is returned.
        /// Otherwise - <code>null</code>.</param>
        /// <returns><code>true</code> when new project instance has been successfully created.</returns>
        public async Task <NuGetProject> TryCreateNuGetProjectAsync(
            IVsProjectAdapter vsProjectAdapter,
            ProjectProviderContext context)
        {
            Assumes.Present(vsProjectAdapter);
            Assumes.Present(context);

            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            if (vsProjectAdapter.VsHierarchy != null &&
                VsHierarchyUtility.IsCPSCapabilityCompliant(vsProjectAdapter.VsHierarchy))
            {
                // Lazy load the CPS enabled JoinableTaskFactory for the UI.
                NuGetUIThreadHelper.SetJoinableTaskFactoryFromService(ProjectServiceAccessor.Value as IProjectServiceAccessor);
            }

            var exceptions = new List <Exception>();

            foreach (var provider in _providers)
            {
                try
                {
                    var nuGetProject = await provider.TryCreateNuGetProjectAsync(
                        vsProjectAdapter,
                        context,
                        forceProjectType : false);

                    if (nuGetProject != null)
                    {
                        return(nuGetProject);
                    }
                }
                catch (Exception e)
                {
                    // Ignore failures. If this method returns null, the problem falls
                    // into one of the other NuGet project types.
                    exceptions.Add(e);
                }
            }

            exceptions.ForEach(e => _logger.LogError(e.ToString()));

            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Creates an instance of <see cref="NuGetProject"/> of desired type.
        /// </summary>
        /// <typeparam name="TProject">Type of project to create.</typeparam>
        /// <param name="vsProjectAdapter">Project adapter</param>
        /// <param name="optionalContext">Optional context for project creation. Not all of providers require it.</param>
        /// <returns>Instance of <see cref="NuGetProject"/> or null if failed.</returns>
        /// <remarks>The factory will identify a provider corresponding to given project type and will attempt to force create the project of desired type.</remarks>
        public async Task <TProject> CreateNuGetProjectAsync <TProject>(
            IVsProjectAdapter vsProjectAdapter,
            ProjectProviderContext optionalContext = null)
            where TProject : NuGetProject
        {
            Assumes.Present(vsProjectAdapter);

            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var provider = _providers
                           .FirstOrDefault(p => typeof(TProject).TypeHandle.Equals(p.ProjectType));

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

            if (vsProjectAdapter.VsHierarchy != null &&
                VsHierarchyUtility.IsCPSCapabilityCompliant(vsProjectAdapter.VsHierarchy))
            {
                // Lazy load the CPS enabled JoinableTaskFactory for the UI.
                NuGetUIThreadHelper.SetJoinableTaskFactoryFromService(ProjectServiceAccessor.Value as IProjectServiceAccessor);
            }

            try
            {
                var nuGetProject = await provider.TryCreateNuGetProjectAsync(
                    vsProjectAdapter,
                    optionalContext,
                    forceProjectType : true);

                if (nuGetProject != null)
                {
                    return(nuGetProject as TProject);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e.ToString());
            }

            return(null);
        }