예제 #1
0
        private void Save(CodeUnitCreator unit)
        {
            const int kDynamicVersionLineNum = 3;

            using (MemoryStream stream = new MemoryStream()) {
                StreamWriter    sourceWriter = new StreamWriter(stream);
                CodeCompileUnit compileUnit  = unit.GetCodeCompileUnit();
                provider_.GenerateCodeFromCompileUnit(compileUnit, sourceWriter, options_);
                sourceWriter.Flush();
                stream.Seek(0, SeekOrigin.Begin);

                int          count  = 0;
                string       path   = Path.Combine(args_.OutPut, unit.RootClassName + ".cs");
                StreamReader reader = new StreamReader(stream);
                using (StreamWriter fileWriter = new StreamWriter(path)) {
                    while (true)
                    {
                        string line = reader.ReadLine();
                        if (line != null)
                        {
                            if (count == kDynamicVersionLineNum) //去掉动态版本号,每次编译都会不一样
                            {
                                int post = line.LastIndexOf('.');
                                if (post != -1)
                                {
                                    line = line.Substring(0, post);
                                }
                            }
                            else if (count > kDynamicVersionLineNum)
                            {
                                line = line.Replace(CodeUnitCreator.kPropertieSignReplace, CodeUnitCreator.kPropertieSign); //生成的自动属性代码会多一个';',去掉它
                            }
                            fileWriter.WriteLine(line);
                        }
                        else
                        {
                            break;
                        }
                        ++count;
                    }
                }
            }
        }
예제 #2
0
        private void ToProtobuf(Assembly assembly, CodeUnitCreator unit)
        {
            string     fullName   = args_.Namespace + '.' + unit.RootClassName;
            Type       type       = assembly.GetType(fullName);
            MethodInfo methodInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Static).First(m => m.Name == CodeUnitCreator.kLoadMethodName && !m.IsGenericMethodDefinition);
            object     ins        = methodInfo.Invoke(null, null);

            if (ins != null)
            {
                string path = Path.Combine(args_.DataDir, unit.Root);
                if (!string.IsNullOrEmpty(args_.Extension))
                {
                    path += '.' + args_.Extension.TrimStart('.');
                }
                using (FileStream file = File.Create(path)) {
                    ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(file, ins);
                }
            }
            unit.RemoveProtoCode();
            Save(unit);
        }
예제 #3
0
        public void Do()
        {
            string json  = File.ReadAllText(args_.SchemaFile, Encoding.UTF8);
            JArray array = JArray.Parse(json);

            if (array.Count == 0)
            {
                return;
            }

            Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;

            Utils.CreateDirectory(args_.OutPut);
            Utils.CreateDirectory(args_.DataDir);

            List <CodeUnitCreator> units = new List <CodeUnitCreator>();

            foreach (JObject item in array)
            {
                CodeUnitCreator codeUnitCreator = new CodeUnitCreator(args_, item);
                units.Add(codeUnitCreator);
            }

            if (args_.IsToProtobuf)
            {
                CSharpCodeProvider provider = new CSharpCodeProvider();
                CompilerParameters cp       = new CompilerParameters();
                cp.ReferencedAssemblies.Add(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "protobuf-net.dll"));
                cp.ReferencedAssemblies.Add(GetType().Assembly.Location);
                cp.GenerateExecutable = false;
                cp.GenerateInMemory   = true;

                CompilerResults cr = provider.CompileAssemblyFromDom(cp, units.Select(i => i.GetCodeCompileUnit()).ToArray());
                if (cr.Errors.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (CompilerError ce in cr.Errors)
                    {
                        sb.AppendFormat(" {0}", ce.ToString());
                        sb.AppendLine();
                    }
                    throw new System.Exception(sb.ToString());
                }

                string dir = Path.GetDirectoryName(units.First().ExportFile);
                Xml.GeneratorConfig.ConfigDir  = dir;
                Json.GeneratorConfig.ConfigDir = dir;

                Assembly assembly = cr.CompiledAssembly;
                foreach (CodeUnitCreator unit in units)
                {
                    ToProtobuf(assembly, unit);
                }
            }
            else
            {
                foreach (CodeUnitCreator unit in units)
                {
                    Save(unit);
                }
            }
        }