private static void GenerateClass(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport class {0}", type.Name); var interfaces = type.GetInterfaces().Where(x => x.GetCustomAttribute <TypeScriptAttribute>() != null); if (interfaces.Any()) { stringBuilder.AppendFormat(" implements {0}", string.Join(",", interfaces.Select(x => x.Name))); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, true)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic {0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } if (attribute.GenerateTypeProperty) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\tpublic type: string = \"{0}\";", type.Name); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }
private static void GenerateInterface(StringBuilder stringBuilder, Type type, TypeScriptAttribute attribute, bool camelCase) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\texport interface {0}", type.Name); var extendingInterfaces = new List <string>(); if (type.BaseType != null) { var baseAttribute = type.BaseType.GetCustomAttributes <TypeScriptAttribute>().OfType <TypeScriptAttribute>().FirstOrDefault(); if (baseAttribute != null) { extendingInterfaces.Add(string.Format("{0}.{1}", type.BaseType.Namespace, type.BaseType.Name)); } } if (extendingInterfaces.Any()) { stringBuilder.AppendFormat(" extends {0}", string.Join(", ", extendingInterfaces)); } stringBuilder.Append(" {"); foreach (var propertyInfo in GetAllProperties(type, false)) { stringBuilder.AppendLine(); stringBuilder.AppendFormat("\t\t{0}: {1};", camelCase ? ToCamelCase(propertyInfo.Name) : propertyInfo.Name, TypeScriptConvert.ToScriptType(propertyInfo.PropertyType)); } stringBuilder.AppendLine(); stringBuilder.Append("\t}"); }
private static int GenerateTypescript(Options options) { Console.WriteLine("Assemblies to parse: {0}", string.Join(",", options.InputAssemblies.ToArray())); Console.WriteLine("Output file: " + options.OutputFile); Console.WriteLine("Camelcase: " + options.CamelCase); foreach (var typeMap in options.BuiltinTypeMappings) { var parsedMap = typeMap.Split('@'); Console.WriteLine("Adding default type for {0} mapping on {1}", parsedMap[0], parsedMap[1]); TypeScriptConvert.AddTypeMapping(Type.GetType(parsedMap[0], true, true), parsedMap[1]); } var assemblies = options.InputAssemblies.Select(Assembly.LoadFrom).ToArray(); var typescript = options.CamelCase ? TypeScriptBuilder.GetTypescriptContractsCamelCase(assemblies) : TypeScriptBuilder.GetTypescriptContracts(assemblies); File.WriteAllText(options.OutputFile, typescript, Encoding.UTF8); return(0); }