2016-01-04 12:36:46 -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.
2016-10-27 18:46:43 -07:00
using System.Collections.Generic ;
2016-04-19 22:51:32 -05:00
using Microsoft.DotNet.Cli.CommandLine ;
2016-01-04 12:36:46 -08:00
using Microsoft.DotNet.Cli.Utils ;
2016-10-27 18:46:43 -07:00
using Microsoft.DotNet.Tools.MSBuild ;
2017-02-22 11:43:03 -08:00
using Microsoft.DotNet.Cli ;
using System.Diagnostics ;
2016-01-04 12:36:46 -08:00
namespace Microsoft.DotNet.Tools.Restore
{
2016-10-27 18:46:43 -07:00
public class RestoreCommand
2016-01-04 12:36:46 -08:00
{
2017-02-22 11:43:03 -08:00
private MSBuildForwardingApp _forwardingApp ;
public RestoreCommand ( IEnumerable < string > msbuildArgs , string msbuildPath = null )
{
_forwardingApp = new MSBuildForwardingApp ( msbuildArgs , msbuildPath ) ;
}
public static RestoreCommand FromArgs ( string [ ] args , string msbuildPath = null )
2016-01-04 12:36:46 -08:00
{
DebugHelper . HandleDebugSwitch ( ref args ) ;
2016-10-27 18:46:43 -07:00
CommandLineApplication cmd = new CommandLineApplication ( throwOnUnexpectedArg : false )
{
Name = "restore" ,
2016-11-19 11:45:46 -08:00
FullName = LocalizableStrings . AppFullName ,
Description = LocalizableStrings . AppDescription ,
2016-12-19 22:11:39 -08:00
HandleRemainingArguments = true ,
2016-10-27 18:46:43 -07:00
ArgumentSeparatorHelpText = HelpMessageStrings . MSBuildAdditionalArgsHelpText
} ;
cmd . HelpOption ( "-h|--help" ) ;
var argRoot = cmd . Argument (
2016-12-06 10:04:11 -08:00
$"[{LocalizableStrings.CmdArgument}]" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdArgumentDescription ,
2016-10-27 18:46:43 -07:00
multipleValues : true ) ;
var sourceOption = cmd . Option (
2016-11-30 13:03:24 -08:00
$"-s|--source <{LocalizableStrings.CmdSourceOption}>" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdSourceOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . MultipleValue ) ;
2016-12-18 22:42:54 -06:00
var runtimeOption = cmd . Option (
$"-r|--runtime <{LocalizableStrings.CmdRuntimeOption}>" ,
LocalizableStrings . CmdRuntimeOptionDescription ,
CommandOptionType . MultipleValue ) ;
2016-10-27 18:46:43 -07:00
var packagesOption = cmd . Option (
2016-11-30 13:03:24 -08:00
$"--packages <{LocalizableStrings.CmdPackagesOption}>" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdPackagesOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . SingleValue ) ;
var disableParallelOption = cmd . Option (
"--disable-parallel" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdDisableParallelOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . NoValue ) ;
var configFileOption = cmd . Option (
2016-11-30 13:03:24 -08:00
$"--configfile <{LocalizableStrings.CmdConfigFileOption}>" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdConfigFileOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . SingleValue ) ;
var noCacheOption = cmd . Option (
"--no-cache" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdNoCacheOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . NoValue ) ;
var ignoreFailedSourcesOption = cmd . Option (
"--ignore-failed-sources" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdIgnoreFailedSourcesOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . NoValue ) ;
var noDependenciesOption = cmd . Option (
"--no-dependencies" ,
2016-11-19 11:45:46 -08:00
LocalizableStrings . CmdNoDependenciesOptionDescription ,
2016-10-27 18:46:43 -07:00
CommandOptionType . NoValue ) ;
2016-11-15 11:56:39 -08:00
CommandOption verbosityOption = MSBuildForwardingApp . AddVerbosityOption ( cmd ) ;
2017-02-22 11:43:03 -08:00
List < string > msbuildArgs = null ;
2016-10-27 18:46:43 -07:00
cmd . OnExecute ( ( ) = >
{
2017-02-22 11:43:03 -08:00
msbuildArgs = new List < string > ( )
2016-10-27 18:46:43 -07:00
{
"/NoLogo" ,
"/t:Restore" ,
"/ConsoleLoggerParameters:Verbosity=Minimal"
} ;
if ( sourceOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreSources={string.Join(" % 3 B ", sourceOption.Values)}" ) ;
}
2016-12-18 22:42:54 -06:00
if ( runtimeOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RuntimeIdentifiers={string.Join(" % 3 B ", runtimeOption.Values)}" ) ;
}
2016-10-27 18:46:43 -07:00
if ( packagesOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestorePackagesPath={packagesOption.Value()}" ) ;
}
if ( disableParallelOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreDisableParallel=true" ) ;
}
if ( configFileOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreConfigFile={configFileOption.Value()}" ) ;
}
if ( noCacheOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreNoCache=true" ) ;
}
if ( ignoreFailedSourcesOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreIgnoreFailedSources=true" ) ;
}
if ( noDependenciesOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/p:RestoreRecursive=false" ) ;
}
2016-11-15 11:56:39 -08:00
if ( verbosityOption . HasValue ( ) )
{
msbuildArgs . Add ( $"/verbosity:{verbosityOption.Value()}" ) ;
}
2016-10-27 18:46:43 -07:00
// Add in arguments
msbuildArgs . AddRange ( argRoot . Values ) ;
// Add remaining arguments that the parser did not understand
msbuildArgs . AddRange ( cmd . RemainingArguments ) ;
2017-02-22 11:43:03 -08:00
return 0 ;
2016-10-27 18:46:43 -07:00
} ) ;
2017-02-22 11:43:03 -08:00
int exitCode = cmd . Execute ( args ) ;
if ( msbuildArgs = = null )
{
throw new CommandCreationException ( exitCode ) ;
}
return new RestoreCommand ( msbuildArgs , msbuildPath ) ;
}
public static int Run ( string [ ] args )
{
DebugHelper . HandleDebugSwitch ( ref args ) ;
RestoreCommand cmd ;
try
{
cmd = FromArgs ( args ) ;
}
catch ( CommandCreationException e )
{
return e . ExitCode ;
}
return cmd . Execute ( ) ;
}
public ProcessStartInfo GetProcessStartInfo ( )
{
return _forwardingApp . GetProcessStartInfo ( ) ;
}
public int Execute ( )
{
return GetProcessStartInfo ( ) . Execute ( ) ;
2016-01-04 12:36:46 -08:00
}
}
2016-01-12 16:36:31 -08:00
}