示例#1
0
        public void JSendTest()
        {
            var builder = new TypeScriptModelBuilder();

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

            Assert.Equal(expectedCode, tscode);
        }
示例#2
0
        public void GenericTest2()
        {
            var builder = new TypeScriptModelBuilder();

            builder.CacheType(Type.GetType("TypeSharp.Test.GenericClass`1"));
            var tscode = builder.Compile(new CompileOptions
            {
                OutputNames = true,
            });
            var expectedCode = $"{TestUtil.DeclareContent}\r\n\r\n{File.ReadAllText($"{nameof(GenericTest2)}.ts")}";

            Assert.Equal(expectedCode, tscode);
        }
示例#3
0
        public void SimpleTest()
        {
            var types   = Assembly.GetExecutingAssembly().GetTypesWhichMarkedAs <TypeScriptModelAttribute>();
            var builder = new TypeScriptModelBuilder();

            builder.CacheType <RootClass>();
            builder.CacheType <JSend>();

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

            Assert.Equal(expectedCode, tscode);
        }
示例#4
0
        public void TypeSharpTest()
        {
            var typeSharpVersion = typeof(TypeScriptModelBuilder).Assembly.GetName().Version;
            var builder          = new TypeScriptModelBuilder();

            builder.CacheType(typeof(JSend <>));

            var code = builder.Compile();

            Assert.Equal($@"/* Generated by TypeSharp v{typeSharpVersion} */" +
                         @"

declare namespace Ajax {
    interface JSend<TData> {
        status?: string;
        data?: TData;
        exData?: any;
        message?: string;
        code?: string;
    }
}
", code);
        }
        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}.ts"));
            }
            // if File
            else
            {
                var dir = Path.GetDirectoryName(Out);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                outFile = Path.GetFullPath(Out);
            }

            var builder    = new TypeScriptModelBuilder();
            var markAttr   = assemblyContext.GetType($"{nameof(TypeSharp)}.{nameof(TypeScriptModelAttribute)},{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({include}).");
                }
                builder.CacheType(type);
            }

            foreach (var relative in Relatives)
            {
                if (relative.Count(";") == 1)
                {
                    var pair           = relative.Split(";");
                    var type           = assemblyContext.GetType(pair[0]) ?? Type.GetType(pair[0]);
                    var typescriptName = pair[1];

                    if (type == null)
                    {
                        throw new ArgumentException($"Can not find type for the specified string({relative}).");
                    }
                    builder.AddDeclaredType(type, typescriptName);
                }
                else
                {
                    throw new ArgumentException("Each parameter('Relative') must contain a semicolon(;).");
                }
            }

            builder.WriteTo(outFile, new CompileOptions
            {
                OutputNames = GenerateNames,
            });

            Console.WriteLine($"File saved: {outFile}");
        }