Exemplo n.º 1
0
        public async Task <NuGetProject> TryCreateNuGetProjectAsync(
            IVsProjectAdapter vsProjectAdapter,
            ProjectProviderContext _,
            bool forceProjectType)
        {
            Assumes.Present(vsProjectAdapter);

            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var projectServices = await TryCreateProjectServicesAsync(
                vsProjectAdapter,
                forceCreate : forceProjectType);

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

            return(new LegacyPackageReferenceProject(
                       vsProjectAdapter,
                       vsProjectAdapter.ProjectId,
                       projectServices,
                       _threadingService));
        }
Exemplo n.º 2
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);
            }

            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);
        }
        public bool TryCreateNuGetProject(
            IVsProjectAdapter vsProject,
            ProjectProviderContext context,
            bool forceProjectType,
            out NuGetProject result)
        {
            Assumes.Present(vsProject);
            Assumes.Present(context);

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

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

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

            // Check if the project is not CPS capable or if it is CPS capable then it does not have TargetFramework(s), if so then return false
            if (!hierarchy.IsCapabilityMatch("CPS"))
            {
                return(false);
            }

            var buildProperties = vsProject.BuildProperties;

            // read MSBuild property RestoreProjectStyle, TargetFramework, and TargetFrameworks
            var restoreProjectStyle = buildProperties.GetPropertyValue(ProjectBuildProperties.RestoreProjectStyle);
            var targetFramework     = buildProperties.GetPropertyValue(ProjectBuildProperties.TargetFramework);
            var targetFrameworks    = buildProperties.GetPropertyValue(ProjectBuildProperties.TargetFrameworks);

            // 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(false);
            }
            // check whether TargetFramework or TargetFrameworks property is set, else return false
            else if (string.IsNullOrEmpty(targetFramework) && string.IsNullOrEmpty(targetFrameworks))
            {
                return(false);
            }

            // Lazy load the CPS enabled JoinableTaskFactory for the UI.
            NuGetUIThreadHelper.SetJoinableTaskFactoryFromService(ProjectServiceAccessor.Value as IProjectServiceAccessor);

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

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

            result = new NetCorePackageReferenceProject(
                vsProject.ProjectName,
                vsProject.CustomUniqueName,
                fullProjectPath,
                _projectSystemCache,
                vsProject,
                unconfiguredProject,
                projectServices,
                vsProject.ProjectId);

            return(true);
        }