-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ggrignoli
committed
Sep 23, 2017
1 parent
3c8180e
commit 22848ea
Showing
6 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string> Projects = new List<string>(); | ||
public List<FolderContent> SubDirectories = new List<FolderContent>(); | ||
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<string> _excludedFolders; | ||
public SolutionGenerator(Arguments args) | ||
{ | ||
_args = args; | ||
_excludedFolders = new HashSet<string>(".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("<ProjectGuid>") + 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<ProjectGuid>{5B49E94B-3B6F-4976-A656-8A2D23E408DF}</ProjectGuid> | ||
<OutputType>Exe</OutputType> | ||
<AppDesignerFolder>Properties</AppDesignerFolder> | ||
<RootNamespace>SolutionGenerator</RootNamespace> | ||
<AssemblyName>SolutionGenerator</AssemblyName> | ||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineConstants>DEBUG;TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<WarningLevel>4</WarningLevel> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<PlatformTarget>AnyCPU</PlatformTarget> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineConstants>TRACE</DefineConstants> | ||
<ErrorReport>prompt</ErrorReport> | ||
<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> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="System" /> | ||
<Reference Include="System.Core" /> | ||
<Reference Include="System.Xml.Linq" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
<Reference Include="Microsoft.CSharp" /> | ||
<Reference Include="System.Data" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Xml" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="Program.cs" /> | ||
<Compile Include="Properties\AssemblyInfo.cs" /> | ||
<Compile Include="SolutionGenerator.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="FluentCommandLineParser" version="1.4.3" targetFramework="net46" /> | ||
</packages> |