Ejemplo n.º 1
0
        public static string GetTypeScriptTypeName(Type type)
        {
            if (type.IsGenericType)
            {
                var def = type.GetGenericTypeDefinition();
                if (def == typeof(List <>))
                {
                    return($"{TypeScript.GetTypeScriptTypeName(type.GetGenericArguments()[0])}[]");
                }
                if (def == typeof(Dictionary <,>))
                {
                    return($"Record<{string.Join(", ", type.GetGenericArguments().Select(at => TypeScript.GetTypeScriptTypeName(at)))}>");
                }
            }
            switch (type.Name)
            {
            case "String":
                return("string");

            case "Boolean":
                return("boolean");

            case "Int32":
            case "Double":
                return("number");
            }
            return(type.Name);
        }
Ejemplo n.º 2
0
            public PropertyInfo(FieldInfo info)
            {
                var typeAlias = info.GetCustomAttribute <TypeAliasAttribute>()?.alias;

                this.name     = info.Name;
                this.optional = Nullable.GetUnderlyingType(info.FieldType) != null;
                this.type     = typeAlias ?? TypeScript.GetTypeScriptTypeName(this.optional
          ? info.FieldType.GetGenericArguments()[0]
          : info.FieldType);
                if (info.GetCustomAttributes().Any(a => (a as NullableAttribute) != null))
                {
                    this.optional = true;
                }
            }
Ejemplo n.º 3
0
        static public (Dictionary <string, XElement>, Dictionary <string, List <Asset> >) Convert(string input, string output)
        {
            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }
            var raw = Path.Combine(output, "raw");

            if (!Directory.Exists(raw))
            {
                Directory.CreateDirectory(raw);
            }

            var assetsDoc     = XDocument.Load(Path.Combine(input, "assets.xml"));
            var propertiesDoc = XDocument.Load(Path.Combine(input, "properties.xml"));
            var templatesDoc  = XDocument.Load(Path.Combine(input, "templates.xml"));

            var(assetsMap, assetsDict) = Asset.ResolveRaw(assetsDoc);

            foreach ((string template, List <XElement> list) in assetsDict)
            {
                var dest    = Path.Combine(raw, $"{template}.xml");
                var destDir = Path.GetDirectoryName(dest);
                if (!Directory.Exists(destDir))
                {
                    Directory.CreateDirectory(destDir);
                }
                new XDocument(new XElement("Assets", new XAttribute("Count", list.Count), list)).SaveXML(dest);
                Console.WriteLine(dest);
            }

            var templatesMap = Asset.ResolveTemplates(templatesDoc);

            var(defaultContainerValues, defaultValues) = Asset.ResolveProperties(propertiesDoc);
            Asset.ResolveInheritance(assetsMap, templatesMap);
            Asset.ResolveDefaultValues(assetsMap, defaultContainerValues, defaultValues);
            var dataDict = Asset.AdaptAll(assetsMap);

            foreach ((string template, List <Asset> dataList) in dataDict)
            {
                var dest = Path.Combine(output, $"{template}.json");
                IO.Save(JsonConvert.SerializeObject(dataList, Formatting.Indented), dest);
                Console.WriteLine(dest);
            }

            {
                var dest = Path.Combine(output, "template-asset-map.json");
                var list = dataDict.Values.Aggregate(new List <Asset>(), (agg, cur) => {
                    agg.AddRange(cur);
                    return(agg);
                });
                var map = typeof(Asset)
                          .Assembly
                          .GetTypes()
                          .Where(t => t.IsSubclassOf(typeof(Asset)))
                          .ToDictionary(
                    t => t.Name,
                    t => list.Where(a => a.GetType() == t || a.GetType().IsSubclassOf(t)).Select(a => a.guid).ToList()
                    );
                IO.Save(JsonConvert.SerializeObject(map, Formatting.Indented), dest);
                Console.WriteLine(dest);
            }

            {
                var dest = Path.Combine(output, "definition.ts");
                IO.Save(TypeScript.GetAll(dataDict, typeof(Asset), typeof(BaseAssetObject)), dest);
                Console.WriteLine(dest);
            }

            var assetsTotal = assetsDict.Values.Aggregate(0, (total, list) => total += list.Count);
            var dataTotal   = dataDict.Values.Aggregate(0, (total, list) => total += list.Count);

            Console.WriteLine($"Assets: {assetsTotal}, {dataTotal} extracted.");

            return(assetsMap, dataDict);
        }