From ec420bab24131a6dad84fd4979cf0b71df8d0f1b Mon Sep 17 00:00:00 2001 From: Brian Cristante <33549821+brcrista@users.noreply.github.com> Date: Thu, 14 Oct 2021 16:26:00 -0400 Subject: [PATCH] Try adding steps in JobExtension --- src/Runner.Worker/ActionManager.cs | 68 ++++++++++++----------- src/Runner.Worker/ActionRunner.cs | 10 ++++ src/Runner.Worker/JobExtension.cs | 83 ++++++++++++++++++++++------ src/Runner.Worker/MakefileManager.cs | 24 +++++--- 4 files changed, 127 insertions(+), 58 deletions(-) diff --git a/src/Runner.Worker/ActionManager.cs b/src/Runner.Worker/ActionManager.cs index 4f7585976..73422b557 100644 --- a/src/Runner.Worker/ActionManager.cs +++ b/src/Runner.Worker/ActionManager.cs @@ -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. - 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 - { + // 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"; - } + // } } else { diff --git a/src/Runner.Worker/ActionRunner.cs b/src/Runner.Worker/ActionRunner.cs index 22c892480..fe4712516 100644 --- a/src/Runner.Worker/ActionRunner.cs +++ b/src/Runner.Worker/ActionRunner.cs @@ -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 + { + ["script"] = $"make {target}" + }; + } + // Create the handler. IHandler handler = handlerFactory.Create( ExecutionContext, diff --git a/src/Runner.Worker/JobExtension.cs b/src/Runner.Worker/JobExtension.cs index 613f408bd..93327de17 100644 --- a/src/Runner.Worker/JobExtension.cs +++ b/src/Runner.Worker/JobExtension.cs @@ -265,29 +265,78 @@ namespace GitHub.Runner.Worker if (step.Type == Pipelines.StepType.Action) { var action = step as Pipelines.ActionStep; - Trace.Info($"Adding {action.DisplayName}."); - var actionRunner = HostContext.CreateService(); - actionRunner.Action = action; - actionRunner.Stage = ActionRunStage.Main; - actionRunner.Condition = step.Condition; - var contextData = new Pipelines.ContextData.DictionaryContextData(); - if (message.ContextData?.Count > 0) + + void EmitStep() { - foreach (var pair in message.ContextData) + Trace.Info($"Adding {action.DisplayName}."); + var actionRunner = HostContext.CreateService(); + actionRunner.Action = action; + actionRunner.Stage = ActionRunStage.Main; + actionRunner.Condition = step.Condition; + var contextData = new Pipelines.ContextData.DictionaryContextData(); + if (message.ContextData?.Count > 0) { - contextData[pair.Key] = pair.Value; + foreach (var pair in message.ContextData) + { + contextData[pair.Key] = pair.Value; + } + } + + actionRunner.TryEvaluateDisplayName(contextData, context); + jobSteps.Add(actionRunner); + + if (prepareResult.PreStepTracker.TryGetValue(step.Id, out var preStep)) + { + Trace.Info($"Adding pre-{action.DisplayName}."); + preStep.TryEvaluateDisplayName(contextData, context); + preStep.DisplayName = $"Pre {preStep.DisplayName}"; + preJobSteps.Add(preStep); } } - actionRunner.TryEvaluateDisplayName(contextData, context); - jobSteps.Add(actionRunner); - - if (prepareResult.PreStepTracker.TryGetValue(step.Id, out var preStep)) + // HACK: if the step is "run: make," parse the Makefile and emit $"make dependency {target}" + if (action.DisplayName == "Run make") { - Trace.Info($"Adding pre-{action.DisplayName}."); - preStep.TryEvaluateDisplayName(contextData, context); - preStep.DisplayName = $"Pre {preStep.DisplayName}"; - preJobSteps.Add(preStep); + // 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(); } } } diff --git a/src/Runner.Worker/MakefileManager.cs b/src/Runner.Worker/MakefileManager.cs index c7b833d9d..abb14e68f 100644 --- a/src/Runner.Worker/MakefileManager.cs +++ b/src/Runner.Worker/MakefileManager.cs @@ -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 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(); + } } } \ No newline at end of file