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
        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 SnakeToPascal()
        {
            var result = NamingConverter.SnakeToPascalCase(Snake);

            Assert.Equal(Pascal, result);
        }