73 lines
1.8 KiB
C#
73 lines
1.8 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text.Json.Serialization;
|
|
using ln.logging;
|
|
|
|
namespace ln.build
|
|
{
|
|
|
|
public class PipeLine
|
|
{
|
|
|
|
List<Step> steps = new List<Step>();
|
|
|
|
public PipeLine()
|
|
{
|
|
}
|
|
|
|
public void AddStep(Step step) => steps.Add(step);
|
|
|
|
public virtual void Run(CIJob job)
|
|
{
|
|
job.Logger.Log(LogLevel.INFO,"PipeLine [{0}] start",GetType().Name);
|
|
|
|
foreach (Step step in steps)
|
|
{
|
|
if (step.Run(job) != 0)
|
|
break;
|
|
}
|
|
|
|
job.Logger.Log(LogLevel.INFO,"PipeLine [{0}] ended", GetType().Name);
|
|
}
|
|
|
|
public abstract class Step
|
|
{
|
|
public string Name { get; }
|
|
public Step(string stepName)
|
|
{
|
|
Name = stepName;
|
|
}
|
|
|
|
public abstract int Run(CIJob job);
|
|
|
|
public string DefaultLogFileName => String.Format("step.{0}.log", Name);
|
|
}
|
|
|
|
}
|
|
|
|
public delegate void CommandExitedDelegate(CommandStep commandStep, CIJob job, int exitCode);
|
|
public class CommandStep : PipeLine.Step
|
|
{
|
|
public event CommandExitedDelegate OnCommandExited;
|
|
|
|
public CommandRunner CommandRunner { get; }
|
|
|
|
public CommandStep(string stepName, string filename,params string[] arguments)
|
|
:base(stepName)
|
|
{
|
|
CommandRunner = new CommandRunner(filename, arguments);
|
|
}
|
|
|
|
public override int Run(CIJob job)
|
|
{
|
|
CommandRunner.WorkingDirectory = job.WorkingDirectory;
|
|
|
|
int result = CommandRunner.Run(job.Logger, job.GetLogStream(DefaultLogFileName));
|
|
OnCommandExited?.Invoke(this, job, result);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
} |