Example #1
0
        /// <summary>
        /// Updates the package configuration.
        /// Comment the NuGet which target project within the solution.
        /// </summary>
        /// <param name="project">The project.</param>
        /// <param name="context">The context.</param>
        private static void UpdatePackageConfig(
            ProjectInfo project,
            OperationContext context)
        {
            // Comment out all NuGet packages in packages.config
            string packagesConfigFile = Path.Combine(project.Directory, "packages.config");

            if (File.Exists(packagesConfigFile))
            {
                bool hasFilePendingChanges = TFSUtilities.HasFilePendingChanges(packagesConfigFile, context.TfsServerUri);

                //string origText = File.ReadAllText(packagesConfigFile);
                StringBuilder newText = new StringBuilder();

                foreach (var line in File.ReadLines(packagesConfigFile))
                {
                    if (line.IndexOf("<package ") == -1)
                    {
                        newText.AppendLine(line);
                        continue;
                    }

                    // Extract the package id
                    int    idPos             = line.IndexOf("id");
                    int    packageIdStartPos = line.IndexOf("\"", idPos);
                    int    packageIdEndPos   = line.IndexOf("\"", packageIdStartPos + 1);
                    string packageName       = line.Substring(packageIdStartPos + 1, packageIdEndPos - packageIdStartPos - 1).Trim();

                    if (context.Projects.LoadedProjects.Any(
                            p => (p.Name == packageName)))
                    {
                        newText.AppendLine($"<!--NuGetTool{line}-->");
                    }
                    else
                    {
                        newText.AppendLine(line);
                    }
                }

                File.WriteAllText(packagesConfigFile, newText.ToString());

                // Compute hash for the file (so we can check if it has changed when switching back to NuGet)
                string hash64 = null;
                if (!hasFilePendingChanges)
                {
                    hash64 = GeneralUtils.ComputeHash(packagesConfigFile);
                }
                newText.AppendLine($"<!--DebugMode {hash64}-->");
                File.WriteAllText(packagesConfigFile, newText.ToString());
            }
            else
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debugger.Break();
                }
                System.Diagnostics.Trace.Write("No packages.config found in " + project.Directory);
            }
        }
Example #2
0
        private void RevertBackProjectFileToNuGet(ProjectInfo project, OperationContext context)
        {
            string newText            = "";
            string textWithoutHashKey = "";
            string origHash           = null;

            // Read the hash code
            using (StreamReader reader = new StreamReader(project.ProjectFile))
            {
                // Remove all the project references and add the original NuGet references
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    if (line.IndexOf("<!--DebugMode") != -1)
                    {
                        // read the hash code
                        int hashStartPos = line.IndexOf(" ") + 1;
                        int hashEndPos   = line.IndexOf("-->");
                        origHash = line.Substring(hashStartPos, hashEndPos - hashStartPos);
                    }
                    else
                    {
                        textWithoutHashKey += line + Environment.NewLine;
                    }
                }
            }

            using (StreamReader reader = new StreamReader(project.ProjectFile))
            {
                // Remove all the project references and add the original NuGet references
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    // Skip the debug mode comment line
                    if (line.IndexOf("<!--DebugMode") != -1)
                    {
                        continue;
                    }

                    if (line.IndexOf("<!--NuGetTool") != -1)
                    {
                        int    idx             = line.IndexOf("<!--NuGetTool");
                        string uncommentedLine = line.Substring(idx + 13);
                        newText += uncommentedLine + Environment.NewLine;

                        // Read until the end of the comment
                        line = reader.ReadLine();
                        while (line.IndexOf("</Reference>") == -1)
                        {
                            newText += line + Environment.NewLine;
                            line     = reader.ReadLine();
                        }

                        // Remove the end of the comment
                        int endOfCommentPos = line.IndexOf("-->");
                        uncommentedLine = line.Substring(0, endOfCommentPos);
                        newText        += uncommentedLine + Environment.NewLine;

                        // Remove the project reference
                        line = reader.ReadLine();

                        while (line.IndexOf("</ProjectReference>") == -1)
                        {
                            line = reader.ReadLine();
                        }
                    }
                    else
                    {
                        newText += line + Environment.NewLine;
                    }
                }
            }

            // If the file was checked out first time by the tool, and has not changed
            // outside the tool, then undo the check out when switching back to NuGet mode
            if (!String.IsNullOrEmpty(origHash))
            {
                File.WriteAllText(project.ProjectFile, textWithoutHashKey);
                string newHash = GeneralUtils.ComputeHash(project.ProjectFile);
                File.WriteAllText(project.ProjectFile, newText);
                if (origHash == newHash)
                {
                    TFSUtilities.UndoCheckOut(project.ProjectFile, context.TfsServerUri);
                }
            }
            else
            {
                File.WriteAllText(project.ProjectFile, newText);
            }
        }
Example #3
0
        private static void UpdateProjectFile(
            ProjectInfo project,
            OperationContext context)
        {
            string newText = "";
            bool   hasFilePendingChanges = TFSUtilities.HasFilePendingChanges(project.ProjectFile, context.TfsServerUri);

            using (StreamReader reader = new StreamReader(project.ProjectFile))
            {
                // Comment out all NuGet references to projects in the solution
                // and add project references instead of them
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();

                    if (line.IndexOf("<Reference ") != -1)
                    {
                        // Extract the package od
                        int packageIdStartPos = line.IndexOf("\"");
                        int packageIdEndPos   = line.IndexOf(",", packageIdStartPos + 1);

                        // Some of the packages are without version number (typically system packages)
                        if (packageIdEndPos == -1)
                        {
                            packageIdEndPos = line.IndexOf("\"", packageIdStartPos + 1);
                        }

                        string packageName = line.Substring(packageIdStartPos + 1, packageIdEndPos - packageIdStartPos - 1).Trim();

                        ProjectInfo dependencyProject = context.Projects.LoadedProjects.FirstOrDefault(p => p.AssemblyName == packageName);
                        if (dependencyProject != null)
                        {
                            newText += "<!--NuGetTool" + line + Environment.NewLine;
                            line     = reader.ReadLine();

                            // Read until the end of the reference to close the comment
                            while (line.IndexOf("</Reference>") == -1)
                            {
                                newText += line + Environment.NewLine;
                                line     = reader.ReadLine();
                            }
                            newText += line + "-->" + Environment.NewLine;

                            // Add project reference instead of the Nuget reference
                            string projectRef = BuildProjectReference(dependencyProject);
                            newText += projectRef + Environment.NewLine;
                        }
                        else
                        {
                            newText += line + Environment.NewLine;
                        }
                    }
                    else
                    {
                        newText += line + Environment.NewLine;
                    }
                }
            }
            File.WriteAllText(project.ProjectFile, newText);

            // Compute hash for the file (so we can check if it has changed when switching back to NuGet)
            string hash64 = null;

            if (!hasFilePendingChanges)
            {
                hash64 = GeneralUtils.ComputeHash(project.ProjectFile);
            }
            newText += $"<!--DebugMode {hash64}-->";
            File.WriteAllText(project.ProjectFile, newText.ToString());
        }
Example #4
0
        private static void RevertBackPackageConfigToNuGet(ProjectInfo project, OperationContext context)
        {
            string packagesConfigFile = Path.Combine(project.Directory, "packages.config");

            if (File.Exists(packagesConfigFile))
            {
                string textWithoutHashKey = "";
                string newText            = "";
                string origHash           = null;

                using (StreamReader reader = new StreamReader(packagesConfigFile))
                {
                    while (!reader.EndOfStream)
                    {
                        string line = reader.ReadLine();

                        if (line.IndexOf("<!--DebugMode") != -1)
                        {
                            // read the hash code
                            int hashStartPos = line.IndexOf(" ") + 1;
                            int hashEndPos   = line.IndexOf("-->");
                            origHash = line.Substring(hashStartPos, hashEndPos - hashStartPos);
                            continue;
                        }
                        else
                        {
                            textWithoutHashKey += line + Environment.NewLine;
                        }

                        if (line.IndexOf("<!--NuGetTool") != -1)
                        {
                            int packageStartPos = line.IndexOf("<package");
                            if (packageStartPos != -1)
                            {
                                int    packageEndPos   = line.IndexOf("/>", packageStartPos + 1);
                                string uncommentedLine = line.Substring(packageStartPos, packageEndPos - packageStartPos + 2).Trim();
                                newText += "  " + uncommentedLine + Environment.NewLine;
                            }
                        }
                        else
                        {
                            newText += line + Environment.NewLine;
                        }
                    }
                }

                // If the file was checked out first time by the tool, and has not changed
                // outside the tool, then undo the check out when switching back to NuGet mode
                if (!String.IsNullOrEmpty(origHash))
                {
                    File.WriteAllText(packagesConfigFile, textWithoutHashKey);
                    string newHash = GeneralUtils.ComputeHash(packagesConfigFile);
                    File.WriteAllText(packagesConfigFile, newText);

                    // If the files are identical, then undo the checkout
                    if (origHash == newHash)
                    {
                        TFSUtilities.UndoCheckOut(packagesConfigFile, context.TfsServerUri);
                    }
                }
                else
                {
                    File.WriteAllText(packagesConfigFile, newText);
                }
            }
            else
            {
                System.Diagnostics.Debug.Write("No packages.config found in " + project.Directory);
            }
        }