private static void HelpForTranspile() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry transpile [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Transpile a collection of .cssql files into C# classes."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -p, --project Project directory to resolve relative paths from. Default to the"); DotNetJerryHost.WriteLine(" current directory."); DotNetJerryHost.WriteLine(" -f, --file Add a file to the project. Prefix with @ to read from a file-based"); DotNetJerryHost.WriteLine(" list (.rsp syntax)."); DotNetJerryHost.WriteLine(" -d, --directory Add all .cssql files from a directory and its subdirectories"); DotNetJerryHost.WriteLine(" to the project."); DotNetJerryHost.WriteLine(" -ns, --namespace Root namespace from which to generate sub-namespaces for each .cssql"); DotNetJerryHost.WriteLine(" file based on its relative project path."); DotNetJerryHost.WriteLine(" -i, --import Add a default namespace import to each generated C# class."); DotNetJerryHost.WriteLine(" -o, --output Specify the output directory for generated C# files. Defaults"); DotNetJerryHost.WriteLine(" to 'obj\\Jerrycurl'"); DotNetJerryHost.WriteLine(" --no-clean Do not clean the output directory from existing C# files before"); DotNetJerryHost.WriteLine(" transpiling."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" jerry transpile -d . -ns MovieDb.Data -o MovieDb\\obj"); DotNetJerryHost.WriteLine(" jerry transpile -f Query1.cssql Query2.cssql"); DotNetJerryHost.WriteLine(" jerry transpile -f @FileList.txt"); DotNetJerryHost.WriteLine(" jerry transpile -p MovieDb -d MovieDb"); DotNetJerryHost.WriteLine(" jerry transpile -d . -i MovieDb.Model"); DotNetJerryHost.WriteLine(); }
private static void HelpForTranspile() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry transpile [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Transpile a project of .cssql files into .cs files."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -p, --project Project directory to resolve relative paths and namespaces from."); DotNetJerryHost.WriteLine(" Defaults to the current directory."); DotNetJerryHost.WriteLine(" -f, --file Add a file to the project."); DotNetJerryHost.WriteLine(" -d, --directory Add all .cssql files to the project from a specified directory."); DotNetJerryHost.WriteLine(" -ns, --namespace Root namespace for the project."); DotNetJerryHost.WriteLine(" -i, --import Add a namespace import."); DotNetJerryHost.WriteLine(" -o, --output Output directory for .cs files. Defaults to 'obj\\Jerrycurl'"); DotNetJerryHost.WriteLine(" --no-clean Do not clean the output directory before writing new files."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" Transpile all .cssql files from the current directory with the specified root namespace:"); DotNetJerryHost.WriteLine(" > jerry transpile -d . -ns BlogDb.Data"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine(" Transpile a few .cssql files and import 'BlogDb.Database' namespace:"); DotNetJerryHost.WriteLine(" > jerry transpile -f Query1.cssql Query2.cssql -i BlogDb.Database"); DotNetJerryHost.WriteLine(); }
private static void ScaffoldWriteVerboseOutput(ScaffoldProject project) { foreach (var file in project.Files) { DotNetJerryHost.WriteLine($" File {file.FileName}"); foreach (var obj in file.Objects) { string tableName = !string.IsNullOrEmpty(obj.Table.Schema) ? $"\"{obj.Table.Schema}\".\"{obj.Table.Name}\"" : $"\"{obj.Table.Name}\""; string className = !string.IsNullOrEmpty(obj.Namespace) ? $"{obj.Namespace}.{obj.ClassName}" : obj.ClassName; DotNetJerryHost.WriteLine($" -> Table {tableName} -> Class {className}"); string[] columnNames = obj.Properties.Select(p => $"\"{p.PropertyName}\"").ToArray(); string columnMoniker = string.Join(", ", columnNames.Take(5)); if (columnNames.Length > 5) { columnMoniker += $" [+{columnNames.Length - 5}]"; } DotNetJerryHost.WriteLine($" -> Property {columnMoniker}"); } } }
public async static Task SqlAsync(RunnerArgs args, IConnectionFactory factory) { if (factory == null) { throw new RunnerException("Invalid factory object."); } if (string.IsNullOrWhiteSpace(args.Connection)) { throw new RunnerException("Please specify a connection string using the -c|--connection argument."); } string[] inputs = args.Options["-s", "--sql"]?.Values ?? Array.Empty <string>(); if (inputs.Length == 0) { throw new RunnerException("Please specify at least one SQL input with the -s|--sql argument."); } using (DbConnection connection = await GetOpenConnectionAsync(args, factory)) { foreach (ResponseFile responseFile in inputs.Select(ResponseFile.Parse)) { if (responseFile.IsPath) { DotNetJerryHost.WriteLine($"Executing '@{Path.GetFileName(responseFile.InputPath)}'...", ConsoleColor.Yellow); if (!File.Exists(responseFile.FullPath)) { DotNetJerryHost.WriteLine($"Skipped. File not found.", ConsoleColor.Yellow); continue; } } else if (!responseFile.Ignore) { DotNetJerryHost.WriteLine($"Executing '{responseFile.Value}'...", ConsoleColor.Yellow); } using (DbCommand command = connection.CreateCommand()) { command.CommandText = string.Join(Environment.NewLine, ResponseFile.ExpandStrings(responseFile)); if (!string.IsNullOrWhiteSpace(command.CommandText)) { int affectedRows = await command.ExecuteNonQueryAsync(); string rowsMoniker = affectedRows + " " + (affectedRows == 1 ? "row" : "rows"); DotNetJerryHost.WriteLine($"OK. {rowsMoniker} affected.", ConsoleColor.Green); } else { DotNetJerryHost.WriteLine($"Skipped. SQL text is empty.", ConsoleColor.Yellow); } } } } }
public static void Meow() { DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine(@" |\---/|"); DotNetJerryHost.WriteLine(@" | o_o |"); DotNetJerryHost.WriteLine(@" \_^_/ "); DotNetJerryHost.WriteLine(); }
public static void Help(RunnerArgs args) { if (args.Options.Default.Length == 0) { HelpForInvalid(args); } else if (args.Options.Default.Length == 1 || args.Options.Default[1] == "help") { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry [command] [options] [@file]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Execute a command with the specified options. Use @file[.cli] to expand arguments"); DotNetJerryHost.WriteLine("from an input file."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Commands:"); DotNetJerryHost.WriteLine(" scaffold Generate a C# object model from an existing database."); DotNetJerryHost.WriteLine(" transpile Transpile a project of .cssql files into .cs files."); DotNetJerryHost.WriteLine(" info Show information about a database connector."); DotNetJerryHost.WriteLine(" args Show all arguments. Useful for debugging @file inputs."); DotNetJerryHost.WriteLine(" help [command] Show help information about the commands above."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" Generate a C# model with arguments in a local 'mydb.cli' file:"); DotNetJerryHost.WriteLine(" > jerry scaffold @mydb"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine(" Transpile .cssql files from directories 'Queries' and 'Commands':"); DotNetJerryHost.WriteLine(" > jerry transpile -d Queries -d Commands"); DotNetJerryHost.WriteLine(); } else { switch (args.Options.Default[1]) { case "scaffold": case "sf": HelpForScaffold(); break; case "transpile": case "tp": HelpForTranspile(); break; case "run": HelpForRun(); break; case "info": HelpForInfo(); break; default: throw new RunnerException($"Invalid command '{args.Options.Default[1]}'."); } } }
public static void Info(RunnerArgs info, InfoCommand command) { if (command == null) { throw new RunnerException("Invalid command object."); } DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine($"Package: {info.Proxy.PackageName}"); DotNetJerryHost.WriteLine($"Connector: {command.Connector} v{command.ConnectorVersion}"); }
public static async Task ScaffoldAsync(RunnerArgs args, ScaffoldCommand command) { if (command == null) { throw new RunnerException("Invalid command object."); } if (string.IsNullOrWhiteSpace(args.Connection)) { throw new RunnerException("Please specify a connection string using the -c|--connection parameter."); } DatabaseModel databaseModel; IList <TypeMapping> typeMappings; if (args.Verbose) { DotNetJerryHost.WriteHeader(); } using (DbConnection connection = await GetOpenConnectionAsync(args, command)) { DotNetJerryHost.WriteLine("Generating...", ConsoleColor.Yellow); databaseModel = await command.GetDatabaseModelAsync(connection); typeMappings = command.GetTypeMappings().ToList(); } ScaffoldProject project = ScaffoldProject.FromModel(databaseModel, typeMappings, args); await ScaffoldWriter.WriteAsync(project); if (args.Verbose) { ScaffoldWriteVerboseOutput(project); } int objectCount = project.Files.SelectMany(f => f.Objects).Count(); string classMoniker = objectCount + " " + (objectCount == 1 ? "class" : "classes"); if (project.Files.Count == 1) { DotNetJerryHost.WriteLine($"Created {classMoniker} in {project.Files[0].FileName}.", ConsoleColor.Green); } else { DotNetJerryHost.WriteLine($"Created {classMoniker} in {project.Files.Count} files.", ConsoleColor.Green); } }
public static void HelpForInvalid(RunnerArgs args) { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry [command] [options]. Use 'jerry help' to show commands and options."); if (string.IsNullOrEmpty(args.Command)) { throw new RunnerException("No command specified."); } else { throw new RunnerException($"Invalid command '{args.Command}'."); } }
public static void Help(RunnerArgs args) { if (args.Options.Default.Length == 0) { HelpForInvalid(args); } else if (args.Options.Default.Length == 1 || args.Options.Default[1] == "help") { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry [command] [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Execute a command with the Jerrycurl CLI."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Commands:"); DotNetJerryHost.WriteLine(" scaffold Generate a C# object model from an existing database."); DotNetJerryHost.WriteLine(" transpile Transpile a collection of .cssql files into C# classes."); DotNetJerryHost.WriteLine(" cli Execute CLI with arguments read from one or more input files."); DotNetJerryHost.WriteLine(" info Show information about a database connector."); DotNetJerryHost.WriteLine(" help [command] Show help information the commands above."); DotNetJerryHost.WriteLine(); } else { switch (args.Options.Default[1]) { case "scaffold": case "sf": HelpForScaffold(); break; case "transpile": case "tp": HelpForTranspile(); break; case "cli": HelpForResponseFile(); break; case "info": HelpForInfo(); break; default: throw new RunnerException($"Invalid command '{args.Options.Default[1]}'."); } } }
private static void HelpForInfo() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry info [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Show information about a specific database connector."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -v, --vendor <moniker> Vendor used to connect to database. Moniker can"); DotNetJerryHost.WriteLine(" be 'sqlserver', 'sqlite', 'oracle', 'postgres'"); DotNetJerryHost.WriteLine(" or 'mysql'."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" jerry info -v sqlserver"); DotNetJerryHost.WriteLine(); }
private static void HelpForInfo() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry info [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Show information about a specific database connector."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -v, --vendor <moniker> Type of database to connect to: 'sqlserver', 'sqlite',"); DotNetJerryHost.WriteLine(" 'oracle', 'postgres' or 'mysql'."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" Show information about the Microsoft SQL Server connector:"); DotNetJerryHost.WriteLine(" > jerry info -v sqlserver"); DotNetJerryHost.WriteLine(); }
private async Task BuildAsync() { DotNetJerryHost.WriteLine($"Fetching {this.Args.Proxy.PackageName} v{this.Args.Proxy.PackageVersion}...", ConsoleColor.Yellow); string[] arguments = new[] { "build", this.Args.Proxy.ProjectPath, "--configuration", "Release", "--verbosity", "quiet", $"-p:OutputPath={this.Args.Proxy.BinPath}", $"-p:VendorPackage={this.Args.Proxy.PackageName}", $"-p:VendorVersion={this.Args.Proxy.PackageVersion}", $"-p:AssemblyName={this.Args.Proxy.DllName}", $"-p:IntermediateOutputPath={this.Args.Proxy.IntermediatePath}" }; await ToolRunner.RunAsync("dotnet", arguments); }
private async static Task <DbConnection> GetOpenConnectionAsync(RunnerArgs args, IConnectionFactory factory) { DbConnection connection = factory.GetDbConnection(); if (connection == null) { throw new RunnerException("Connection returned null."); } try { connection.ConnectionString = args.Connection; } catch (Exception ex) { connection.Dispose(); throw new RunnerException("Invalid connection string: " + ex.Message, ex); } if (!string.IsNullOrEmpty(connection.Database)) { DotNetJerryHost.WriteLine($"Connecting to '{connection.Database}'...", ConsoleColor.Yellow); } else { DotNetJerryHost.WriteLine("Connecting to database...", ConsoleColor.Yellow); } try { await connection.OpenAsync().ConfigureAwait(false); return(connection); } catch (Exception ex) { connection.Dispose(); throw new RunnerException("Unable to open connection: " + ex.Message, ex); } }
private static void HelpForRun() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry run [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Run SQL statements against a database."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -v, --vendor <moniker> Type of database to connect to: 'sqlserver', 'sqlite',"); DotNetJerryHost.WriteLine(" 'oracle', 'postgres' or 'mysql'."); DotNetJerryHost.WriteLine(" -s, --sql <statements> SQL string to execute on the server."); DotNetJerryHost.WriteLine(" -f, --file <file> SQL file to execute. Expands lines from @-prefixed paths."); DotNetJerryHost.WriteLine(" --raw <file> SQL file to execute. Does not expand lines."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" Run a DELETE statement using config from a local 'mydb.cli' file:"); DotNetJerryHost.WriteLine(" > jerry run @mydb --sql \"DELETE FROM [BlogPost]\""); DotNetJerryHost.WriteLine(); }
private static void HelpForResponseFile() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry cli [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Execute CLI with arguments read from one or more input files."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -c, --command <cmd> Command to prefix parsed arguments with."); DotNetJerryHost.WriteLine(" -f, --file Add a file to read command-line arguments from."); DotNetJerryHost.WriteLine(" Defaults to '<command>.cli' in the current directory."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" jerry cli"); DotNetJerryHost.WriteLine(" jerry cli -f MyFile.cli"); DotNetJerryHost.WriteLine(" jerry cli -c scaffold"); DotNetJerryHost.WriteLine(" jerry cli -c scaffold -f Database.cli"); DotNetJerryHost.WriteLine(); }
private static void HelpForScaffold() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry scaffold [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Generate a C# object model from an existing database."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -v, --vendor <moniker> Type of database to connect to: 'sqlserver', 'sqlite',"); DotNetJerryHost.WriteLine(" 'oracle', 'postgres' or 'mysql'."); DotNetJerryHost.WriteLine(" -c, --connection <cs> Connection string used to connect."); DotNetJerryHost.WriteLine(" -ns, --namespace <ns> Namespace to place generated classes in."); DotNetJerryHost.WriteLine(" -o, --output <file> File or directory to generate .cs files into. Defaults"); DotNetJerryHost.WriteLine(" to Database.cs."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" Generate model for a local SQL Server database into 'Database.cs':"); DotNetJerryHost.WriteLine(" > jerry scaffold -v sqlserver -c \"DATABASE=blogdb\" -ns BlogDb.Data"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine(" Generate model into the 'Database' directory using one file per table:"); DotNetJerryHost.WriteLine(" > jerry scaffold [...] -o Database"); DotNetJerryHost.WriteLine(); }
private static void HelpForScaffold() { DotNetJerryHost.WriteHeader(); DotNetJerryHost.WriteLine("Usage: jerry scaffold [options]"); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Generate a C# object model from an existing database."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Options:"); DotNetJerryHost.WriteLine(" -v, --vendor <moniker> Vendor used to connect to database. Moniker can be"); DotNetJerryHost.WriteLine(" 'sqlserver', 'sqlite', 'oracle', 'postgres' or"); DotNetJerryHost.WriteLine(" 'mysql'."); DotNetJerryHost.WriteLine(" -c, --connection <cs> Connection string used to connect to database."); DotNetJerryHost.WriteLine(" -ns, --namespace <ns> Namespace to place scaffolded C# classes in."); DotNetJerryHost.WriteLine(" -o, --output <file> Path to scaffold .cs files into. Writes one"); DotNetJerryHost.WriteLine(" file per class unless specified with .cs"); DotNetJerryHost.WriteLine(" extension. Defaults to Database.cs."); DotNetJerryHost.WriteLine(); DotNetJerryHost.WriteLine("Examples:"); DotNetJerryHost.WriteLine(" jerry scaffold -v sqlserver -c \"SERVER=...\" -ns MovieDb.Data.Database"); DotNetJerryHost.WriteLine(" jerry scaffold -v sqlserver -c \"SERVER=...\" -o MyModel.cs"); DotNetJerryHost.WriteLine(" jerry scaffold -v sqlserver -c \"SERVER=...\" -o Database"); DotNetJerryHost.WriteLine(); }
static Task <int> Main(string[] args) => DotNetJerryHost.Main(args);
public async static Task SqlAsync(RunnerArgs args, IConnectionFactory factory) { if (factory == null) { throw new RunnerException("Invalid factory object."); } if (string.IsNullOrWhiteSpace(args.Connection)) { throw new RunnerException("Please specify a connection string using the -c|--connection argument."); } int numberOfInputs = 0; using (DbConnection connection = await GetOpenConnectionAsync(args, factory)) { foreach (ToolOption option in args.Options) { if (IsSqlInput(option)) { string sqlText = string.Join("\r\n", option.Values); await ExecuteSqlAsync(connection, sqlText); numberOfInputs++; } else if (IsFileInput(option)) { ResponseSettings settings = new ResponseSettings() { IgnoreWhitespace = false, }; string[] expanded = ResponseFile.ExpandFiles(option.Values, settings).ToArray(); string sqlText = string.Join("\r\n", expanded); await ExecuteSqlAsync(connection, sqlText); numberOfInputs++; } else if (IsRawInput(option)) { string sqlText = string.Join("", option.Values.Select(File.ReadAllText)); await ExecuteSqlAsync(connection, sqlText); numberOfInputs++; } } } if (numberOfInputs == 0) { throw new RunnerException("Please specify at least one SQL input with the --sql, --file or --raw arguments."); } async Task ExecuteSqlAsync(DbConnection connection, string sqlText) { using (DbCommand command = connection.CreateCommand()) { command.CommandText = sqlText; if (!string.IsNullOrWhiteSpace(command.CommandText)) { if (args.Verbose) { DotNetJerryHost.WriteLine($"Executing...", ConsoleColor.Yellow); DotNetJerryHost.WriteLine(sqlText, ConsoleColor.Blue); } else { DotNetJerryHost.WriteLine($"Executing '{GetSqlPreviewText(sqlText)}'...", ConsoleColor.Yellow); } int affectedRows = await command.ExecuteNonQueryAsync(); string rowsMoniker = affectedRows + " " + (affectedRows == 1 ? "row" : "rows"); DotNetJerryHost.WriteLine($"OK. {rowsMoniker} affected.", ConsoleColor.Green); } else { DotNetJerryHost.WriteLine($"Skipped. SQL text is empty.", ConsoleColor.Yellow); } } } string GetSqlPreviewText(string sqlText) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < sqlText.Length && builder.Length <= 30; i++) { if (!char.IsWhiteSpace(sqlText[i])) { builder.Append(sqlText[i]); } else if (builder.Length > 0 && !char.IsWhiteSpace(builder[builder.Length - 1])) { builder.Append(' '); } } return(builder.ToString()); } bool IsRawInput(ToolOption option) => (option.Name == "raw" || option.ShortName == "r"); bool IsSqlInput(ToolOption option) => (option.Name == "sql" || option.ShortName == "s"); bool IsFileInput(ToolOption option) => (option.Name == "file" || option.ShortName == "f"); }