WriteApiManagementTemplatesCmdlet.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.IO;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using APIManagementTemplate.Models;
 
namespace APIManagementTemplate
{
    [Cmdlet("Write", "APIManagementTemplates", ConfirmImpact = ConfirmImpact.None)]
    public class WriteApiManagementTemplatesCmdlet : PSCmdlet
    {
        [Parameter(Mandatory = false, HelpMessage = "Piped input from Get-APIManagementTemplate", ValueFromPipeline = true)]
        public string ARMTemplate;
 
        [Parameter(Mandatory = false, HelpMessage = "Generate templates for APIs that can be deployed standalone (without the rest of the resources)")]
        public bool ApiStandalone = true;
 
        [Parameter(Mandatory = false, HelpMessage = "The output directory")]
        public string OutputDirectory = ".";
 
        [Parameter(Mandatory = false, HelpMessage = "Policies are written to a separate file")]
        public bool SeparatePolicyFile = false;
 
        [Parameter(Mandatory = false, HelpMessage = "If the template already exists in the output directory, it will be merged with the new result.")]
        public bool MergeTemplates = false;
 
        [Parameter(Mandatory = false, HelpMessage = "If parameter files should be generated")]
        public bool GenerateParameterFiles = false;
 
        [Parameter(Mandatory = false, HelpMessage = "If the key to an Azure Function should be defined in a parameter instead of calling listsecrets")]
        public bool ReplaceListSecretsWithParameter = false;
 
        [Parameter(Mandatory = false, HelpMessage = "If set, the input template is written to this file ")]
        public string DebugTemplateFile = "";
 
        protected override void ProcessRecord()
        {
            if (!string.IsNullOrEmpty(DebugTemplateFile))
                File.WriteAllText(DebugTemplateFile, ARMTemplate);
            var templates= new TemplatesGenerator().Generate(ARMTemplate, ApiStandalone, SeparatePolicyFile, GenerateParameterFiles, ReplaceListSecretsWithParameter);
            foreach (GeneratedTemplate template in templates)
            {
                string filename = $@"{OutputDirectory}\{template.FileName}";
                if (!String.IsNullOrWhiteSpace(template.Directory))
                {
                    var directory = $@"{OutputDirectory}\{template.Directory}";
                    Directory.CreateDirectory(directory);
                    filename = $@"{directory}\{template.FileName}";
                }
 
                if (File.Exists(filename) && MergeTemplates && template.Type == ContentType.Json)
                {
                    JObject oldTemplate = JObject.Parse(File.ReadAllText(filename));
                    template.Content = TemplateMerger.Merge(oldTemplate, template.Content);
                }
                File.WriteAllText(filename, template.Type == ContentType.Json ? template.Content.ToString() : template.XmlContent);
            }
        }
    }
}