1 Star 0 Fork 16

青侠oO/SmartCode

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
FileOutput.cs 2.97 KB
一键复制 编辑 原始数据 按行查看 历史
Ahoo-Wang 提交于 2019-10-21 14:46 +08:00 . add support Java-SpringBoot-MyBatis template
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using HandlebarsDotNet;
using SmartCode.Configuration;
using SmartCode.Utilities;
namespace SmartCode.App.Outputs
{
public class FileOutput : IOutput
{
private readonly ILogger<FileOutput> _logger;
public FileOutput(ILogger<FileOutput> logger)
{
_logger = logger;
}
public bool Initialized { get; private set; }
public string Name { get; private set; } = "File";
public void Initialize(IDictionary<string, object> parameters)
{
if (parameters == null)
{
return;
}
if (parameters.Value(nameof(Name), out string name))
{
Name = name;
}
Initialized = true;
}
public async Task Output(BuildContext context, Output output = null)
{
if (output == null)
{
output = context.Output;
}
_logger.LogInformation($"------ Mode:{output.Mode},Build:{context.BuildKey} Start! ------");
var outputPath = Handlebars.Compile(output.Path)(context);
outputPath = Path.Combine(context.Project.OutputPath, outputPath);
if (output.DotSplit == true)
{
outputPath = outputPath.Replace('.', '/');
}
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
_logger.LogWarning($"------ Directory:{outputPath} is not Exists,Created! ------");
}
var fileName = Handlebars.Compile(output.Name)(context) + output.Extension;
var filePath = Path.Combine(outputPath, fileName);
var fileExists = File.Exists(filePath);
if (fileExists)
{
switch (output.Mode)
{
case Configuration.CreateMode.None:
case Configuration.CreateMode.Incre:
{
_logger.LogWarning(
$"------ Mode:{output.Mode},Build:{context.BuildKey},FilePath:{filePath} Exists ignore output End! ------");
return;
}
case Configuration.CreateMode.Full:
{
File.Delete(filePath);
_logger.LogWarning($"------ Mode:{output.Mode},FilePath:{filePath} Exists Deleted ! ------");
break;
}
}
}
using (StreamWriter streamWriter = new StreamWriter(filePath))
{
await streamWriter.WriteAsync(context.Result.Trim());
}
_logger.LogInformation($"------ Mode:{output.Mode},Build:{context.BuildKey} -> {filePath} End! ------");
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/MuNet/SmartCode.git
git@gitee.com:MuNet/SmartCode.git
MuNet
SmartCode
SmartCode
master

搜索帮助