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
{
public class Program
{
public static int Main ( string [ ] args )
{
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" ) ;
2016-01-06 19:06:15 +00:00
var input = app . Option ( "-i|--input <INPUT_DIR>" , "Directory from where the assets to be packages are going to be picked up" , CommandOptionType . SingleValue ) ;
2015-11-12 11:50:38 +00:00
var output = app . Option ( "-o|--output <OUTPUT_DIR>" , "Directory in which to place outputs" , CommandOptionType . SingleValue ) ;
var intermediateOutput = app . Option ( "-t|--temp-output <OUTPUT_DIR>" , "Directory in which to place temporary outputs" , CommandOptionType . SingleValue ) ;
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 ;
2016-01-06 19:06:15 +00:00
var inputValue = input . Value ( ) ;
2015-11-12 11:50:38 +00:00
var outputValue = output . Value ( ) ;
2016-01-06 19:06:15 +00:00
var intermediateOutputValue = intermediateOutput . Value ( ) ;
var project = contexts . First ( ) . ProjectFile ;
var artifactPathsCalculator = new ArtifactPathsCalculator ( project , inputValue , outputValue , configValue ) ;
var buildProjectCommand = new BuildProjectCommand ( project , artifactPathsCalculator , intermediateOutputValue , configValue ) ;
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
}
}