private async Task <object> MigrateProjectToPackageRefAsync(string projectUniqueName)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var project = await _solutionManager.Value.GetVsProjectAdapterAsync(projectUniqueName);

            if (project == null)
            {
                throw new InvalidOperationException(string.Format(VsResources.Error_ProjectNotInCache, projectUniqueName));
            }

            var projectSafeName = project.CustomUniqueName;

            var nuGetProject = await _solutionManager.Value.GetNuGetProjectAsync(projectSafeName);

            // If the project already has PackageReference, do nothing.
            if (nuGetProject is LegacyPackageReferenceProject)
            {
                return(new VsProjectJsonToPackageReferenceMigrateResult(success: true, errorMessage: null));
            }

            try
            {
                await nuGetProject.SaveAsync(CancellationToken.None);

                var legacyPackageRefBasedProject = await _projectFactory.Value
                                                   .CreateNuGetProjectAsync <LegacyPackageReferenceProject>(
                    project, optionalContext : null);

                Assumes.Present(legacyPackageRefBasedProject);

                await ProjectJsonToPackageRefMigrator.MigrateAsync(
                    legacyPackageRefBasedProject as BuildIntegratedNuGetProject);

                var result = new VsProjectJsonToPackageReferenceMigrateResult(success: true, errorMessage: null);
                await nuGetProject.SaveAsync(CancellationToken.None);

                await _solutionManager.Value.UpgradeProjectToPackageReferenceAsync(nuGetProject);

                return(result);
            }
            catch (Exception ex)
            {
                // reload the project in memory from the file on disk, discarding any changes that might have
                // been made as a result of an incomplete migration.
                await ReloadProjectAsync(project);

                return(new VsProjectJsonToPackageReferenceMigrateResult(success: false, errorMessage: ex.Message));
            }
        }
Пример #2
0
        private async Task <object> MigrateProjectToPackageRefAsync(string projectUniqueName)
        {
            await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            var project = _solutionManager.Value.GetDTEProject(projectUniqueName);

            if (project == null)
            {
                throw new InvalidOperationException(string.Format(VsResources.Error_ProjectNotInCache, projectUniqueName));
            }

            var projectSafeName = await EnvDTEProjectInfoUtility.GetCustomUniqueNameAsync(project);

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

            // If the project already has PackageReference, do nothing.
            if (nuGetProject is LegacyCSProjPackageReferenceProject)
            {
                return(new VsProjectJsonToPackageReferenceMigrateResult(success: true, errorMessage: null));
            }

            try
            {
                _solutionManager.Value.SaveProject(nuGetProject);

                var legacyPackageRefBasedProject = new LegacyCSProjPackageReferenceProject(
                    new EnvDTEProjectAdapter(project),
                    VsHierarchyUtility.GetProjectId(project));

                await ProjectJsonToPackageRefMigrator.MigrateAsync(
                    legacyPackageRefBasedProject,
                    legacyPackageRefBasedProject.MSBuildProjectPath);

                var result = new VsProjectJsonToPackageReferenceMigrateResult(success: true, errorMessage: null);
                _solutionManager.Value.SaveProject(nuGetProject);
                await _solutionManager.Value.UpdateNuGetProjectToPackageRef(nuGetProject);

                return(result);
            }
            catch (Exception ex)
            {
                // reload the project in memory from the file on disk, discarding any changes that might have
                // been made as a result of an incomplete migration.
                ReloadProject(project);
                return(new VsProjectJsonToPackageReferenceMigrateResult(success: false, errorMessage: ex.Message));
            }
        }