Ejemplo n.º 1
0
        public static string InterfacesToIdl(string input, string outputDir, string outputType)
        {
            var toFile = GetToSourceName(input, outputType);
            if (toFile == "")
            {
                return "";
            }

            toFile = Path.Combine(outputDir, toFile);

            //
            // code gen 
            //
            var c = new CodeBuilder();
            var asm = Assembly.LoadFrom(input);
            var generator = IdlGenerator.GetInstance(outputType);

            generator.Generate(asm, c);

            var writer = new StreamWriter(toFile);
            writer.Write(c.ToString());
            writer.Close();

            return toFile;  
        }
Ejemplo n.º 2
0
 public virtual void Generate(Assembly asm)
 {
     this.asm = asm;
     c = new CodeBuilder();
     GenerateHeader();
     GenerateEnums();
     GenerateStructs();
     GenerateApis();
 }
Ejemplo n.º 3
0
 public virtual void Generate(Assembly asm, CodeBuilder c)
 {
     this.asm = asm;
     this.c = c;
     GenerateHeader();
     GenerateEnums();
     GenerateStructs();
     GenerateApis();
 }
Ejemplo n.º 4
0
 //public string[] ToCommonSpec(ServiceSpec spec, string dir)
 //{
 //    return FromAllSpecToSources(
 //                Path.Combine(spec.Directory, spec.MainSpecFile),
 //                spec.ReferencedSpecFiles.Select(rs => Path.Combine(spec.Directory, rs)).ToArray(),
 //                dir,
 //                new GeneratedFileType[] { GeneratedFileType.IDL },
 //                new GeneratedFileType[] { GeneratedFileType.IDL_NoSerivce }
 //                );
 //}
 public void GenerateClientCall(
     CodeBuilder builder,
     MethodCallExpression call, 
     Service svc,
     Dictionary<Type, string> reWrittenTypes
     )
 {
     var proxyName = svc.TypeName() + "_Proxy";
     builder.AppendLine("return " + (call.Object as MemberExpression).Member.Name + "." + call.Method.Name + "(req);");
 }
Ejemplo n.º 5
0
 public void GenerateClientDeclaration(CodeBuilder builder, MemberExpression exp, Service svc)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 6
0
 public void GenerateClientCall(CodeBuilder builder, MethodCallExpression call, Service svc, Dictionary<Type, string> reWrittenTypes)
 {
     builder.AppendLine(call.Type.GetCompilableTypeName(reWrittenTypes) + " resp;");
     builder.AppendLine((call.Object as MemberExpression).Member.Name + "." + call.Method.Name + "(req, out resp);");
     builder.AppendLine("return resp;");
 }
Ejemplo n.º 7
0
 public abstract void GenerateCode(LVertex v, CodeBuilder builder);
Ejemplo n.º 8
0
        public static void BuildReader(this Type t, CodeBuilder builder, Dictionary<Type, string> rewrittenTypes)
        {
            var typeName = GetCompilableTypeName(t, rewrittenTypes);
            var cTypeName = GetTypeNameAsFunctionName(typeName);

            builder.AppendLine("private static " + typeName + " Read_" + cTypeName + "(BinaryReader reader)");
            builder.AppendLine("{");
            builder++;

            if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(TimeSpan))
            {
                builder.AppendLine("return reader.Read" + typeName + "();");
            }

            else if (t.IsEnum)
            {
                builder.AppendLine("return (" + typeName + ")reader.ReadByte();");
            }

            else if (t == typeof(Guid))
            {
                builder.AppendLine("return new Guid(reader.ReadString());");
            }

            else if (t.IsTuple())
            {
                Trace.Assert(t.IsGenericType);
                var temp = new StringBuilder("return Tuple.Create(");
                temp = t.GetGenericArguments().Aggregate(temp, (current, parameterType) => current.Append("Read_" + GetTypeNameAsFunctionName(parameterType, rewrittenTypes) + "(reader), "));
                temp.Remove(temp.Length - 2, 2);
                temp.Append(");");
                builder.AppendLine(temp.ToString());
            }

            else if (t.IsAnonymous())
            {
                var tempObject = new StringBuilder("{ ");
                foreach (var propertyType in t.GetProperties())
                {
                    tempObject.Append(propertyType.Name + " = Read_" + GetTypeNameAsFunctionName(propertyType.PropertyType, rewrittenTypes) + "(reader), ");
                }
                tempObject.Remove(tempObject.Length - 2, 2);
                tempObject.Append("}");
                builder.AppendLine("return new " + typeName + "() " + tempObject + ";");
            }

            else if (t.IsGrouping())
            {
                var keyType = t.GetGenericArguments()[0];
                var elementType = t.GetGenericArguments()[1];

                builder.AppendLine("var temp = new SimpleGrouping<"
                    + GetCompilableTypeName(keyType, rewrittenTypes) + ", "
                    + GetCompilableTypeName(elementType, rewrittenTypes) + ">();");
                builder.AppendLine("temp.Key = Read_" + GetTypeNameAsFunctionName(keyType, rewrittenTypes) + "(reader);");
                builder.AppendLine("int count = reader.ReadInt32();");
                builder.AppendLine("for(int i = 0; i< count; i++)");
                builder.AppendLine("{");
                builder++;
                builder.AppendLine("temp.Add(Read_" + GetTypeNameAsFunctionName(elementType, rewrittenTypes) + "(reader));");
                builder--;
                builder.AppendLine("}");
                builder.AppendLine("return temp;");
            }

            else if (t.IsDirectEnumerable() || t.IsDirectQueryable() || t.IsArray)
            {
                var elementType = TypeHelper.GetElementType(t);
                builder.AppendLine("int count = reader.ReadInt32();");
                builder.AppendLine("var temp = new List<" + GetCompilableTypeName(elementType, rewrittenTypes) + ">();");
                builder.AppendLine("for(int i = 0; i< count; i++)");
                builder.AppendLine("{");
                builder++;
                builder.AppendLine("temp.Add(Read_" + GetTypeNameAsFunctionName(elementType, rewrittenTypes) + "(reader));");
                builder--;
                builder.AppendLine("}");
                builder.AppendLine(t.IsArray ? "return temp.ToArray();" : "return temp;");
            }

            else
            {
                builder.AppendLine("var temp = new " + typeName + "();");
                builder.AppendLine("if (temp.Read(reader)) return temp;");
                builder.AppendLine("else return null;");
            }

            builder--;
            builder.AppendLine("}");
        }
Ejemplo n.º 9
0
        public static void BuildWriter(this Type t, CodeBuilder builder, Dictionary<Type, string> rewrittenTypes)
        {
            var typeName = t.GetCompilableTypeName(rewrittenTypes);
            var cTypeName = GetTypeNameAsFunctionName(typeName);

            builder.AppendLine("private static void Write_" + cTypeName + "(BinaryWriter writer, " + typeName + " obj)");
            builder.AppendLine("{");
            builder++;

            if (t.IsPrimitive || t == typeof(string) || t == typeof(DateTime) || t == typeof(TimeSpan))
            {
                builder.AppendLine("writer.Write(obj);");
            }

            else if (t.IsEnum)
            {
                builder.AppendLine("writer.Write((byte)(int)obj);");
            }

            else if (t == typeof(Guid))
            {
                builder.AppendLine("writer.Write(obj.ToString());");
            }

            else if (t.IsTuple())
            {
                Trace.Assert(t.IsGenericType);
                var itemNum = 0;
                foreach (var parameterType in t.GetGenericArguments())
                {
                    itemNum++;
                    builder.AppendLine("Write_" + GetTypeNameAsFunctionName(parameterType, rewrittenTypes) + "(writer, obj.Item" + itemNum + ");");
                }
            }

            else if (t.IsAnonymous())
            {
                foreach (var propertyType in t.GetProperties())
                {
                    builder.AppendLine("Write_" + GetTypeNameAsFunctionName(propertyType.PropertyType, rewrittenTypes) + "(writer, obj." + propertyType.Name + ");");
                }
            }

            else if (t.IsGrouping())
            {
                var keyType = t.GetGenericArguments()[0];
                var elementType = t.GetGenericArguments()[1];

                builder.AppendLine("Write_" + GetTypeNameAsFunctionName(keyType, rewrittenTypes) + "(writer, obj.Key);");
                builder.AppendLine("int count = obj.Count();");
                builder.AppendLine("writer.Write(count);");
                builder.AppendLine("foreach (var temp in obj)");
                builder.AppendLine("{");
                builder++;
                builder.AppendLine("Write_" + GetTypeNameAsFunctionName(elementType, rewrittenTypes) + "(writer, temp);");
                builder--;
                builder.AppendLine("}");
            }

            else if (t.IsDirectEnumerable() || t.IsDirectQueryable() || t.IsArray)
            {
                var elementType = TypeHelper.GetElementType(t);
                builder.AppendLine("int count = obj.Count();");
                builder.AppendLine("writer.Write(count);");
                builder.AppendLine("foreach (var temp in obj)");
                builder.AppendLine("{");
                builder++;
                builder.AppendLine("Write_" + GetTypeNameAsFunctionName(elementType, rewrittenTypes) + "(writer, temp);");
                builder--;
                builder.AppendLine("}");
            }

            else
            {
                Trace.Assert(t.IsClass && t.GetInterface("ISerializable") != null);
                builder.AppendLine("obj.Write(writer);");
            }

            builder--;
            builder.AppendLine("}");
        }
Ejemplo n.º 10
0
 public void GenerateClientDeclaration(CodeBuilder builder, MemberExpression exp, Service svc)
 {
     var proxyName = svc.TypeName() + "_Proxy";
     builder.AppendLine("private " + proxyName + " " + exp.Member.Name + " = new " + proxyName + "((new NetlibTransport(new BinaryProtocolFactory())).Connect(" + svc.Url +"));");
 }
Ejemplo n.º 11
0
        //public string[] ToCommonSpec(ServiceSpec spec, string dir)
        //{
        //    return FromAllSpecToSources(
        //                Path.Combine(spec.Directory, spec.MainSpecFile),
        //                spec.ReferencedSpecFiles.Select(rs => Path.Combine(spec.Directory, rs)).ToArray(),
        //                dir,
        //                new GeneratedFileType[] { GeneratedFileType.IDL },
        //                new GeneratedFileType[] { GeneratedFileType.IDL_NoSerivce }
        //                );
        //}

        public void GenerateClientCall(
            CodeBuilder builder,
            MethodCallExpression call, 
            Service svc,
            Dictionary<Type, string> reWrittenTypes
            )
        {
            var argTypeName = call.Method.GetParameters()[0].Name;
            builder.AppendLine(svc.TypeName() + "." + argTypeName + "_result resp;");
            builder.AppendLine((call.Object as MemberExpression).Member.Name + "." + call.Method.Name + "(new " + svc.TypeName() + "." + call.Method.Name + "_args(){" + argTypeName + "= req}, out resp);");
            builder.AppendLine("return resp.Success;");
        }
Ejemplo n.º 12
0
 public void Insert(int after, CodeBuilder cb)
 {
     // TODO
 }