Beispiel #1
0
        public void CreateModelStruct()
        {
            var model = _data.First(n => n.Name == "model");

            if (model == null)
            {
                return;
            }

            var name = model.Attributes["name"]?.InnerText ?? "";

            if (string.IsNullOrEmpty(name))
            {
                var comment = _data
                              .FirstOrDefault(n => n.GetType() == typeof(XmlComment))
                              ?.InnerText;
                var getName = new Regex(@" *\d+\: *(.*) *").Match(comment);
                if (getName.Success)
                {
                    name = getName.Groups[1].Value;
                }
            }

            name = name.Trim()
                   .Replace(" ", "_")
                   .Replace("-", "_");

            var structName = NamingConverter.SnakeToPascalCase(name);
            var fileName   = Path.Combine(_outputPath, $"{structName}.cs");

            CreateModelFile(fileName, structName, model);
        }
Beispiel #2
0
        private ViewModelInfo GetViewModelInfo(string route, string pageText)
        {
            route = string.IsNullOrEmpty(route)
                ? ""
                : route.Substring(0, 1).ToUpper() + route.Substring(1);
            var info = new ViewModelInfo(route.ToLower(), route + "Vm");

            var match = ExtractElement.Match(pageText);

            if (match.Success)
            {
                info.ElementName = NamingConverter.SnakeToPascalCase(route);
                if (!string.IsNullOrEmpty(match.Groups[2].Value))
                {
                    info.Bindings = match.Groups[2].Value
                                    .Split(',')
                                    .Select(b => b.Trim())
                                    .ToList();
                }
                info.VmName    = null;
                info.SortIndex = 0;
            }
            else
            {
                match = ExtractName.Match(pageText);
                if (match.Success)
                {
                    info.VmName = match.Groups[1].Value;
                }
                match = ExtractTitle.Match(pageText);
                if (match.Success)
                {
                    info.Title     = match.Groups[1].Value;
                    info.SortIndex = (string.IsNullOrEmpty(match.Groups[3].Value))
                        ? 0
                        : int.Parse(match.Groups[3].Value);
                }
                else
                {
                    info.Title = route;
                }
            }
            info.Visible   = info.SortIndex > 0;
            info.SortIndex = Math.Abs(info.SortIndex);

            if (!string.IsNullOrEmpty(info.VmName))
            {
                _viewModels.Add(info);
            }
            return(info);
        }
Beispiel #3
0
        static MethodMapEntry FromElement(XElement element, string parameterName)
        {
            var entry = new MethodMapEntry {
                JavaPackage   = element.Parent.Parent.XGetAttribute("name")?.Replace('.', '/'),
                JavaType      = element.Parent.XGetAttribute("name")?.Replace('.', '$'),
                JavaName      = element.XGetAttribute("name"),
                ParameterName = parameterName,
                ApiLevel      = NamingConverter.ParseApiLevel(element),
                IsInterface   = element.Parent.Name == "interface"
            };

            if (element.Name == "constructor")
            {
                entry.JavaName = "ctor";
            }

            return(entry);
        }
        public void ConvertTextToPascalIdentifier()
        {
            const string expected = "ThisIsThe1stIdentifier";
            var          texts    = new[]
            {
                "THIS_IS_THE_1ST_IDENTIFIER",
                "This is the 1st identifier!!!",
                "This is the 1st identifier.",
                "This, is the 1st identifier?",
                "This! is the 1st-identifier"
            };

            foreach (var text in texts)
            {
                var result = NamingConverter.ToPascalIdentifier(text);
                Assert.Equal(expected, result);
            }
        }
Beispiel #5
0
        public static ConstantEntry FromElement(XElement elem)
        {
            var entry = new ConstantEntry {
                Action        = ConstantAction.None,
                ApiLevel      = NamingConverter.ParseApiLevel(elem.Attribute("merge.SourceFile")?.Value),
                JavaSignature = elem.Parent.Parent.Attribute("name").Value,
                Value         = elem.Attribute("value")?.Value,
            };

            var java_package = elem.Parent.Parent.Attribute("name").Value.Replace('.', '/');
            var java_type    = elem.Parent.Attribute("name").Value.Replace('.', '$');
            var java_member  = elem.Attribute("name").Value;

            entry.JavaSignature = $"{java_package}/{java_type}.{java_member}".TrimStart('/');

            // Interfaces get an "I:" prefix
            if (elem.Parent.Name.LocalName == "interface")
            {
                entry.JavaSignature = "I:" + entry.JavaSignature;
            }

            return(entry);
        }
Beispiel #6
0
        private void CreateModelFile(string fileName, string structName, XmlNode model)
        {
            var id     = UniversalConverter.ConvertTo <long>(model.Attributes["id"].InnerText);
            var length = UniversalConverter.ConvertTo <long>(model.Attributes["len"].InnerText);

            if (structName == "Common" && id != 1)
            {
                // Test Model
                return;
            }

            Console.WriteLine($"Generating struct {structName}");

            foreach (var @using in GeneratorSettings.Usings)
            {
                _codeText.AppendLine($"using {@using};");
            }
            _codeText.AppendLine();

            _codeText.AppendLine("// ReSharper disable InconsistentNaming");
            _codeText.AppendLine("// ReSharper disable IdentifierTypo");
            _codeText.AppendLine("// ReSharper disable CommentTypo");
            _codeText.AppendLine("// ReSharper disable UnusedType.Global");
            _codeText.AppendLine("// ReSharper disable UnusedMember.Global");
            _codeText.AppendLine("// ReSharper disable MemberCanBePrivate.Global");
            _codeText.AppendLine("// ReSharper disable UnusedAutoPropertyAccessor.Local");
            _codeText.AppendLine("// ReSharper disable ArgumentsStyleLiteral");
            _codeText.AppendLine("// ReSharper disable BuiltInTypeReferenceStyle");
            _codeText.AppendLine();

            _codeText.AppendLine($"namespace {GeneratorSettings.Namespace}");
            _codeText.AppendLine("{");
            {
                AddComment("  ", null);

                _codeText.AppendLine($"  [SunSpecModel(id: {id}, length: {length})]");
                _codeText.AppendLine($"  public struct {structName}");
                _codeText.AppendLine("  {");
                {
                    var repeating = 1;
                    var blocks    = model.ChildNodes
                                    .Cast <XmlNode>()
                                    .Where(n => n.Name == "block")
                                    .ToArray();

                    foreach (var block in blocks)
                    {
                        var blockName = block.Attributes["name"]?.InnerText ?? $"Block{repeating++}";
                        var blockType = block.Attributes["type"]?.InnerText ?? "fixed";
                        var points    = block
                                        .ChildNodes
                                        .Cast <XmlNode>();

                        blockName = blockName.Trim()
                                    .Replace(" ", "_")
                                    .Replace("-", "_");


                        if (blockType == "fixed")
                        {
                            AddPoints("    ", points);
                        }
                        else
                        {
                            blockName = NamingConverter.SnakeToPascalCase(blockName);
                            _codeText.AppendLine($"    public struct S_{blockName}");
                            _codeText.AppendLine("    {");
                            AddPoints("      ", points);
                            _codeText.AppendLine("    };");
                            _codeText.AppendLine($"    public S_{blockName}[] {blockName};");
                        }
                    }
                }
                _codeText.AppendLine("  }");
            }
            _codeText.AppendLine("}");
            File.WriteAllText(fileName, _codeText.ToString());
        }
        public void LowerPascalUpperToPascal()
        {
            var result = NamingConverter.ToPascalCase(Camel);

            Assert.Equal(Pascal, result);
        }
        public void UpperPascalToLowerPascal()
        {
            var result = NamingConverter.ToCamelCase(Pascal);

            Assert.Equal(Camel, result);
        }
        public void SnakeToPascal()
        {
            var result = NamingConverter.SnakeToPascalCase(Snake);

            Assert.Equal(Pascal, result);
        }
        public void KebabToPascal()
        {
            var result = NamingConverter.KebabToPascalCase(Kebab);

            Assert.Equal(Pascal, result);
        }
        public void LowerPascalToKebab()
        {
            var result = NamingConverter.PascalToKebabCase(Camel);

            Assert.Equal(Kebab, result);
        }