2016-05-11 17:20:40 -07:00
using System.IO ;
using System.Text ;
using Microsoft.DotNet.Cli.Build ;
namespace Microsoft.DotNet.Host.Build
{
public class StubPackageBuilder
{
private DotNetCli _dotnet ;
private string _intermediateDirectory ;
private string _outputDirectory ;
2016-05-19 10:46:46 -05:00
private bool _dummyFileCreated ;
2016-05-11 17:20:40 -07:00
public StubPackageBuilder ( DotNetCli dotnet , string intermediateDirectory , string outputDirectory )
{
_dotnet = dotnet ;
_intermediateDirectory = intermediateDirectory ;
_outputDirectory = outputDirectory ;
}
public void GeneratePackage ( string packageId , string version )
{
2016-05-19 10:46:46 -05:00
if ( ! _dummyFileCreated )
2016-05-11 17:20:40 -07:00
{
2016-05-19 10:46:46 -05:00
CreateDummyFile ( _dotnet , _intermediateDirectory ) ;
2016-05-11 17:20:40 -07:00
}
CreateStubPackage ( _dotnet , packageId , version , _intermediateDirectory , _outputDirectory ) ;
}
2016-05-19 10:46:46 -05:00
private void CreateDummyFile ( DotNetCli dotnet , string intermediateDirectory )
2016-05-11 17:20:40 -07:00
{
2016-05-19 10:46:46 -05:00
var dummyTxt = "dummy text" ;
2016-05-11 17:20:40 -07:00
var tempPjDirectory = Path . Combine ( intermediateDirectory , "dummyNuGetPackageIntermediate" ) ;
FS . Rmdir ( tempPjDirectory ) ;
Directory . CreateDirectory ( tempPjDirectory ) ;
2016-05-19 10:46:46 -05:00
var dummyTextFile = Path . Combine ( tempPjDirectory , "dummy.txt" ) ;
2016-05-11 17:20:40 -07:00
2016-05-19 10:46:46 -05:00
File . WriteAllText ( dummyTextFile , dummyTxt ) ;
2016-05-11 17:20:40 -07:00
2016-05-19 10:46:46 -05:00
_dummyFileCreated = true ;
2016-05-11 17:20:40 -07:00
}
private static void CreateStubPackage ( DotNetCli dotnet ,
string packageId ,
string version ,
string intermediateDirectory ,
string outputDirectory )
{
var projectJson = new StringBuilder ( ) ;
projectJson . Append ( "{" ) ;
projectJson . Append ( $" \" version \ ": \"{version}\"," ) ;
projectJson . Append ( $" \" name \ ": \"{packageId}\"," ) ;
2016-05-19 10:46:46 -05:00
projectJson . Append ( " \"packOptions\": { \"files\": { \"include\": \"dummy.txt\" } }," ) ;
2016-05-18 10:51:44 -05:00
projectJson . Append ( " \"frameworks\": { \"netcoreapp1.0\": { } }," ) ;
2016-05-11 17:20:40 -07:00
projectJson . Append ( "}" ) ;
var tempPjDirectory = Path . Combine ( intermediateDirectory , "dummyNuGetPackageIntermediate" ) ;
var tempPjFile = Path . Combine ( tempPjDirectory , "project.json" ) ;
File . WriteAllText ( tempPjFile , projectJson . ToString ( ) ) ;
dotnet . Pack (
tempPjFile , "--no-build" ,
"--output" , outputDirectory )
. WorkingDirectory ( tempPjDirectory )
. Execute ( )
. EnsureSuccessful ( ) ;
}
}
}