Beispiel #1
0
        public static Code FromClassItem(Class source, GeneratorSettings settings)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            Code.ValidateSettings(settings);

            Code result = Code.CreateInstance(settings);

            List <String> lines = new List <String>();

            result.Filename = result.GetFilename(source.Name);
            result.AddHeader(lines, settings);
            result.AddImports(source, lines, settings);

            result.AddNamespaceOpen(lines, settings);

            result.AddClassOpen(source, lines, settings);

            result.AddProperties(source, lines, settings);

            result.AddClassClose(lines);

            result.AddNamespaceClose(lines);

            result.Lines = lines;

            return(result);
        }
Beispiel #2
0
        protected Boolean TryGetAttribute(GeneratorSettings settings, Class source, out String attribute)
        {
            attribute = String.Empty;

            if (settings is null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (settings.SourceType == SourceType.Xml)
            {
                if (source.IsClass)
                {
                    if (source.Entity.HasXmlNamespace)
                    {
                        attribute = String.Format("XmlRoot({0}, {1})",
                                                  String.Format(this.XmlElementNameFormat, source.ClassName),
                                                  String.Format(this.XmlNamespaceFormat, source.Entity.XmlNamespace));
                    }
                    else
                    {
                        attribute = String.Format("XmlRoot(\"{0}\")", source.ClassName);
                    }

                    return(true);
                }
            }

            return(false);
        }
Beispiel #3
0
        protected override void AddClassOpen(Class source, IList <String> lines, GeneratorSettings settings)
        {
            String indent = base.GetIndent(1);

            if (base.TryGetAttribute(settings, source, out String attribute))
            {
                lines.Add($"{indent}<{attribute}>");
            }

            lines.Add($"{indent}Public Class {source.Name}");
            lines.Add(String.Empty);
        }
Beispiel #4
0
        protected Boolean TryGetAttribute(GeneratorSettings settings, Property source, out String attribute)
        {
            attribute = String.Empty;

            if (settings is null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (settings.SourceType == SourceType.Json)
            {
                switch (settings.AttributeType)
                {
                case AttributeType.Newtonsoft:
                    attribute = $"JsonProperty(\"{source.AttributeName}\")";
                    return(true);

                case AttributeType.Microsoft:
                    attribute = $"JsonPropertyName(\"{source.AttributeName}\")";
                    return(true);

                default:
                    return(false);
                }
            }
            else if (settings.SourceType == SourceType.Xml)
            {
                if (source.Origin.StartsWith("@", StringComparison.InvariantCultureIgnoreCase))
                {
                    attribute = $"XmlAttribute(\"{source.Origin.TrimStart('@')}\")";
                    return(true);
                }

                if (source.Origin.Equals("#text", StringComparison.InvariantCultureIgnoreCase))
                {
                    // Must be XmlText no matter what the real type is!
                    attribute = "XmlText";
                    return(true);
                }

                attribute = $"XmlElement(\"{source.AttributeName}\")";
                return(true);
            }

            return(false);
        }
Beispiel #5
0
        public static Code FromClassList(IEnumerable <Class> sources, GeneratorSettings settings)
        {
            if (sources == null || !sources.Any())
            {
                throw new ArgumentNullException(nameof(sources));
            }

            Code.ValidateSettings(settings);

            Code result = Code.CreateInstance(settings);

            List <String> lines = new List <String>();

            result.Filename = result.GetFilename(settings.RootClass);
            result.AddHeader(lines, settings);

            List <String> imports = new List <String>();

            foreach (Class source in sources)
            {
                result.AddImports(source, imports, settings);
            }

            imports = imports
                      .Where(x => !String.IsNullOrWhiteSpace(x))
                      .Distinct()
                      .OrderBy(x => x, new ImportsComparer(settings.TargetType))
                      .ToList();

            imports.Add(String.Empty);

            lines.AddRange(imports);

            result.AddNamespaceOpen(lines, settings);

            foreach (Class source in sources)
            {
                result.AddClassOpen(source, lines, settings);
                result.AddProperties(source, lines, settings);
                result.AddClassClose(lines);
                lines.Add(String.Empty);
            }

            lines.RemoveAt(lines.Count - 1);

            result.AddNamespaceClose(lines);

            result.Lines = lines;

            return(result);
        }
Beispiel #6
0
        private static Code CreateInstance(GeneratorSettings settings)
        {
            if (settings.TargetType == TargetType.CSharp)
            {
                return(new CodeCs());
            }

            if (settings.TargetType == TargetType.VisualBasic)
            {
                return(new CodeVb());
            }

            throw new InvalidOperationException($"Type of {settings.TargetType} is not valid.");
        }
Beispiel #7
0
        private static void ValidateSettings(GeneratorSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            if (!settings.IsValid)
            {
                throw new InvalidOperationException("Generator settings are not considered as valid.");
            }

            if (settings.TargetType != TargetType.CSharp && settings.TargetType != TargetType.VisualBasic)
            {
                throw new NotSupportedException($"Target type of {settings.TargetType} is not supported");
            }
        }
Beispiel #8
0
        protected void AddHeader(String comment, IList <String> lines, GeneratorSettings settings)
        {
            lines.Add($"{comment} <auto-generated>");
            lines.Add($"{comment}");
            lines.Add($"{comment} This code was generated by a tool.");
            lines.Add($"{comment}");
            lines.Add($"{comment} Generator: {settings.ProductName}");
            lines.Add($"{comment} Version:   {settings.ProductVersion}.");
            lines.Add($"{comment} Timestamp: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");

            if (settings.HasPackageName)
            {
                lines.Add($"{comment}");
                lines.Add($"{comment} NuGet package \"{settings.PackageName}\" should ");
                lines.Add($"{comment} be installed to be able to use this code.");
            }

            lines.Add($"{comment}");
            lines.Add($"{comment} </auto-generated>");

            lines.Add(String.Empty);
        }
Beispiel #9
0
        protected override void AddProperties(Class source, IList <String> lines, GeneratorSettings settings)
        {
            String indent = base.GetIndent(2);

            // {{0}} => Type
            // {{1}} => Name

            String format = String.Empty;

            if (settings.MemberType == MemberType.Property)
            {
                format = $"{indent}Public Property {{1}} As {{0}}";
            }

            if (settings.MemberType == MemberType.Field)
            {
                format = $"{indent}Public {{1}} As {{0}}";
            }

            foreach (Property property in source.Properties)
            {
                if (property.IsComment)
                {
                    lines.Add($"{indent}' {property.Comment}");
                }

                if (base.TryGetAttribute(settings, property, out String attribute))
                {
                    lines.Add($"{indent}<{attribute}>");
                }

                lines.Add(String.Format(format, base.GetTypeDescriptor(property), base.GetPropertyName(source, property)));

                lines.Add(String.Empty);
            }

            lines.RemoveAt(lines.Count - 1);
        }
Beispiel #10
0
        protected override void AddImports(Class source, IList <String> lines, GeneratorSettings settings)
        {
            List <String> items = source.Namespaces.ToList();

            if (settings.SourceType == SourceType.Json && source.Properties.Any(x => x.IsOrigin))
            {
                items.AddRange(settings.PackageNamespaces);
            }

            if (settings.SourceType == SourceType.Xml)
            {
                items.AddRange(settings.PackageNamespaces);
            }

            items.Sort();

            foreach (String item in items)
            {
                lines.Add($"Imports {item}");
            }

            lines.Add(String.Empty);
        }
Beispiel #11
0
 protected abstract void AddProperties(Class source, IList <String> lines, GeneratorSettings settings);
Beispiel #12
0
 protected abstract void AddClassOpen(Class source, IList <String> lines, GeneratorSettings settings);
Beispiel #13
0
 protected abstract void AddNamespaceOpen(IList <String> lines, GeneratorSettings settings);
Beispiel #14
0
 protected abstract void AddHeader(IList <String> lines, GeneratorSettings settings);
Beispiel #15
0
 protected override void AddHeader(IList <String> lines, GeneratorSettings settings)
 {
     base.AddHeader("'", lines, settings);
 }
Beispiel #16
0
 protected override void AddNamespaceOpen(IList <String> lines, GeneratorSettings settings)
 {
     lines.Add($"Namespace {settings.Namespace}");
     lines.Add(String.Empty);
 }