private static async TPL.Task BuildProjectsWithNoPackages(
     OperationContext context)
 {
     using (var progress = GeneralUtils.StartProgressProgress("Build", context.Projects.LoadedProjects.Length))
     {
         int i = 0;
         foreach (ProjectInfo project in context.Projects.LoadedProjects)
         {
             i++;
             progress.Report(i);
             if (!project.ProjectBuilt)
             {
                 // TODO: 2017-01 Bnaya, use UpdateNuGetDependencies instead of AddBuildEvents
                 //using (await UpdateNuGetDependencies(project, context, context.PackagesUpdatedSoFar))
                 using (AddBuildEvents(project, context, context.PackagesUpdatedSoFar))
                 {
                     if (await context.Projects.BuildProject(project))
                     {
                         project.ProjectBuilt = true;
                     }
                     else
                     {
                         GeneralUtils.ShowMessage($"Failed to build project {project.Name}", OLEMSGICON.OLEMSGICON_CRITICAL);
                     }
                 } // RemoveBuildEvents(project);
             }
         }
     }
 }
Esempio n. 2
0
        private static void SwitchToNuGetMode(OperationContext context)
        {
            if (String.IsNullOrEmpty(context.TfsServerUri))
            {
                GeneralUtils.ShowMessage("TFS server uri is missing in Tools->Options->Nuget Tool page", OLEMSGICON.OLEMSGICON_WARNING);
            }

            using (GeneralUtils.StartAnimation())
                using (var progress = GeneralUtils.StartProgressProgress("Back to NuGet",
                                                                         context.Projects.LoadedProjects.Length + 1))
                {
                    foreach (ProjectInfo project in context.Projects.LoadedProjects)
                    {
                        try
                        {
                            progress.Increment();
                            GeneralUtils.ReportStatus($"Switching [{project.Name}]");
                            SwitchProjectToNuGet(project, context);
                        }
                        catch (Exception ex)
                        {
                            GeneralUtils.ShowMessage("Failed to convert project: " + project.Name + ", Error message: " + ex.Message, OLEMSGICON.OLEMSGICON_CRITICAL);
                        }
                    }

                    progress.Increment();
                    context.Projects.ReOpenSolution();
                    GeneralUtils.ShowMessage("Conversion to NuGet mode finished successfully.");
                }
        }
Esempio n. 3
0
        private bool LoadProjects()
        {
            var solution = _serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution;

            if (solution == null)
            {
                GeneralUtils.ShowMessage("Failed to get Solution service", OLEMSGICON.OLEMSGICON_CRITICAL);
                return(false);
            }

            // Verify that the solution and all its projects are fully loaded
            var solution4 = _serviceProvider.GetService(typeof(SVsSolution)) as IVsSolution4;

            solution4.EnsureSolutionIsLoaded(0);

            IEnumHierarchies enumerator = null;
            Guid             guid       = Guid.Empty;

            solution.GetProjectEnum((uint)__VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION, ref guid, out enumerator);
            IVsHierarchy[] hierarchy = new IVsHierarchy[1] {
                null
            };
            uint fetched = 0;

            try
            {
                for (enumerator.Reset(); enumerator.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1; /*nothing*/)
                {
                    // Verify that this is a project node and not a folder
                    IVsProject project = (IVsProject)hierarchy[0];
                    string     path;
                    int        hr = project.GetMkDocument((uint)VSConstants.VSITEMID.Root, out path);
                    if (hr == VSConstants.S_OK)
                    {
                        AddNewProject(project);
                    }
                }
            }
            catch (Exception)
            {
                GeneralUtils.ShowMessage("Failed to load projects info", OLEMSGICON.OLEMSGICON_CRITICAL);
                return(false);
            }
            return(true);
        }
Esempio n. 4
0
        public string GetProjectOutputPath(string name)
        {
            DTE     dte     = (DTE)_serviceProvider.GetService(typeof(DTE));
            Project project = FindProjectByName(dte, name);

            if (project != null)
            {
                string fullPath           = project.Properties.Item("FullPath").Value.ToString();
                string relativeOutputPath = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString();
                string outputPath         = Path.Combine(fullPath, relativeOutputPath);
                return(outputPath);
            }
            else
            {
                GeneralUtils.ShowMessage("Project " + name + ": output path cannot be found", OLEMSGICON.OLEMSGICON_CRITICAL);
                return(null);
            }
        }
        public static OperationContext LoadNuGetPackages(bool preRelease)
        {
            DTE        dte   = (DTE)_serviceLocator.GetService(typeof(DTE));
            Properties props = dte.get_Properties("NuGet Tool", "General");

            Setting setting;

            try
            {
                setting = props.Item(nameof(Setting)).Value as Setting;
                if (setting == null)
                {
                    GeneralUtils.ShowMessage("Packages sources are not defined. Use Tools->Options->NuGet.Extension to define the sources", OLEMSGICON.OLEMSGICON_WARNING);
                    return(null);
                }
            }
            #region Exception Handling

            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine($"Setting not found {ex}");
                GeneralUtils.ShowMessage("Packages sources are not defined. Use Tools->Options->NuGet.Extension to define the sources", OLEMSGICON.OLEMSGICON_WARNING);
                return(null);
            }

            #endregion // Exception Handling

            try
            {
                var context = new OperationContext(_serviceLocator, preRelease, setting);
                if (!string.IsNullOrEmpty(context.Errors))
                {
                    GeneralUtils.ShowMessage(context.Errors, OLEMSGICON.OLEMSGICON_CRITICAL);
                    return(null);
                }
                return(context);
            }
            catch (Exception ex)
            {
                GeneralUtils.ShowMessage("Failed to Create context: " + ex.Message, OLEMSGICON.OLEMSGICON_CRITICAL);
                return(null);
            }
        }
        private static async void Rollback(
            OperationContext context)
        {
            // Recover the old packages
            for (int i = 0; i < context.PackagesUpdatedSoFar.Count; i++)
            {
                NuGetPackageInfo p = context.PackagesUpdatedSoFar[i];
                GeneralUtils.ReportStatus($"Rollback: {p.Name}");

                string errorMsg = await RollbackPackage(p, context);

                if (errorMsg != null)
                {
                    string       msg    = $@"Failed to recover package {p.Id}. Error: {errorMsg}.
{context.RecoveredPackages.Count} NuGet packages have been recovered so far.
Choose one of the following actions: 
    Abort:  to stop the process
            (you can repeat the process after fixing the problem).
    Ignore: to continue recovering the other packages
    Retry:  to try again (from current stage).";
                    DialogResult option = MessageBox.Show(msg, "Rollback",
                                                          MessageBoxButtons.AbortRetryIgnore,
                                                          MessageBoxIcon.Question,
                                                          MessageBoxDefaultButton.Button1);
                    switch (option)
                    {
                    case DialogResult.Abort:
                        return;

                    case DialogResult.Retry:
                        i--;
                        continue;

                    case DialogResult.Ignore:
                        continue;
                    }
                }
            }
            Directory.Delete(context.ArchiveSession);
            GeneralUtils.ShowMessage($"{context.RecoveredPackages.Count} NuGet packages have been restored");
        }
        /// <summary>
        /// Finds the path of the package .
        /// </summary>
        /// <param name="packageName">Name of the package.</param>
        /// <returns></returns>
        private string FindPackagePath(
            string packageName)
        {
            string packagePath = "";

            foreach (string packageSource in PackagesSources)
            {
                string path = Path.Combine(packageSource, packageName);
                if (File.Exists(path))
                {
                    if (packagePath == "")
                    {
                        packagePath = packageSource;
                    }
                    else
                    {
                        GeneralUtils.ShowMessage($"Package {packageName} exists in more than one repository. The tool will update only the package in {packagePath}");
                    }
                }
            }
            return(packagePath);
        }
Esempio n. 8
0
 private static void SwitchToDebugMode(OperationContext context)
 {
     using (GeneralUtils.StartAnimation())
         using (var progress = GeneralUtils.StartProgressProgress("To Project Reference",
                                                                  context.Projects.LoadedProjects.Length))
         {
             foreach (ProjectInfo project in context.Projects.LoadedProjects)
             {
                 try
                 {
                     progress.Increment();
                     GeneralUtils.ReportStatus($"Switching [{project.Name}]");
                     SwitchProjectToDebug(project, context);
                 }
                 catch (Exception ex)
                 {
                     GeneralUtils.ShowMessage("Failed to convert project: " + project.Name + ", Error message: " + ex.Message, OLEMSGICON.OLEMSGICON_CRITICAL);
                 }
             }
         }
     context.Projects.ReOpenSolution();
     GeneralUtils.ShowMessage("Conversion to debug mode finished successfully.");
 }
        /// <summary>
        /// Handle the process of Updating the NuGet packages.
        /// </summary>
        /// <param name="preRelease">if set to <c>true</c> [pre release].</param>
        public static async void UpdateNuGetPackages(bool preRelease)
        {
            using (GeneralUtils.StartAnimation())
                using (var progress = GeneralUtils.StartProgressProgress("NuGet Upgrade", UPDATE_NUGET_COUNT))
                {
                    try
                    {
                        GeneralUtils.ReportStatus($"Start NuGet Upgrade: pre-release = {preRelease}");
                        OperationContext context = LoadNuGetPackages(preRelease);

                        #region Validation

                        if (context == null)
                        {
                            return;
                        }

                        if (context.Projects.LoadedProjects.Length == 0)
                        {
                            return;
                        }

                        if (context.Projects.IsSolutionInDebugMode())
                        {
                            GeneralUtils.ShowMessage("Solution is in debug mode. Switch back to NuGet mode before updating the packages.", OLEMSGICON.OLEMSGICON_WARNING);
                            return;
                        }

                        if (!await context.Projects.BuildSolution())
                        {
                            if (MessageBox.Show(@"Make sure that the solution is build 
with no errors, before NuGet upgrade!
Do you want to continue", "Pre build solution",
                                                MessageBoxButtons.YesNo,
                                                MessageBoxIcon.Question) == DialogResult.No)
                            {
                                return;
                            }
                        }

                        #endregion // Validation

                        // Build only packages with corresponding projects
                        // in the current solution
                        var packagesToBuild = (from p in context.PackagesInfo
                                               where p.ProjectInfo != null
                                               select p).ToList();

                        // Add NuGet packages that have no corrseponding projects in current solution,
                        // but depend on other NuGet packages that need to upgrade
                        var dependentPackagesWithoutProjects = GetDependentPackagesWithoutProjects(context);
                        packagesToBuild.AddRange(dependentPackagesWithoutProjects);

                        #region Validation

                        if (packagesToBuild.Count == 0)
                        {
                            GeneralUtils.ShowMessage(@"No NuGet package needs to be updated.
Goto: Tools -> Options -> NuGet Tool
and add path for local NuGet repositories.");
                            return;
                        }

                        #endregion // Validation

                        //RemoveCachedNuGetFiles();
                        context.Projects.CleanSolution();
                        progress.Report(1);

                        using (var currentProgress = GeneralUtils.StartProgressProgress("NuGet: Current", packagesToBuild.Count))
                        {
                            DialogResult result =
                                await BuildAndUpdate(context, packagesToBuild, currentProgress);

                            switch (result)
                            {
                            case DialogResult.Cancel:
                                GeneralUtils.ReportStatus("Operation Canceled");
                                return;

                            case DialogResult.Abort:
                                GeneralUtils.ReportStatus("Operation aborted");
                                return;
                            }
                            currentProgress.Report(packagesToBuild.Count);
                        }

                        progress.Report(5);
                        if (context.ShouldArchiveOldNuGet)
                        {
                            GeneralUtils.ReportStatus($"NuGet: Archive");
                            await CreateArchiveZipFile(context.ArchiveSession);
                        }

                        GeneralUtils.ReportStatus($"NuGet: Build");
                        progress.Report(6);
                        // Build all the other projects that don't have corresponding NuGet packages
                        await BuildProjectsWithNoPackages(context);

                        progress.Report(7);

                        GeneralUtils.ReportStatus($"NuGet: Re-open projects");
                        await TPL.Task.Factory.StartNew(() =>
                                                        context.Projects.ReOpenSolution(),
                                                        TPL.TaskCreationOptions.LongRunning);

                        progress.Report(8);
                        #region Clipboard.SetText(context.ArchiveFolder)

                        try
                        {
                            Clipboard.SetText(context.ArchiveFolder);
                        }
                        catch { }

                        #endregion // Clipboard.SetText(context.ArchiveFolder)

                        GeneralUtils.ShowMessage($@"{context.PackagesUpdatedSoFar.Count} NuGet packages have been updated
Old packages moved to [{context.ArchiveFolder}] + copied to the clipboard");
                    }
                    catch (FileNotFoundException ex)
                    {
                        MessageBox.Show($@"Make sure to build the solution before running the tool

{ex}", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
        }