50 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.IO;
using Microsoft.Build.Utilities;
namespace Capnpc.Csharp.MsBuild.Generation
{
public class CodeBehindWriter
{
public CodeBehindWriter(TaskLoggingHelper log)
{
Log = log;
}
public TaskLoggingHelper Log { get; }
2019-09-05 21:59:25 +02:00
public string WriteCodeBehindFile(string outputPath, string capnpFile, TestFileGeneratorResult testFileGeneratorResult)
{
//if (string.IsNullOrEmpty(testFileGeneratorResult.Filename))
//{
// Log?.LogWithNameTag(Log.LogError, $"{featureFile} has no generated filename");
// return null;
//}
2019-09-05 21:59:25 +02:00
string directoryPath = Path.GetDirectoryName(outputPath) ?? throw new InvalidOperationException();
Log?.LogWithNameTag(Log.LogMessage, directoryPath);
Log?.LogWithNameTag(Log.LogMessage, $"Writing data to {outputPath}; path = {directoryPath}; generatedFilename = {testFileGeneratorResult.Filename}");
if (File.Exists(outputPath))
{
if (!FileSystemHelper.FileCompareContent(outputPath, testFileGeneratorResult.GeneratedCode))
{
File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedCode);
}
}
else
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedCode);
}
return outputPath;
}
}
}