Exemplo n.º 1
0
        private void Generate(ITaskItem source, ITaskItem dest, TaskLoggingHelper logger)
        {
            string filename = source.GetMetadata("Filename");

            if (string.IsNullOrWhiteSpace(filename))
            {
                logger.LogError("Can't get 'Filename' metadata");
                throw new ArgumentException();
            }
            string reflectionClass = filename.ToNameCs() + "Reflection";

            Type           reflection = typeof(Proto.Config.VsharpstudioReflection).Assembly.GetType("Proto.Config.Connection." + reflectionClass);
            var            descr      = reflection.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static);
            object         value      = descr.GetValue(null, null);
            FileDescriptor typedValue = (FileDescriptor)value;

            NameSpace ns  = new NameSpace(typedValue);
            string    res = ns.TransformText();

            string filedest = dest.GetMetadata("FullPath");

            logger.LogMessage("Output file: " + filedest);
            if (!File.Exists(filedest))
            {
                File.CreateText(filedest);
            }
            using (var fs = File.Open(filedest, FileMode.OpenOrCreate | FileMode.Truncate, FileAccess.Write, FileShare.Write))
            {
                var bytes = Encoding.UTF8.GetBytes(res);
                fs.Write(bytes, 0, bytes.Count());
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            //    Console.WriteLine($"Hello {subject}!");
            LoggerInit.Init();
            var _logger = Logger.CreateLogger <Program>();

            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                try
                {
                    RunOptions = o;
                    _logger.LogInformation("***  App Starting IsModel={1}".CallerInfo(), o.IsModel);
                    //_logger.LogInformation("***  App Starting {@Args}".CallerInfo(), args.Aggregate((s1, s2) =>
                    //{
                    //    return s1 + " " + s2;
                    //}));
                    var ncs = o.ProtoFileName.ToNameCs();
                    string reflectionClass = ncs + "Reflection";
                    //Type reflection = typeof(Proto.Config.Connection.ConnMssqlReflection).Assembly.GetType(destNS + "." + reflectionClass);
                    var types       = typeof(Proto.Doc.ProtoDocReflection).Assembly.GetTypes();
                    Type reflection = null;
                    foreach (var t in types)
                    {
                        if (t.Name == reflectionClass)
                        {
                            reflection = t;
                            break;
                        }
                    }
                    var protoNS = reflection.FullName.Substring(0, reflection.FullName.LastIndexOf('.'));
                    //Type reflection = typeof(Proto.Config.Connection.ConnMssqlReflection).Assembly.GetType(reflectionClass);
                    var descr    = reflection.GetProperty("Descriptor", BindingFlags.Public | BindingFlags.Static);
                    object value = descr.GetValue(null, null);
                    FileDescriptor typedValue = (FileDescriptor)value;

                    Dictionary <string, List <MessageDescriptor> > dicParents = new Dictionary <string, List <MessageDescriptor> >();
                    List <MessageDescriptor> messages = CollectMessages(typedValue, dicParents);

                    //string path = "../../../../doc/" + protofilename + ".json";
                    string path = o.JsonDocFolder + o.ProtoFileName + ".json";
                    ProtoDoc.CreateDoc(path);

                    string res = null;
                    if (o.IsModel)
                    {
                        NameSpace ns = new NameSpace(typedValue, messages, dicParents, o.Namespace, protoNS, o.BaseclassDefault);
                        res          = ns.TransformText();
                    }
                    else if (o.IsInterface)
                    {
                        ModelInterfaces ns = new ModelInterfaces(typedValue, messages, dicParents, o.Namespace, protoNS);
                        res = ns.TransformText();
                    }
                    else
                    {
                        throw new ArgumentException("Expected 'model' or 'interface'");
                    }

                    string filedest = o.OutputFile;
                    if (!File.Exists(filedest))
                    {
                        File.CreateText(filedest);
                    }
                    using (var fs = File.Open(filedest, FileMode.OpenOrCreate | FileMode.Truncate, FileAccess.Write, FileShare.Write))
                    {
                        var bytes = Encoding.UTF8.GetBytes(res);
                        fs.Write(bytes, 0, bytes.Count());
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    //System.Diagnostics.Trace.WriteLine("##### GenFromProto current dir: " + Directory.GetCurrentDirectory());
                }
            });
        }