2015-11-16 19:21:57 +00: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.
2015-11-12 11:50:38 +00:00
using System ;
using System.IO ;
using System.Linq ;
2016-04-20 03:51:32 +00:00
using Microsoft.DotNet.Cli.CommandLine ;
2015-11-12 11:50:38 +00:00
using Microsoft.DotNet.Cli.Utils ;
2016-04-20 03:51:32 +00:00
using Microsoft.DotNet.ProjectModel ;
2016-05-02 17:21:12 +00:00
using Microsoft.DotNet.Tools.Common ;
2016-01-06 19:06:15 +00:00
using Microsoft.DotNet.Tools.Pack ;
2015-11-12 11:50:38 +00:00
namespace Microsoft.DotNet.Tools.Compiler
{
2016-01-31 05:47:50 +00:00
public class PackCommand
2015-11-12 11:50:38 +00:00
{
2016-01-31 05:47:50 +00:00
public static int Run ( string [ ] args )
2015-11-12 11:50:38 +00:00
{
DebugHelper . HandleDebugSwitch ( ref args ) ;
var app = new CommandLineApplication ( ) ;
2015-12-01 00:30:28 +00:00
app . Name = "dotnet pack" ;
app . FullName = ".NET Packager" ;
app . Description = "Packager for the .NET Platform" ;
2015-11-12 11:50:38 +00:00
app . HelpOption ( "-h|--help" ) ;
var output = app . Option ( "-o|--output <OUTPUT_DIR>" , "Directory in which to place outputs" , CommandOptionType . SingleValue ) ;
2016-02-09 20:31:22 +00:00
var noBuild = app . Option ( "--no-build" , "Do not build project before packing" , CommandOptionType . NoValue ) ;
2016-02-03 18:57:25 +00:00
var buildBasePath = app . Option ( "-b|--build-base-path <OUTPUT_DIR>" , "Directory in which to place temporary build outputs" , CommandOptionType . SingleValue ) ;
2015-11-12 11:50:38 +00:00
var configuration = app . Option ( "-c|--configuration <CONFIGURATION>" , "Configuration under which to build" , CommandOptionType . SingleValue ) ;
2015-12-05 02:03:57 +00:00
var versionSuffix = app . Option ( "--version-suffix <VERSION_SUFFIX>" , "Defines what `*` should be replaced with in version field in project.json" , CommandOptionType . SingleValue ) ;
2016-01-06 19:06:15 +00:00
var path = app . Argument ( "<PROJECT>" , "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory" ) ;
2015-11-12 11:50:38 +00:00
app . OnExecute ( ( ) = >
{
// Locate the project and get the name and full path
2016-01-06 19:06:15 +00:00
var pathValue = path . Value ;
if ( string . IsNullOrEmpty ( pathValue ) )
2015-11-12 11:50:38 +00:00
{
2016-01-06 19:06:15 +00:00
pathValue = Directory . GetCurrentDirectory ( ) ;
2015-11-12 11:50:38 +00:00
}
2016-02-18 10:27:35 +00:00
if ( ! pathValue . EndsWith ( Project . FileName ) )
2015-12-01 00:30:28 +00:00
{
2016-01-06 19:06:15 +00:00
pathValue = Path . Combine ( pathValue , Project . FileName ) ;
2015-12-01 00:30:28 +00:00
}
2016-02-18 10:27:35 +00:00
if ( ! File . Exists ( pathValue ) )
2015-12-01 00:30:28 +00:00
{
2016-01-06 19:06:15 +00:00
Reporter . Error . WriteLine ( $"Unable to find a project.json in {pathValue}" ) ;
2015-12-01 00:30:28 +00:00
return 1 ;
}
2016-01-03 16:18:25 +00:00
// Set defaults based on the environment
2016-05-02 18:32:24 +00:00
var workspace = BuildWorkspace . Create ( versionSuffix . Value ( ) ) ;
2015-12-05 02:03:57 +00:00
2015-11-12 11:50:38 +00:00
var configValue = configuration . Value ( ) ? ? Cli . Utils . Constants . DefaultConfiguration ;
var outputValue = output . Value ( ) ;
2016-05-02 17:21:12 +00:00
var buildBasePathValue = PathUtility . GetFullPath ( buildBasePath . Value ( ) ) ;
2016-01-06 19:06:15 +00:00
2016-05-02 18:32:24 +00:00
var contexts = workspace . GetProjectContextCollection ( pathValue ) . FrameworkOnlyContexts ;
2016-01-06 19:06:15 +00:00
var project = contexts . First ( ) . ProjectFile ;
2016-02-03 18:57:25 +00:00
var artifactPathsCalculator = new ArtifactPathsCalculator ( project , buildBasePathValue , outputValue , configValue ) ;
2016-01-06 19:06:15 +00:00
var packageBuilder = new PackagesGenerator ( contexts , artifactPathsCalculator , configValue ) ;
2015-11-12 11:50:38 +00:00
2016-02-09 20:31:22 +00:00
int buildResult = 0 ;
if ( ! noBuild . HasValue ( ) )
{
2016-05-02 18:32:24 +00:00
var buildProjectCommand = new BuildProjectCommand ( project , buildBasePathValue , configValue , workspace ) ;
2016-02-09 20:31:22 +00:00
buildResult = buildProjectCommand . Execute ( ) ;
}
2016-01-06 19:06:15 +00:00
return buildResult ! = 0 ? buildResult : packageBuilder . Build ( ) ;
2015-11-12 11:50:38 +00:00
} ) ;
try
{
return app . Execute ( args ) ;
}
catch ( Exception ex )
{
#if DEBUG
Console . Error . WriteLine ( ex ) ;
#else
Console . Error . WriteLine ( ex . Message ) ;
#endif
return 1 ;
}
2016-02-18 10:27:35 +00:00
}
2015-11-12 11:50:38 +00:00
}
}