/// <summary>
        /// 生成代码文件
        /// </summary>
        /// <param name="results"></param>
        /// <param name="settingCodeIgnorePattern"></param>
        /// <param name="forceAll"></param>
        /// <param name="genManagerClass"></param>
        /// <param name="templateVars">如果是生成Manager Class 一定要在外部初始化此字段</param>
        public void GenCodeFile(TableCompileResult compileResult, string genCodeTemplateString, string genCodeFilePath,
                                string nameSpace = "AppSettings", string changeExtension = ".tml", string settingCodeIgnorePattern = null, bool forceAll = false, bool genManagerClass = false, Dictionary <string, TableTemplateVars> templateVars = null)
        {
            // 根据编译结果,构建vars,同class名字的,进行合并
            if (!genManagerClass)
            {
                templateVars = new Dictionary <string, TableTemplateVars>();
            }
            if (!string.IsNullOrEmpty(settingCodeIgnorePattern))
            {
                var ignoreRegex = new Regex(settingCodeIgnorePattern);
                if (ignoreRegex.IsMatch(compileResult.TabFileRelativePath))
                {
                    return; // ignore this
                }
            }

            var customExtraStr = CustomExtraString != null?CustomExtraString(compileResult) : null;

            var templateVar = new TableTemplateVars(compileResult, customExtraStr);

            // 尝试类过滤
            var ignoreThisClassName = false;

            if (GenerateCodeFilesFilter != null)
            {
                for (var i = 0; i < GenerateCodeFilesFilter.Length; i++)
                {
                    var filterClass = GenerateCodeFilesFilter[i];
                    if (templateVar.ClassName.Contains(filterClass))
                    {
                        ignoreThisClassName = true;
                        break;
                    }
                }
            }
            if (!ignoreThisClassName)
            {
                if (!templateVars.ContainsKey(templateVar.ClassName))
                {
                    templateVars.Add(templateVar.ClassName, templateVar);
                }
                else
                {
                    templateVars[templateVar.ClassName].RelativePaths.Add(compileResult.TabFileRelativePath);
                }
                //templateVars[templateVar.ClassName].TabFileNames = compileResult.TabFileNames;
            }

            if (!genManagerClass)
            {
                //首字母大写,符合微软命名规范
                var newFileName = string.Concat(DefaultClassNameParse(compileResult.TabFileRelativePath), FileNameSuffix, ".cs");
                if (string.IsNullOrEmpty(genCodeFilePath))
                {
                    genCodeFilePath += string.Concat(DefaultGenCodeDir, newFileName);
                }
                else
                {
                    genCodeFilePath += newFileName;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(genCodeFilePath))
                {
                    genCodeFilePath += string.Concat(DefaultGenCodeDir, ManagerClassName);
                }
                else
                {
                    genCodeFilePath += ManagerClassName;
                }
            }


            // 整合成字符串模版使用的List
            var templateHashes = new List <Hash>();

            foreach (var kv in templateVars)
            {
                //TODO render 加多一项TabFilName
                var templateVar2       = kv.Value;
                var renderTemplateHash = Hash.FromAnonymousObject(templateVar2);
                templateHashes.Add(renderTemplateHash);
            }

            if (forceAll)
            {
                // force 才进行代码编译
                GenerateCode(genCodeTemplateString, genCodeFilePath, nameSpace, templateHashes);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Compile one directory 's all settings, and return behaivour results
        /// </summary>
        /// <param name="sourcePath"></param>
        /// <param name="compilePath"></param>
        /// <param name="genCodeFilePath"></param>
        /// <param name="changeExtension"></param>
        /// <param name="forceAll">no diff! only force compile will generate code</param>
        /// <returns></returns>
        ///
        //public List<TableCompileResult> CompileTableMLAll(string sourcePath, string compilePath,
        //string genCodeFilePath, string genCodeTemplateString = null, string nameSpace = "AppSettings", string changeExtension = ".tml", string settingCodeIgnorePattern = null, bool forceAll = false)

        public List <TableCompileResult> CompileTableMLAll(string sourcePath, string compilePath,
                                                           string bundlesFilePath, string luaFilePath, string nameSpace = "AppSettings", string changeExtension = ".tml", string settingCodeIgnorePattern = null, bool forceAll = false)
        {
            var results        = new List <TableCompileResult>();
            var compileBaseDir = compilePath;
            // excel compiler
            var compiler = new Compiler(new CompilerConfig()
            {
                ConditionVars = CompileSettingConditionVars
            });

            var excelExt = new HashSet <string>()
            {
                ".xls", ".xlsx", ".tsv"
            };
            var copyExt = new HashSet <string>()
            {
                ".txt"
            };
            var findDir = sourcePath;

            try
            {
                var allFiles      = Directory.GetFiles(findDir, "*.*", SearchOption.AllDirectories);
                var allFilesCount = allFiles.Length;
                var nowFileIndex  = -1;                // 开头+1, 起始为0
                foreach (var excelPath in allFiles)
                {
                    nowFileIndex++;
                    var ext      = Path.GetExtension(excelPath);
                    var fileName = Path.GetFileNameWithoutExtension(excelPath);

                    var relativePath = excelPath.Replace(findDir, "").Replace("\\", "/");
                    if (relativePath.StartsWith("/"))
                    {
                        relativePath = relativePath.Substring(1);
                    }
                    if (excelExt.Contains(ext) && !fileName.StartsWith("~"))                     // ~开头为excel临时文件,不要读
                    {
                        // it's an excel file

                        var compileToPath = string.Format("{0}/{1}", compileBaseDir,
                                                          Path.ChangeExtension(relativePath, changeExtension));
                        var srcFileInfo = new FileInfo(excelPath);

                        Console.WriteLine("Compiling Excel to Tab..." + string.Format("{0} -> {1}", excelPath, compileToPath));

                        // 如果已经存在,判断修改时间是否一致,用此来判断是否无需compile,节省时间
                        bool doCompile = true;
                        if (File.Exists(compileToPath))
                        {
                            var toFileInfo = new FileInfo(compileToPath);

                            if (!forceAll && srcFileInfo.LastWriteTime == toFileInfo.LastWriteTime)
                            {
                                //Log.DoLog("Pass!SameTime! From {0} to {1}", excelPath, compileToPath);
                                doCompile = false;
                            }
                        }
                        if (doCompile)
                        {
                            Console.WriteLine("[SettingModule]Compile from {0} to {1}", excelPath, compileToPath);

                            var compileResult = compiler.Compile(excelPath, compileToPath, compileBaseDir, doCompile);

                            // 添加模板值
                            results.Add(compileResult);

                            var compiledFileInfo = new FileInfo(compileToPath);
                            compiledFileInfo.LastWriteTime = srcFileInfo.LastWriteTime;
                        }
                    }
                    else if (copyExt.Contains(ext)) // .txt file, just copy
                    {
                        // just copy the files with these ext
                        var compileToPath = string.Format("{0}/{1}", compileBaseDir,
                                                          relativePath);
                        var compileToDir = Path.GetDirectoryName(compileToPath);
                        if (!Directory.Exists(compileToDir))
                        {
                            Directory.CreateDirectory(compileToDir);
                        }
                        File.Copy(excelPath, compileToPath, true);

                        Console.WriteLine("Copy File ..." + string.Format("{0} -> {1}", excelPath, compileToPath));
                    }
                }

                // 根据模板生成所有代码,  如果不是强制重建,无需进行代码编译
                //if (!AutoGenerateCode)
                //{
                //	Log.Warning("Ignore Gen Settings code");
                //}
                //else if (!force)
                //{
                //	Log.Warning("Ignore Gen Settings Code, not a forcing compiling");
                //}
                //else
                {
                    // 根据编译结果,构建vars,同class名字的,进行合并
                    var templateVars = new Dictionary <string, TableTemplateVars>();
                    foreach (var compileResult in results)
                    {
                        if (!string.IsNullOrEmpty(settingCodeIgnorePattern))
                        {
                            var ignoreRegex = new Regex(settingCodeIgnorePattern);
                            if (ignoreRegex.IsMatch(compileResult.TabFileRelativePath))
                            {
                                continue;                                 // ignore this
                            }
                        }

                        var customExtraStr = CustomExtraString != null?CustomExtraString(compileResult) : null;

                        var templateVar = new TableTemplateVars(compileResult, customExtraStr);

                        // 尝试类过滤
                        var ignoreThisClassName = false;
                        if (GenerateCodeFilesFilter != null)
                        {
                            for (var i = 0; i < GenerateCodeFilesFilter.Length; i++)
                            {
                                var filterClass = GenerateCodeFilesFilter[i];
                                if (templateVar.ClassName.Contains(filterClass))
                                {
                                    ignoreThisClassName = true;
                                    break;
                                }
                            }
                        }
                        if (!ignoreThisClassName)
                        {
                            if (!templateVars.ContainsKey(templateVar.ClassName))
                            {
                                templateVars.Add(templateVar.ClassName, templateVar);
                            }
                            else
                            {
                                templateVars[templateVar.ClassName].RelativePaths.Add(compileResult.TabFileRelativePath);
                            }
                        }
                    }

                    // 整合成字符串模版使用的List
                    var templateHashes = new List <Hash>();
                    foreach (var kv in templateVars)
                    {
                        var templateVar        = kv.Value;
                        var renderTemplateHash = Hash.FromAnonymousObject(templateVar);
                        templateHashes.Add(renderTemplateHash);
                    }


                    if (forceAll)
                    {
                        // force 才进行代码编译
                        //GenerateCode(genCodeTemplateString, genCodeFilePath, nameSpace, templateHashes);
                        GenerateLuaCode(luaFilePath, compilePath, bundlesFilePath, nameSpace, templateHashes);
                    }
                }
            }
            finally
            {
                //EditorUtility.ClearProgressBar();
            }
            return(results);
        }