Example #1
0
        public override bool Execute()
        {
            if (ProjectNames == null)
            {
                return(true);
            }
            try
            {
                var builder = new StringBuilder();
                foreach (var value in ProjectNames)
                {
                    builder.Append(value);
                    builder.Append('.');
                }

                LogFormat("Message", "----------------------------------");
                LogFormat("Message", "GenerateSolutionFiles Logging Info");
                LogFormat("Message", "----------------------------------");
                LogFormat("Message", CreatedFile);
                LogFormat("Message", builder.ToString());

                var content = "";
                using (var tr = new StreamReader(OriginalFile, true))
                {
                    content = tr.ReadToEnd();
                }

                var globalIndex     = content.IndexOf("Global");
                var globalSection   = content.Substring(globalIndex, content.Length - globalIndex);
                var globalSections  = globalSection.Split(new string[] { "GlobalSection(" }, StringSplitOptions.RemoveEmptyEntries);
                var projectsSection = content.Substring(0, globalIndex);
                projectsSection = projectsSection.Remove(0, projectsSection.IndexOf("Project("));
                var projects = projectsSection.Split(new string[] { "Project(" }, StringSplitOptions.RemoveEmptyEntries);
                var nestedProjectsSection = string.Empty;
                if (RemoveTfsBindings)
                {
                    //Remove the TFS section.
                    content = globalSections.Where(glb => glb.Contains("TeamFoundationVersionControl")).Aggregate(content, (current, glb) => current.Replace(string.Concat("GlobalSection(", glb), string.Empty));
                    content = content.Replace("SAK", string.Empty);
                }
                foreach (var glb in globalSections.Where(glb => glb.Contains("NestedProjects")))
                {
                    nestedProjectsSection = string.Concat("GlobalSection(", glb);
                }
                var nestedProjects = nestedProjectsSection.Split(new char[] { '\r', '\n', '\t', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                foreach (var projectName in ProjectNames)
                {
                    ProcessProject(projectName, ref projects, ref content, ref nestedProjects);
                }
                var filteredNestedProjects = string.Concat(string.Join("\r\n\t\t", nestedProjects.ToArray()), "\r\n\t").Replace("\tEndGlobalSection", "EndGlobalSection");
                content = content.Replace(nestedProjectsSection, filteredNestedProjects);

                if (Replacements != null)
                {
                    for (var index = 0; index <= Replacements.Length - 1; index = index + 2)
                    {
                        content = content.Replace(Replacements[index], Replacements[index + 1]);
                    }
                }

                if (CreatedFile.Contains("_VS2008"))
                {
                    //Convert a copy to VS2008
                    content = content.Replace("# Visual Studio 2010", "# Visual Studio 2008");
                    content = content.Replace("Microsoft Visual Studio Solution File, Format Version 11.00", "Microsoft Visual Studio Solution File, Format Version 10.00");
                    content = content.Replace("vbproj", "VS2008.vbproj").Replace("csproj", "VS2008.csproj");
                }

                if (content.StartsWith(_byteOrderMarkUtf8))
                {
                    LogFormat("Message", "Removed Byte Order Mark");
                    content = content.Remove(0, _byteOrderMarkUtf8.Length);
                }

                using (TextWriter tw = File.CreateText(CreatedFile))
                {
                    tw.Write(content);
                }

                LogFormat("Message", "New File " + CreatedFile);
                LogFormat("Message", "--------------------------------------");
                LogFormat("Message", "End GenerateSolutionFiles Logging Info");
                LogFormat("Message", "--------------------------------------");
            }
            catch (Exception ex)
            {
                LogFormat("Error", ex.StackTrace);
                return(false);
            }
            return(true);
        }