Esempio n. 1
0
        /// <summary>
        /// Compile a setting file, return a hash for template
        /// </summary>
        /// <param name="path"></param>
        /// <param name="compileToFilePath"></param>
        /// <param name="compileBaseDir"></param>
        /// <param name="doRealCompile">Real do, or just get the template var?</param>
        /// <returns></returns>
        public TableCompileResult Compile(string path, string compileToFilePath, string compileBaseDir = null, bool doRealCompile = true)
        {
            // 确保目录存在
            compileToFilePath = Path.GetFullPath(compileToFilePath);
            var compileToFileDirPath = Path.GetDirectoryName(compileToFilePath);

            if (!Directory.Exists(compileToFileDirPath))
            {
                Directory.CreateDirectory(compileToFileDirPath);
            }

            var ext = Path.GetExtension(path);

            ITableSourceFile sourceFile;

            if (ext == ".tsv")
            {
                sourceFile = new SimpleTSVFile(path);
            }
            else
            {
                sourceFile = new SimpleExcelFile(path);
            }

            var hash = DoCompilerExcelReader(path, sourceFile, compileToFilePath, compileBaseDir, doRealCompile);

            return(hash);
        }
        /// <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> CompileTableMLAllInSingleFile(string sourcePath, string compilePath,
                                                                       string genCodeFilePath, string genCodeTemplateString = null, 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"
            };

            if (Directory.Exists(sourcePath) == false)
            {
                Console.WriteLine("Error! {0} 路径不存在!", sourcePath);
                return(results);
            }
            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

                        /*
                         * NOTE 开始编译Excel 成 tml文件
                         * 每编译一个Excel就生成一个代码文件
                         */
                        //NOTE 设置编译后文件的文件名(tml文件名)
                        relativePath = SimpleExcelFile.GetOutFileName(excelPath);
                        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);
                            Console.WriteLine(); //美观一下 打印空白行
                            var compileResult = compiler.Compile(excelPath, compileToPath, compileBaseDir, doCompile);

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

                            var compiledFileInfo = new FileInfo(compileToPath);
                            compiledFileInfo.LastWriteTime = srcFileInfo.LastWriteTime;
                            //仅仅是生成单个Class,只需要当前的CompileResult
                            GenCodeFile(compileResult, genCodeTemplateString, genCodeFilePath, nameSpace, changeExtension, settingCodeIgnorePattern, forceAll);
                        }
                    }
                    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));
                    }
                }

                //生成Manager class
                GenManagerClass(results, DefaultTemplate.GenManagerCodeTemplate, genCodeFilePath, nameSpace, changeExtension, settingCodeIgnorePattern, forceAll);
            }
            finally
            {
                //EditorUtility.ClearProgressBar();
            }
            return(results);
        }