Example #1
0
        public static string OutputFile(this ApplicationCommand cmd, string defaultOutputFile, bool createDirectoryIfNotExists = true)
        {
            string outputFile = cmd.OutputPath();

            if (!string.IsNullOrEmpty(outputFile))
            {
                try
                {
                    if (Directory.Exists(outputFile))
                    {
                        string directory = outputFile;
                        if (string.IsNullOrEmpty(defaultOutputFile))
                        {
                            return(Path.Combine(directory, "sqlcon.out"));
                        }
                        else
                        {
                            if (Path.IsPathRooted(defaultOutputFile))
                            {
                                return(Path.Combine(directory, Path.GetFileName(defaultOutputFile)));
                            }
                            else
                            {
                                return(Path.Combine(directory, defaultOutputFile));
                            }
                        }
                    }
                    else
                    {
                        string directory = Path.GetDirectoryName(outputFile);
                        if (directory != string.Empty && !Directory.Exists(directory))
                        {
                            if (createDirectoryIfNotExists)
                            {
                                Directory.CreateDirectory(directory);
                            }
                        }

                        return(outputFile);
                    }
                }
                catch (Exception ex)
                {
                    cerr.WriteLine($"invalid file or directory \"{outputFile}\", {ex.Message}");
                }
            }

            if (Path.IsPathRooted(defaultOutputFile))
            {
                return(defaultOutputFile);
            }
            else
            {
                return(Path.Combine(Directory.GetCurrentDirectory(), defaultOutputFile));
            }
        }
Example #2
0
        public void ExportClass()
        {
            DpoOption option = new DpoOption
            {
                NameSpace          = cfg.GetValue <string>(ConfigKey._GENERATOR_DPO_NS, "Sys.DataModel.Dpo"),
                OutputPath         = cmd.OutputPath(ConfigKey._GENERATOR_DPO_PATH, $"{ConfigurationEnvironment.MyDocuments}\\DataModel\\Dpo"),
                Level              = cfg.GetValue <Level>(ConfigKey._GENERATOR_DPO_LEVEL, Level.Application),
                HasProvider        = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASPROVIDER, false),
                HasTableAttribute  = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASTABLEATTR, true),
                HasColumnAttribute = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_HASCOLUMNATTR, true),
                IsPack             = cfg.GetValue <bool>(ConfigKey._GENERATOR_DPO_ISPACK, true),
                CodeSorted         = cmd.Has("sort"),

                ClassNameSuffix = cfg.GetValue <string>(ConfigKey._GENERATOR_DPO_SUFFIX, Setting.DPO_CLASS_SUFFIX_CLASS_NAME)
            };

            option.ClassNameRule =
                name => name.Substring(0, 1).ToUpper() + name.Substring(1).ToLower() + option.ClassNameSuffix;

            if (tname != null)
            {
                var clss = new DpoGenerator(tname)
                {
                    Option = option
                };
                clss.CreateClass();
                cout.WriteLine("generated class {0} at {1}", tname.ShortName, option.OutputPath);
            }
            else if (dname != null)
            {
                cout.WriteLine("start to generate database {0} class to directory: {1}", dname, option.OutputPath);
                CancelableWork.CanCancel(cts =>
                {
                    var md             = new MatchedDatabase(dname, cmd);
                    TableName[] tnames = md.TableNames();
                    foreach (var tn in tnames)
                    {
                        if (cts.IsCancellationRequested)
                        {
                            return;
                        }


                        try
                        {
                            var clss = new DpoGenerator(tn)
                            {
                                Option = option
                            };
                            clss.CreateClass();
                            cout.WriteLine("generated class for {0} at {1}", tn.ShortName, option.OutputPath);
                        }
                        catch (Exception ex)
                        {
                            cerr.WriteLine($"failed to generate class {tn.ShortName}, {ex.Message}");
                        }
                    }

                    cout.WriteLine("completed");
                    return;
                });
            }
            else
            {
                cerr.WriteLine("warning: database is not selected");
            }
        }