Exemple #1
0
        static string ReadProperties()
        {
            string properties = FileReaderAndWriter.ReadFile("D:\\Code\\netcore\\DomenicCodeGen\\Main\\Inputs\\Proprities.txt");

            if (string.IsNullOrWhiteSpace(properties))
            {
                throw new Exception("读取属性文件Proprities.txt出错!");
            }
            return(properties);
        }
Exemple #2
0
        /// <summary>
        /// 获取某个模板中所有的 变量
        /// </summary>
        /// <returns></returns>
        private Dictionary <string, string> GetTemplateVariables(string filePath)
        {
            Dictionary <string, string> vars = new Dictionary <string, string>();
            string fileContent = FileReaderAndWriter.ReadFile(filePath);
            //解析代码中的变量,规则为找出所有类似于: {xx} 的字符串。
            Regex            reg          = new Regex(@"(?is)(?<=\$\{)[^\}]+(?=\})");
            MatchCollection  mc           = reg.Matches(fileContent);
            HashSet <string> noRepeatVars = new HashSet <string>();

            foreach (var item in mc)
            {
                noRepeatVars.Add(item.ToString());
            }
            foreach (var item in noRepeatVars)
            {
                vars.Add(item, "");
            }
            return(vars);
        }
Exemple #3
0
        /// <summary>
        /// 获取CodeGen需要的元数据的 实体列表
        /// </summary>
        private List <CodeGenMetadataEntity> GetCodeGenMetadataEntities()
        {
            List <CodeGenMetadataEntity> codeGenMetadataEntities = new List <CodeGenMetadataEntity>();
            List <string> templateFullFilePaths = FileReaderAndWriter.GetFileList(_templatesPath);

            foreach (var tempFullPath in templateFullFilePaths)
            {
                //获取文件名
                string tempFileName = Path.GetFileName(tempFullPath);
                Dictionary <string, string> variables             = GetTemplateVariables(tempFullPath);
                CodeGenMetadataEntity       codeGenMetadataEntity = new CodeGenMetadataEntity {
                    TemplateFileName = tempFileName,
                    Variables        = variables,
                    FileContent      = FileReaderAndWriter.ReadFile(tempFullPath)
                };
                codeGenMetadataEntities.Add(codeGenMetadataEntity);
            }

            return(codeGenMetadataEntities);
        }
Exemple #4
0
        public void Run(CodeGenInput input)
        {
            foreach (var codeGenMetadataEntity in _codeGenMetadataEntities)
            {
                string codeContent = codeGenMetadataEntity.FileContent;
                codeContent = codeContent
                              .Replace("${namespace}", input.Namespace)
                              .Replace("${entity}", input.Entity)
                              .Replace("${lowerCaseEntity}", input.LowerCaseEntity)
                              .Replace("${properties}", input.Properties)
                              .Replace("${areaName}", input.AreaName)
                              .Replace("${moduleName}", input.ModuleName)
                ;

                string outFileName = codeGenMetadataEntity.TemplateFileName.Replace("XX", input.Entity).Replace(".template", "");
                codeGenMetadataEntity.OutputPath     = Path.Combine(input.OutBasePath, outFileName);
                codeGenMetadataEntity.OutFileContent = codeContent;//赋值输出内容。
            }
            //生成文件
            foreach (var item in _codeGenMetadataEntities)
            {
                FileReaderAndWriter.WriteFile(item.OutFileContent, item.OutputPath);
            }
        }