Azure Config Generator
 All Classes Namespaces Files Functions Variables Properties
AzureConfigGen.cs
Go to the documentation of this file.
1 using Newtonsoft.Json;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 
9 namespace FUSE.AzureConfig.Tasks
10 {
11  public class AzureConfigGen : Microsoft.Build.Utilities.Task
12  {
13  public string ConfigurationFile { get; set; }
14 
15  public string ConfigurationDirectory { get; set; }
16 
17  public string OutputDirectory { get; set; }
18 
19  public override bool Execute()
20  {
21  if (string.IsNullOrEmpty(this.ConfigurationFile))
22  throw new ArgumentNullException("Must specify ConfigurationFile");
23 
24  if (!string.IsNullOrEmpty(this.OutputDirectory) && !Directory.Exists(this.OutputDirectory))
25  throw new ArgumentException(this.OutputDirectory + " output directory must exist if specified");
26 
27  this.OutputDirectory = this.OutputDirectory ?? this.ConfigurationDirectory;
28 
29  if (!string.IsNullOrEmpty(this.ConfigurationDirectory))
30  {
31  if (!Directory.Exists(this.ConfigurationDirectory))
32  throw new ArgumentException(this.ConfigurationDirectory + " must exist.");
33  if (!Path.IsPathRooted(this.ConfigurationFile))
34  this.ConfigurationFile = Path.Combine(this.ConfigurationDirectory, this.ConfigurationFile);
35  }
36  if (!File.Exists(this.ConfigurationFile))
37  throw new ArgumentException(this.ConfigurationFile + " does not exist");
38 
39  AzureConfigSpec confSpec;
40  try
41  {
42  confSpec = JsonConvert.DeserializeObject<AzureConfigSpec>(File.ReadAllText(this.ConfigurationFile));
43  } catch (Exception e)
44  {
45  throw new ArgumentException("Failed to parse " + this.ConfigurationFile, e);
46  }
47 
48  var conf = new SpecProcessor().Process(confSpec);
49  var generator = new FileGenerator();
50  generator.AlterCsdef(conf, this.ConfigurationDirectory, this.OutputDirectory);
51  generator.AlterCscfgs(conf, this.ConfigurationDirectory, this.OutputDirectory);
52 
53  return true;
54  }
55  }
56 }
JSON-based azure configuration specification.