Ejemplo n.º 1
0
 protected TArmObject AggregateArmObject <TArmObject>(
     ArmBuilder <TArmObject> builder,
     ScriptBlock body)
     where TArmObject : ArmObject
 {
     foreach (PSObject output in InvokeBody(body))
     {
         if (output.BaseObject is ArmEntry armEntry)
         {
             builder.AddEntry(armEntry);
         }
     }
     return(builder.Build());
 }
Ejemplo n.º 2
0
        protected override void EndProcessing()
        {
            string templateName = Name;

            if (templateName is null)
            {
                try
                {
                    templateName = Path.GetFileName(MyInvocation.ScriptName);
                    templateName = templateName.Substring(0, templateName.Length - PSArmTemplateExecutor.PSArmFileExtension.Length);
                }
                catch
                {
                    // If we fail, just proceed with templateName = null
                }
            }

            ScriptBlock transformedBody;
            ArmObject <ArmParameter> armParameters;
            ArmObject <ArmVariable>  armVariables;

            object[] psArgsArray;

            using (var pwsh = PowerShell.Create(RunspaceMode.CurrentRunspace))
            {
                try
                {
                    transformedBody = new TemplateScriptBlockTransformer(pwsh).GetDeparameterizedTemplateScriptBlock(
                        Body,
                        out armParameters,
                        out armVariables,
                        out psArgsArray);
                }
                catch (Exception e)
                {
                    this.ThrowTerminatingError(e, "TemplateScriptBlockTransformationFailure", ErrorCategory.InvalidArgument, Body);
                    return;
                }
            }

            var template = new ArmTemplate(templateName);

            if (armParameters is not null && armParameters.Count > 0)
            {
                template.Parameters = armParameters;
            }

            if (armVariables is not null && armVariables.Count > 0)
            {
                template.Variables = armVariables;
            }

            var templateBuilder = new ArmBuilder <ArmTemplate>(template);

            foreach (PSObject output in InvokeCommand.InvokeScript(useLocalScope: true, transformedBody, input: null, psArgsArray))
            {
                if (output.BaseObject is ArmEntry armEntry)
                {
                    templateBuilder.AddEntry(armEntry);
                }
            }

            WriteObject(template);
        }