dotnet-installer/build_projects/dotnet-host-build/StubPackageBuilder.cs

76 lines
2.5 KiB
C#
Raw Normal View History

2016-05-12 00:20:40 +00: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;
private bool _dummyFileCreated;
2016-05-12 00:20:40 +00:00
public StubPackageBuilder(DotNetCli dotnet, string intermediateDirectory, string outputDirectory)
{
_dotnet = dotnet;
_intermediateDirectory = intermediateDirectory;
_outputDirectory = outputDirectory;
}
public void GeneratePackage(string packageId, string version)
{
if (!_dummyFileCreated)
2016-05-12 00:20:40 +00:00
{
CreateDummyFile(_dotnet, _intermediateDirectory);
2016-05-12 00:20:40 +00:00
}
CreateStubPackage(_dotnet, packageId, version, _intermediateDirectory, _outputDirectory);
}
private void CreateDummyFile(DotNetCli dotnet, string intermediateDirectory)
2016-05-12 00:20:40 +00:00
{
var dummyTxt = "dummy text";
2016-05-12 00:20:40 +00:00
var tempPjDirectory = Path.Combine(intermediateDirectory, "dummyNuGetPackageIntermediate");
FS.Rmdir(tempPjDirectory);
Directory.CreateDirectory(tempPjDirectory);
var dummyTextFile = Path.Combine(tempPjDirectory, "dummy.txt");
2016-05-12 00:20:40 +00:00
File.WriteAllText(dummyTextFile, dummyTxt);
2016-05-12 00:20:40 +00:00
_dummyFileCreated = true;
2016-05-12 00:20:40 +00: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}\",");
projectJson.Append(" \"packOptions\": { \"files\": { \"include\": \"dummy.txt\" } },");
projectJson.Append(" \"frameworks\": { \"netcoreapp1.0\": { } },");
2016-05-12 00:20:40 +00: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();
}
}
}