Skip to content

Commit

Permalink
make async
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Oct 7, 2023
1 parent 1e26222 commit b008639
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 61 deletions.
41 changes: 21 additions & 20 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using GitHub.DistributedTask.Expressions2;
using GitHub.DistributedTask.Expressions2.Sdk.Functions.v1;
using GitHub.DistributedTask.ObjectTemplating;
Expand Down Expand Up @@ -55,7 +56,7 @@ public static TemplateContext CreateTemplateContext(GitHub.DistributedTask.Objec
return templateContext;
}

public static void ParseVariables(Runner.Server.Azure.Devops.Context context, IDictionary<string, VariableValue> vars, TemplateToken rawvars, bool onlyStaticVars = false) {
public static async Task ParseVariables(Runner.Server.Azure.Devops.Context context, IDictionary<string, VariableValue> vars, TemplateToken rawvars, bool onlyStaticVars = false) {
if(rawvars is MappingToken mvars) {
foreach(var kv in mvars) {
// Skip expressions if we parse static variables
Expand Down Expand Up @@ -116,15 +117,15 @@ public static void ParseVariables(Runner.Server.Azure.Devops.Context context, ID
}
}
} else if(template != null) {
var file = ReadTemplate(context, template, parameters != null ? parameters.AssertMapping("param").ToDictionary(kv => kv.Key.AssertString("").Value, kv => kv.Value) : null, "variable-template-root");
ParseVariables(context.ChildContext(file, template), vars, (from e in file where e.Key.AssertString("").Value == "variables" select e.Value).First());
var file = await ReadTemplate(context, template, parameters != null ? parameters.AssertMapping("param").ToDictionary(kv => kv.Key.AssertString("").Value, kv => kv.Value) : null, "variable-template-root");
await ParseVariables(context.ChildContext(file, template), vars, (from e in file where e.Key.AssertString("").Value == "variables" select e.Value).First());
} else {
vars[name] = new VariableValue(value, isReadonly: isReadonly);
}
}
}
}
public static void ParseSteps(Runner.Server.Azure.Devops.Context context, IList<TaskStep> steps, TemplateToken step) {
public static async Task ParseSteps(Runner.Server.Azure.Devops.Context context, IList<TaskStep> steps, TemplateToken step) {
var values = "task, powershell, pwsh, bash, script, checkout, download, downloadBuild, getPackage, publish, reviewApp, template";
var mstep = step.AssertMapping($"steps.* a step must contain one of the following keyworlds as the first key {values}");
mstep.AssertNotEmpty($"steps.* a step must contain one of the following keyworlds as the first key {values}");
Expand Down Expand Up @@ -304,17 +305,17 @@ public static void ParseSteps(Runner.Server.Azure.Devops.Context context, IList<
if(unparsedTokens.Count == 1 && (unparsedTokens[0].Key as StringToken)?.Value != "parameters") {
throw new Exception($"Unexpected yaml key {(unparsedTokens[0].Key as StringToken)?.Value} expected parameters");
}
var file = ReadTemplate(context, primaryValue, unparsedTokens.Count == 1 ? unparsedTokens[0].Value.AssertMapping("param").ToDictionary(kv => kv.Key.AssertString("").Value, kv => kv.Value) : null, "step-template-root");
var file = await ReadTemplate(context, primaryValue, unparsedTokens.Count == 1 ? unparsedTokens[0].Value.AssertMapping("param").ToDictionary(kv => kv.Key.AssertString("").Value, kv => kv.Value) : null, "step-template-root");
foreach(var step2 in (from e in file where e.Key.AssertString("").Value == "steps" select e.Value).First().AssertSequence("")) {
ParseSteps(context.ChildContext(file, primaryValue), steps, step2);
await ParseSteps(context.ChildContext(file, primaryValue), steps, step2);
}
break;
default:
throw new Exception($"{GitHub.DistributedTask.ObjectTemplating.Tokens.TemplateTokenExtensions.GetAssertPrefix(step)}Unexpected step.key got {primaryKey} expected {values}");
}
}

private static PipelineContextData ConvertValue(Runner.Server.Azure.Devops.Context context, TemplateToken val, string type, TemplateToken values) {
private static async Task<PipelineContextData> ConvertValue(Runner.Server.Azure.Devops.Context context, TemplateToken val, string type, TemplateToken values) {
var steps = new List<TaskStep>();
var jobs = new List<Job>();
var stages = new List<Stage>();
Expand Down Expand Up @@ -352,14 +353,14 @@ private static PipelineContextData ConvertValue(Runner.Server.Azure.Devops.Conte
if(val == null) {
return null;
}
ParseSteps(context, steps, val);
await ParseSteps(context, steps, val);
return steps[0].ToContextData();
case "stepList":
if(val == null) {
return new ArrayContextData();
}
foreach(var step2 in val.AssertSequence("")) {
ParseSteps(context, steps, step2);
await ParseSteps(context, steps, step2);
}
var stepList = new ArrayContextData();
foreach(var step in steps) {
Expand All @@ -370,12 +371,12 @@ private static PipelineContextData ConvertValue(Runner.Server.Azure.Devops.Conte
if(val == null) {
return null;
}
return new Job().Parse(context, val).ToContextData();
return (await new Job().Parse(context, val)).ToContextData();
case "jobList":
if(val == null) {
return new ArrayContextData();
}
Job.ParseJobs(context, jobs, val.AssertSequence(""));
await Job.ParseJobs(context, jobs, val.AssertSequence(""));
var jobList = new ArrayContextData();
foreach(var job in jobs) {
jobList.Add(job.ToContextData());
Expand All @@ -385,14 +386,14 @@ private static PipelineContextData ConvertValue(Runner.Server.Azure.Devops.Conte
if(val == null) {
return null;
}
var djob = new Job().Parse(context, val);
var djob = await new Job().Parse(context, val);
if(!djob.DeploymentJob) throw new Exception("Only Deployment Jobs are valid");
return djob.ToContextData();
case "deploymentList":
if(val == null) {
return new ArrayContextData();
}
Job.ParseJobs(context, jobs, val.AssertSequence(""));
await Job.ParseJobs(context, jobs, val.AssertSequence(""));
var djobList = new ArrayContextData();
foreach(var job in jobs) {
if(!job.DeploymentJob) throw new Exception("Only Deployment Jobs are valid");
Expand All @@ -403,12 +404,12 @@ private static PipelineContextData ConvertValue(Runner.Server.Azure.Devops.Conte
if(val == null) {
return null;
}
return new Stage().Parse(context, val).ToContextData();
return (await new Stage().Parse(context, val)).ToContextData();
case "stageList":
if(val == null) {
return new ArrayContextData();
}
Stage.ParseStages(context, stages, val.AssertSequence(""));
await Stage.ParseStages(context, stages, val.AssertSequence(""));
var stageList = new ArrayContextData();
foreach(var stage in stages) {
stageList.Add(stage.ToContextData());
Expand Down Expand Up @@ -458,7 +459,7 @@ public static string RelativeTo(string cwd, string filename) {
return string.Join('/', path.ToArray());
}

public static MappingToken ReadTemplate(Runner.Server.Azure.Devops.Context context, string filenameAndRef, Dictionary<string, TemplateToken> cparameters = null, string schemaName = null) {
public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.Context context, string filenameAndRef, Dictionary<string, TemplateToken> cparameters = null, string schemaName = null) {
var variables = context.VariablesProvider?.GetVariablesForEnvironment("");
var templateContext = AzureDevops.CreateTemplateContext(context.TraceWriter ?? new EmptyTraceWriter(), new List<string>(), context.Flags);
var afilenameAndRef = filenameAndRef.Split("@", 2);
Expand All @@ -470,7 +471,7 @@ public static MappingToken ReadTemplate(Runner.Server.Azure.Devops.Context conte
if(finalFileName == null) {
throw new Exception($"Couldn't find template location {filenameAndRef}");
}
var fileContent = context.FileProvider.ReadFile(finalRepository, finalFileName);
var fileContent = await context.FileProvider.ReadFile(finalRepository, finalFileName);
if(fileContent == null) {
throw new Exception($"Couldn't read template {filenameAndRef} resolved to {finalFileName} ({finalRepository ?? "self"})");
}
Expand Down Expand Up @@ -515,7 +516,7 @@ public static MappingToken ReadTemplate(Runner.Server.Azure.Devops.Context conte
}
if(rawStaticVariables != null) {
IDictionary<string, VariableValue> pvars = new Dictionary<string, VariableValue>(StringComparer.OrdinalIgnoreCase);
ParseVariables(context, pvars, rawStaticVariables, true);
await ParseVariables(context, pvars, rawStaticVariables, true);
foreach(var v in pvars) {
variablesData[v.Key] = new StringContextData(v.Value.Value);
}
Expand Down Expand Up @@ -562,9 +563,9 @@ public static MappingToken ReadTemplate(Runner.Server.Azure.Devops.Context conte
break;
}
}
var defCtxData = ConvertValue(context, def, type, values);
var defCtxData = await ConvertValue(context, def, type, values);
if(cparameters?.TryGetValue(name, out var value) == true) {
parametersData[name] = ConvertValue(context, value, type, values);
parametersData[name] = await ConvertValue(context, value, type, values);
providedParameter++;
} else {
parametersData[name] = defCtxData;
Expand Down
6 changes: 4 additions & 2 deletions src/Sdk/AzurePipelines/DefaultFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;

public class DefaultFileProvider : IFileProvider
{
public string ReadFile(string repositoryAndRef, string path)
public Task<string> ReadFile(string repositoryAndRef, string path)
{
return System.IO.File.ReadAllText(path);
return Task.FromResult(System.IO.File.ReadAllText(path));
}
}
4 changes: 3 additions & 1 deletion src/Sdk/AzurePipelines/IFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Threading.Tasks;

public interface IFileProvider {
string ReadFile(string repositoryAndRef, string path);
Task<string> ReadFile(string repositoryAndRef, string path);
}
9 changes: 5 additions & 4 deletions src/Sdk/AzurePipelines/InMemoryFileProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks;

public class DefaultInMemoryFileProviderFileProvider : IFileProvider
{
Expand All @@ -12,13 +13,13 @@ public DefaultInMemoryFileProviderFileProvider(KeyValuePair<string, string>[] wo
private Func<string, string, string> readFile;
public Dictionary<string, string> Workflows { get; private set; }

public string ReadFile(string repositoryAndRef, string path)
public Task<string> ReadFile(string repositoryAndRef, string path)
{
if(repositoryAndRef == null && Workflows.TryGetValue(path, out var content)) {
return content;
return Task.FromResult(content);
} else if(readFile != null) {
return readFile(path, repositoryAndRef);
return Task.FromResult(readFile(path, repositoryAndRef));
}
return null;
return Task.FromResult<string>(null);
}
}
Loading

0 comments on commit b008639

Please sign in to comment.