Try adding steps in JobExtension

This commit is contained in:
Brian Cristante
2021-10-14 16:26:00 -04:00
parent 07cda747b2
commit ec420bab24
4 changed files with 127 additions and 58 deletions

View File

@@ -488,44 +488,48 @@ namespace GitHub.Runner.Worker
else if (action.Reference.Type == Pipelines.ActionSourceType.Script)
{
// Load the inputs.
executionContext.Debug("Loading inputs");
var templateEvaluator = executionContext.ToPipelineTemplateEvaluator();
var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, executionContext.ExpressionValues, executionContext.ExpressionFunctions);
// executionContext.Debug("Loading inputs");
// var templateEvaluator = executionContext.ToPipelineTemplateEvaluator();
// var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, executionContext.ExpressionValues, executionContext.ExpressionFunctions);
// Check if we are running a Makefile.
// Only works for the default target right now.
if (inputs["script"] == "make")
{
// Get the path of the Makefile in the repository root.
var githubContext = executionContext.ExpressionValues["github"] as GitHubContext;
var workspaceDir = githubContext["workspace"] as StringContextData;
var makefile = Path.Combine(workspaceDir, "Makefile");
if (!File.Exists(makefile))
{
// Forget about trying to be smart. Just do the normal thing.
// if (inputs["script"] == "make")
// {
// // Get the path of the Makefile in the repository root.
// var githubContext = executionContext.ExpressionValues["github"] as GitHubContext;
// var workspaceDir = githubContext["workspace"] as StringContextData;
// var makefile = Path.Combine(workspaceDir, "Makefile");
// if (!File.Exists(makefile))
// {
// // Forget about trying to be smart. Just do the normal thing.
// definition.Data.Execution = new ScriptActionExecutionData();
// definition.Data.Name = "Run";
// definition.Data.Description = "Execute a script";
// }
// else
// {
// // Assume the default target is named `all`.
// var definitionData = MakefileManager.Load(executionContext, makefile, target: "all");
// if (definitionData is null)
// {
// // Forget about trying to be smart. Just do the normal thing.
// definition.Data.Execution = new ScriptActionExecutionData();
// definition.Data.Name = "Run";
// definition.Data.Description = "Execute a script";
// }
// else
// {
// definition.Data = definitionData;
// }
// }
// }
// else
// {
definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Name = "Run";
definition.Data.Description = "Execute a script";
}
// Assume the default target is named `all`.
var definitionData = MakefileManager.Load(executionContext, makefile, target: "all");
if (definitionData is null)
{
// Forget about trying to be smart. Just do the normal thing.
definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Name = "Run";
definition.Data.Description = "Execute a script";
}
definition.Data = definitionData;
}
else
{
definition.Data.Execution = new ScriptActionExecutionData();
definition.Data.Name = "Run";
definition.Data.Description = "Execute a script";
}
// }
}
else
{

View File

@@ -262,6 +262,16 @@ namespace GitHub.Runner.Worker
environment[$"STATE_{state.Key}"] = state.Value ?? string.Empty;
}
// HACK
if (Action.DisplayName != null && Action.DisplayName.StartsWith("make dependency"))
{
var target = Action.DisplayName.Split()[2];
inputs = new Dictionary<string, string>
{
["script"] = $"make {target}"
};
}
// Create the handler.
IHandler handler = handlerFactory.Create(
ExecutionContext,

View File

@@ -265,6 +265,9 @@ namespace GitHub.Runner.Worker
if (step.Type == Pipelines.StepType.Action)
{
var action = step as Pipelines.ActionStep;
void EmitStep()
{
Trace.Info($"Adding {action.DisplayName}.");
var actionRunner = HostContext.CreateService<IActionRunner>();
actionRunner.Action = action;
@@ -290,6 +293,52 @@ namespace GitHub.Runner.Worker
preJobSteps.Add(preStep);
}
}
// HACK: if the step is "run: make," parse the Makefile and emit $"make dependency {target}"
if (action.DisplayName == "Run make")
{
// Load the inputs.
jobContext.Debug("Loading inputs");
var inputs = templateEvaluator.EvaluateStepInputs(action.Inputs, jobContext.ExpressionValues, jobContext.ExpressionFunctions);
// Check if we are running a Makefile.
// Only works for the default target right now.
if (inputs["script"] == "make")
{
// Get the path of the Makefile in the repository root.
var githubContext = jobContext.ExpressionValues["github"] as GitHubContext;
var workspaceDir = githubContext["workspace"] as StringContextData;
var makefile = Path.Combine(workspaceDir, "Makefile");
if (!File.Exists(makefile))
{
// Forget about trying to be smart. Just do the normal thing.
EmitStep();
}
else
{
// Assume the default target is named `all`.
var targetDependencies = MakefileManager.ReadTargetDependencies(jobContext, makefile, target: "all");
if (targetDependencies.Count == 0)
{
// Forget about trying to be smart. Just do the normal thing.
EmitStep();
}
else
{
foreach (var target in targetDependencies)
{
action.DisplayName = $"make dependency {target}";
EmitStep();
}
}
}
}
}
else
{
EmitStep();
}
}
}
var intraActionStates = new Dictionary<Guid, Dictionary<string, string>>();

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -9,15 +10,7 @@ namespace GitHub.Runner.Worker
// Does not recurse into the dependencies of those steps.
public static ActionDefinitionData Load(IExecutionContext executionContext, string makefile, string target)
{
var targetToFind = target + ":";
var lines = File.ReadLines(makefile);
string targetLine = lines.FirstOrDefault(line => line.TrimStart().StartsWith(targetToFind));
if (targetLine is null)
{
return null;
}
var dependencies = targetLine.Split().Skip(1).ToList();
var dependencies = ReadTargetDependencies(executionContext, makefile, target);
if (dependencies.Count == 0)
{
return null;
@@ -35,5 +28,18 @@ namespace GitHub.Runner.Worker
}
};
}
public static List<string> ReadTargetDependencies(IExecutionContext executionContext, string makefile, string target)
{
var targetToFind = target + ":";
var lines = File.ReadLines(makefile);
string targetLine = lines.FirstOrDefault(line => line.TrimStart().StartsWith(targetToFind));
if (targetLine is null)
{
return null;
}
return targetLine.Split().Skip(1).ToList();
}
}
}