2020-12-09 10:05:52 +01:00
|
|
|
using System;
|
2020-12-03 00:05:51 +01:00
|
|
|
using System.Collections.Generic;
|
2020-12-09 10:05:52 +01:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.IO;
|
2020-12-03 00:05:51 +01:00
|
|
|
using ln.build.commands;
|
2020-12-09 10:05:52 +01:00
|
|
|
using ln.build.semver;
|
2020-12-03 00:05:51 +01:00
|
|
|
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;
|
|
|
|
|
2020-12-09 10:05:52 +01:00
|
|
|
public Versioning Versioning { get; set; }
|
|
|
|
|
|
|
|
List<string> loadedTemplates = new List<string>();
|
|
|
|
public IEnumerable<string> LoadedTemplates => loadedTemplates;
|
|
|
|
|
2020-12-03 00:05:51 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-12-09 10:05:52 +01:00
|
|
|
if (jsonPipeLine.ContainsKey("templates"))
|
|
|
|
{
|
|
|
|
foreach (JSONString jsonTemplateName in jsonPipeLine["templates"].Children)
|
|
|
|
{
|
|
|
|
if (!loadedTemplates.Contains(jsonTemplateName.Value))
|
|
|
|
{
|
|
|
|
loadedTemplates.Add(jsonTemplateName.Value);
|
|
|
|
|
|
|
|
string repoTemplate = Path.Combine(CommandEnvironment.WorkingDirectory, String.Format("{0}.ln", jsonTemplateName.Value));
|
|
|
|
string systemTemplate = Path.Combine(CIService.ContextDirectory, "scripts", "pipeline", String.Format("{0}.ln", jsonTemplateName.Value));
|
|
|
|
|
|
|
|
if (File.Exists(repoTemplate))
|
|
|
|
{
|
|
|
|
LoadJson(JSONParser.ParseFile(repoTemplate) as JSONObject);
|
|
|
|
} else if (File.Exists(systemTemplate))
|
|
|
|
{
|
|
|
|
LoadJson(JSONParser.ParseFile(systemTemplate) as JSONObject);
|
|
|
|
} else
|
|
|
|
throw new FileNotFoundException(String.Format("{0}.ln", jsonTemplateName.Value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 00:05:51 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-12-09 10:05:52 +01:00
|
|
|
Stage stage = GetStage(jsonStage["name"].ToNative().ToString());
|
2020-12-03 00:05:51 +01:00
|
|
|
stage.LoadJson(jsonStage);
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 10:05:52 +01:00
|
|
|
|
|
|
|
if (jsonPipeLine.ContainsKey("versioning"))
|
|
|
|
Versioning = new Versioning(jsonPipeLine["versioning"] as JSONObject);
|
2020-12-03 00:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-09 10:05:52 +01:00
|
|
|
public Stage GetStage(string stageName)
|
|
|
|
{
|
|
|
|
foreach (Stage stage in stages)
|
|
|
|
{
|
|
|
|
if (stageName.Equals(stage.Name))
|
|
|
|
return stage;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stage _stage = new Stage(this);
|
|
|
|
_stage.Name = stageName;
|
|
|
|
stages.Add(_stage);
|
|
|
|
return _stage;
|
|
|
|
}
|
2020-12-03 00:05:51 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Stage
|
|
|
|
{
|
|
|
|
public DefaultPipeLine PipeLine { get; }
|
|
|
|
public string Name { get; set; }
|
2020-12-09 10:05:52 +01:00
|
|
|
public int Priority { get; set; }
|
2020-12-03 00:05:51 +01:00
|
|
|
|
|
|
|
public CommandEnvironment CommandEnvironment { get; }
|
|
|
|
|
2020-12-09 10:05:52 +01:00
|
|
|
public List<string> commands = new List<string>();
|
2020-12-03 00:05:51 +01:00
|
|
|
|
|
|
|
public Stage(DefaultPipeLine pipeLine)
|
|
|
|
{
|
|
|
|
PipeLine = pipeLine;
|
|
|
|
CommandEnvironment = new CommandEnvironment(pipeLine.CommandEnvironment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadJson(JSONObject jsonStage)
|
|
|
|
{
|
|
|
|
Name = jsonStage["name"].ToNative().ToString();
|
2020-12-09 10:05:52 +01:00
|
|
|
if (jsonStage.ContainsKey("priority"))
|
|
|
|
Priority = (int)(long)jsonStage["priority"].ToNative();
|
2020-12-03 00:05:51 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-12-09 10:05:52 +01:00
|
|
|
Logging.Log(LogLevel.DEBUG, "stage {0} command: {1}", Name, jsonValue.ToNative().ToString());
|
|
|
|
commands.Add(jsonValue.ToNative().ToString());
|
2020-12-03 00:05:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (jsonStage.ContainsKey("secrets"))
|
|
|
|
{
|
|
|
|
JSONObject jsonSecrets = jsonStage["secrets"] as JSONObject;
|
|
|
|
foreach (string key in jsonSecrets.Keys)
|
|
|
|
{
|
2020-12-03 14:19:28 +01:00
|
|
|
CommandEnvironment.Set(key, CommandEnvironment.SecretStorage?.GetSecret(jsonSecrets[key]?.ToNative()?.ToString()));
|
2020-12-03 00:05:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
{
|
2020-12-09 10:05:52 +01:00
|
|
|
foreach (string command in commands)
|
|
|
|
PipeLine.CIService.StageCommands.Run(this, command.Split());
|
2020-12-03 00:05:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|