ln.build/ln.build/pipeline/DotNetPipeLine.cs

120 lines
3.9 KiB
C#

using System;
using System.IO;
using System.Reflection.Metadata.Ecma335;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Text.RegularExpressions;
using LibGit2Sharp.Handlers;
using ln.build.commands;
using ln.logging;
using ln.type;
namespace ln.build
{
public class DotNetPipeLine : PipeLine
{
static Regex regexPackages = new Regex(".*(Successfully created package '(.*)').*");
public DotNetPipeLine()
{
AddStep(new RestoreStep());
AddStep(new CleanStep());
BuildStep buildStep = new BuildStep();
buildStep.OnCommandExited += FilterPackageFilenames;
AddStep(buildStep);
AddStep(new TestStep());
PackStep packStep = new PackStep();
packStep.OnCommandExited += FilterPackageFilenames;
AddStep(packStep);
AddStep(new PublishStep());
}
public override bool DetectValidity(CIJob job)
{
string[] sln = Directory.GetFileSystemEntries(job.WorkingDirectory,"*.sln");
if (sln.Length > 0)
return true;
sln = Directory.GetFileSystemEntries(job.WorkingDirectory,"*.csproj");
if (sln.Length > 0)
return true;
return false;
}
void FilterPackageFilenames(CommandStep commandStep, CIJob job, int exitCode)
{
Stream outStream = job.GetLogStream(commandStep.DefaultLogFileName);
outStream.Position = 0;
job.Logger.Log(LogLevel.INFO,"filterPackageFilenames(): started");
String consoleOutput = Encoding.UTF8.GetString(outStream.ReadToEnd());
MatchCollection matches = regexPackages.Matches(consoleOutput);
foreach (Match match in matches)
{
Logging.Log(LogLevel.INFO,"filterPackageFilenames(): {0}", match.Groups[2].Value);
job.Environment.Extend("DOTNET_PACKAGES", match.Groups[2].Value);
}
}
class CleanStep : CommandStep
{
public CleanStep():base("clean", "dotnet","clean"){ }
}
class RestoreStep : CommandStep
{
public RestoreStep():base("restore", "dotnet","restore"){ }
}
class TestStep : CommandStep
{
public TestStep():base("test", "dotnet","test"){ }
}
class BuildStep : CommandStep
{
public BuildStep():base("build", "dotnet","build"){ }
}
class PackStep : CommandStep
{
public PackStep():base("pack", "dotnet","pack"){ }
}
class PublishStep : CommandStep
{
CommandRunner.Argument FileName { get; } = new CommandRunner.Argument(null);
CommandRunner.Option NugetApiKey { get; } = new CommandRunner.Option("-k", (e) => e.Get("NUGET_APIKEY"), true);
CommandRunner.Option NugetSource { get; } = new CommandRunner.Option("-s", (e) => e.Get("NUGET_SOURCE"));
public PublishStep():base("push", "dotnet","nuget", "push" )
{
CommandRunner.AddArguments(FileName, NugetApiKey, NugetSource);
}
public override int Run(CIJob job)
{
bool success = true;
// CommandRunner.Arguments[4] = "http://nuget.l--n.de/nuget/l--n/v3/index.json";
// CommandRunner.Arguments[6] = "3yAJPMxcaEhb_HP62dxK";
foreach (string package in job.GetVariable("DOTNET_PACKAGES","").Split(':',StringSplitOptions.RemoveEmptyEntries))
{
FileName.SetValue(package);
if (base.Run(job) != 0)
success = false;
}
return success ? 0 : -1;
}
}
}
}