public ProjectJsonBuildIntegratedProjectSystem(
            string jsonConfigPath,
            string msbuildProjectFilePath,
            EnvDTEProject envDTEProject,
            string uniqueName)
            : base(jsonConfigPath, msbuildProjectFilePath)
        {
            _envDTEProject = envDTEProject;

            // set project id
            var projectId = VsHierarchyUtility.GetProjectId(envDTEProject);

            InternalMetadata.Add(NuGetProjectMetadataKeys.ProjectId, projectId);

            // Override the JSON TFM value from the csproj for UAP framework
            if (InternalMetadata.TryGetValue(NuGetProjectMetadataKeys.TargetFramework, out object targetFramework))
            {
                var jsonTargetFramework = targetFramework as NuGetFramework;
                if (IsUAPFramework(jsonTargetFramework))
                {
                    var platfromMinVersion = VsHierarchyUtility.GetMSBuildProperty(VsHierarchyUtility.ToVsHierarchy(envDTEProject), EnvDTEProjectInfoUtility.TargetPlatformMinVersion);

                    if (!string.IsNullOrEmpty(platfromMinVersion))
                    {
                        // Found the TPMinV in csproj, store this as a new target framework to be replaced in project.json
                        var newTargetFramework = new NuGetFramework(jsonTargetFramework.Framework, new Version(platfromMinVersion));
                        InternalMetadata[NuGetProjectMetadataKeys.TargetFramework] = newTargetFramework;
                    }
                }
            }

            InternalMetadata.Add(NuGetProjectMetadataKeys.UniqueName, uniqueName);
        }
        public bool TryCreateNuGetProject(EnvDTE.Project dteProject, ProjectSystemProviderContext context, out NuGetProject result)
        {
            if (dteProject == null)
            {
                throw new ArgumentNullException(nameof(dteProject));
            }

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

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

            var projectK = GetProjectKProject(dteProject);

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

            result = new ProjectKNuGetProject(
                projectK,
                dteProject.Name,
                EnvDTEProjectInfoUtility.GetCustomUniqueName(dteProject),
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }
        public bool TryCreateNuGetProject(EnvDTE.Project dteProject, ProjectSystemProviderContext context, out NuGetProject result)
        {
            if (dteProject == null)
            {
                throw new ArgumentNullException(nameof(dteProject));
            }

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

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

            var project = new EnvDTEProjectAdapter(dteProject);

            if (!project.IsLegacyCSProjPackageReferenceProject)
            {
                return(false);
            }

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

            result = new LegacyCSProjPackageReferenceProject(
                project,
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }
示例#4
0
        public async Task <NuGetProject> UpdateNuGetProjectToPackageRef(NuGetProject oldProject)
        {
#if VS14
            // do nothing for VS 2015 and simply return the existing NuGetProject
            if (NuGetProjectUpdated != null)
            {
                NuGetProjectUpdated(this, new NuGetProjectEventArgs(oldProject));
            }

            return(await Task.FromResult(oldProject));
#else
            if (oldProject == null)
            {
                throw new ArgumentException(
                          Strings.Argument_Cannot_Be_Null_Or_Empty,
                          nameof(oldProject));
            }

            var projectName = GetNuGetProjectSafeName(oldProject);
            var dteProject  = GetDTEProject(projectName);

            ProjectNames oldEnvDTEProjectName;
            _projectSystemCache.TryGetProjectNames(projectName, out oldEnvDTEProjectName);

            RemoveEnvDTEProjectFromCache(projectName);

            var nuGetProject = await NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var settings = ServiceLocator.GetInstance <ISettings>();

                var context = new ProjectSystemProviderContext(
                    EmptyNuGetProjectContext,
                    () => PackagesFolderPathUtility.GetPackagesFolderPath(this, settings));

                return(new LegacyCSProjPackageReferenceProject(
                           new EnvDTEProjectAdapter(dteProject),
                           VsHierarchyUtility.GetProjectId(dteProject)));
            });

            var added = _projectSystemCache.AddProject(oldEnvDTEProjectName, dteProject, nuGetProject);

            if (DefaultNuGetProjectName == null)
            {
                DefaultNuGetProjectName = projectName;
            }

            if (NuGetProjectUpdated != null)
            {
                NuGetProjectUpdated(this, new NuGetProjectEventArgs(nuGetProject));
            }

            return(nuGetProject);
#endif
        }
        public ProjectJsonBuildIntegratedProjectSystem(
            string jsonConfigPath,
            string msbuildProjectFilePath,
            EnvDTEProject envDTEProject,
            string uniqueName)
            : base(jsonConfigPath, msbuildProjectFilePath)
        {
            _envDTEProject = envDTEProject;

            // set project id
            var projectId = VsHierarchyUtility.GetProjectId(envDTEProject);

            InternalMetadata.Add(NuGetProjectMetadataKeys.ProjectId, projectId);

            InternalMetadata.Add(NuGetProjectMetadataKeys.UniqueName, uniqueName);
        }
        public VSMSBuildNuGetProject(
            EnvDTEProject project,
            IMSBuildNuGetProjectSystem msbuildNuGetProjectSystem,
            string folderNuGetProjectPath,
            string packagesConfigFolderPath) : base(
                msbuildNuGetProjectSystem,
                folderNuGetProjectPath,
                packagesConfigFolderPath)
        {
            _project = project;

            // set project id
            var projectId = VsHierarchyUtility.GetProjectId(project);

            InternalMetadata.Add(NuGetProjectMetadataKeys.ProjectId, projectId);
        }
        public bool TryCreateNuGetProject(EnvDTE.Project dteProject, ProjectSystemProviderContext context, out NuGetProject result)
        {
            if (dteProject == null)
            {
                throw new ArgumentNullException(nameof(dteProject));
            }

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

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

            // The project must be an IVsHierarchy.
            var hierarchy = VsHierarchyUtility.ToVsHierarchy(dteProject);

            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 buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;

            // read MSBuild property RestoreProjectStyle, TargetFramework, and TargetFrameworks
            var restoreProjectStyle = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, RestoreProjectStyle);

            var targetFramework = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, TargetFramework);

            var targetFrameworks = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, TargetFrameworks);

            // check for RestoreProjectStyle property is set and if not set to PackageReference then return false
            if (!(string.IsNullOrEmpty(restoreProjectStyle) ||
                  restoreProjectStyle.Equals(ProjectStyle.PackageReference.ToString(), 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        = ProjectNames.FromDTEProject(dteProject);
            var fullProjectPath     = EnvDTEProjectInfoUtility.GetFullProjectPath(dteProject);
            var unconfiguredProject = GetUnconfiguredProject(dteProject);

            result = new CpsPackageReferenceProject(
                dteProject.Name,
                EnvDTEProjectInfoUtility.GetCustomUniqueName(dteProject),
                fullProjectPath,
                _projectSystemCache,
                dteProject,
                unconfiguredProject,
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }
        public NuGetProject CreateNuGetProject(EnvDTEProject envDTEProject, INuGetProjectContext nuGetProjectContext)
        {
            if (envDTEProject == null)
            {
                throw new ArgumentNullException(nameof(envDTEProject));
            }

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

            ThreadHelper.ThrowIfNotOnUIThread();

            NuGetProject result = null;

            var projectK = GetProjectKProject(envDTEProject);

            if (projectK != null)
            {
                result = new ProjectKNuGetProject(
                    projectK,
                    envDTEProject.Name,
                    EnvDTEProjectUtility.GetCustomUniqueName(envDTEProject),
                    VsHierarchyUtility.GetProjectId(envDTEProject));
            }
            else if ((result = GetMSBuildShellOutNuGetProject(envDTEProject)) != null)
            {
                // Use the NuGetProject result initialized in the condition.
            }
            else
            {
                var msBuildNuGetProjectSystem = MSBuildNuGetProjectSystemFactory.CreateMSBuildNuGetProjectSystem(
                    envDTEProject,
                    nuGetProjectContext);

                var isWebSite = msBuildNuGetProjectSystem is WebSiteProjectSystem;

                // Web sites cannot have project.json
                if (!isWebSite)
                {
                    // Find the project file path
                    var projectFilePath = EnvDTEProjectUtility.GetFullProjectPath(envDTEProject);

                    if (!string.IsNullOrEmpty(projectFilePath))
                    {
                        var msbuildProjectFile         = new FileInfo(projectFilePath);
                        var projectNameFromMSBuildPath = Path.GetFileNameWithoutExtension(msbuildProjectFile.Name);

                        // Treat projects with project.json as build integrated projects
                        // Search for projectName.project.json first, then project.json
                        // If the name cannot be determined, search only for project.json
                        string projectJsonPath = null;
                        if (string.IsNullOrEmpty(projectNameFromMSBuildPath))
                        {
                            projectJsonPath = Path.Combine(msbuildProjectFile.DirectoryName,
                                                           ProjectJsonPathUtilities.ProjectConfigFileName);
                        }
                        else
                        {
                            projectJsonPath = ProjectJsonPathUtilities.GetProjectConfigPath(msbuildProjectFile.DirectoryName,
                                                                                            projectNameFromMSBuildPath);
                        }

                        if (File.Exists(projectJsonPath))
                        {
                            result = new BuildIntegratedProjectSystem(
                                projectJsonPath,
                                msbuildProjectFile.FullName,
                                envDTEProject,
                                msBuildNuGetProjectSystem,
                                EnvDTEProjectUtility.GetCustomUniqueName(envDTEProject));
                        }
                    }
                }

                // Create a normal MSBuild project if no project.json was found
                if (result == null)
                {
                    var folderNuGetProjectFullPath = _packagesPath();

                    // Project folder path is the packages config folder path
                    var packagesConfigFolderPath = EnvDTEProjectUtility.GetFullPath(envDTEProject);

                    result = new VSMSBuildNuGetProject(
                        envDTEProject,
                        msBuildNuGetProjectSystem,
                        folderNuGetProjectFullPath,
                        packagesConfigFolderPath);
                }
            }

            return(result);
        }