-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
142 lines (123 loc) · 5.81 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Developed By Pezhvak ;)
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main(string[] args)
{
if (args.Length == 0 || args[0] != "--Generate")
{
Console.WriteLine("Usage: Anion --Generate");
return;
}
// Get the current directory (project root)
string projectPath = Directory.GetCurrentDirectory();
// Get the project name from the directory name
string projectName = new DirectoryInfo(projectPath).Name;
// Find the solution file in the root directory
string solutionFile = Directory.GetFiles(projectPath, "*.sln").FirstOrDefault();
if (string.IsNullOrEmpty(solutionFile))
{
Console.WriteLine("Error: No solution file found in the current directory.");
return;
}
// Create Class Library projects at the same level as the Web project
CreateClassLibraries(projectPath, projectName, solutionFile);
// Add project references
AddReferences(projectPath, projectName);
// Inform the user to reload the solution
Console.ForegroundColor = ConsoleColor.Green; // Set text color to green
Console.WriteLine("Onion architecture Class Libraries created and added to the solution successfully.");
Console.WriteLine("IMPORTANT: Please click 'Reload' in Visual Studio when prompted to apply changes.");
Console.ResetColor(); // Reset text color to default
}
static void CreateClassLibraries(string projectPath, string projectName, string solutionFile)
{
// Define the class library layers
string[] layers = {
$"{projectName}.Common",
$"{projectName}.Data",
$"{projectName}.Entities",
$"{projectName}.IocConfig",
$"{projectName}.Services",
$"{projectName}.ViewModels"
};
foreach (var layerName in layers)
{
// Create each class library at the same level as the main project
string layerPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, layerName);
Directory.CreateDirectory(layerPath);
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"new classlib -n {layerName} -o \"{layerPath}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
Console.WriteLine($"Created: {layerPath}");
}
// Add the created class library to the solution
AddToSolution(solutionFile, layerPath);
}
}
static void AddToSolution(string solutionFile, string projectPath)
{
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = $"sln \"{solutionFile}\" add \"{projectPath}\\{new DirectoryInfo(projectPath).Name}.csproj\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
Console.WriteLine($"Added to solution: {projectPath}");
}
}
static void AddReferences(string projectPath, string projectName)
{
// Define paths for the projects
string commonPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.Common");
string dataPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.Data");
string entitiesPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.Entities");
string iocConfigPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.IocConfig");
string servicesPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.Services");
string viewModelsPath = Path.Combine(new DirectoryInfo(projectPath).Parent.FullName, $"{projectName}.ViewModels");
string webPath = projectPath;
// Add references based on the required dependencies
RunDotnetCommand($"add \"{webPath}\\{new DirectoryInfo(webPath).Name}.csproj\" reference \"{commonPath}\\{new DirectoryInfo(commonPath).Name}.csproj\"");
RunDotnetCommand($"add \"{webPath}\\{new DirectoryInfo(webPath).Name}.csproj\" reference \"{dataPath}\\{new DirectoryInfo(dataPath).Name}.csproj\"");
RunDotnetCommand($"add \"{webPath}\\{new DirectoryInfo(webPath).Name}.csproj\" reference \"{iocConfigPath}\\{new DirectoryInfo(iocConfigPath).Name}.csproj\"");
RunDotnetCommand($"add \"{dataPath}\\{new DirectoryInfo(dataPath).Name}.csproj\" reference \"{entitiesPath}\\{new DirectoryInfo(entitiesPath).Name}.csproj\"");
RunDotnetCommand($"add \"{dataPath}\\{new DirectoryInfo(dataPath).Name}.csproj\" reference \"{viewModelsPath}\\{new DirectoryInfo(viewModelsPath).Name}.csproj\"");
RunDotnetCommand($"add \"{iocConfigPath}\\{new DirectoryInfo(iocConfigPath).Name}.csproj\" reference \"{dataPath}\\{new DirectoryInfo(dataPath).Name}.csproj\"");
RunDotnetCommand($"add \"{iocConfigPath}\\{new DirectoryInfo(iocConfigPath).Name}.csproj\" reference \"{servicesPath}\\{new DirectoryInfo(servicesPath).Name}.csproj\"");
RunDotnetCommand($"add \"{servicesPath}\\{new DirectoryInfo(servicesPath).Name}.csproj\" reference \"{commonPath}\\{new DirectoryInfo(commonPath).Name}.csproj\"");
RunDotnetCommand($"add \"{servicesPath}\\{new DirectoryInfo(servicesPath).Name}.csproj\" reference \"{dataPath}\\{new DirectoryInfo(dataPath).Name}.csproj\"");
RunDotnetCommand($"add \"{viewModelsPath}\\{new DirectoryInfo(viewModelsPath).Name}.csproj\" reference \"{commonPath}\\{new DirectoryInfo(commonPath).Name}.csproj\"");
RunDotnetCommand($"add \"{viewModelsPath}\\{new DirectoryInfo(viewModelsPath).Name}.csproj\" reference \"{entitiesPath}\\{new DirectoryInfo(entitiesPath).Name}.csproj\"");
}
static void RunDotnetCommand(string arguments)
{
var startInfo = new ProcessStartInfo
{
FileName = "dotnet",
Arguments = arguments,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
Console.WriteLine($"Executed: dotnet {arguments}");
}
}
}