예제 #1
0
        public GeneratorApplication(string[] args)
        {
            this.Args       = new List <string>(args);
            this.ArgsBackup = new List <string>(args);

            Initialize();

            if (settings.HasConfigFile || settings.HasSchemaAndTemplate)
            {
                Console.Write(
                    Gen.Messages.AppConf
                    .Replace("{config}", settings.FileConfig == null ? "null" : settings.FileConfig.FullName)
                    .Replace("{input}", settings.FileIn == null     ? "null" : settings.FileIn.FullName)
                    .Replace("{output}", settings.FileOut == null    ? "null" : settings.FileOut.FullName)
                    .Replace("{template}", settings.TemplateName ?? "null")
                    .Replace("{db}", settings.DatabaseName ?? "null")
                    .Replace("{table}", settings.TableName ?? "null")
                    );

                if (
                    (settings.FileSchema == null && settings.FileTemplates == null) &&
                    (settings.FileConfig == null))
                {
                    Console.WriteLine("conf not provided.");
                    Console.ReadKey();
                    return;
                }

                var reader = new GeneratorReader()
                {
                    Model = settings.FileConfig != null ?
                            new GeneratorModel(settings.FileConfig.FullName) :
                            new GeneratorModel(settings.FileSchema.FullName, settings.FileTemplates.FullName)
                };
                reader.Initialize();

                var output = reader.Generate(
                    reader.Model.Databases[settings.DatabaseName][settings.TableName],
                    reader.Model.Templates[settings.TemplateName]);

                string input = null;

                if (settings.FileIn != null)
                {
                    input = File.ReadAllText(settings.FileIn.FullName);
                }

                if (
                    !string.IsNullOrEmpty(settings.ReplacementTag) &&
                    !string.IsNullOrEmpty(input)
                    )
                {
                    output = input.Replace(settings.ReplacementTag, output);
                }

                File.WriteAllText(settings.FileOut.FullName, output);
            }
        }
예제 #2
0
        /// <summary>
        /// in the future  it would probably be better to use tokens
        /// or something to split our input a bit more wisely.
        ///
        /// E.G.
        ///
        /// "table=tablename:database=dbname:template=tplName"
        /// </summary>
        /// <param name="config"></param>
        /// <param name="template"></param>
        /// <param name="database"></param>
        /// <param name="output"></param>
        static public void Generate(string config, string database, string template, string output)
        {
            string dbname = null, tablename = null;

            if (database.Contains(":"))
            {
                var values = database.Split(':');
                dbname    = values[0].Trim();
                tablename = values[1].Trim();
            }

            if (config != null)
            {
                Logger.Error(ConsoleColor.Green, "Generator-Config", "config exists");
            }
            else
            {
                Logger.Error(ConsoleColor.Red, "Generator-Config", "NO config exists");
            }
            var reader = new GeneratorReader()
            {
                Model = config != null ?
                        new GeneratorModel(config) :
                        new GeneratorModel(database, template)
            };

            if (System.IO.File.Exists(config))
            {
                Logger.Error(ConsoleColor.Magenta,
                             "datafile", "file={0}, absolute={1}, conf-dir={2}",
                             reader.Model.Configuration.datafile,
                             System.IO.Path.IsPathRooted(reader.Model.Configuration.datafile),
                             System.IO.Path.GetDirectoryName(config)
                             );

                var origin = System.IO.Path.GetDirectoryName(config);
                if (!System.IO.Path.IsPathRooted(reader.Model.Configuration.datafile))
                {
                    reader.Model.Configuration.datafile = System.IO.Path.GetFullPath(System.IO.Path.Combine(origin, reader.Model.Configuration.datafile));
                    Logger.Error(ConsoleColor.Magenta, "dat", reader.Model.Configuration.datafile);
                }
                if (!System.IO.Path.IsPathRooted(reader.Model.Configuration.templatefile))
                {
                    reader.Model.Configuration.templatefile = System.IO.Path.GetFullPath(System.IO.Path.Combine(origin, reader.Model.Configuration.templatefile));
                    Logger.Error(ConsoleColor.Magenta, "tpl", reader.Model.Configuration.templatefile);
                }
            }


            reader.Initialize();

            var generated = reader.Generate(
                reader.Model.Databases[dbname][tablename],
                reader.Model.Templates[template]);

            if (output == null)
            {
                Console.Out.Write(generated);
                //        Console.ReadKey();
            }
            else
            {
                System.IO.File.WriteAllText(output, generated);
            }
        }