Exemplo n.º 1
0
        protected override int ExecuteCommand(CommandLineApplication app, IConsole console)
        {
            var outputFile = OutputFile ?? Path.Combine(Directory.GetCurrentDirectory(), $"{Path.GetFileNameWithoutExtension(InputFilePath)}.i");

            Utils.CreateDirectoryForFileIfNeeded(outputFile);

            string output;

            if (InputFilePath.EndsWith(".xml", StringComparison.OrdinalIgnoreCase))
            {
                var temporaryXsd = Path.Combine(Path.GetDirectoryName(outputFile.ToAbsolutePath()), Path.GetTempFileName());
                try {
                    using (XmlReader reader = XmlReader.Create(InputFilePath)) {
                        XmlSchemaInference schema = new XmlSchemaInference();
                        var schemaSet             = schema.InferSchema(reader);
                        foreach (XmlSchema s in schemaSet.Schemas())
                        {
                            using (var stringWriter = new StringWriterWithEncoding(Encoding.UTF8)) {
                                using (var writer = XmlWriter.Create(stringWriter)) {
                                    s.Write(writer);
                                }

                                File.WriteAllText(temporaryXsd, stringWriter.ToString());
                            }
                        }
                    }

                    output = UoeUtilities.GenerateDatasetDefinition(GetDlcPath(), temporaryXsd, outputFile);
                } finally {
                    Utils.DeleteFileIfNeeded(temporaryXsd);
                }
            }
            else
            {
                output = UoeUtilities.GenerateDatasetDefinition(GetDlcPath(), InputFilePath.ToAbsolutePath(), outputFile);
            }

            Log.Debug(output);

            if (!File.Exists(outputFile))
            {
                throw new CommandException($"The dataset definition file (.i) was not successfully generated, the following file has not been found: {outputFile.PrettyQuote()}.");
            }

            return(0);
        }
Exemplo n.º 2
0
        protected override byte[] GenerateCode(string inputFileContent)
        {
            StringBuilder codes = new StringBuilder();

            System.Diagnostics.Trace.WriteLine("Start GenerateOnFile...");
            log.Clear();
            log.AppendLine("//Code Gen By " + System.Reflection.Assembly.GetCallingAssembly().FullName);
            log.AppendLine("//Start Run:");
            log.AppendLine("//CodeBase " + System.Reflection.Assembly.GetCallingAssembly().CodeBase);
            log.AppendLine("//wszInputFilePath:" + InputFilePath);
            log.AppendLine("//wszDefaultNamespace:" + FileNameSpace);

            if (Config == null)
            {
                try
                {
                    //插件搜索
                    BasePath = new FileInfo(System.Reflection.Assembly.GetCallingAssembly().CodeBase.Replace(@"file:///", "")).Directory.FullName;

                    if (File.Exists(BasePath + ConfigName))
                    {
                        Config = Serializable.Deserialize4File(typeof(Config), BasePath + ConfigName) as Config;
                    }
                }
                catch (Exception e)
                {
                    log.AppendLine(e.ToString());
                }
            }
            log.AppendLine("//BasePath:" + BasePath);
            System.Diagnostics.Trace.WriteLine(log.ToString());
            codes.AppendLine(log.ToString());

            log.Clear();
            if (Config == null)
            {
                Config = new Config();
            }

            if (Config.PlugPath == null)
            {
                System.Diagnostics.Trace.WriteLine("Run PlugManager...");

                try
                {
                    new PlugManager().Show();
                }
                catch
                {
                }
            }
            else
            {
                bool isMatch = false;

                try
                {
                    foreach (var config in Config.PlugConfigs)
                    {
                        if (InputFilePath.EndsWith(config.Exction))
                        {
                            var assembly = Assembly.Load(File.ReadAllBytes(Config.PlugPath + "\\" + config.DllName));
                            if (assembly != null)
                            {
                                var type = assembly.GetType(config.Type).BaseType;
                                if (type != null && type.Name == typeof(BasePlug).Name)
                                {
                                    var plug = Activator.CreateInstance(assembly.GetType(config.Type));
                                    if (plug != null)
                                    {
                                        log.AppendLine("//User Plug:" + config.DllName + " Type:" + config.Type);
                                        var method = plug.GetType().GetMethod("GenerateOnFile");
                                        if (method != null)
                                        {
                                            //plug.GenerateOnFile(codes, wszInputFilePath, bstrInputFileContents,
                                            //                    wszDefaultNamespace);
                                            method.Invoke(plug, new object[]
                                            {
                                                codes,
                                                InputFilePath,
                                                inputFileContent,
                                                FileNameSpace,
                                                Config.PlugPath + config.DllName
                                            });
                                            isMatch = true;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    isMatch = false;
                    MessageBox.Show(e.ToString());
                }
                if (!isMatch)
                {
                    System.Diagnostics.Trace.WriteLine("Run PlugManager...");

                    try
                    {
                        new PlugManager().Show();
                    }
                    catch
                    {
                    }
                }
            }
            codes.AppendLine(log.ToString());

            using (StringWriter writer = new StringWriter(new StringBuilder()))
            {
                if (this.CodeGeneratorProgress != null)
                {
                    //Report that we are done
                    this.CodeGeneratorProgress.Progress(100, 100);
                }
                writer.Write(codes.ToString());
                writer.Flush();


                //Get the Encoding used by the writer. We're getting the WindowsCodePage encoding,
                //which may not work with all languages
                Encoding enc = Encoding.GetEncoding(writer.Encoding.WindowsCodePage);

                //Get the preamble (byte-order mark) for our encoding
                byte[] preamble       = enc.GetPreamble();
                int    preambleLength = preamble.Length;

                //Convert the writer contents to a byte array
                byte[] body = enc.GetBytes(writer.ToString());

                //Prepend the preamble to body (store result in resized preamble array)
                Array.Resize <byte>(ref preamble, preambleLength + body.Length);
                Array.Copy(body, 0, preamble, preambleLength, body.Length);

                //Return the combined byte array
                return(preamble);
            }
        }