Skip to content

Commit

Permalink
Changed to CommandLineParser. Added help & README.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrignoli committed Sep 24, 2017
1 parent 22848ea commit 751ee0b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 50 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Create a Visual Studio Solution from Existing Projects

This console application traverse a folder tree finding every existing Visual Studio Project and adding them to a new solution. It creates 'solution folders' so the projects are grouped in the same hierarchy as in the file system.

Usage:
```
-f, --folder Required. Folder to traverse and where the solution will be
generated.
-o, --output (Default: AllProjects.sln) FileName of the solution to
create.
-e, --exclude (Default: .git,bin,obj,packages,node_modules) Comma
separated list of folders to exclude.
-v, --verbose Verbose
```

Example:
```
SolutionGenerator.exe --folder C:\git\SomeSolutionRoot --output MySolutionFile.sln
```

## Supported Project Types:
Right now it only supports *.csproj file types. It can be extended to other project types by correctly handling the each Project Type GUIDs.
45 changes: 16 additions & 29 deletions Sources/Program.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
using Fclp;
using CommandLine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

[assembly: CommandLine.AssemblyUsageAttribute("Finds all Visual Studio projects in a folder tree and adds them to a new solution file.")]

namespace SolutionGenerator
{
public class Arguments
public class Options
{
[Option('f', "folder", HelpText = "Folder to traverse and where the solution will be generated.", Required = true)]
public string Folder { get; set; }
[Option('o', "output", HelpText = "FileName of the solution to create.", DefaultValue = "AllProjects.sln")]
public string SolutionFileName { get; set; }
[Option('e', "exclude", HelpText = "Comma separated list of folders to exclude.", DefaultValue = ".git,bin,obj,packages,node_modules")]
public string ExcludedFolders { get; set; }
[Option('v', "verbose", HelpText = "Verbose")]
public bool Verbose { get; set; }

}

class Program
{
static void Main(string[] args)
{
var parser = new FluentCommandLineParser<Arguments>();

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();
var options = new Options();
var isValid = Parser.Default.ParseArgumentsStrict(args, options);
if (isValid)
new SolutionGenerator(options).Render();
}

}
}
3 changes: 1 addition & 2 deletions Sources/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SolutionGenerator")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyCopyright("Copyright 2017 - Gerardo Grignoli")]
[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.
Expand Down
39 changes: 23 additions & 16 deletions Sources/SolutionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class FolderContent
class SolutionGenerator
{
string Template =
@"
Microsoft Visual Studio Solution File, Format Version 12.00
@"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Expand All @@ -31,23 +30,23 @@ class SolutionGenerator
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{1}
EndGlobalSection
{1} EndGlobalSection
EndGlobal
";


private readonly Arguments _args;
private readonly Options _args;
private HashSet<string> _excludedFolders;
public SolutionGenerator(Arguments args)
public SolutionGenerator(Options args)
{
_args = args;
_excludedFolders = new HashSet<string>(".git,bin,obj,packages,node_modules".Split(','));
_excludedFolders = new HashSet<string>(_args.ExcludedFolders.Split(','));
}

public void Render()
{
var content = GetContent(_args.Folder);
content.FolderId = string.Empty; // Root folder is a special case

StringBuilder projectSection = new StringBuilder();
StringBuilder folderSection = new StringBuilder();
Expand All @@ -58,6 +57,9 @@ public void Render()
Path.Combine(_args.Folder, _args.SolutionFileName),
string.Format(Template, projectSection.ToString(), folderSection.ToString())
);

Console.WriteLine("Done.");

}

private void WriteContent(FolderContent content, StringBuilder projectSection, StringBuilder folderSection)
Expand All @@ -67,7 +69,10 @@ private void WriteContent(FolderContent content, StringBuilder projectSection, S
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}}}");
if (!string.IsNullOrEmpty(content.FolderId))
{
folderSection.AppendLine($" {{{dir.FolderId}}} = {{{content.FolderId}}}");
}
WriteContent(dir, projectSection, folderSection);
}

Expand All @@ -78,7 +83,8 @@ private void WriteContent(FolderContent content, StringBuilder projectSection, S

projectSection.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{ Path.GetFileNameWithoutExtension(project) }\", \"{ MakeRelative(project) }\", \"{{{projectId}}}\"");
projectSection.AppendLine("EndProject");
folderSection.AppendLine($" {{{projectId}}} = {{{content.FolderId}}}");
if (!string.IsNullOrEmpty(content.FolderId))
folderSection.AppendLine($" {{{projectId}}} = {{{content.FolderId}}}");
}
}

Expand All @@ -89,7 +95,8 @@ private string MakeRelative(string path)

FolderContent GetContent(string folder)
{
Console.WriteLine(folder);
if(_args.Verbose)
Console.WriteLine(folder);
var content = new FolderContent();
content.Path = folder;
try
Expand All @@ -102,15 +109,14 @@ FolderContent GetContent(string folder)
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 if (subContent.SubDirectories.Count() == 0 && subContent.Projects.Count()==1)
content.Projects.AddRange(subContent.Projects); // Flatten folders with only one project.
else
content.SubDirectories.Add(subContent);
}
}
catch (PathTooLongException) { }
catch (PathTooLongException)
{ Console.Error.WriteLine($"PathTooLongException: {folder}"); }

try
{
Expand All @@ -119,7 +125,8 @@ FolderContent GetContent(string folder)
content.Projects.Add(subProj);
}
}
catch (PathTooLongException) { }
catch (PathTooLongException)
{ Console.Error.WriteLine($"PathTooLongException: {folder}"); }
return content;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SolutionGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="FluentCommandLineParser, Version=1.4.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll</HintPath>
<Reference Include="CommandLine, Version=1.9.71.2, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
2 changes: 1 addition & 1 deletion Sources/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="FluentCommandLineParser" version="1.4.3" targetFramework="net46" />
<package id="CommandLineParser" version="1.9.71" targetFramework="net46" />
</packages>

0 comments on commit 751ee0b

Please sign in to comment.