2016-12-13 14:31:35 -10:00
|
|
|
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
|
|
|
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2016-12-16 01:04:09 -08:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
using Microsoft.DotNet.Cli.CommandLine;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using Microsoft.DotNet.Tools;
|
2016-12-16 01:04:09 -08:00
|
|
|
|
using Microsoft.DotNet.Tools.Common;
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.Cli
|
|
|
|
|
{
|
|
|
|
|
public abstract class DotNetTopLevelCommandBase
|
|
|
|
|
{
|
|
|
|
|
protected abstract string CommandName { get; }
|
|
|
|
|
protected abstract string FullCommandNameLocalized { get; }
|
2016-12-16 01:04:09 -08:00
|
|
|
|
internal abstract List<Func<DotNetSubCommandBase>> SubCommands { get; }
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
|
|
|
|
public int RunCommand(string[] args)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.HandleDebugSwitch(ref args);
|
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
|
CommandLineApplication command = new CommandLineApplication(throwOnUnexpectedArg: true)
|
2016-12-13 14:31:35 -10:00
|
|
|
|
{
|
|
|
|
|
Name = $"dotnet {CommandName}",
|
|
|
|
|
FullName = FullCommandNameLocalized,
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
|
command.HelpOption("-h|--help");
|
2016-12-13 14:31:35 -10:00
|
|
|
|
|
2016-12-16 01:04:09 -08:00
|
|
|
|
command.Argument(
|
2016-12-13 14:31:35 -10:00
|
|
|
|
Constants.ProjectOrSolutionArgumentName,
|
|
|
|
|
CommonLocalizableStrings.ArgumentsProjectOrSolutionDescription);
|
|
|
|
|
|
|
|
|
|
foreach (var subCommandCreator in SubCommands)
|
|
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
|
var subCommand = subCommandCreator();
|
|
|
|
|
command.Command(subCommand);
|
|
|
|
|
|
|
|
|
|
subCommand.OnExecute(() => {
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (!command.Arguments.Any())
|
|
|
|
|
{
|
|
|
|
|
throw new GracefulException(CommonLocalizableStrings.RequiredArgumentNotPassed, Constants.ProjectOrSolutionArgumentName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var projectOrDirectory = command.Arguments.First().Value;
|
|
|
|
|
if (string.IsNullOrEmpty(projectOrDirectory))
|
|
|
|
|
{
|
|
|
|
|
projectOrDirectory = PathUtility.EnsureTrailingSlash(Directory.GetCurrentDirectory());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return subCommand.Run(projectOrDirectory);
|
|
|
|
|
}
|
|
|
|
|
catch (GracefulException e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
|
|
|
|
subCommand.ShowHelp();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-12-13 14:31:35 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-12-16 01:04:09 -08:00
|
|
|
|
return command.Execute(args);
|
2016-12-13 14:31:35 -10:00
|
|
|
|
}
|
|
|
|
|
catch (GracefulException e)
|
|
|
|
|
{
|
|
|
|
|
Reporter.Error.WriteLine(e.Message.Red());
|
2016-12-16 01:04:09 -08:00
|
|
|
|
command.ShowHelp();
|
2016-12-13 14:31:35 -10:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
catch (CommandParsingException e)
|
|
|
|
|
{
|
2016-12-16 06:41:47 -10:00
|
|
|
|
string errorMessage = e.IsRequireSubCommandMissing
|
|
|
|
|
? CommonLocalizableStrings.RequiredCommandNotPassed
|
|
|
|
|
: e.Message;
|
|
|
|
|
|
|
|
|
|
Reporter.Error.WriteLine(errorMessage.Red());
|
2016-12-13 14:31:35 -10:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|