Example #1
0
        private List <PatternFile> LoadPatternsInfo(List <PatternProject.PatternFile> patternsList)
        {
            List <PatternFile> result         = new List <PatternFile>();
            string             patternsFolder = Path.GetDirectoryName(
                Common.AppVarPathMakeAbsolute(_projectDefinition.CodeGenSettings.CodeGenPatternFile));

            foreach (var pattern in patternsList)
            {
                if (pattern.Action == PatternsListItemAction.Generate)
                {
                    PatternFile patternFile = PatternFile.ReadFromFile(Path.Combine(patternsFolder, pattern.Path));
                    result.Add(patternFile);
                }
            }
            return(result);
        }
        private void reloadPatternList()
        {
            PatternPanel.Children.Clear();

            if (!Directory.Exists("Save Data\\Pattern"))
            {
                Directory.CreateDirectory("Save Data\\Pattern");
            }

            string[] files = Directory.GetFiles("Save Data\\Pattern");

            FileStream   fileStream;
            StreamReader streamReader;

            string[] ifStatementsData;

            PatternFile patternFile;

            foreach (string filePath in files)
            {
                if ((fileStream = File.Open(filePath, FileMode.Open)) == null)
                {
                    continue;
                }

                streamReader = new StreamReader(fileStream);

                patternFile = new PatternFile(filePath, streamReader.ReadLine());

                ifStatementsData = streamReader.ReadLine().Split(',');
                for (int i = 1; i < ifStatementsData.Length; ++i)
                {
                    patternFile.LoadedPattern.IfStatements.AddLast(PatternMaker.IfStatements[int.Parse(ifStatementsData[i])].Clone());
                }

                patternFile.LoadedPattern.Behaviour = PatternMaker.Behaviours[int.Parse(streamReader.ReadLine())].Clone();
                patternFile.MouseLeftButtonDown    += OnMouseLeftButtonDown_PatternFile;

                PatternPanel.Children.Add(patternFile);

                streamReader.Close();
                fileStream.Close();
            }
        }
        private void OnMouseLeftButtonDown_PatternFile(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount != 2)
            {
                return;
            }

            if (mLoadedPattern == null)
            {
                PatternFile file = (PatternFile)sender;

                mLoadedPattern = file.LoadedPattern;
                reloadPatternView();
            }
            else
            {
                MessageBoxResult msgResult = MessageBox.Show("작업중인 내용이 있습니다. 저장하고 계속하시겠습니까?", "Message", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                switch (msgResult)
                {
                case MessageBoxResult.Yes:
                    savePattern();

                    goto case MessageBoxResult.No;

                case MessageBoxResult.No:
                    PatternFile file = (PatternFile)sender;

                    mLoadedPattern = file.LoadedPattern;
                    reloadPatternView();
                    break;

                default:
                    return;
                }
            }
        }
        /// <summary>
        /// Pattern file - AppliesTo - TablesAndViews
        /// </summary>
        private void PatternFileAppliesTo_TablesAndViewsApplier(DbTable[] tablesList,
			DbTable[] viewsList,
			PatternFileWhereToApply whatListToUse,
			PatternFile patternFile)
        {
            if (patternFile.IsAllToOne)
            {
                var genPath = _projectDef.GenerationPath;
                genPath = Common.ProjectPathMakeAbsolute(genPath, _projectDef.ProjectFileName);

                // the destination filename
                string fileName = Replacer_PatternFileName(patternFile.Options.FilePath, null, null, null);
                fileName = Path.Combine(genPath, fileName);

                // don't overwrite if exists and overwriting is not requested
                if (patternFile.Options.Overwrite == false && File.Exists(fileName))
                    return;

                // create directory
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                // generated file content
                string genContent = patternFile.BaseContent;

                // BaseContent Replacements
                genContent = Replacer_PatternBaseContent(genContent, null, null);

                // for each pattern content
                genContent = PatternContentAppliesTo_TablesAndViews(genContent,
                    patternFile.PatternContents,
                    tablesList,
                    viewsList);

                // all is done! Write to file
                File.WriteAllText(fileName, genContent);
            }
            else
            {
                // combine the list
                var tablesAndViews = new List<DbTable>();
                if (whatListToUse == PatternFileWhereToApply.Both ||
                    whatListToUse == PatternFileWhereToApply.Tables)
                    tablesAndViews.AddRange(tablesList);
                if (whatListToUse == PatternFileWhereToApply.Both ||
                    whatListToUse == PatternFileWhereToApply.Views)
                    tablesAndViews.AddRange(viewsList);

                // surf in the tables/views
                tablesAndViews.ForEach(table =>
                {
                    if (table.TableType == DbTable.TableTypeInfo.Table)
                    {
                        // check if selected by user
                        if (!UserHasSelectedTable(table.TableName))
                            //continue;
                            return;
                    }
                    else if (table.TableType == DbTable.TableTypeInfo.View)
                    {
                        // check if selected by user
                        if (!UserHasSelectedView(table.TableName))
                            //continue;
                            return;
                    }
                    else
                    {
                        // What?! Invalid table type!
                        return;
                    }

                    var genPath = _projectDef.GenerationPath;
                    genPath = Common.ProjectPathMakeAbsolute(genPath, _projectDef.ProjectFileName);

                    // the destination filename
                    string fileName = Replacer_PatternFileName(patternFile.Options.FilePath,
                                          table.TableNameSchema,
                                          table.TableName,
                                          table.TableNameSchemaCS);
                    fileName = Path.Combine(genPath, fileName);

                    // don't overwrite if exists an overwriting is not requested
                    if (patternFile.Options.Overwrite == false && File.Exists(fileName))
                        //continue;
                        return;

                    // create directory
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                    // generated file content
                    string genContent = patternFile.BaseContent;

                    // BaseContent Replacements
                    genContent = Replacer_PatternBaseContent(genContent,
                                          table.TableNameSchema,
                                          table.TableName);

                    // for each pattern content
                    genContent = PatternContentAppliesTo_OneTable(
                        genContent,
                        patternFile.PatternContents,
                        table,
                        null,
                        null);

                    // all is done! Write to file
                    File.WriteAllText(fileName, genContent);
                });
            }
        }
        /// <summary>
        /// Starts to apply project files to the pattern
        /// </summary>
        private void PatternFileAppliesTo_ProjectFileApplier(PatternFile commonPattern)
        {
            var generationPath = _projectDef.GenerationPath;
            generationPath = Common.ProjectPathMakeAbsolute(generationPath, _projectDef.ProjectFileName);

            string fileName = Common.ReplaceEx(commonPattern.Options.FilePath, ReplaceConsts.ProjectName, _projectDef.ProjectName, StringComparison.CurrentCultureIgnoreCase);
            fileName = Path.Combine(generationPath, fileName);

            // don't overwrite if exists an overwriting is not requested
            if (commonPattern.Options.Overwrite == false && File.Exists(fileName))
                return;

            // create directory
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            // generated file content
            string genContent = commonPattern.BaseContent;

            // general
            genContent = Replacer_GeneratorGeneral(genContent);

            // database provider class
            genContent = Replacer_DatabaseProvider(genContent);

            // Replacements
            genContent = Common.ReplaceEx(genContent, ReplaceConsts.ConnectionString, _projectDef.DbSettions.GetConnectionString(), StringComparison.CurrentCultureIgnoreCase);

            // search for pattern in the general content
            foreach (PatternContent pattern in commonPattern.PatternContents)
            {
                string partialName = string.Format(ReplaceConsts.PatternContentReplacer, pattern.Name);

                // is there a pattern for that
                if (genContent.IndexOf(partialName) == -1)
                    continue;

                if (pattern.ConditionKeyMode == PatternConditionKeyMode.ProjectFiles)
                {
                    // Get project generated files, extnsion is specfied by the project
                    string codeFilePattern = String.Format("*{0}", _patternProject.FileExtension);

                    // Search the files
                    string[] projectFiles = Directory.GetFiles(generationPath, codeFilePattern, SearchOption.AllDirectories);

                    string contentToReplace = "";
                    foreach (var projectFile in projectFiles)
                    {
                        string columnReplace = PatternContentAppliesTo_ProjectFiles(pattern, projectFile);

                        if (!string.IsNullOrEmpty(columnReplace))
                            contentToReplace += columnReplace + pattern.ItemsSeperator;
                    }

                    // removing unnecessary ItemsSeperator
                    if (!string.IsNullOrEmpty(pattern.ItemsSeperator) && contentToReplace.EndsWith(pattern.ItemsSeperator))
                        contentToReplace = contentToReplace.Remove(contentToReplace.Length - pattern.ItemsSeperator.Length, pattern.ItemsSeperator.Length);

                    // replace
                    genContent = genContent.Replace(partialName, contentToReplace);
                }
                else if (pattern.ConditionKeyMode == PatternConditionKeyMode.General ||
                    pattern.ConditionKeyMode == PatternConditionKeyMode.DatabaseProvider)
                {
                    string contentToReplace = PatternContentAppliesTo_General(pattern);

                    genContent = genContent.Replace(partialName, contentToReplace);
                }
            }

            // Write to file
            File.WriteAllText(fileName, genContent);
        }
        /// <summary>
        /// Starts to apply general replacements to the pattern
        /// </summary>
        private void PatternFileAppliesTo_GeneralApplier(PatternFile patternFile)
        {
            string fileName = patternFile.Options.FilePath;

            var generationPath = _projectDef.GenerationPath;
            generationPath = Common.ProjectPathMakeAbsolute(generationPath, _projectDef.ProjectFileName);

            // the general replacements
            fileName = Replacer_GeneratorGeneral(fileName);

            // the destination path
            fileName = Path.Combine(generationPath, fileName);

            // create directory
            Directory.CreateDirectory(Path.GetDirectoryName(fileName));

            // don't overwrite if exists an overwriting is not requested
            if (patternFile.Options.Overwrite == false && File.Exists(fileName))
                return;

            // generated file content
            string genContent = patternFile.BaseContent;

            // general
            genContent = Replacer_GeneratorGeneral(genContent);

            // database provider
            genContent = Replacer_DatabaseProvider(genContent);

            // search for pattern in the general content
            foreach (PatternContent pattern in patternFile.PatternContents)
            {
                string partialName = string.Format(ReplaceConsts.PatternContentReplacer, pattern.Name);

                // is there a pattern for that
                if (genContent.IndexOf(partialName) == -1)
                    continue;

                if (pattern.ConditionKeyMode == PatternConditionKeyMode.General ||
                   pattern.ConditionKeyMode == PatternConditionKeyMode.DatabaseProvider)
                {
                    string contentToReplace = PatternContentAppliesTo_General(pattern);

                    genContent = genContent.Replace(partialName, contentToReplace);
                }
            }

            // Write to file
            File.WriteAllText(fileName, genContent);
        }