-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #37 (CLI) from felix-b/pr-cli
- Loading branch information
Showing
30 changed files
with
1,143 additions
and
39 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
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
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
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,163 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.CommandLine; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace NWheels.Cli | ||
{ | ||
public abstract class CommandBase : ICommand | ||
{ | ||
protected CommandBase(string name, string helpText) | ||
{ | ||
this.Name = name; | ||
this.HelpText = helpText; | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
public abstract void DefineArguments(ArgumentSyntax syntax); | ||
public abstract void ValidateArguments(ArgumentSyntax arguments); | ||
public abstract void Execute(); | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
public string Name { get; } | ||
public string HelpText { get; } | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected int ExecuteProgram( | ||
string nameOrFilePath, | ||
string[] args = null, | ||
string workingDirectory = null, | ||
bool validateExitCode = true) | ||
{ | ||
return ExecuteProgram( | ||
out IEnumerable<string> output, | ||
nameOrFilePath, | ||
args, | ||
workingDirectory, | ||
validateExitCode, | ||
shouldInterceptOutput: false); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected int ExecuteProgram( | ||
out IEnumerable<string> output, | ||
string nameOrFilePath, | ||
string[] args = null, | ||
string workingDirectory = null, | ||
bool validateExitCode = true) | ||
{ | ||
return ExecuteProgram( | ||
out output, | ||
nameOrFilePath, | ||
args, | ||
workingDirectory, | ||
validateExitCode, | ||
shouldInterceptOutput: true); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void Log(string message) | ||
{ | ||
Console.WriteLine(message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void LogDebug(string message) | ||
{ | ||
Program.LogMessageWithColor(ConsoleColor.DarkGray, message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void LogImportant(string message) | ||
{ | ||
Program.LogMessageWithColor(ConsoleColor.Cyan, message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void LogSuccess(string message) | ||
{ | ||
Program.LogMessageWithColor(ConsoleColor.Green, message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void LogWarning(string message) | ||
{ | ||
Program.LogMessageWithColor(ConsoleColor.Yellow, message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void LogError(string message) | ||
{ | ||
Program.LogMessageWithColor(ConsoleColor.Red, message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void ReportFatalError(Exception error) | ||
{ | ||
Console.Error.WriteLine(error.ToString()); | ||
ReportFatalError(error.Message); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
protected void ReportFatalError(string message) | ||
{ | ||
LogError("FATAL ERROR: " + message); | ||
Environment.Exit(2); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
private int ExecuteProgram( | ||
out IEnumerable<string> output, | ||
string nameOrFilePath, | ||
string[] args, | ||
string workingDirectory, | ||
bool validateExitCode, | ||
bool shouldInterceptOutput) | ||
{ | ||
var info = new ProcessStartInfo() { | ||
FileName = nameOrFilePath, | ||
Arguments = (args != null ? string.Join(" ", args) : string.Empty), | ||
WorkingDirectory = workingDirectory, | ||
RedirectStandardOutput = shouldInterceptOutput | ||
}; | ||
|
||
var process = Process.Start(info); | ||
List<string> outputLines = null; | ||
|
||
if (shouldInterceptOutput) | ||
{ | ||
outputLines = new List<string>(capacity: 100); | ||
string line; | ||
while ((line = process.StandardOutput.ReadLine()) != null) | ||
{ | ||
outputLines.Add(line); | ||
} | ||
} | ||
|
||
process.WaitForExit(); | ||
output = outputLines; | ||
|
||
if (validateExitCode && process.ExitCode != 0) | ||
{ | ||
throw new Exception($"Program '{nameOrFilePath}' failed with code {process.ExitCode}."); | ||
} | ||
|
||
return process.ExitCode; | ||
} | ||
} | ||
} |
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,26 @@ | ||
using System; | ||
using System.CommandLine; | ||
|
||
namespace NWheels.Cli | ||
{ | ||
public interface ICommand | ||
{ | ||
void DefineArguments(ArgumentSyntax syntax); | ||
void ValidateArguments(ArgumentSyntax arguments); | ||
void Execute(); | ||
string Name { get; } | ||
string HelpText { get; } | ||
} | ||
|
||
//--------------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
public static class CommandExtensions | ||
{ | ||
public static void BindToCommandLine(this ICommand command, ArgumentSyntax syntax) | ||
{ | ||
var commandSyntax = syntax.DefineCommand(command.Name); | ||
commandSyntax.Help = command.HelpText; | ||
command.DefineArguments(syntax); | ||
} | ||
} | ||
} |
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,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
<AssemblyName>nwheels</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="System.CommandLine" Version="0.1.0-e170329-6" /> | ||
<PackageReference Include="System.Xml.XPath.XDocument" Version="4.3.0" /> | ||
<!-- | ||
<PackageReference Include="Microsoft.Build.Framework" Version="15.1.548" /> | ||
<PackageReference Include="Microsoft.NET.Sdk" Version="1.1.0-alpha-20170315-1" /> | ||
--> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NWheels.Api\NWheels.Api.csproj" /> | ||
<ProjectReference Include="..\NWheels.Implementation\NWheels.Implementation.csproj" /> | ||
</ItemGroup> | ||
|
||
</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,81 @@ | ||
using System; | ||
using System.CommandLine; | ||
using System.Linq; | ||
|
||
namespace NWheels.Cli | ||
{ | ||
class Program | ||
{ | ||
private static readonly ICommand[] _s_commands = { | ||
new Publish.PublishCommand(), | ||
new Run.RunCommand() | ||
}; | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
static int Main(string[] args) | ||
{ | ||
var arguments = ParseCommandLine(args); // will Environment.Exit with code 1 if not parsed | ||
var activeCommand = FindActiveCommand(arguments); // will Environment.Exit with code 1 if not found | ||
activeCommand.ValidateArguments(arguments); // will Environment.Exit with code 1 if not valid | ||
|
||
var exitCode = 0; | ||
|
||
try | ||
{ | ||
activeCommand.Execute(); | ||
} | ||
catch (Exception e) | ||
{ | ||
LogMessageWithColor(ConsoleColor.Red, "FAILED: " + e.Message); | ||
Console.Error.WriteLine(e.ToString()); | ||
exitCode = 2; | ||
} | ||
|
||
return exitCode; | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
public static void LogMessageWithColor(ConsoleColor color, string message) | ||
{ | ||
var saveColor = Console.ForegroundColor; | ||
Console.ForegroundColor = color; | ||
Console.WriteLine(message); | ||
Console.ForegroundColor = saveColor; | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
private static ArgumentSyntax ParseCommandLine(string[] args) | ||
{ | ||
return ArgumentSyntax.Parse(args, syntax => { | ||
foreach (var command in _s_commands) | ||
{ | ||
command.BindToCommandLine(syntax); | ||
} | ||
}); | ||
} | ||
|
||
//----------------------------------------------------------------------------------------------------------------------------------------------------- | ||
|
||
private static ICommand FindActiveCommand(ArgumentSyntax arguments) | ||
{ | ||
ICommand activeCommand = null; | ||
|
||
if (arguments.ActiveCommand != null) | ||
{ | ||
activeCommand = _s_commands | ||
.FirstOrDefault( | ||
c => string.Equals(c.Name, arguments.ActiveCommand.Name, StringComparison.OrdinalIgnoreCase)); | ||
} | ||
|
||
if (activeCommand == null) | ||
{ | ||
arguments.ReportError("Command not understood."); | ||
} | ||
|
||
return activeCommand; | ||
} | ||
} | ||
} |
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,9 @@ | ||
{ | ||
"profiles": { | ||
"NWheels.Cli": { | ||
"commandName": "Project", | ||
"commandLineArgs": "run NWheels.Samples.FirstHappyPath --no-publish", | ||
"workingDirectory": "$(SolutionDir)" | ||
} | ||
} | ||
} |
Oops, something went wrong.