diff --git a/Capnpc.Csharp.MsBuild.Generation/FeatureCodeBehindGenerator.cs b/Capnpc.Csharp.MsBuild.Generation/CapnpCodeBehindGenerator.cs similarity index 79% rename from Capnpc.Csharp.MsBuild.Generation/FeatureCodeBehindGenerator.cs rename to Capnpc.Csharp.MsBuild.Generation/CapnpCodeBehindGenerator.cs index 1f6d631..97ab0ba 100644 --- a/Capnpc.Csharp.MsBuild.Generation/FeatureCodeBehindGenerator.cs +++ b/Capnpc.Csharp.MsBuild.Generation/CapnpCodeBehindGenerator.cs @@ -4,12 +4,12 @@ using System.IO; namespace Capnpc.Csharp.MsBuild.Generation { - public class FeatureCodeBehindGenerator : IDisposable + public class CapnpCodeBehindGenerator : IDisposable { //private SpecFlowProject _specFlowProject; //private ITestGenerator _testGenerator; - public void InitializeProject(string projectPath, string rootNamespace, IEnumerable generatorPlugins) + public void InitializeProject(string projectPath) { //_specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(Path.GetFullPath(projectPath), rootNamespace); @@ -21,14 +21,16 @@ namespace Capnpc.Csharp.MsBuild.Generation } - public TestFileGeneratorResult GenerateCodeBehindFile(string featureFile) + public TestFileGeneratorResult GenerateCodeBehindFile(string capnpFile) { //var featureFileInput = new FeatureFileInput(featureFile); //var generatedFeatureFileName = Path.GetFileName(_testGenerator.GetTestFullPath(featureFileInput)); //var testGeneratorResult = _testGenerator.GenerateTestFile(featureFileInput, new GenerationSettings()); - return new TestFileGeneratorResult(null, null); + return new TestFileGeneratorResult( + new TestGeneratorResult() { GeneratedTestCode = "//dummy" }, + capnpFile + ".cs"); } public void Dispose() diff --git a/Capnpc.Csharp.MsBuild.Generation/FeatureFileCodeBehindGenerator.cs b/Capnpc.Csharp.MsBuild.Generation/CapnpFileCodeBehindGenerator.cs similarity index 66% rename from Capnpc.Csharp.MsBuild.Generation/FeatureFileCodeBehindGenerator.cs rename to Capnpc.Csharp.MsBuild.Generation/CapnpFileCodeBehindGenerator.cs index eeb369f..ff6d30e 100644 --- a/Capnpc.Csharp.MsBuild.Generation/FeatureFileCodeBehindGenerator.cs +++ b/Capnpc.Csharp.MsBuild.Generation/CapnpFileCodeBehindGenerator.cs @@ -7,11 +7,11 @@ using Microsoft.Build.Utilities; namespace Capnpc.Csharp.MsBuild.Generation { - public class FeatureFileCodeBehindGenerator : ICapnpCsharpGenerator + public class CapnpFileCodeBehindGenerator : ICapnpcCsharpGenerator { private readonly FilePathGenerator _filePathGenerator; - public FeatureFileCodeBehindGenerator(TaskLoggingHelper log) + public CapnpFileCodeBehindGenerator(TaskLoggingHelper log) { Log = log ?? throw new ArgumentNullException(nameof(log)); _filePathGenerator = new FilePathGenerator(); @@ -21,27 +21,24 @@ namespace Capnpc.Csharp.MsBuild.Generation public IEnumerable GenerateFilesForProject( string projectPath, - string rootNamespace, - List CapnpFiles, - List generatorPlugins, - string projectFolder, - string outputPath) + List capnpFiles, + string projectFolder) { - using (var featureCodeBehindGenerator = new FeatureCodeBehindGenerator()) + using (var capnpCodeBehindGenerator = new CapnpCodeBehindGenerator()) { - featureCodeBehindGenerator.InitializeProject(projectPath, rootNamespace, generatorPlugins); + capnpCodeBehindGenerator.InitializeProject(projectPath); var codeBehindWriter = new CodeBehindWriter(null); - if (CapnpFiles == null) + if (capnpFiles == null) { yield break; } - foreach (var featureFile in CapnpFiles) + foreach (var capnpFile in capnpFiles) { - var featureFileItemSpec = featureFile; - var generatorResult = featureCodeBehindGenerator.GenerateCodeBehindFile(featureFileItemSpec); + var capnpFileItemSpec = capnpFile; + var generatorResult = capnpCodeBehindGenerator.GenerateCodeBehindFile(capnpFileItemSpec); if (!generatorResult.Success) { @@ -63,11 +60,10 @@ namespace Capnpc.Csharp.MsBuild.Generation var targetFilePath = _filePathGenerator.GenerateFilePath( projectFolder, - outputPath, - featureFile, + capnpFile, generatorResult.Filename); - var resultedFile = codeBehindWriter.WriteCodeBehindFile(targetFilePath, featureFile, generatorResult); + var resultedFile = codeBehindWriter.WriteCodeBehindFile(targetFilePath, capnpFile, generatorResult); yield return FileSystemHelper.GetRelativePath(resultedFile, projectFolder); } diff --git a/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.csproj b/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.csproj index bb09bb6..67c482c 100644 --- a/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.csproj +++ b/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.csproj @@ -1,6 +1,6 @@  - netstandard2.0;netcoreapp2.1 + net471;netcoreapp2.1 false $(MSBuildThisFileDirectory)Capnpc.Csharp.MsBuild.Generation.nuspec true @@ -68,7 +68,7 @@ - + MSBuild:Compile diff --git a/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.nuspec b/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.nuspec index 35629ed..887d527 100644 --- a/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.nuspec +++ b/Capnpc.Csharp.MsBuild.Generation/Capnpc.Csharp.MsBuild.Generation.nuspec @@ -1,7 +1,7 @@ - Capnpc.Csharp.Generation + Capnpc.Csharp.MsBuild.Generation 1.0.0 Capnpc.Csharp.MsBuild.Generation Christian Köllner and contributors @@ -20,7 +20,7 @@ - + diff --git a/Capnpc.Csharp.MsBuild.Generation/CodeBehindWriter.cs b/Capnpc.Csharp.MsBuild.Generation/CodeBehindWriter.cs index f147ee7..f581de3 100644 --- a/Capnpc.Csharp.MsBuild.Generation/CodeBehindWriter.cs +++ b/Capnpc.Csharp.MsBuild.Generation/CodeBehindWriter.cs @@ -13,7 +13,7 @@ namespace Capnpc.Csharp.MsBuild.Generation public TaskLoggingHelper Log { get; } - public string WriteCodeBehindFile(string outputPath, string featureFile, TestFileGeneratorResult testFileGeneratorResult) //todo needs unit tests + public string WriteCodeBehindFile(string outputPath, string capnpFile, TestFileGeneratorResult testFileGeneratorResult) { //if (string.IsNullOrEmpty(testFileGeneratorResult.Filename)) //{ @@ -21,27 +21,27 @@ namespace Capnpc.Csharp.MsBuild.Generation // return null; //} - //string directoryPath = Path.GetDirectoryName(outputPath) ?? throw new InvalidOperationException(); - //Log?.LogWithNameTag(Log.LogMessage, directoryPath); + 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}"); + Log?.LogWithNameTag(Log.LogMessage, $"Writing data to {outputPath}; path = {directoryPath}; generatedFilename = {testFileGeneratorResult.Filename}"); - //if (File.Exists(outputPath)) - //{ - // if (!FileSystemHelper.FileCompareContent(outputPath, testFileGeneratorResult.GeneratedTestCode)) - // { - // File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedTestCode); - // } - //} - //else - //{ - // if (!Directory.Exists(directoryPath)) - // { - // Directory.CreateDirectory(directoryPath); - // } + 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.GeneratedTestCode); - //} + File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedCode); + } return outputPath; } diff --git a/Capnpc.Csharp.MsBuild.Generation/FilePathGenerator.cs b/Capnpc.Csharp.MsBuild.Generation/FilePathGenerator.cs index c1e71a1..4ac982d 100644 --- a/Capnpc.Csharp.MsBuild.Generation/FilePathGenerator.cs +++ b/Capnpc.Csharp.MsBuild.Generation/FilePathGenerator.cs @@ -5,16 +5,16 @@ namespace Capnpc.Csharp.MsBuild.Generation { public class FilePathGenerator { - public string GenerateFilePath(string projectFolder, string relativeOutputPath, string featureFileName, string generatedCodeBehindFileName) + public string GenerateFilePath(string projectFolder, string capnpFileName, string generatedCodeBehindFileName) { if (projectFolder is null) { throw new ArgumentNullException(nameof(projectFolder)); } - if (featureFileName is null) + if (capnpFileName is null) { - throw new ArgumentNullException(nameof(featureFileName)); + throw new ArgumentNullException(nameof(capnpFileName)); } if (generatedCodeBehindFileName is null) @@ -22,7 +22,7 @@ namespace Capnpc.Csharp.MsBuild.Generation throw new ArgumentNullException(nameof(generatedCodeBehindFileName)); } - string featureFileFullPath = Path.GetFullPath(Path.Combine(projectFolder, relativeOutputPath ?? "", featureFileName)); + string featureFileFullPath = Path.GetFullPath(Path.Combine(projectFolder, capnpFileName)); string featureFileDirPath = Path.GetDirectoryName(featureFileFullPath); return Path.Combine(featureFileDirPath, generatedCodeBehindFileName); diff --git a/Capnpc.Csharp.MsBuild.Generation/FileSystemHelper.cs b/Capnpc.Csharp.MsBuild.Generation/FileSystemHelper.cs index 14cfd41..d27428f 100644 --- a/Capnpc.Csharp.MsBuild.Generation/FileSystemHelper.cs +++ b/Capnpc.Csharp.MsBuild.Generation/FileSystemHelper.cs @@ -69,136 +69,44 @@ namespace Capnpc.Csharp.MsBuild.Generation // Open the two files. using (FileStream fs1 = new FileStream(filePath1, FileMode.Open, FileAccess.Read)) - using (FileStream fs2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) { - // Check the file sizes. If they are not the same, the files - // are not the same. - if (fs1.Length != fs2.Length) + using (FileStream fs2 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) { - // Return false to indicate files are different - return false; + // Check the file sizes. If they are not the same, the files + // are not the same. + if (fs1.Length != fs2.Length) + { + // Return false to indicate files are different + return false; + } + + // Read and compare a byte from each file until either a + // non-matching set of bytes is found or until the end of + // file1 is reached. + do + { + // Read one byte from each file. + file1byte = fs1.ReadByte(); + file2byte = fs2.ReadByte(); + } while ((file1byte == file2byte) && (file1byte != -1)); } - - // Read and compare a byte from each file until either a - // non-matching set of bytes is found or until the end of - // file1 is reached. - do - { - // Read one byte from each file. - file1byte = fs1.ReadByte(); - file2byte = fs2.ReadByte(); - } while ((file1byte == file2byte) && (file1byte != -1)); } - // Return the success of the comparison. "file1byte" is // equal to "file2byte" at this point only if the files are // the same. return ((file1byte - file2byte) == 0); } - public static void CopyDirectory(string sourcePath, string destPath, bool cleanDestination = true) + // This method accepts two strings the represent two files to + // compare. A return value of true indicates that the contents of the files + // are the same. A return value of any other value indicates that the + // files are not the same. + public static bool FileCompareContent(string filePath1, string fileContent) { - if (cleanDestination) - EnsureEmptyFolder(destPath); - else - { - if (!Directory.Exists(destPath)) - { - Directory.CreateDirectory(destPath); - } - } + var currentFileContent = File.ReadAllText(filePath1); - foreach (string file in Directory.GetFiles(sourcePath)) - { - var fileName = Path.GetFileName(file); - Debug.Assert(fileName != null); - string dest = Path.Combine(destPath, fileName); - File.Copy(file, dest, true); - File.SetAttributes(dest, File.GetAttributes(dest) & (~FileAttributes.ReadOnly)); - } + return string.CompareOrdinal(currentFileContent, fileContent) == 0; - foreach (string folder in Directory.GetDirectories(sourcePath)) - { - var folderName = Path.GetFileName(folder); - Debug.Assert(folderName != null); - string dest = Path.Combine(destPath, folderName); - CopyDirectory(folder, dest); - } - } - - public static void EnsureEmptyFolder(string folderName) - { - folderName = Path.GetFullPath(folderName); - - if (!Directory.Exists(folderName)) - { - Directory.CreateDirectory(folderName); - return; - } - - DeleteFolderContent(folderName); - } - - public static void EnsureFolderOfFile(string filePath) - { - string directory = Path.GetDirectoryName(filePath); - if (directory != null && !Directory.Exists(directory)) - Directory.CreateDirectory(directory); - } - - - private static void Retry(int number, Action action) - { - try - { - action(); - } - catch (UnauthorizedAccessException) - { - var i = number - 1; - - if (i == 0) - throw; - - Retry(i, action); - } - } - - public static void DeleteFolderContent(string folderName) - { - foreach (string file in Directory.GetFiles(folderName)) - { - try - { - Retry(5, () => File.Delete(file)); - } - catch (Exception ex) - { - throw new Exception("Unable to delete file: " + file, ex); - } - } - - foreach (string folder in Directory.GetDirectories(folderName)) - { - try - { - Retry(5, () => Directory.Delete(folder, true)); - } - catch (Exception ex) - { - throw new Exception("Unable to delete folder: " + folder, ex); - } - } - } - - public static void DeleteFolder(string path) - { - if (!Directory.Exists(path)) - return; - - DeleteFolderContent(path); - - Retry(5, () => Directory.Delete(path, true)); } } } diff --git a/Capnpc.Csharp.MsBuild.Generation/GenerateCapnpFileCodeBehindTask.cs b/Capnpc.Csharp.MsBuild.Generation/GenerateCapnpFileCodeBehindTask.cs index ee69aa8..c120448 100644 --- a/Capnpc.Csharp.MsBuild.Generation/GenerateCapnpFileCodeBehindTask.cs +++ b/Capnpc.Csharp.MsBuild.Generation/GenerateCapnpFileCodeBehindTask.cs @@ -13,23 +13,18 @@ namespace Capnpc.Csharp.MsBuild.Generation { public GenerateCapnpFileCodeBehindTask() { - CodeBehindGenerator = new FeatureFileCodeBehindGenerator(Log); + CodeBehindGenerator = new CapnpFileCodeBehindGenerator(Log); } - public ICapnpCsharpGenerator CodeBehindGenerator { get; set; } + public ICapnpcCsharpGenerator CodeBehindGenerator { get; set; } [Required] public string ProjectPath { get; set; } - public string RootNamespace { get; set; } - public string ProjectFolder => Path.GetDirectoryName(ProjectPath); - public string OutputPath { get; set; } public ITaskItem[] CapnpFiles { get; set; } - public ITaskItem[] GeneratorPlugins { get; set; } - [Output] public ITaskItem[] GeneratedFiles { get; private set; } @@ -55,21 +50,16 @@ namespace Capnpc.Csharp.MsBuild.Generation AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; - var generator = CodeBehindGenerator ?? new FeatureFileCodeBehindGenerator(Log); + var generator = CodeBehindGenerator ?? new CapnpFileCodeBehindGenerator(Log); Log.LogWithNameTag(Log.LogMessage, "Starting GenerateFeatureFileCodeBehind"); - var generatorPlugins = GeneratorPlugins?.Select(gp => gp.ItemSpec).ToList() ?? new List(); - var capnpFiles = CapnpFiles?.Select(i => i.ItemSpec).ToList() ?? new List(); var generatedFiles = generator.GenerateFilesForProject( ProjectPath, - RootNamespace, capnpFiles, - generatorPlugins, - ProjectFolder, - OutputPath); + ProjectFolder); GeneratedFiles = generatedFiles.Select(file => new TaskItem { ItemSpec = file }).ToArray(); diff --git a/Capnpc.Csharp.MsBuild.Generation/ICapnpCsharpGenerator.cs b/Capnpc.Csharp.MsBuild.Generation/ICapnpCsharpGenerator.cs index 5368331..4213d8d 100644 --- a/Capnpc.Csharp.MsBuild.Generation/ICapnpCsharpGenerator.cs +++ b/Capnpc.Csharp.MsBuild.Generation/ICapnpCsharpGenerator.cs @@ -2,8 +2,8 @@ namespace Capnpc.Csharp.MsBuild.Generation { - public interface ICapnpCsharpGenerator + public interface ICapnpcCsharpGenerator { - IEnumerable GenerateFilesForProject(string projectPath, string rootNamespace, List CapnpFiles, List generatorPlugins, string projectFolder, string outputPath); + IEnumerable GenerateFilesForProject(string projectPath, List capnpFiles, string projectFolder); } } \ No newline at end of file diff --git a/Capnpc.Csharp.MsBuild.Generation/TestFileGeneratorResult.cs b/Capnpc.Csharp.MsBuild.Generation/TestFileGeneratorResult.cs index 5eb0160..36ab361 100644 --- a/Capnpc.Csharp.MsBuild.Generation/TestFileGeneratorResult.cs +++ b/Capnpc.Csharp.MsBuild.Generation/TestFileGeneratorResult.cs @@ -17,7 +17,7 @@ namespace Capnpc.Csharp.MsBuild.Generation Errors = generatorResult.Errors; IsUpToDate = generatorResult.IsUpToDate; - GeneratedTestCode = generatorResult.GeneratedTestCode; + GeneratedCode = generatorResult.GeneratedTestCode; } /// @@ -31,9 +31,9 @@ namespace Capnpc.Csharp.MsBuild.Generation public bool IsUpToDate { get; } /// - /// The generated test code. + /// The generated code. /// - public string GeneratedTestCode { get; } + public string GeneratedCode { get; } public bool Success => Errors == null || !Errors.Any(); diff --git a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/CpsExtension.DesignTime.targets b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/CpsExtension.DesignTime.targets index b23690f..4f6883c 100644 --- a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/CpsExtension.DesignTime.targets +++ b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/CpsExtension.DesignTime.targets @@ -8,7 +8,7 @@ - + File;BrowseObject diff --git a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/FeatureFileType.xaml b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/CapnpFileType.xaml similarity index 92% rename from Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/FeatureFileType.xaml rename to Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/CapnpFileType.xaml index 0864f1e..51e0ab0 100644 --- a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/FeatureFileType.xaml +++ b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/CapnpFileType.xaml @@ -2,10 +2,10 @@ diff --git a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/ProjectItemsSchema.xaml b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/ProjectItemsSchema.xaml index 47300db..1cf7ddb 100644 --- a/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/ProjectItemsSchema.xaml +++ b/Capnpc.Csharp.MsBuild.Generation/build/CPS/Buildsystem/Rules/ProjectItemsSchema.xaml @@ -4,15 +4,15 @@ xmlns="http://schemas.microsoft.com/build/2009/properties"> - - + - + - + + \ No newline at end of file diff --git a/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.props b/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.props index e51a8aa..fe7e57a 100644 --- a/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.props +++ b/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.props @@ -21,7 +21,7 @@ true false - <_SpecFlowPropsImported Condition="'$(_SpecFlowPropsImported)'==''">true + <_CapnpcCsharpPropsImported Condition="'$(_CapnpcCsharpPropsImported)'==''">true true - $(CapnpcCsharp_EnableDefaultCompileItems) + $(CapnpcCsharp_EnableDefaultCompileItems) - $(DefaultItemExcludes);**/*.feature + $(DefaultItemExcludes);**/*.capnp - %(RelativeDir)%(Filename).capnp(DefaultLanguageSourceExtension) + %(RelativeDir)%(Filename).capnp.cs $(UsingMicrosoftNETSdk) @@ -54,7 +54,7 @@ - after deletion of a feature file - after pulling latest changes from version control with above changes --> - + - - + CapnpFiles="@(CapnpFiles)" > - - + + - + + diff --git a/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.tasks b/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.tasks index 9e4e977..b20fdae 100644 --- a/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.tasks +++ b/Capnpc.Csharp.MsBuild.Generation/build/Capnpc.Csharp.MsBuild.Generation.tasks @@ -1,3 +1,3 @@  - + \ No newline at end of file