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.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Text ;
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 ;
2015-12-14 16:33:11 +00:00
using Microsoft.DotNet.Cli.Compiler.Common ;
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" ) ;
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 ) ;
2015-11-12 11:50:38 +00:00
var project = app . Argument ( "<PROJECT>" , "The project to compile, defaults to the current directory. Can be a path to a project.json or a project directory" ) ;
app . OnExecute ( ( ) = >
{
// Locate the project and get the name and full path
var path = project . Value ;
if ( string . IsNullOrEmpty ( path ) )
{
path = Directory . GetCurrentDirectory ( ) ;
}
2015-12-01 00:30:28 +00:00
if ( ! path . EndsWith ( Project . FileName ) )
{
path = Path . Combine ( path , Project . FileName ) ;
}
if ( ! File . Exists ( path ) )
{
Reporter . Error . WriteLine ( $"Unable to find a project.json in {path}" ) ;
return 1 ;
}
2015-12-09 08:47:57 +00:00
ProjectReaderSettings settings = null ;
2015-12-05 02:03:57 +00:00
if ( versionSuffix . HasValue ( ) )
{
2015-12-09 08:47:57 +00:00
settings = new ProjectReaderSettings ( ) ;
2015-12-05 02:03:57 +00:00
settings . VersionSuffix = versionSuffix . Value ( ) ;
}
2015-11-12 11:50:38 +00:00
var configValue = configuration . Value ( ) ? ? Cli . Utils . Constants . DefaultConfiguration ;
var outputValue = output . Value ( ) ;
2015-12-05 02:03:57 +00:00
return TryBuildPackage ( path , configValue , outputValue , intermediateOutput . Value ( ) , settings ) ? 0 : 1 ;
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 ;
}
}
2015-12-09 08:47:57 +00:00
private static bool TryBuildPackage ( string path , string configuration , string outputValue , string intermediateOutputValue , ProjectReaderSettings settings = null )
2015-11-12 11:50:38 +00:00
{
2015-12-05 02:03:57 +00:00
var contexts = ProjectContext . CreateContextForEachFramework ( path , settings ) ;
2015-11-12 11:50:38 +00:00
var project = contexts . First ( ) . ProjectFile ;
if ( project . Files . SourceFiles . Any ( ) )
{
var argsBuilder = new StringBuilder ( ) ;
argsBuilder . Append ( $"--configuration {configuration}" ) ;
if ( ! string . IsNullOrEmpty ( outputValue ) )
{
argsBuilder . Append ( $" --output \" { outputValue } \ "" ) ;
}
if ( ! string . IsNullOrEmpty ( intermediateOutputValue ) )
{
argsBuilder . Append ( $" --temp-output \" { intermediateOutputValue } \ "" ) ;
}
argsBuilder . Append ( $" \" { path } \ "" ) ;
var result = Command . Create ( "dotnet-compile" , argsBuilder . ToString ( ) )
. ForwardStdOut ( )
. ForwardStdErr ( )
. Execute ( ) ;
if ( result . ExitCode ! = 0 )
{
return false ;
}
}
var packDiagnostics = new List < DiagnosticMessage > ( ) ;
2015-12-14 23:49:40 +00:00
var mainPackageGenerator = new PackageGenerator ( project , configuration , outputValue ) ;
var symbolsPackageGenerator = new SymbolPackageGenerator ( project , configuration , outputValue ) ;
2015-11-12 11:50:38 +00:00
2015-12-14 23:49:40 +00:00
return mainPackageGenerator . BuildPackage ( contexts , packDiagnostics ) & &
symbolsPackageGenerator . BuildPackage ( contexts , packDiagnostics ) ;
2015-11-12 11:50:38 +00:00
}
}
}