Exemplo n.º 1
0
        public CompiledBinding AddBinding(TemplateNode templateNode, CompiledBindingType bindingType)
        {
            CompiledBinding  binding = new CompiledBinding();
            TemplateRootNode root    = templateNode as TemplateRootNode ?? templateNode.root;

            binding.filePath     = root.templateShell.filePath;
            binding.bindingType  = bindingType;
            binding.elementTag   = templateNode.originalString;
            binding.bindingId    = compiledBindings.size;
            binding.guid         = Guid.NewGuid().ToString().Replace('-', '_');
            binding.templateName = root.templateName;
            compiledBindings.Add(binding);
            return(binding);
        }
Exemplo n.º 2
0
        private static void GenerateTemplateCode(string path, string extension, CompiledTemplateData compiledTemplateData)
        {
            TemplateSettings templateSettings = compiledTemplateData.templateSettings;

            for (int i = 0; i < compiledTemplateData.compiledTemplates.size; i++)
            {
                CompiledTemplate compiled = compiledTemplateData.compiledTemplates.array[i];

                string file = compiled.filePath;

                if (compiled.elementType.rawType.IsGenericType)
                {
                    file = Path.ChangeExtension(file, "");
                    file = file.Substring(0, file.Length - 1);

                    if (!string.IsNullOrEmpty(compiled.templateName))
                    {
                        file += "__" + compiled.templateName;
                    }

                    string typeName = compiled.elementType.rawType.ToString();
                    int    start    = typeName.IndexOf('[');
                    file += typeName.Substring(start);
                    file  = Path.Combine(path, file + extension);
                }
                else
                {
                    if (!string.IsNullOrEmpty(compiled.templateName))
                    {
                        file  = Path.ChangeExtension(file, "");
                        file  = file.Substring(0, file.Length - 1);
                        file += "__" + compiled.templateName;
                        file  = Path.Combine(path, Path.ChangeExtension(file, extension));
                    }
                    else
                    {
                        file = Path.Combine(path, Path.ChangeExtension(file, extension));
                    }
                }

                Directory.CreateDirectory(Path.GetDirectoryName(file));

                string bindingCode = string.Empty;
                string slotCode    = string.Empty;

                CompiledTemplate compiledTemplate = compiledTemplateData.compiledTemplates[i];

                LightList <CompiledBinding> compiledBindings = compiledTemplate.bindings;
                LightList <CompiledSlot>    compiledSlots    = compiledTemplate.slots;

                if (compiledBindings != null)
                {
                    for (int b = 0; b < compiledBindings.size; b++)
                    {
                        CompiledBinding binding = compiledBindings[b];
                        bindingCode += $"\n{s_Indent8}// binding id = {binding.bindingId}";
                        bindingCode += $"\n{s_Indent8}public Action<UIElement, UIElement> Binding_{compiledBindings.array[b].bindingType}_{binding.guid} = ";
                        bindingCode += binding.bindingFn.ToTemplateBodyFunction();
                        bindingCode += "\n";
                    }
                }

                if (compiledSlots != null)
                {
                    for (int s = 0; s < compiledSlots.size; s++)
                    {
                        CompiledSlot compiledSlot = compiledSlots[s];

                        if (compiledSlot.filePath == compiledTemplate.filePath && compiledSlot.templateName == compiled.templateName)
                        {
                            slotCode += $"\n{s_Indent8}// {compiledSlot.GetComment()}";
                            slotCode += $"\n{s_Indent8}public Func<UIElement, UIElement, TemplateScope, UIElement> {compiledSlot.GetVariableName()} = ";
                            slotCode += compiledSlot.templateFn.ToTemplateBodyFunction();
                            slotCode += "\n";
                        }
                    }
                }

                string templateBody = compiledTemplate.templateFn.ToTemplateBodyFunction();
                string template     = TemplateConstants.TemplateSource;
                template = template.Replace("::TEMPLATE_COMMENT::", compiledTemplate.templateMetaData.filePath);
                template = template.Replace("::GUID::", compiledTemplate.guid);
                template = template.Replace("::CODE::", templateBody);
                template = template.Replace("::BINDINGS::", bindingCode);
                template = template.Replace("::SLOTS::", slotCode);
                template = template.Replace("::APPNAME::", templateSettings.StrippedApplicationName);
                File.WriteAllText(file, template);
            }
        }