ln.build/ln.build/pipeline/DefaultPipeLine.cs

111 lines
3.5 KiB
C#

using System.Collections.Generic;
using ln.build.commands;
using ln.json;
using ln.logging;
namespace ln.build.pipeline
{
public class DefaultPipeLine
{
public CommandEnvironment CommandEnvironment { get; }
public CIService CIService { get; }
List<Stage> stages = new List<Stage>();
public IEnumerable<Stage> Stages => stages;
public DefaultPipeLine(CIService ciService)
{
CIService = ciService;
CommandEnvironment = new CommandEnvironment();
}
public DefaultPipeLine(CIService ciService, CommandEnvironment commandEnvironment)
{
CIService = ciService;
CommandEnvironment = new CommandEnvironment(commandEnvironment);
}
public void LoadJson(JSONObject jsonPipeLine)
{
if (jsonPipeLine.ContainsKey("env"))
CommandEnvironment.Apply(jsonPipeLine["env"] as JSONObject);
if (jsonPipeLine.ContainsKey("stages"))
{
JSONArray jsonStages = jsonPipeLine["stages"] as JSONArray;
foreach (JSONObject jsonStage in jsonStages.Children)
{
Stage stage = new Stage(this);
stage.LoadJson(jsonStage);
stages.Add(stage);
}
}
}
public void Run()
{
foreach (Stage stage in stages)
{
CommandEnvironment.Logger.Log(LogLevel.INFO,"-------------------------------------------------------------------------------------");
CommandEnvironment.Logger.Log(LogLevel.INFO,"STAGE: {0}", stage.Name);
CommandEnvironment.Logger.Log(LogLevel.INFO,"-------------------------------------------------------------------------------------");
stage.Run();
}
}
}
public class Stage
{
public DefaultPipeLine PipeLine { get; }
public string Name { get; set; }
public CommandEnvironment CommandEnvironment { get; }
List<StageCommand> commands = new List<StageCommand>();
public IEnumerable<StageCommand> Commands => commands;
public Stage(DefaultPipeLine pipeLine)
{
PipeLine = pipeLine;
CommandEnvironment = new CommandEnvironment(pipeLine.CommandEnvironment);
}
public void LoadJson(JSONObject jsonStage)
{
Name = jsonStage["name"].ToNative().ToString();
if (jsonStage.ContainsKey("env"))
{
CommandEnvironment.Apply(jsonStage["env"] as JSONObject);
}
if (jsonStage.ContainsKey("commands"))
{
JSONArray jsonCommands = jsonStage["commands"] as JSONArray;
foreach (JSONValue jsonValue in jsonCommands.Children)
{
commands.Add(StageCommand.Create(jsonValue.ToNative().ToString()));
}
}
if (jsonStage.ContainsKey("secrets"))
{
JSONObject jsonSecrets = jsonStage["secrets"] as JSONObject;
foreach (string key in jsonSecrets.Keys)
{
CommandEnvironment.Set(key, CommandEnvironment.SecretStorage?.GetSecret(jsonSecrets[key]?.ToNative()?.ToString()));
}
}
}
public void Run()
{
foreach (StageCommand command in commands)
command.Run(this);
}
}
}