dotnet-installer/src/dotnet/commands/dotnet-add/Program.cs

101 lines
3 KiB
C#
Raw Normal View History

2016-11-15 15:42:15 -08: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.Collections.Generic;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Utils;
using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Construction;
using Microsoft.DotNet.ProjectJsonMigration;
using NuGet.Frameworks;
2016-11-15 16:41:29 -08:00
using Microsoft.DotNet.Tools.Add.ProjectToProjectReference;
2016-11-15 15:42:15 -08:00
namespace Microsoft.DotNet.Tools.Add
{
public class AddCommand
{
public const string HelpText = @".NET Add Command
Usage: dotnet add [options] [object] <command> [[--] <arg>...]]
2016-11-15 15:42:15 -08:00
Arguments:
<object> The object of the operation. If a project file is not specified, it defaults to the current directory.
<command> Command to be executed on <object>.
Options:
-h|--help Show help information
Args:
Any extra arguments passed to the command.
2016-11-15 15:42:15 -08:00
Commands:
p2p Add project to project (p2p) reference to a project";
2016-11-15 15:42:15 -08:00
private static Dictionary<string, Func<string[], int>> s_builtIns = new Dictionary<string, Func<string[], int>>
{
2016-11-15 16:41:29 -08:00
["p2p"] = AddProjectToProjectReferenceCommand.Run,
2016-11-15 15:42:15 -08:00
};
public static int Run(string[] args)
{
DebugHelper.HandleDebugSwitch(ref args);
if (args.Length == 0 || args[0] == "--help" || args[0] == "-h")
{
Reporter.Output.WriteLine(HelpText);
return 0;
2016-11-15 15:42:15 -08:00
}
string commandObject;
string command;
if (IsValidCommandName(args[0]))
{
command = args[0];
commandObject = GetCurrentDirectoryWithDirSeparator();
args = args.Skip(1).Prepend(commandObject).ToArray();
}
else if (args.Length == 1)
2016-11-15 15:42:15 -08:00
{
Reporter.Error.WriteLine("Required argument <command> was not passed.".Red());
Reporter.Output.WriteLine(HelpText);
2016-11-15 15:42:15 -08:00
return 1;
}
else
{
commandObject = args[0];
command = args[1];
args = args.Skip(2).Prepend(commandObject).ToArray();
}
2016-11-15 15:42:15 -08:00
Func<string[], int> builtin;
if (s_builtIns.TryGetValue(command, out builtin))
2016-11-15 15:42:15 -08:00
{
return builtin(args);
2016-11-15 15:42:15 -08:00
}
Reporter.Error.WriteLine("Required argument <command> is invalid.".Red());
Reporter.Output.WriteLine(HelpText);
return 1;
}
private static bool IsValidCommandName(string s)
{
return s_builtIns.ContainsKey(s);
}
private static string GetCurrentDirectoryWithDirSeparator()
{
string ret = Directory.GetCurrentDirectory();
if (ret[ret.Length - 1] != Path.DirectorySeparatorChar)
2016-11-15 15:42:15 -08:00
{
ret += Path.DirectorySeparatorChar;
2016-11-15 15:42:15 -08:00
}
return ret;
2016-11-15 15:42:15 -08:00
}
}
}