Beispiel #1
0
        public void Run()
        {
            var wsdlFiles = Directory.GetFiles(@"AdWords\v201609\wsdl", "*.wsdl").OrderByDescending(x => x).ToList();
            var wsdl      = new WsdlStructure();

            foreach (var file in wsdlFiles)
            {
                var set = new XmlSchemaSet();
                var sd  = ServiceDescription.Read(file);
                foreach (XmlSchema s in sd.Types.Schemas)
                {
                    set.Add(s);
                }
                set.Compile();
                wsdl.AddServiceDescription(sd);
            }
            using (var reader = new StreamReader(@"AdWords\v201609\reportDefinition.xsd", Encoding.UTF8))
            {
                var       set          = new XmlSchemaSet();
                XmlSchema reportDefXsd = XmlSchema.Read(reader, null);
                set.Add(reportDefXsd);
                set.Compile();
                wsdl.AddXmlSchema(reportDefXsd);
            }
            wsdl.Compile();

            var programmer      = new Programmer();
            var targetNamespace = "Manychois.GoogleApis.AdWords.v201609";
            var outputDir       = @"Manychois.GoogleApis.AdWords\v201609\generated";

            programmer.Code(wsdl, outputDir, targetNamespace);
            programmer.CodeSelectorEnums(wsdlFiles, outputDir, targetNamespace);
        }
Beispiel #2
0
 private void SetCodeNames(WsdlStructure wsdl)
 {
     foreach (var t in wsdl.Types)
     {
         t.CodeName = StringUtil.ToPascalCaseName(t.XName.Name);
         bool isErrorType = t.BaseType?.XName.Name == "ApiError";
         foreach (var p in t.Properties)
         {
             p.CodeName = StringUtil.ToPascalCaseName(p.XName.Name);
             if (isErrorType && p.CodeName == "Message")
             {
                 p.CodeName = t.CodeName + p.CodeName;
             }
         }
         if (t is WsdlEnumType)
         {
             var et = t as WsdlEnumType;
             foreach (var i in et.Items)
             {
                 i.CodeName = string.Join("", i.Name.Split('_').Select(x => StringUtil.ToPascalCaseName(x.ToLowerInvariant())));
             }
         }
     }
     foreach (var e in wsdl.Elements)
     {
         string serviceName = "";
         if (e.Wsdl.Services.Count > 0)
         {
             serviceName = e.Wsdl.Services[0].Name;
         }
         e.CodeName = serviceName + StringUtil.ToPascalCaseName(e.XName.Name);
         foreach (var p in e.Properties)
         {
             p.CodeName = StringUtil.ToPascalCaseName(p.XName.Name);
         }
     }
     foreach (var pt in wsdl.PortTypes)
     {
         pt.CodeName = "I" + StringUtil.ToPascalCaseName(pt.XName.Name.Replace("Interface", ""));
         foreach (var op in pt.Operations)
         {
             op.CodeName = StringUtil.ToPascalCaseName(op.Name);
         }
     }
     foreach (var b in wsdl.Bindings)
     {
         b.CodeName = StringUtil.ToPascalCaseName(b.XName.Name);
     }
     foreach (var s in wsdl.Services)
     {
         s.CodeName = StringUtil.ToPascalCaseName(s.XName.Name);
     }
 }
Beispiel #3
0
 private void CodeInstanceCreator(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "InstanceCreator.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("using System.Xml.Linq;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             var allParentTypes = wsdl.Types.Where(x => x.IsAbstract || x.Properties.Any(y => y.IsTypeProperty)).OrderBy(x => x.CodeName).ToList();
             using (var classScope = file.CreateClassScope("internal static", "InstanceCreator"))
             {
                 foreach (var parent in allParentTypes)
                 {
                     using (var methodScope = file.CreateMethodScope("public static", parent.CodeName, $"Create{parent.CodeName}", "XElement xElement"))
                     {
                         file.WriteLine("var type = XmlUtility.GetXmlTypeLocalName(xElement);");
                         bool isFirst = true;
                         foreach (var concreteType in wsdl.GetConcreteChildTypes(parent).OrderBy(x => x.CodeName))
                         {
                             var elseKeyword = isFirst ? "" : "else ";
                             file.WriteLine($"{elseKeyword}if (type == \"{concreteType.XName.Name}\")");
                             file.Ocb();
                             file.WriteLine($"return new {concreteType.CodeName}();");
                             file.Ccb();
                             isFirst = false;
                         }
                         if (parent.IsAbstract)
                         {
                             file.WriteLine("throw new ArgumentException($\"Unknown type {type}\", \"xElement\");");
                         }
                         else
                         {
                             file.WriteLine("else");
                             file.Ocb();
                             file.WriteLine($"return new {parent.CodeName}();");
                             file.Ccb();
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #4
0
 private void CodeEnumExtensions(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "EnumExtensions.cs"))
     {
         file.WriteLine("using System;");
         file.WriteLine("");
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             foreach (var et in wsdl.Types.OfType <WsdlEnumType>().OrderBy(x => x.CodeName))
             {
                 using (var classScope = file.CreateClassScope("public static", $"{et.CodeName}Extensions"))
                 {
                     using (var methodScope = file.CreateMethodScope("public static", "string", "ToXmlValue", $"this {et.CodeName} enumValue"))
                     {
                         file.WriteLine("switch (enumValue)");
                         file.Ocb();
                         foreach (var item in et.Items)
                         {
                             file.WriteLine($"case {et.CodeName}.{item.CodeName}: return \"{item.Name}\";");
                         }
                         file.WriteLine("default: return null;");
                         file.Ccb();
                     }
                     using (var methodScope = file.CreateMethodScope("public static", et.CodeName, "Parse", "string xmlValue"))
                     {
                         file.WriteLine("switch (xmlValue)");
                         file.Ocb();
                         foreach (var item in et.Items)
                         {
                             file.WriteLine($"case \"{item.Name}\": return {et.CodeName}.{item.CodeName};");
                         }
                         file.WriteLine($"default: throw new ArgumentException($\"Unknown value \\\"{{xmlValue}}\\\" for type {et.CodeName}.\", nameof(xmlValue));");
                         file.Ccb();
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
 public void Code(WsdlStructure wsdl, string directory, string namespaceName)
 {
     SetCodeNames(wsdl);
     CodeInstanceCreator(wsdl, directory, namespaceName);
     CodeEnums(wsdl, directory, namespaceName);
     foreach (var t in wsdl.Types.OrderBy(x => x.CodeName))
     {
         if (t is WsdlEnumType)
         {
             continue;
         }
         if (t.XName.Name == "ApiError")
         {
             CodeApiError(t, directory, namespaceName);
         }
         else
         {
             CodeComplexType(t, true, directory, namespaceName);
         }
     }
     foreach (var e in wsdl.Elements.OrderBy(x => x.CodeName))
     {
         CodeComplexType(e, e.Wsdl.Name == "", directory, namespaceName);
     }
     foreach (var pt in wsdl.PortTypes.OrderBy(x => x.CodeName))
     {
         CodePortType(pt, directory, namespaceName);
     }
     foreach (var b in wsdl.Bindings.OrderBy(x => x.CodeName))
     {
         CodeBinding(b, directory, namespaceName);
     }
     foreach (var s in wsdl.Services.OrderBy(x => x.CodeName))
     {
         CodeService(s, directory, namespaceName);
     }
     CodeEnumExtensions(wsdl, directory, namespaceName);
 }
Beispiel #6
0
 private void CodeEnums(WsdlStructure wsdl, string directory, string namespaceName)
 {
     using (var file = new CodeFile(directory, "Enums.cs"))
     {
         using (var nsScope = file.CreateNamespaceScope(namespaceName))
         {
             foreach (var t in wsdl.Types.OfType <WsdlEnumType>().OrderBy(x => x.CodeName))
             {
                 file.Comment(t.Documentation);
                 file.WriteLine($"public enum {t.CodeName}");
                 file.Ocb();
                 var lastItem = t.Items.Last();
                 foreach (var enumItem in t.Items)
                 {
                     file.Comment(enumItem.Documentation);
                     file.WriteLine(enumItem.CodeName + (enumItem == lastItem ? "" : ","));
                 }
                 file.Ccb();
                 file.WriteLine("");
             }
         }
     }
 }