Skip to content

Instantly share code, notes, and snippets.

@mandel-macaque
Last active Mar 26, 2020
Embed
What would you like to do?
using System;
using System.Collections.Generic;
using Mono.Options;
namespace TestCommandSet {
public enum TemplateType {
Managed,
Native,
}
class MainClass {
public static int Main (string [] args)
{
// create the root command, this will use the platform specific commands to do the right thing.
var commands = new CommandSet ("xharness");
// add the command sets per platform, each will have the same verbs but diff implemenation.
commands.Add (new iOSCommandSet ());
commands.Add (new AndroidCommandSet ());
// add shared commands, for example, help and so on. --version, --help, --verbosity and so on
return commands.Run (args);
}
}
class MyHelp : HelpCommand {
public override int Invoke (IEnumerable<string> arguments)
{
var s = Options.Parse (arguments);
Console.WriteLine (string.Join (" ", s));
return base.Invoke (arguments);
}
}
class iOSCommandSet : CommandSet {
public iOSCommandSet () : base ("ios") {
Add (new PackageCommand ());
Add (new HelpCommand ());
}
}
class PackageCommand : Command {
// working directories
string workingDirectory;
// will be used as the output dir of the generated projects.
string outputDirectory;
// path that is the root of the .ignore files that will be used to skip tests if needed.
string ignoreFilesRootDirectory;
// path that is the root of the traits txt files that willl be used to skip tests if needed.
string traitsRootDirectory;
string name;
string mtouchExtraArgs;
TemplateType templateType;
bool showHelp = false;
public PackageCommand () : base ("package"){
Options = new OptionSet () {
"usage: ios package [OPTIONS]",
"",
"Packaging command that will create a iOS/tvOS/watcOS or macOS application that can be used to run nunit or xunit based tests dlls.",
{ "name|n=", "The naming of the testing application.", v => name = v},
{ "working-directory=", "The directory that will be used to output to generate all the different projects.", v => workingDirectory = v },
{ "output-directory=", "The directory in which the resulting package will be output.", v => outputDirectory = v},
{ "ignore-directory=", "The root directory that contains all the *.ignore files that will be used to skip tests if needed.", v => ignoreFilesRootDirectory = v },
{ "traits-directory=", "The root director that contains all the .txt files with the traits that will be skipped if needed.", v => traitsRootDirectory = v },
{ "template=", "Indicates which template to use. There are two available ones. Managed, which uses Xamarin.[iOS|Mac] and native. Default is managed.",
v=> {
if (Enum.TryParse<TemplateType>(v, out var template)) {
templateType = template;
} else {
templateType = TemplateType.Managed; // TODO, throw an error.
}
}
},
{ "help|h", "Show this message", v => showHelp = v != null },
{ "mtouch-extraargs=", "Extra arguments to be passed to mtouch.", v => mtouchExtraArgs = v },
};
}
public override int Invoke (IEnumerable<string> arguments)
{
// Deal with unknown options and print nicely. MATT this is what we want to do
var extra = Options.Parse (arguments);
if (showHelp) {
Options.WriteOptionDescriptions (Console.Out);
}
if (extra.Count > 0) {
Console.WriteLine ($"Unknown {string.Join (" ", extra)}");
Options.WriteOptionDescriptions (Console.Out);
}
return 0;
}
}
class AndroidCommandSet : CommandSet {
public AndroidCommandSet () : base ("android") {
Add (new AndroidTestCommand ());
Add (new HelpCommand ());
}
}
class AndroidTestCommand : Command {
string name;
bool showHelp = false;
public AndroidTestCommand () : base ("test")
{
Options = new OptionSet () {
"usage: android test [OPTIONS]",
"",
"Packaging command that will create a iOS/tvOS/watcOS or macOS application that can be used to run nunit or xunit based tests dlls.",
{ "name|n=", "The naming of the testing application.", v => name = v},
};
}
public override int Invoke (IEnumerable<string> arguments)
{
// Deal with unknown options and print nicely. MATT this is what we want to do
var extra = Options.Parse (arguments);
if (showHelp) {
Options.WriteOptionDescriptions (Console.Out);
}
if (extra.Count > 0) {
Console.WriteLine ($"Unknown {string.Join (" ", extra)}");
Options.WriteOptionDescriptions (Console.Out);
}
return 0;
}
}
}
Unknown bar
usage: ios package [OPTIONS]
Packaging command that will create a iOS/tvOS/watcOS or macOS application that
can be used to run nunit or xunit based tests dlls.
--name, -n=VALUE The naming of the testing application.
--working-directory=VALUE
The directory that will be used to output to
generate all the different projects.
--output-directory=VALUE
The directory in which the resulting package will
be output.
--ignore-directory=VALUE
The root directory that contains all the *.ignore
files that will be used to skip tests if needed.
--traits-directory=VALUE
The root director that contains all the .txt files
with the traits that will be skipped if needed.
--template=VALUE Indicates which template to use. There are two
available ones. Managed, which uses Xamarin.[iOS|
Mac] and native. Default is managed.
--help, -h Show this message
--mtouch-extraargs=VALUE
Extra arguments to be passed to mtouch.
Debug mono TestCommandSet.exe android test foo
Unknown foo
usage: android test [OPTIONS]
Packaging command that will create a iOS/tvOS/watcOS or macOS application that
can be used to run nunit or xunit based tests dlls.
--name, -n=VALUE The naming of the testing application.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment