From 22848ea62744657b09fbafeb944f51cafdc3cb2e Mon Sep 17 00:00:00 2001 From: ggrignoli Date: Sat, 23 Sep 2017 11:02:45 -0300 Subject: [PATCH] Initial commit --- Sources/Program.cs | 45 +++++++++++ Sources/Properties/AssemblyInfo.cs | 36 +++++++++ Sources/SolutionGenerator.cs | 126 +++++++++++++++++++++++++++++ Sources/SolutionGenerator.csproj | 65 +++++++++++++++ Sources/SolutionGenerator.sln | 22 +++++ Sources/packages.config | 4 + 6 files changed, 298 insertions(+) create mode 100644 Sources/Program.cs create mode 100644 Sources/Properties/AssemblyInfo.cs create mode 100644 Sources/SolutionGenerator.cs create mode 100644 Sources/SolutionGenerator.csproj create mode 100644 Sources/SolutionGenerator.sln create mode 100644 Sources/packages.config diff --git a/Sources/Program.cs b/Sources/Program.cs new file mode 100644 index 0000000..9abb234 --- /dev/null +++ b/Sources/Program.cs @@ -0,0 +1,45 @@ +using Fclp; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SolutionGenerator +{ + public class Arguments + { + public string Folder { get; set; } + public string SolutionFileName { get; set; } + } + + class Program + { + static void Main(string[] args) + { + var parser = new FluentCommandLineParser(); + + parser.Setup(a => a.Folder) + .As('f', "folder") + .SetDefault(Directory.GetCurrentDirectory()); + + parser.Setup(a => a.SolutionFileName) + .As('d', "dest") + .Required(); + + var result = parser.Parse(args); + + if (result.HasErrors) + { + Console.WriteLine( result.ErrorText); + Console.Read(); + return; + } + + parser.Object.Folder = Path.GetFullPath(parser.Object.Folder); + + new SolutionGenerator(parser.Object).Render(); + } + } +} diff --git a/Sources/Properties/AssemblyInfo.cs b/Sources/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..44f336a --- /dev/null +++ b/Sources/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SolutionGenerator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SolutionGenerator")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("5b49e94b-3b6f-4976-a656-8a2d23e408df")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Sources/SolutionGenerator.cs b/Sources/SolutionGenerator.cs new file mode 100644 index 0000000..4bb53ef --- /dev/null +++ b/Sources/SolutionGenerator.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SolutionGenerator +{ + class FolderContent + { + public List Projects = new List(); + public List SubDirectories = new List(); + public bool IsEmpty() => !Projects.Any() && SubDirectories.All(sd=>sd.IsEmpty()); + public string FolderId = Guid.NewGuid().ToString().ToUpper(); + public string Path; + + } + + class SolutionGenerator + { + string Template = +@" +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +{0} +Global + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution +{1} + EndGlobalSection +EndGlobal +"; + + + private readonly Arguments _args; + private HashSet _excludedFolders; + public SolutionGenerator(Arguments args) + { + _args = args; + _excludedFolders = new HashSet(".git,bin,obj,packages,node_modules".Split(',')); + } + + public void Render() + { + var content = GetContent(_args.Folder); + + StringBuilder projectSection = new StringBuilder(); + StringBuilder folderSection = new StringBuilder(); + + WriteContent(content, projectSection, folderSection); + + File.WriteAllText( + Path.Combine(_args.Folder, _args.SolutionFileName), + string.Format(Template, projectSection.ToString(), folderSection.ToString()) + ); + } + + private void WriteContent(FolderContent content, StringBuilder projectSection, StringBuilder folderSection) + { + foreach (var dir in content.SubDirectories) + { + projectSection.AppendLine($"Project(\"{{2150E333-8FDC-42A3-9474-1A3956D46DE8}}\") = \"{ Path.GetFileName(dir.Path) }\", \"{ Path.GetFileName(dir.Path) }\", \"{{{dir.FolderId}}}\""); + projectSection.AppendLine("EndProject"); + + folderSection.AppendLine($" {{{dir.FolderId}}} = {{{content.FolderId}}}"); + WriteContent(dir, projectSection, folderSection); + } + + foreach (var project in content.Projects) + { + var projectId = File.ReadAllText(Path.Combine(content.Path, project)) ; + projectId = projectId.Substring(projectId.IndexOf("") + 14, 36); + + projectSection.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{ Path.GetFileNameWithoutExtension(project) }\", \"{ MakeRelative(project) }\", \"{{{projectId}}}\""); + projectSection.AppendLine("EndProject"); + folderSection.AppendLine($" {{{projectId}}} = {{{content.FolderId}}}"); + } + } + + private string MakeRelative(string path) + { + return path.Substring(_args.Folder.Length+1); + } + + FolderContent GetContent(string folder) + { + Console.WriteLine(folder); + var content = new FolderContent(); + content.Path = folder; + try + { + foreach (var subFolder in Directory.GetDirectories(folder)) + { + var folderName = Path.GetFileName(folder); + if (_excludedFolders.Contains(folderName)) continue; + + var subContent = GetContent(subFolder); + if (subContent.IsEmpty()) + continue; + else if (subContent.SubDirectories.Count() == 0 && subContent.Projects.Count()==1) + content.Projects.AddRange(subContent.Projects); +// else if (subContent.Projects.Count == 1 && Path.GetFileNameWithoutExtension(subContent.Projects[0]) == folderName && subContent.SubDirectories.Count == 0) +// content.Projects.Add(subContent.Projects[0]); + else + content.SubDirectories.Add(subContent); + } + } + catch (PathTooLongException) { } + + try + { + foreach (var subProj in Directory.GetFiles(folder, "*.CSPROJ")) + { + content.Projects.Add(subProj); + } + } + catch (PathTooLongException) { } + return content; + } + } +} diff --git a/Sources/SolutionGenerator.csproj b/Sources/SolutionGenerator.csproj new file mode 100644 index 0000000..d4afd02 --- /dev/null +++ b/Sources/SolutionGenerator.csproj @@ -0,0 +1,65 @@ + + + + + Debug + AnyCPU + {5B49E94B-3B6F-4976-A656-8A2D23E408DF} + Exe + Properties + SolutionGenerator + SolutionGenerator + v4.6 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll + True + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Sources/SolutionGenerator.sln b/Sources/SolutionGenerator.sln new file mode 100644 index 0000000..fbb2286 --- /dev/null +++ b/Sources/SolutionGenerator.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolutionGenerator", "SolutionGenerator.csproj", "{5B49E94B-3B6F-4976-A656-8A2D23E408DF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Sources/packages.config b/Sources/packages.config new file mode 100644 index 0000000..0a540c7 --- /dev/null +++ b/Sources/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file