CodeGen
 All Classes Namespaces Files Functions Variables Properties Pages
Execute.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Xml.Serialization;
5 using CodeGen.generators;
6 using CodeGen.utils;
7 using Newtonsoft.Json;
8 using YamlDotNet.Core;
9 using YamlDotNet.Serialization;
10 using YamlDotNet.Serialization.NamingConventions;
11 
12 namespace CodeGen
13 {
17  public static class ExecuteConf
18  {
19  private const string OutputDir = "out";
20 
24  public static void Execute(string langName, string fileName, bool toStdout)
25  {
26  var lang = GeneratorConf.GetLanguage(langName);
27  var gen = lang.Generator;
28  if (gen == null) throw new NullReferenceException("This language has no generator");
29 
30  var pkg = string.IsNullOrWhiteSpace(fileName)
31  ? Program.DefaultPkg
32  : ParseFile(fileName);
33 
34  lang.Normalizer?.NormalizePackage(ref pkg);
35 
36  var data = gen.Generate(pkg);
37 
38  if (toStdout)
39  {
40  PutDataToStdout(data, lang);
41  }
42  else
43  {
44  PutDataToFiles(data, lang);
45  }
46  }
47 
48  private static Package ParseFile(string filename)
49  {
50  return ParseFileByFormat(GetSerializedData(filename), filename);
51  }
52 
60  public static Package ParseFileByFormat(string body, string fileName)
61  {
62  Package pkg;
63 
64  var fileFormat = Utils.GetFileFormat(fileName);
65 
66  switch (fileFormat)
67  {
68  case "xml":
69  pkg = DeserializeXml(body);
70  break;
71  case "json":
72  pkg = DeserializeJson(body);
73  break;
74  case "yml":
75  pkg = DeserializeYaml(body);
76  break;
77  default:
78  throw new InvalidDataException("Invalid format of '" + fileName + "' file.");
79  }
80 
81  return pkg;
82  }
83 
84  private static string GetSerializedData(string fileName)
85  {
86  var allowedSchemes = new[] {Uri.UriSchemeHttp, Uri.UriSchemeHttps, Uri.UriSchemeFtp, Uri.UriSchemeFtp};
87 
88  var result = Uri.TryCreate(fileName, UriKind.Absolute, out var uriResult)
89  && Array.IndexOf(allowedSchemes, uriResult.Scheme) > -1
90  ? Utils.Download(fileName)
91  : Utils.Read(fileName);
92 
93  return result;
94  }
95 
96  private static void PutDataToFiles(Dictionary<string, string> fileContextMap, Languange lang)
97  {
98  var ext = lang.Extension;
99  Directory.CreateDirectory(OutputDir);
100  foreach (var file in fileContextMap)
101  {
102  Utils.Write(OutputDir + "/" + file.Key + '.' + ext, file.Value);
103  }
104 
105  Console.WriteLine("Generated successfully.");
106  }
107 
108  private static void PutDataToStdout(Dictionary<string, string> fileContextMap, Languange lang)
109  {
110  var ext = lang.Extension;
111  var comment = lang.Comment;
112  var filename = "";
113  foreach (var file in fileContextMap)
114  {
115  if (comment != "")
116  {
117  filename = string.Format(comment, file.Key + "." + ext);
118  }
119 
120  Console.WriteLine(filename);
121  Console.WriteLine(file.Value);
122  }
123  }
124 
130  public static Package DeserializeYaml(string body)
131  {
132  try
133  {
134  var deserializer = new DeserializerBuilder()
135  .WithNamingConvention(new CamelCaseNamingConvention())
136  .Build();
137  var pkg = deserializer.Deserialize<Package>(new StringReader(body));
138  return pkg;
139  }
140  catch (YamlException)
141  {
142  throw new InvalidDataException("invalid Yaml file content");
143  }
144  }
145 
152  public static Package DeserializeJson(string body)
153  {
154  try
155  {
156  return JsonConvert.DeserializeObject<Package>(body);
157  }
158  catch (Exception)
159  {
160  throw new InvalidDataException("invalid Json file content");
161  }
162  }
163 
170  public static Package DeserializeXml(string body)
171  {
172  try
173  {
174  var serializer = new XmlSerializer(typeof(Package));
175  using (TextReader reader = new StringReader(body))
176  {
177  return (Package) serializer.Deserialize(reader);
178  }
179  }
180  catch (Exception)
181  {
182  throw new InvalidDataException("invalid XML file content");
183  }
184  }
185  }
186 }
The class that describes programming language and has a generator for it
The structure that describes package. Contains classes and subpackages
Definition: Models.cs:9