/// <summary>
        /// Génère les définitions Typescript.
        /// </summary>
        /// <param name="rootNamespace">Namespace de l'application.</param>
        /// <param name="parameters">Paramètres.</param>
        /// <param name="modelRootList">La liste des modèles.</param>
        public static void Generate(string rootNamespace, JavascriptParameters parameters, ICollection <ModelRoot> modelRootList)
        {
            if (parameters.ModelOutputDirectory == null)
            {
                return;
            }

            var nameSpaceMap = new Dictionary <string, List <ModelClass> >();

            foreach (var model in modelRootList)
            {
                foreach (var modelNameSpace in model.Namespaces.Values)
                {
                    var namespaceName = TSUtils.ToNamespace(modelNameSpace.Name);

                    if (!nameSpaceMap.ContainsKey(namespaceName))
                    {
                        nameSpaceMap.Add(namespaceName, new List <ModelClass>());
                    }

                    nameSpaceMap[namespaceName].AddRange(modelNameSpace.ClassList);
                }
            }

            foreach (var entry in nameSpaceMap)
            {
                var staticLists = new List <ModelClass>();

                foreach (var model in entry.Value)
                {
                    if (!model.IsStatique)
                    {
                        if (!parameters.IsGenerateEntities && model.DataContract.IsPersistent)
                        {
                            continue;
                        }

                        var fileName = model.Name.ToDashCase();
                        Console.Out.WriteLine($"Generating Typescript file: {fileName}.ts ...");

                        fileName = $"{parameters.ModelOutputDirectory}/{entry.Key.ToDashCase(false)}/{fileName}.ts";
                        var fileInfo = new FileInfo(fileName);

                        var isNewFile = !fileInfo.Exists;

                        var directoryInfo = fileInfo.Directory;
                        if (!directoryInfo.Exists)
                        {
                            Directory.CreateDirectory(directoryInfo.FullName);
                        }

                        var template = new TypescriptTemplate {
                            RootNamespace = rootNamespace, Model = model
                        };
                        var result = template.TransformText();
                        File.WriteAllText(fileName, result, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
                    }
                    else
                    {
                        staticLists.Add(model);
                    }
                }

                if (staticLists.Any())
                {
                    Console.Out.WriteLine($"Generating Typescript file: references.ts ...");
                    var fileName = $"{parameters.ModelOutputDirectory}/{entry.Key.ToDashCase(false)}/references.ts";
                    var fileInfo = new FileInfo(fileName);

                    var isNewFile = !fileInfo.Exists;

                    var directoryInfo = fileInfo.Directory;
                    if (!directoryInfo.Exists)
                    {
                        Directory.CreateDirectory(directoryInfo.FullName);
                    }

                    var template = new ReferenceTemplate {
                        References = staticLists.OrderBy(r => r.Name)
                    };
                    var result = template.TransformText();
                    File.WriteAllText(fileName, result, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Create the template output
        /// </summary>
        public virtual string TransformText()
        {
            Write("/*\r\n    Ce fichier a été généré automatiquement.\r\n    Toute modification sera per" +
                  "due.\r\n*/\r\n\r\nimport {EntityToType, StoreNode} from \"focus4/entity\";\r\nimport {");

            Write(string.Join(", ", GetDomainList()));
            Write("} from \"../../domains\";\r\n");

            var imports = GetImportList();

            foreach (var import in imports)
            {
                Write("\r\nimport {");
                Write(import.import);
                Write("} from \"");
                Write(import.path);
                Write("\";");
            }
            if (imports.Any())
            {
                Write("\r\n");
            }

            Write("\r\nexport type ");
            Write(Model.Name);
            Write(" = EntityToType<typeof ");
            Write(Model.Name);
            Write("Entity>;\r\nexport type ");
            Write(Model.Name);
            Write("Node = StoreNode<typeof ");
            Write(Model.Name);
            Write("Entity>;\r\n\r\nexport const ");
            Write(Model.Name);
            Write("Entity = {\r\n    name: \"");
            Write(Model.Name.ToFirstLower());
            Write("\",\r\n    fields: {\r\n");

            if (Model.ParentClass != null)
            {
                Write("        ...");
                Write(Model.ParentClass.Name);
                Write("Entity.fields,\r\n");
            }

            foreach (var property in Model.PropertyList)
            {
                Write("        ");
                Write(property.Name.ToFirstLower());
                Write(": {\r\n            type: ");
                if (IsArray(property))
                {
                    Write("\"list\" as \"list\"");
                }
                else if (property.IsFromComposition)
                {
                    Write("\"object\" as \"object\"");
                }
                else
                {
                    Write("\"field\" as \"field\"");
                }

                Write(",\r\n        ");

                if (GetDomain(property) != null)
                {
                    Write("    name: \"");
                    Write(property.Name.ToFirstLower());
                    Write("\",\r\n            fieldType: {} as ");
                    Write(TSUtils.CSharpToTSType(property));
                    Write(",\r\n            domain: ");
                    Write(GetDomain(property));
                    Write(",\r\n            isRequired: ");
                    Write((property.DataMember.IsRequired && (!property.IsPrimaryKey || property.DataType != "int?")).ToString().ToFirstLower());
                    Write(",\r\n            label: \"");
                    Write(TSUtils.ToNamespace(Model.Namespace.Name));
                    Write(".");
                    Write(Model.Name.ToFirstLower());
                    Write(".");
                    Write(property.Name.ToFirstLower());
                    Write("\"\r\n        ");
                }
                else
                {
                    Write("    entity: ");
                    Write(GetReferencedType(property));
                    Write("Entity\r\n        ");
                }

                Write("}");

                if (property != Model.PropertyList.Last())
                {
                    Write(",");
                }

                Write("\r\n");
            }

            Write("    }\r\n};\r\n");

            if (Model.IsReference)
            {
                Write("\r\nexport const ");
                Write(Model.Name.ToFirstLower());
                Write(" = {type: {} as ");
                Write(Model.Name);
                Write(", valueKey: \"");
                Write(Model.PrimaryKey.First().Name.ToFirstLower());
                Write("\", labelKey: \"");
                Write(Model.DefaultProperty?.ToFirstLower() ?? "libelle");
                Write("\"};\r\n");
            }

            return(GenerationEnvironment.ToString());
        }