-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
269 lines (241 loc) · 14.3 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using rokono_cl.CLHandlers;
using rokono_cl.Data_Hanlders;
using RokonoDbManager.Models;
namespace rokono_cl
{
class Program
{
private static SavedConnection SavedConnection {get; set;}
private static string Password {get;set;}
private static string User {get;set;}
private static string Database { get; set; }
private static string FilePath {get; set;}
public static string DbContextPath {get; set;}
private static string Ip {get; set;}
private static bool Os {get; set;}
public static bool IsLinux
{
get
{
int p = (int) Environment.OSVersion.Platform;
return (p == 4) || (p == 6) || (p == 128);
}
}
static void Main(string[] args)
{
for(int i = 0; i < args.Length; i++)
{
switch(args[i])
{
case "-u":
User = args[i+1];
break;
case "-password":
Password = args[i+1];
break;
case "-d":
Database = args[i+1];
break;
case "-file":
FilePath = args[i+1];
break;
case "-a":
Ip = args[i+1];
break;
case "-e":
EditConnection();
break;
case "-r":
RemoveConnection();
break;
case "-s":
SaveDatabaseGen();
break;
case "-L":
GetConnections();
break;
case "-CP":
DbContextPath = args[i+1];
break;
case "-Context":
GenerateDbContext();
break;
case "-Connection":
SavedConnection = GetConnectionById(args[i+1]);
System.Console.WriteLine(SavedConnection.ConnectionString);
break;
case "-GS":
DiagramHandlers.GenerateSchema(SavedConnection,SavedConnection.Database,SavedConnection.FilePath);
break;
case "-GF":
DiagramHandlers.GenerateSchema(SavedConnection,Database,FilePath);
break;
case "--Help":
ShowHelpMenu();
break;
}
}
if(args.Length == 0)
System.Console.WriteLine("Use rokono-cl --Help *for more information*");
}
private static void GenerateDbContext()
{
var cmd = string.Empty;
if(Os)
{
cmd = $"dotnet ef dbcontext scaffold \"Server={Ip};Database={Database};User ID={User};Password='{Password}';\" Microsoft.EntityFrameworkCore.SqlServer -o {DbContextPath} -f";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = cmd;
process.StartInfo = startInfo;
process.Start();
}
else
{
if(SavedConnection != null)
{
DbContextPath = SavedConnection.DbContextPath;
cmd = $"cd {DbContextPath} & dotnet ef dbcontext scaffold \"{SavedConnection.ConnectionString}\" Microsoft.EntityFrameworkCore.SqlServer -o Models -f";
}
else
cmd = $"cd {DbContextPath} & dotnet ef dbcontext scaffold \"Server={Ip};Database={Database};User ID={User};Password='{Password}';\" Microsoft.EntityFrameworkCore.SqlServer -f";
var bashResult = Bash(cmd);
}
}
public static string Bash(string cmd)
{
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{escapedArgs}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return result;
}
private static void RemoveConnection()
{
var data = InputHandler.GetSavedConnections();
var getCons = data == null ? new List<SavedConnection>() : data;
if(getCons.Count == 0)
{
System.Console.WriteLine("Collection is empty, you can't delete an non existing row!");
}
SavedConnection = getCons.FirstOrDefault( x=> x.ConnectionId == SavedConnection.ConnectionId);
getCons.Remove(SavedConnection);
var rebase = new List<SavedConnection>();
getCons.ForEach(x=>{
var current = x;
current.ConnectionId = current.ConnectionId > 1 ? current.ConnectionId -1 : 1;
rebase.Add(current);
});
InputHandler.SavedConnections(getCons);
}
private static void EditConnection()
{
System.Console.WriteLine("In");
var data = InputHandler.GetSavedConnections();
var getCons = data == null ? new List<SavedConnection>() : data;
var conStirng =$"Server={Ip};Database={Database};User ID={User};Password='{Password}';";
var updatingConnection= new SavedConnection();
updatingConnection.ConnectionId = SavedConnection.ConnectionId;
updatingConnection.Username = string.IsNullOrEmpty(User) ? "" : User;
updatingConnection.Password = string.IsNullOrEmpty(Password) ? "" : Password;
updatingConnection.FilePath = string.IsNullOrEmpty(FilePath) ? "" : FilePath;
updatingConnection.Database = string.IsNullOrEmpty(Database) ? "" : Database;
updatingConnection.Host = string.IsNullOrEmpty(Ip) ? "" : Ip;
updatingConnection.ConnectionString = string.IsNullOrEmpty(conStirng) ? "" : conStirng;
updatingConnection.DbContextPath = string.IsNullOrEmpty(DbContextPath) ? "" : DbContextPath;
SavedConnection = getCons.FirstOrDefault( x=> x.ConnectionId == SavedConnection.ConnectionId);
getCons.Remove(SavedConnection);
getCons.Add(updatingConnection);
InputHandler.SavedConnections(getCons);
}
private static void ShowHelpMenu()
{
System.Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
System.Console.WriteLine("Name: Rokono-Cl");
System.Console.WriteLine("Description: Quick and easy tool to generate UML diagrams from relational databases released as an extension of plantUML extension for visual studio code");
System.Console.WriteLine("The tool comes as is and its not supported or developed by the team behind plantUML, but it is fully integrated to work with the drawing libaray to generate database UML diagrams.");
System.Console.WriteLine("Author: Kristifor Milchev");
System.Console.WriteLine("Contact for support: Kristifor@rttinternational.com");
System.Console.WriteLine("Github: //");
System.Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
System.Console.WriteLine();
System.Console.WriteLine();
System.Console.WriteLine("Usage example rokono-cl [options] [commands]");
System.Console.WriteLine("-u : Prompts for a username it should be an account that has access to view table relationships. ");
System.Console.WriteLine("-password: requires a valid password for the current sql user");
System.Console.WriteLine("-d: the default database that will be used to generate a *.wsd diagram ");
System.Console.WriteLine("-file: requires a path that will point to a *.wsd file (optional) ");
System.Console.WriteLine("-a: the endpoint of the sql server, it could be a domain or an ip and if it doesn't run on the default port please specify it with ip:port");
System.Console.WriteLine("-L: returns a list of saved database diagrams for quick access.");
System.Console.WriteLine("-r: removes a record from the saved connections");
System.Console.WriteLine("-CP: Specifies a directory to save the generated edmx context. Used for generating database first approach with entity framework core, eventually it executes DbScaffold on the database. the command can be specified at the end to ensure that the project context is the same as the generated UML diagram.");
System.Console.WriteLine("-e: edits a saved connection");
System.Console.WriteLine("-Connection: requires specified Id after the command in order to select a connection from the quick access list. Saved quick connectison can be viewd with -L for identified use the ID column result");
System.Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
System.Console.WriteLine(" Please include the commnds after the supplied options ");
System.Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
System.Console.WriteLine("-s: specify this command at the end of the new connection in order to save it for quick access in the future. Example rokono-cl -u User -password \"Password\" -a ip -d DatabaseName -file PathToWsdFile -s ");
System.Console.WriteLine("-e: specify this command at the end of the new connection followed by -Connection ID in order to edit a record in the saved connections list.");
System.Console.WriteLine("-r: specitfy this command after -Connection ID in order to remove a connection from the saved connections list.");
System.Console.WriteLine("-GF: Uses a saved connection to generate a plantUML diagram for a specific database that is on the same server as the quick access connection with a custom filepath. Usage rokono-cl -Connection ID -d DatabaseName -file customfilePath -GF");
System.Console.WriteLine("-GS: Uses a saved connection to generate a plantUML diagram for the default set database using the default saved filepath. Usage rokono-cl -Connection ID -GS");
System.Console.WriteLine("-Context: runs dbscaffold on a database to generate database first update or initalization on the project directory ensuring consitancy between the generated UML diagram and database model inside the project. Important, must be -CP must be pointed to the root project folder in order to generate database context!!!");
System.Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
}
private static SavedConnection GetConnectionById(string conId)
{
var result = InputHandler.GetSavedConnection(conId);
if(result == null)
{
System.Console.WriteLine("Connection doesn't exist please try connecting to the database using the following syntax. ");
System.Console.WriteLine("-U username -password password -d databasename -file filepath -a hostip -s to save for later use");
System.Console.WriteLine("You can also do --help for more information");
}
return result;
}
private static void GetConnections()
{
var getCons = InputHandler.GetSavedConnections();
Console.WriteLine(getCons.ToStringTable(
new[] {"ID", "Database Name", "Host", "File Path", "Context Path"},
a => a.ConnectionId, a => a.Database, a => a.Host, a=> a.FilePath, a=> a.DbContextPath));
}
private static void SaveDatabaseGen()
{
System.Console.WriteLine("In");
var data = InputHandler.GetSavedConnections();
var getCons = data == null ? new List<SavedConnection>() : data;
var count = getCons.Count == 0 ? getCons.Count + 1 : getCons.Count + 1;
var conStirng =$"Server={Ip};Database={Database};User ID={User};Password='{Password}';";
var savedConnection = new SavedConnection{
Username = string.IsNullOrEmpty(User) ? "" : User,
Password = string.IsNullOrEmpty(Password) ? "" : Password,
FilePath = string.IsNullOrEmpty(FilePath) ? "" : FilePath,
Database = string.IsNullOrEmpty(Database) ? "" : Database,
Host = string.IsNullOrEmpty(Ip) ? "" : Ip,
ConnectionString = string.IsNullOrEmpty(conStirng) ? "" : conStirng,
ConnectionId = count,
DbContextPath = string.IsNullOrEmpty(DbContextPath) ? "" : DbContextPath
};
getCons.Add(savedConnection);
InputHandler.SavedConnections(getCons);
}
}
}