Skip to content

Commit 22848ea

Browse files
author
ggrignoli
committed
Initial commit
1 parent 3c8180e commit 22848ea

File tree

6 files changed

+298
-0
lines changed

6 files changed

+298
-0
lines changed

Sources/Program.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Fclp;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace SolutionGenerator
10+
{
11+
public class Arguments
12+
{
13+
public string Folder { get; set; }
14+
public string SolutionFileName { get; set; }
15+
}
16+
17+
class Program
18+
{
19+
static void Main(string[] args)
20+
{
21+
var parser = new FluentCommandLineParser<Arguments>();
22+
23+
parser.Setup(a => a.Folder)
24+
.As('f', "folder")
25+
.SetDefault(Directory.GetCurrentDirectory());
26+
27+
parser.Setup(a => a.SolutionFileName)
28+
.As('d', "dest")
29+
.Required();
30+
31+
var result = parser.Parse(args);
32+
33+
if (result.HasErrors)
34+
{
35+
Console.WriteLine( result.ErrorText);
36+
Console.Read();
37+
return;
38+
}
39+
40+
parser.Object.Folder = Path.GetFullPath(parser.Object.Folder);
41+
42+
new SolutionGenerator(parser.Object).Render();
43+
}
44+
}
45+
}

Sources/Properties/AssemblyInfo.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SolutionGenerator")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SolutionGenerator")]
13+
[assembly: AssemblyCopyright("Copyright © 2017")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5b49e94b-3b6f-4976-a656-8a2d23e408df")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Sources/SolutionGenerator.cs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace SolutionGenerator
9+
{
10+
class FolderContent
11+
{
12+
public List<string> Projects = new List<string>();
13+
public List<FolderContent> SubDirectories = new List<FolderContent>();
14+
public bool IsEmpty() => !Projects.Any() && SubDirectories.All(sd=>sd.IsEmpty());
15+
public string FolderId = Guid.NewGuid().ToString().ToUpper();
16+
public string Path;
17+
18+
}
19+
20+
class SolutionGenerator
21+
{
22+
string Template =
23+
@"
24+
Microsoft Visual Studio Solution File, Format Version 12.00
25+
# Visual Studio 14
26+
VisualStudioVersion = 14.0.25420.1
27+
MinimumVisualStudioVersion = 10.0.40219.1
28+
{0}
29+
Global
30+
GlobalSection(SolutionProperties) = preSolution
31+
HideSolutionNode = FALSE
32+
EndGlobalSection
33+
GlobalSection(NestedProjects) = preSolution
34+
{1}
35+
EndGlobalSection
36+
EndGlobal
37+
";
38+
39+
40+
private readonly Arguments _args;
41+
private HashSet<string> _excludedFolders;
42+
public SolutionGenerator(Arguments args)
43+
{
44+
_args = args;
45+
_excludedFolders = new HashSet<string>(".git,bin,obj,packages,node_modules".Split(','));
46+
}
47+
48+
public void Render()
49+
{
50+
var content = GetContent(_args.Folder);
51+
52+
StringBuilder projectSection = new StringBuilder();
53+
StringBuilder folderSection = new StringBuilder();
54+
55+
WriteContent(content, projectSection, folderSection);
56+
57+
File.WriteAllText(
58+
Path.Combine(_args.Folder, _args.SolutionFileName),
59+
string.Format(Template, projectSection.ToString(), folderSection.ToString())
60+
);
61+
}
62+
63+
private void WriteContent(FolderContent content, StringBuilder projectSection, StringBuilder folderSection)
64+
{
65+
foreach (var dir in content.SubDirectories)
66+
{
67+
projectSection.AppendLine($"Project(\"{{2150E333-8FDC-42A3-9474-1A3956D46DE8}}\") = \"{ Path.GetFileName(dir.Path) }\", \"{ Path.GetFileName(dir.Path) }\", \"{{{dir.FolderId}}}\"");
68+
projectSection.AppendLine("EndProject");
69+
70+
folderSection.AppendLine($" {{{dir.FolderId}}} = {{{content.FolderId}}}");
71+
WriteContent(dir, projectSection, folderSection);
72+
}
73+
74+
foreach (var project in content.Projects)
75+
{
76+
var projectId = File.ReadAllText(Path.Combine(content.Path, project)) ;
77+
projectId = projectId.Substring(projectId.IndexOf("<ProjectGuid>") + 14, 36);
78+
79+
projectSection.AppendLine($"Project(\"{{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}}\") = \"{ Path.GetFileNameWithoutExtension(project) }\", \"{ MakeRelative(project) }\", \"{{{projectId}}}\"");
80+
projectSection.AppendLine("EndProject");
81+
folderSection.AppendLine($" {{{projectId}}} = {{{content.FolderId}}}");
82+
}
83+
}
84+
85+
private string MakeRelative(string path)
86+
{
87+
return path.Substring(_args.Folder.Length+1);
88+
}
89+
90+
FolderContent GetContent(string folder)
91+
{
92+
Console.WriteLine(folder);
93+
var content = new FolderContent();
94+
content.Path = folder;
95+
try
96+
{
97+
foreach (var subFolder in Directory.GetDirectories(folder))
98+
{
99+
var folderName = Path.GetFileName(folder);
100+
if (_excludedFolders.Contains(folderName)) continue;
101+
102+
var subContent = GetContent(subFolder);
103+
if (subContent.IsEmpty())
104+
continue;
105+
else if (subContent.SubDirectories.Count() == 0 && subContent.Projects.Count()==1)
106+
content.Projects.AddRange(subContent.Projects);
107+
// else if (subContent.Projects.Count == 1 && Path.GetFileNameWithoutExtension(subContent.Projects[0]) == folderName && subContent.SubDirectories.Count == 0)
108+
// content.Projects.Add(subContent.Projects[0]);
109+
else
110+
content.SubDirectories.Add(subContent);
111+
}
112+
}
113+
catch (PathTooLongException) { }
114+
115+
try
116+
{
117+
foreach (var subProj in Directory.GetFiles(folder, "*.CSPROJ"))
118+
{
119+
content.Projects.Add(subProj);
120+
}
121+
}
122+
catch (PathTooLongException) { }
123+
return content;
124+
}
125+
}
126+
}

Sources/SolutionGenerator.csproj

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{5B49E94B-3B6F-4976-A656-8A2D23E408DF}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>SolutionGenerator</RootNamespace>
11+
<AssemblyName>SolutionGenerator</AssemblyName>
12+
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="FluentCommandLineParser, Version=1.4.3.0, Culture=neutral, processorArchitecture=MSIL">
37+
<HintPath>packages\FluentCommandLineParser.1.4.3\lib\net35\FluentCommandLineParser.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="System" />
41+
<Reference Include="System.Core" />
42+
<Reference Include="System.Xml.Linq" />
43+
<Reference Include="System.Data.DataSetExtensions" />
44+
<Reference Include="Microsoft.CSharp" />
45+
<Reference Include="System.Data" />
46+
<Reference Include="System.Net.Http" />
47+
<Reference Include="System.Xml" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
<Compile Include="SolutionGenerator.cs" />
53+
</ItemGroup>
54+
<ItemGroup>
55+
<None Include="packages.config" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>

Sources/SolutionGenerator.sln

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SolutionGenerator", "SolutionGenerator.csproj", "{5B49E94B-3B6F-4976-A656-8A2D23E408DF}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5B49E94B-3B6F-4976-A656-8A2D23E408DF}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Sources/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="FluentCommandLineParser" version="1.4.3" targetFramework="net46" />
4+
</packages>

0 commit comments

Comments
 (0)