restore loc strings, subcommand parsers into separate files

This commit is contained in:
jonsequitur 2017-03-19 14:35:11 -07:00 committed by Jon Sequeira
parent 43c13f2f53
commit daac945742
16 changed files with 269 additions and 175 deletions

View file

@ -158,8 +158,8 @@ namespace Microsoft.DotNet.Tools
/// sln
public const string ArgumentsProjectDescription = "The project file to operate on. If a file is not specified, the command will search the current directory for one.";
public const string ArgumentsSolutionDescription = "Solution file to operate on. If not specified, the command will search the current directory for one.";
public const string CmdSlnFile = "<SLN_FILE>";
public const string CmdProjectFile = "<PROJECT>";
public const string CmdSlnFile = "SLN_FILE";
public const string CmdProjectFile = "PROJECT";
/// commands
public const string CmdFramework = "FRAMEWORK";

View file

@ -2,13 +2,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using Microsoft.DotNet.Cli.CommandLine;
using Newtonsoft.Json.Linq;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.PackageReference.LocalizableStrings;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
@ -17,81 +14,12 @@ namespace Microsoft.DotNet.Cli
public static Command Add() =>
Create.Command(
"add",
".NET Add Command",
LocalizableStrings.NetAddCommand,
Accept.ExactlyOneArgument()
.DefaultToCurrentDirectory()
.With(name: "PROJECT",
description: "The project file to operate on. If a file is not specified, the command will search the current directory for one."),
Create.Command(
"package",
".NET Add Package reference Command",
Accept.ExactlyOneArgument(errorMessage: o => LocalizableStrings.SpecifyExactlyOnePackageReference)
.WithSuggestionsFrom(QueryNuGet)
.With(name: "PACKAGE_NAME",
description: "Package references to add"),
CommonOptions.HelpOption(),
Create.Option("-v|--version",
"Version for the package to be added.",
Accept.ExactlyOneArgument()
.With(name: "VERSION")
.ForwardAsSingle(o => $"--version {o.Arguments.Single()}")),
Create.Option("-f|--framework",
LocalizableStrings.CmdFrameworkDescription,
Accept.ExactlyOneArgument()
.With(name: "FRAMEWORK")
.ForwardAsSingle(o => $"--framework {o.Arguments.Single()}")),
Create.Option("-n|--no-restore ",
"Add reference without performing restore preview and compatibility check."),
Create.Option("-s|--source",
"Use specific NuGet package sources to use during the restore.",
Accept.ExactlyOneArgument()
.With(name: "SOURCE")
.ForwardAsSingle(o => $"--source {o.Arguments.Single()}")),
Create.Option("--package-directory",
"Restore the packages to this Directory .",
Accept.ExactlyOneArgument()
.With(name: "PACKAGE_DIRECTORY")
.ForwardAsSingle(o => $"--package-directory {o.Arguments.Single()}"))),
Create.Command(
"reference",
Tools.Add.ProjectToProjectReference.LocalizableStrings.AppFullName,
Accept.OneOrMoreArguments()
.With(name: "args",
description: Tools.Add.ProjectToProjectReference.LocalizableStrings.AppHelpText),
CommonOptions.HelpOption(),
Create.Option("-f|--framework",
LocalizableStrings.CmdFrameworkDescription,
Accept
.ExactlyOneArgument()
.WithSuggestionsFrom(_ => Suggest.TargetFrameworksFromProjectFile())
.With(name: "FRAMEWORK"))),
.With(name: CommonLocalizableStrings.CmdProjectFile,
description: CommonLocalizableStrings.ArgumentsProjectDescription), AddPackageParser.AddPackage(),
AddProjectToProjectReferenceParser.AddProjectReference(),
CommonOptions.HelpOption());
public static IEnumerable<string> QueryNuGet(string match)
{
var httpClient = new HttpClient();
string result;
try
{
var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var response = httpClient.GetAsync($"https://api-v2v3search-0.nuget.org/query?q={match}&skip=0&take=100&prerelease=true", cancellation.Token)
.Result;
result = response.Content.ReadAsStringAsync().Result;
}
catch (Exception)
{
yield break;
}
var json = JObject.Parse(result);
foreach (var id in json["data"])
{
yield return id["id"].Value<string>();
}
}
}
}

View file

@ -0,0 +1,78 @@
// 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;
using System.Linq;
using System.Net.Http;
using System.Threading;
using Microsoft.DotNet.Cli.CommandLine;
using Newtonsoft.Json.Linq;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.PackageReference.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
internal static class AddPackageParser
{
public static Command AddPackage()
{
return Create.Command(
"package",
LocalizableStrings.AppFullName,
Accept.ExactlyOneArgument(errorMessage: o => LocalizableStrings.SpecifyExactlyOnePackageReference)
.WithSuggestionsFrom(QueryNuGet)
.With(name: LocalizableStrings.CmdPackage,
description: LocalizableStrings.CmdPackageDescription),
CommonOptions.HelpOption(),
Create.Option("-v|--version",
LocalizableStrings.CmdVersionDescription,
Accept.ExactlyOneArgument()
.With(name: LocalizableStrings.CmdVersion)
.ForwardAsSingle(o => $"--version {o.Arguments.Single()}")),
Create.Option("-f|--framework",
LocalizableStrings.CmdFrameworkDescription,
Accept.ExactlyOneArgument()
.With(name: LocalizableStrings.CmdFramework)
.ForwardAsSingle(o => $"--framework {o.Arguments.Single()}")),
Create.Option("-n|--no-restore ",
LocalizableStrings.CmdNoRestoreDescription),
Create.Option("-s|--source",
LocalizableStrings.CmdSourceDescription,
Accept.ExactlyOneArgument()
.With(name: LocalizableStrings.CmdSource)
.ForwardAsSingle(o => $"--source {o.Arguments.Single()}")),
Create.Option("--package-directory",
LocalizableStrings.CmdPackageDirectoryDescription,
Accept.ExactlyOneArgument()
.With(name: LocalizableStrings.CmdPackageDirectory)
.ForwardAsSingle(o => $"--package-directory {o.Arguments.Single()}")));
}
public static IEnumerable<string> QueryNuGet(string match)
{
var httpClient = new HttpClient();
string result;
try
{
var cancellation = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var response = httpClient.GetAsync($"https://api-v2v3search-0.nuget.org/query?q={match}&skip=0&take=100&prerelease=true", cancellation.Token)
.Result;
result = response.Content.ReadAsStringAsync().Result;
}
catch (Exception)
{
yield break;
}
var json = JObject.Parse(result);
foreach (var id in json["data"])
{
yield return id["id"].Value<string>();
}
}
}
}

View file

@ -0,0 +1,26 @@
// 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 Microsoft.DotNet.Cli.CommandLine;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.ProjectToProjectReference.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
internal static class AddProjectToProjectReferenceParser
{
public static Command AddProjectReference()
{
return Create.Command(
"reference",
LocalizableStrings.AppFullName,
Accept.OneOrMoreArguments()
.With(name: "args",
description: LocalizableStrings.AppHelpText),
CommonOptions.HelpOption(),
Create.Option("-f|--framework", LocalizableStrings.CmdFrameworkDescription,
Accept.ExactlyOneArgument()
.WithSuggestionsFrom(_ => Suggest.TargetFrameworksFromProjectFile())
.With(name: Tools.Add.PackageReference.LocalizableStrings.CmdFramework)));
}
}
}

View file

@ -3,6 +3,7 @@
using System.Linq;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Build.LocalizableStrings;
namespace Microsoft.DotNet.Cli
@ -14,7 +15,7 @@ namespace Microsoft.DotNet.Cli
"build",
LocalizableStrings.AppFullName,
Accept.ZeroOrMoreArguments()
.With(name: "PROJECT",
.With(name: CommonLocalizableStrings.CmdProjectFile,
description:
"The MSBuild project file to build. If a project file is not specified, MSBuild searches the current working directory for a file that has a file extension that ends in `proj` and uses that file."),
CommonOptions.HelpOption(),

View file

@ -2,7 +2,8 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Microsoft.DotNet.Cli.CommandLine;
using LocalizableStrings = Microsoft.DotNet.Tools.List.ProjectToProjectReferences.LocalizableStrings;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.List.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
@ -10,15 +11,15 @@ namespace Microsoft.DotNet.Cli
{
public static Command List() =>
Create.Command("list",
".NET List Command",
LocalizableStrings.NetListCommand,
Accept.ZeroOrOneArgument()
.With(name: "PROJECT",
.With(name: CommonLocalizableStrings.CmdProjectFile,
description:
"The project file to operate on. If a file is not specified, the command will search the current directory for one.")
CommonLocalizableStrings.ArgumentsProjectDescription)
.DefaultToCurrentDirectory(),
CommonOptions.HelpOption(),
Create.Command("reference",
LocalizableStrings.AppFullName,
Tools.List.ProjectToProjectReferences.LocalizableStrings.AppFullName,
Accept.ZeroOrOneArgument(),
CommonOptions.HelpOption()));
}

View file

@ -15,34 +15,32 @@ namespace Microsoft.DotNet.Cli
"migrate",
".NET Migrate Command",
Accept.ZeroOrOneArgument()
.MaterializeAs(o =>
{
return new MigrateCommand(
o.ValueOrDefault<string>("--template-file"),
o.Arguments.FirstOrDefault(),
o.ValueOrDefault<string>("--sdk-package-version"),
o.ValueOrDefault<string>("--xproj-file"),
o.ValueOrDefault<string>("--report-file"),
o.ValueOrDefault<bool>("--skip-project-references"),
o.ValueOrDefault<bool>("--format-report-file-json"),
o.ValueOrDefault<bool>("--skip-backup"));
})
.With(name: LocalizableStrings.CmdProjectArgument,
description: LocalizableStrings.CmdProjectArgumentDescription),
.MaterializeAs(o =>
new MigrateCommand(
o.ValueOrDefault<string>("--template-file"),
o.Arguments.FirstOrDefault(),
o.ValueOrDefault<string>("--sdk-package-version"),
o.ValueOrDefault<string>("--xproj-file"),
o.ValueOrDefault<string>("--report-file"),
o.ValueOrDefault<bool>("--skip-project-references"),
o.ValueOrDefault<bool>("--format-report-file-json"),
o.ValueOrDefault<bool>("--skip-backup")))
.With(name: LocalizableStrings.CmdProjectArgument,
description: LocalizableStrings.CmdProjectArgumentDescription),
CommonOptions.HelpOption(),
Create.Option("-t|--template-file",
LocalizableStrings.CmdTemplateDescription),
LocalizableStrings.CmdTemplateDescription),
Create.Option("-v|--sdk-package-version",
LocalizableStrings.CmdVersionDescription),
LocalizableStrings.CmdVersionDescription),
Create.Option("-x|--xproj-file",
LocalizableStrings.CmdXprojFileDescription),
LocalizableStrings.CmdXprojFileDescription),
Create.Option("-s|--skip-project-references",
LocalizableStrings.CmdSkipProjectReferencesDescription),
LocalizableStrings.CmdSkipProjectReferencesDescription),
Create.Option("-r|--report-file",
LocalizableStrings.CmdReportFileDescription),
LocalizableStrings.CmdReportFileDescription),
Create.Option("--format-report-file-json",
LocalizableStrings.CmdReportOutputDescription),
LocalizableStrings.CmdReportOutputDescription),
Create.Option("--skip-backup",
LocalizableStrings.CmdSkipBackupDescription));
LocalizableStrings.CmdSkipBackupDescription));
}
}

View file

@ -3,7 +3,7 @@
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Remove.ProjectToProjectReference.LocalizableStrings;
using LocalizableStrings = Microsoft.DotNet.Tools.Remove.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
@ -11,30 +11,14 @@ namespace Microsoft.DotNet.Cli
{
public static Command Remove() =>
Create.Command("remove",
".NET Remove Command",
LocalizableStrings.NetRemoveCommand,
Accept.ExactlyOneArgument()
.DefaultToCurrentDirectory()
.With(name: "PROJECT",
.With(name: CommonLocalizableStrings.CmdProjectFile,
description: CommonLocalizableStrings.ArgumentsProjectDescription)
.DefaultToCurrentDirectory(),
CommonOptions.HelpOption(),
Create.Command(
"package",
LocalizableStrings.AppFullName,
CommonOptions.HelpOption()),
Create.Command(
"reference",
LocalizableStrings.AppFullName,
Accept
.OneOrMoreArguments()
.WithSuggestionsFrom(_ => Suggest.ProjectReferencesFromProjectFile())
.With(name: "args",
description: LocalizableStrings.AppHelpText),
CommonOptions.HelpOption(),
Create.Option(
"-f|--framework",
"Remove reference only when targeting a specific framework",
Accept.ExactlyOneArgument()
.With(name: "FRAMEWORK"))));
RemovePackageParser.RemovePackage(),
RemoveProjectToProjectReferenceParser.RemoveReference());
}
}

View file

@ -0,0 +1,14 @@
using Microsoft.DotNet.Cli.CommandLine;
using LocalizableStrings = Microsoft.DotNet.Tools.Remove.ProjectToProjectReference.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
internal static class RemovePackageParser
{
public static Command RemovePackage() =>
Create.Command(
"package",
LocalizableStrings.AppFullName,
CommonOptions.HelpOption());
}
}

View file

@ -0,0 +1,25 @@
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Remove.ProjectToProjectReference.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
internal static class RemoveProjectToProjectReferenceParser
{
public static Command RemoveReference() =>
Create.Command(
"reference",
LocalizableStrings.AppFullName,
Accept
.OneOrMoreArguments()
.WithSuggestionsFrom(_ => Suggest.ProjectReferencesFromProjectFile())
.With(name: "args",
description: LocalizableStrings.AppHelpText),
CommonOptions.HelpOption(),
Create.Option(
"-f|--framework",
LocalizableStrings.CmdFrameworkDescription,
Accept.ExactlyOneArgument()
.With(name: CommonLocalizableStrings.CmdFramework)));
}
}

View file

@ -16,14 +16,10 @@ namespace Microsoft.DotNet.Tools.Run
public string Configuration { get; set; }
public string Framework { get; set; }
public string Project { get; set; }
public IReadOnlyList<string> Args { get; set; }
public IReadOnlyCollection<string> Args { get; set; }
private List<string> _args;
public RunCommand()
{
}
public int Start()
{
Initialize();

View file

@ -1,7 +1,6 @@
// 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.Tools.Run;
using LocalizableStrings = Microsoft.DotNet.Tools.Run.LocalizableStrings;
@ -14,27 +13,28 @@ namespace Microsoft.DotNet.Cli
Create.Command(
"run",
LocalizableStrings.AppFullName,
Accept.ZeroOrMoreArguments()
.MaterializeAs(o =>
{
return new RunCommand()
{
Configuration = o.SingleArgumentOrDefault("--configuration"),
Framework = o.SingleArgumentOrDefault("--framework"),
Project = o.SingleArgumentOrDefault("--project"),
Args = (IReadOnlyList<string>)o.Arguments
};
}),
CommonOptions.HelpOption(),
CommonOptions.ConfigurationOption(),
CommonOptions.FrameworkOption(),
Create.Option(
"-p|--project",
LocalizableStrings.CommandOptionProjectDescription,
Accept.ExactlyOneArgument()),
Create.Option(
"--no-build",
LocalizableStrings.CommandOptionNoBuildDescription,
Accept.NoArguments().ForwardAs("/p:NoBuild=true")));
treatUnmatchedTokensAsErrors: false,
arguments: Accept.ZeroOrMoreArguments()
.MaterializeAs(o => new RunCommand
{
Configuration = o.SingleArgumentOrDefault("--configuration"),
Framework = o.SingleArgumentOrDefault("--framework"),
Project = o.SingleArgumentOrDefault("--project"),
Args = o.Arguments
}),
options: new[]
{
CommonOptions.HelpOption(),
CommonOptions.ConfigurationOption(),
CommonOptions.FrameworkOption(),
Create.Option(
"-p|--project",
LocalizableStrings.CommandOptionProjectDescription,
Accept.ExactlyOneArgument()),
Create.Option(
"--no-build",
LocalizableStrings.CommandOptionNoBuildDescription,
Accept.NoArguments().ForwardAs("/p:NoBuild=true"))
});
}
}
}

View file

@ -12,27 +12,14 @@ namespace Microsoft.DotNet.Cli
public static Command Sln() =>
Create.Command(
"sln",
".NET modify solution file command",
LocalizableStrings.AppFullName,
Accept.ExactlyOneArgument()
.DefaultToCurrentDirectory()
.With(name: "SLN_FILE",
.With(name: CommonLocalizableStrings.CmdSlnFile,
description: CommonLocalizableStrings.ArgumentsSolutionDescription),
CommonOptions.HelpOption(),
Create.Command("add",
".NET Add project(s) to a solution file Command",
Accept.OneOrMoreArguments(o => CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd)
.With(name: "args",
description: LocalizableStrings.AddSubcommandHelpText),
CommonOptions.HelpOption()),
Create.Command("list",
".NET List project(s) in a solution file Command",
CommonOptions.HelpOption()),
Create.Command("remove",
".NET Remove project(s) from a solution file Command",
Accept.OneOrMoreArguments(o => CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove)
.With(name: "args",
description: LocalizableStrings.RemoveSubcommandHelpText),
CommonOptions.HelpOption()));
SlnAddParser.SlnAdd(),
SlnListParser.SlnList(),
SlnRemoveParser.SlnRemove());
}
}

View file

@ -0,0 +1,20 @@
// 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 Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Sln.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
public static class SlnAddParser
{
public static Command SlnAdd() =>
Create.Command("add",
LocalizableStrings.AddAppFullName,
Accept.OneOrMoreArguments(o => CommonLocalizableStrings.SpecifyAtLeastOneProjectToAdd)
.With(name: "args",
description: LocalizableStrings.AddSubcommandHelpText),
CommonOptions.HelpOption());
}
}

View file

@ -0,0 +1,16 @@
// 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 Microsoft.DotNet.Cli.CommandLine;
using LocalizableStrings = Microsoft.DotNet.Tools.Sln.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
public static class SlnListParser
{
public static Command SlnList() =>
Create.Command("list",
LocalizableStrings.ListAppFullName,
CommonOptions.HelpOption());
}
}

View file

@ -0,0 +1,20 @@
// 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 Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Tools;
using LocalizableStrings = Microsoft.DotNet.Tools.Sln.LocalizableStrings;
namespace Microsoft.DotNet.Cli
{
public static class SlnRemoveParser
{
public static Command SlnRemove() =>
Create.Command("remove",
LocalizableStrings.RemoveAppFullName,
Accept.OneOrMoreArguments(o => CommonLocalizableStrings.SpecifyAtLeastOneProjectToRemove)
.With(name: "args",
description: LocalizableStrings.RemoveSubcommandHelpText),
CommonOptions.HelpOption());
}
}