Ejemplo n.º 1
0
        public override void Run()
        {
            var targetAssemblyName = Project.AssemblyName;
            var assemblyContext    = new AssemblyContext($"{TargetBinFolder}/{targetAssemblyName}.dll", DotNetFramework.Parse(Project.TargetFramework));

            string outFile;

            // if Directory
            if (Directory.Exists(Out) || Path.GetExtension(Out) == "" || Out.Last().For(c => c == '/' || c == '\\'))
            {
                if (!Directory.Exists(Out))
                {
                    Directory.CreateDirectory(Out);
                }
                outFile = Path.GetFullPath(Path.Combine(Out, $"{targetAssemblyName}.api.ts"));
            }
            // if File
            else
            {
                var dir = Path.GetDirectoryName(Out);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                outFile = Path.GetFullPath(Out);
            }

            var builder    = new TypeScriptApiBuilder(Uri);
            var markAttr   = assemblyContext.GetType($"{nameof(TypeSharp)}.{nameof(TypeScriptApiAttribute)},{nameof(TypeSharp)}");
            var modelTypes = assemblyContext.RootAssembly.GetTypesWhichMarkedAs(markAttr);

            builder.CacheTypes(modelTypes);

            foreach (var include in Includes)
            {
                var type = assemblyContext.GetType(include);
                if (type == null)
                {
                    throw new ArgumentException($"Can not find type({type.FullName}).");
                }
                builder.CacheType(type);
            }

            foreach (var relative in Relatives)
            {
                if (relative.Count(";") == 1)
                {
                    var pair = relative.Split(";");
                    builder.AddDeclaredType(assemblyContext.GetType(pair[0]), pair[1]);
                }
                else
                {
                    throw new ArgumentException("Each parameter('Relative') must contain a semicolon(;).");
                }
            }

            builder.WriteTo(outFile);

            Console.WriteLine($"File saved: {outFile}");
        }
Ejemplo n.º 2
0
        private static void GenerateTypeScript(string outFolder, bool setBOM)
        {
            if (!Directory.Exists(outFolder))
            {
                Directory.CreateDirectory(outFolder);
            }

            var targetAssemblyName = Program.ProjectInfo.AssemblyName;
            var assemblyContext    = new AssemblyContext($"{TargetBinFolder}/{targetAssemblyName}.dll", DotNetFramework.Parse(Program.ProjectInfo.TargetFramework));

            var dbContextType = assemblyContext.GetType($"Microsoft.EntityFrameworkCore.DbContext,Microsoft.EntityFrameworkCore");
            var types         = assemblyContext.RootAssembly.GetTypesWhichExtends(dbContextType, true);

            foreach (var type in types)
            {
                var outFile = $"{Path.GetFullPath($"{outFolder}/{type.Name}.html")}";

                var builder = new DbStructureBuilder();
                builder.Cache(type);
                var csvContent = builder.GetHtml();
                var bytes      = setBOM
                    ? new byte[] { 0xef, 0xbb, 0xbf }.Concat(csvContent.Bytes(Encoding.UTF8)).ToArray()
                    : csvContent.Bytes(Encoding.UTF8).ToArray();

                File.WriteAllBytes(outFile, bytes);
                Console.WriteLine($"File Saved: {outFile}");
            }
        }