Skip to content

Commit

Permalink
feat: azexpand command (#248)
Browse files Browse the repository at this point in the history
* fix conditionally assign variable
* azexpand and azpipelines are now subcommands
  • Loading branch information
ChristopherHX authored Oct 28, 2023
1 parent e88755b commit 72ec97c
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 110 deletions.
284 changes: 198 additions & 86 deletions src/Runner.Client/Program.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Runner.Service/Windows/RunnerService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.*" />
</ItemGroup>

</Project>
20 changes: 13 additions & 7 deletions src/Sdk/AzurePipelines/AzureDevops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,13 +515,6 @@ public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.C
variablesData[v.Key] = new StringContextData(v.Value);
}
}
if(rawStaticVariables != null) {
IDictionary<string, VariableValue> pvars = new Dictionary<string, VariableValue>(StringComparer.OrdinalIgnoreCase);
await ParseVariables(context, pvars, rawStaticVariables, true);
foreach(var v in pvars) {
variablesData[v.Key] = new StringContextData(v.Value.Value);
}
}

if(parameters?.Type == TokenType.Mapping) {
int providedParameter = 0;
Expand Down Expand Up @@ -593,6 +586,19 @@ public static async Task<MappingToken> ReadTemplate(Runner.Server.Azure.Devops.C

templateContext.Errors.Check();

if(rawStaticVariables != null) {
// See "testworkflows/azpipelines/expressions-docs/Conditionally assign a variable.yml"
templateContext = AzureDevops.CreateTemplateContext(context.TraceWriter ?? new EmptyTraceWriter(), templateContext.GetFileTable().ToArray(), context.Flags, contextData);
rawStaticVariables = TemplateEvaluator.Evaluate(templateContext, "workflow-value", rawStaticVariables, 0, fileId);
templateContext.Errors.Check();

IDictionary<string, VariableValue> pvars = new Dictionary<string, VariableValue>(StringComparer.OrdinalIgnoreCase);
await ParseVariables(context, pvars, rawStaticVariables, true);
foreach(var v in pvars) {
variablesData[v.Key] = new StringContextData(v.Value.Value);
}
}

templateContext = AzureDevops.CreateTemplateContext(context.TraceWriter ?? new EmptyTraceWriter(), templateContext.GetFileTable().ToArray(), context.Flags, contextData);

var evaluatedResult = TemplateEvaluator.Evaluate(templateContext, schemaName ?? "pipeline-root", pipelineroot, 0, fileId);
Expand Down
24 changes: 24 additions & 0 deletions src/Sdk/AzurePipelines/AzurePipelinesUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.IO;
using GitHub.DistributedTask.ObjectTemplating;
using GitHub.DistributedTask.ObjectTemplating.Tokens;
using GitHub.DistributedTask.Pipelines.ObjectTemplating;

namespace Runner.Server.Azure.Devops {

public class AzurePipelinesUtils {
public static TemplateToken ConvertStringToTemplateToken(string res) {
if(res == null) {
return null;
}
var templateContext = AzureDevops.CreateTemplateContext(new EmptyTraceWriter(), new List<string>(), GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives);
using (var stringReader = new StringReader(res))
{
var yamlObjectReader = new YamlObjectReader(null, stringReader, preserveString: true, forceAzurePipelines: true);
var ret = TemplateReader.Read(templateContext, "any", yamlObjectReader, null, out _);
templateContext.Errors.Check();
return ret;
}
}
}
}
2 changes: 1 addition & 1 deletion src/azure-pipelines-vscode-ext/ext-core/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static async Task<string> ExpandCurrentPipeline(JSObject handle, string c
};
Dictionary<string, TemplateToken> cparameters = new Dictionary<string, TemplateToken>();
foreach(var kv in JsonConvert.DeserializeObject<Dictionary<string, string>>(parameters)) {
cparameters[kv.Key] = RequiredParametersProvider.ConvertStringToTemplateToken(kv.Value);
cparameters[kv.Key] = AzurePipelinesUtils.ConvertStringToTemplateToken(kv.Value);
}
var template = await AzureDevops.ReadTemplate(context, currentFileName, cparameters);
var pipeline = await new Runner.Server.Azure.Devops.Pipeline().Parse(context.ChildContext(template, currentFileName), template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@ public RequiredParametersProvider(JSObject handle) {
this.handle = handle;
}

public static TemplateToken ConvertStringToTemplateToken(string res) {
if(res == null) {
return null;
}
var templateContext = AzureDevops.CreateTemplateContext(new EmptyTraceWriter(), new List<string>(), GitHub.DistributedTask.Expressions2.ExpressionFlags.DTExpressionsV1 | GitHub.DistributedTask.Expressions2.ExpressionFlags.ExtendedDirectives);
using (var stringReader = new StringReader(res))
{
var yamlObjectReader = new YamlObjectReader(null, stringReader, preserveString: true, forceAzurePipelines: true);
var ret = TemplateReader.Read(templateContext, "any", yamlObjectReader, null, out _);
templateContext.Errors.Check();
return ret;
}
}

public async Task<TemplateToken> GetRequiredParameter(string name) {
return ConvertStringToTemplateToken(await Interop.RequestRequiredParameter(handle, name));
return AzurePipelinesUtils.ConvertStringToTemplateToken(await Interop.RequestRequiredParameter(handle, name));
}
}

0 comments on commit 72ec97c

Please sign in to comment.