-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI #37
Merged
CLI #37
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
62377ac
CLI: initial skeleton
felix-b 4ea9ab7
CLI: separated 'service' command into 'publish' and 'run' ; introduce…
felix-b 9f4959c
CLI: added missing files (apparently, .gitignore interfered with ever…
felix-b 18196a7
CLI: got rid of NWheels.Cli.UnitTests in favor of system testing ; im…
felix-b ee15288
Kernel/Extensions: added StringExtensions.DefaultIfNullOrEmpty
felix-b 2d240df
Kernel/Microservices + CLI: progress with Publish and Run commands
felix-b 6c360c3
CLI: temporarily(?) removing "all projects compile to solution-level …
felix-b 2dddb25
Kernel/Microservices: added AssemblyLocationMap to BootConfiguration …
1d3b7d0
CLI: mostly complete implementation - in debugging phase.
32a5eac
Platform/Messaging: bugfix in KestrelLifecycleListenerComponent - a c…
felix-b 924ee42
CLI: bugfix in CommandBase - output interception in ExecuteProgram ca…
felix-b ac6f6cf
CLI/run: performance improvement - invoke MSBuild only once, rather t…
felix-b b6d137a
CLI: fixed/added support for relative paths in the command line; now …
felix-b File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I run Cli without any parameters it crashed with:
"error: missing command
Press any key to continue . . ."
It seems Cli itself should gives/takes -h|--help command when there is no any.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah. You're right. Will fix that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll track it in #38. The fix will be part of PR #33.