Exemplo n.º 1
0
        public static void FromControllerRoslyn(string textFilePath, string outputNamespace, WriterConfig config, TextWriter w)
        {
            var tc = new NetTsControllerConverter();

            var css = new RoslynTypeScanner(outputNamespace);

            css.RegisterCodeFile(textFilePath);

            var ww = new TsWriter(config, w, css.NetAssembly.Namespaces.Select(n => n.Name));

            w.Write(RenderingHelpers.GetHeader(new[] { outputNamespace }));

            var netClasses = css.NetAssembly.Namespaces.SelectMany(netNamespace => netNamespace.TypeDeclarations).OfType <NetClass>().ToList();

            var tsNamespace = new TsNamespace
            {
                Name = outputNamespace
            };

            foreach (var netClass in netClasses)
            {
                var module = tc.GetControllerTsModule(netClass);
                tsNamespace.Modules.Add(module);
            }

            w.Write(ww.WriteNamespace(tsNamespace, false));
        }
Exemplo n.º 2
0
        public static void FromEnumRoslyn(IEnumerable <string> inputFiles, string outputNamespace, WriterConfig config, TextWriter w)
        {
            var tc = new NetTsPocoConverter();

            var css = new RoslynTypeScanner(outputNamespace);

            foreach (var filePath in inputFiles)
            {
                css.RegisterCodeFile(filePath);
            }

            var ww = new TsWriter(config, w, css.NetAssembly.Namespaces.Select(n => n.Name));

            w.Write(RenderingHelpers.GetHeader(new[] { outputNamespace }));

            var netEnums = css.NetAssembly.Namespaces.SelectMany(netNamespace => netNamespace.TypeDeclarations).OfType <NetEnum>().ToList();

            var tsNamespace = new TsNamespace
            {
                Name = outputNamespace
            };

            //write enums
            foreach (var netEnum in netEnums)
            {
                //var tsEnum = tc.GetTsEnum(netEnum);
                //tsEnum.IsPublic = true;

                tsNamespace.TypeDeclarations.Add(tc.GetTsEnum(netEnum));
            }

            w.Write(ww.WriteNamespace(tsNamespace, false));
        }
Exemplo n.º 3
0
        public static void FromPocoRoslyn(IEnumerable <string> inputFiles, string outputNamespace, WriterConfig config, TextWriter w)
        {
            var tc = new NetTsPocoConverter();

            var css = new RoslynTypeScanner(outputNamespace);

            foreach (var filePath in inputFiles)
            {
                css.RegisterCodeFile(filePath);
            }

            var ww = new TsWriter(config, w, css.NetAssembly.Namespaces.Select(n => n.Name));

            w.Write(RenderingHelpers.GetHeader(new[] { outputNamespace }));

            var netClasses = css.NetAssembly.Namespaces.SelectMany(netNamespace => netNamespace.TypeDeclarations).OfType <NetClass>().ToList();

            var tsNamespace = new TsNamespace
            {
                Name = outputNamespace
            };

            //write classes as export interfaces
            foreach (var netClass in netClasses)
            {
                if (netClass.Attributes.All(a => a != "TsExport"))
                {
                    continue;
                }
                tsNamespace.TypeDeclarations.Add(tc.GetTsInterface(netClass));
            }

            w.Write(ww.WriteNamespace(tsNamespace, true));
        }
Exemplo n.º 4
0
        public virtual string WriteNamespace(TsNamespace netModule, bool isTopLevelDeclareNamespace)
        {
            var typeDeclarationsContent      = string.Join(_config.NewLines(1), netModule.TypeDeclarations.Select(WriteType));
            var functionDeclarationsContent  = string.Join(_config.NewLines(1), netModule.FunctionDeclarations.Select(fd => WriteMethod(fd, true)));
            var namespaceDeclarationsContent = string.Join(_config.NewLines(1), netModule.Modules.Select(ns => WriteModule(ns, false)));

            var content = JoinBodyText(typeDeclarationsContent, functionDeclarationsContent, namespaceDeclarationsContent);

            if (!string.IsNullOrWhiteSpace(content))
            {
                content = content.Indent(_indent) + _config.NewLine;
            }

            var declare = isTopLevelDeclareNamespace ? "declare " : "";

            return($@"{declare}namespace {netModule.Name}" + _config.NewLine +
                   @"{" + _config.NewLine +
                   content +
                   @"}");
        }
Exemplo n.º 5
0
        public void Simple()
        {
            var tsNamespace = new TsNamespace
            {
                Name         = "SomeNamespace",
                ExportKind   = TsExportKind.Named,
                Declarations = new[]
                {
                    new TsEnum
                    {
                        Name       = "SomeEnum",
                        ExportKind = TsExportKind.Named,
                        Values     = new[]
                        {
                            new TsEnumNumberValue
                            {
                                Name  = "FirstValue",
                                Value = 0
                            },
                            new TsEnumNumberValue
                            {
                                Name  = "SecondValue",
                                Value = 1
                            }
                        }
                    }
                }
            };

            tsNamespace.ShouldBeTranslatedTo("export namespace SomeNamespace {",
                                             "    export enum SomeEnum {",
                                             "        FirstValue = 0,",
                                             "        SecondValue = 1,",
                                             "    }",
                                             "}");
        }