Ejemplo n.º 1
0
        protected async Task GenerateFileAsync(string sourceDirectory, string targetDirectory, string file, object model,
                                               bool overwrite)
        {
            Logger.LogDebug("使用模板文件生成: {file}", file);
            var targetFilePathNameTemplate = file.Replace(sourceDirectory, targetDirectory);
            var targetFilePathName         = TextGenerator.GenerateByTemplateText(targetFilePathNameTemplate, model)
                                             .RemovePostFix(".sbntxt");

            if (File.Exists(targetFilePathName) && !overwrite)
            {
                Logger.LogInformation("文件 “{targetFilePathName}” 已经存在,跳过生成。",
                                      targetFilePathName);
                return;
            }

            var templateText = await File.ReadAllTextAsync(file);

            var contents = TextGenerator.GenerateByTemplateText(templateText, model);

            var dir = Path.GetDirectoryName(targetFilePathName);

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

            await File.WriteAllTextAsync(targetFilePathName, contents);

            Logger.LogInformation("文件 “{targetFilePathName}” 成功生成。", targetFilePathName);
        }