Exemplo 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}");
        }
Exemplo n.º 2
0
        public void ApiBuildTest()
        {
            var builder = new TypeScriptApiBuilder("");

            builder.CacheType <SimpleController>();

            var tscode       = builder.Compile();
            var expectedCode = $"{TestUtil.DeclareContent}\r\n{File.ReadAllText($"{nameof(ApiBuildTest)}.ts")}";

            Assert.Equal(expectedCode, tscode);
        }