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 ;
2015-11-28 00:19:54 +00:00
using Microsoft.DotNet.ProjectModel ;
2015-11-12 11:50:38 +00:00
using Microsoft.Dnx.Runtime.Common.CommandLine ;
using Microsoft.DotNet.Cli.Utils ;
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-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-01-06 19:06:15 +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-01-06 19:06:15 +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
var settings = new ProjectReaderSettings ( ) ;
settings . VersionSuffix = Environment . GetEnvironmentVariable ( "DOTNET_BUILD_VERSION" ) ;
settings . AssemblyFileVersion = Environment . GetEnvironmentVariable ( "DOTNET_ASSEMBLY_FILE_VERSION" ) ;
2015-12-05 02:03:57 +00:00
if ( versionSuffix . HasValue ( ) )
{
settings . VersionSuffix = versionSuffix . Value ( ) ;
}
2016-01-06 19:06:15 +00:00
var contexts = ProjectContext . CreateContextForEachFramework ( pathValue , settings ) ;
2015-11-12 11:50:38 +00:00
var configValue = configuration . Value ( ) ? ? Cli . Utils . Constants . DefaultConfiguration ;
var outputValue = output . Value ( ) ;
2016-02-03 18:57:25 +00:00
var buildBasePathValue = buildBasePath . Value ( ) ;
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 ) ;
var buildProjectCommand = new BuildProjectCommand ( project , artifactPathsCalculator , buildBasePathValue , 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-01-06 19:06:15 +00:00
var buildResult = buildProjectCommand . Execute ( ) ;
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-01-06 19:06:15 +00:00
}
2015-11-12 11:50:38 +00:00
}
}