Skip to content

Commit

Permalink
fix msbuild task
Browse files Browse the repository at this point in the history
  • Loading branch information
gasparnagy committed Nov 25, 2009
1 parent 8a83666 commit 2bef638
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 191 deletions.
79 changes: 0 additions & 79 deletions Generator/BatchTestGenerator.cs

This file was deleted.

16 changes: 0 additions & 16 deletions Tools/MsBuild/BatchTestGeneratorTask.cs

This file was deleted.

3 changes: 2 additions & 1 deletion Tools/MsBuild/GeneratorTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ protected override void DoExecute()
{
SpecFlowProject specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(ProjectPath);

BatchGenerator batchGenerator = new MsBuildBatchGenerator(GetMessageWriter(), VerboseOutput, this);
BatchGenerator batchGenerator = new MsBuildBatchGenerator(
GetMessageWriter(MessageImportance.High), VerboseOutput, this);
batchGenerator.ProcessProject(specFlowProject, ForceGeneration);
}
}
Expand Down
75 changes: 65 additions & 10 deletions Tools/MsBuild/GeneratorTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public abstract class GeneratorTaskBase : TaskBase
{
public bool BuildServerMode { get; set; }
public bool OverwriteReadOnlyFiles { get; set; }
public bool OnlyUpdateIfChanged { get; set; }

public class OutputFile
{
Expand All @@ -31,7 +32,7 @@ public virtual void Done(CompilerErrorCollection result)
}
}

public class VerifyDifferenceOutputFile : OutputFile
public abstract class TempOutputFile : OutputFile
{
public string TempFilePath { get; private set; }

Expand All @@ -40,23 +41,51 @@ public override string FilePathForWriting
get { return TempFilePath; }
}

public VerifyDifferenceOutputFile(string filePath) : base(filePath)
protected TempOutputFile(string filePath)
: base(filePath)
{
TempFilePath = Path.Combine(Path.GetTempPath(), "tmp" + Path.GetFileName(filePath));
}

public override void Done(CompilerErrorCollection result)
{
Compare(TempFilePath, FilePath, result);
SafeDeleteFile(TempFilePath);
}
}

public class VerifyDifferenceOutputFile : TempOutputFile
{
public VerifyDifferenceOutputFile(string filePath) : base(filePath)
{
}

private void Compare(string path1, string path2, CompilerErrorCollection result)
public override void Done(CompilerErrorCollection result)
{
if (FileCompare(path1, path2))
return;
if (!FileCompare(TempFilePath, FilePath))
{
string message = String.Format("Error during file generation. The target file '{0}' is read-only, but different from the transformation result. This problem can be a sign of an inconsistent source code package. Compile and check-in the current version of the file from the development environment or remove the read-only flag from the generation result. To compile a solution that contains messaging project on a build server, you can also exclude the messaging project from the build-server solution or set the <OverwriteReadOnlyFiles> msbuild project parameter to 'true' in the messaging project file.",
Path.GetFullPath(FilePath));
result.Add(new CompilerError(String.Empty, 0, 0, null, message));
}

string message = String.Format("Error during file generation. The target file '{0}' is read-only, but different from the transformation result. This problem can be a sign of an inconsistent source code package. Compile and check-in the current version of the file from the development environment or remove the read-only flag from the generation result. To compile a solution that contains messaging project on a build server, you can also exclude the messaging project from the build-server solution or set the <OverwriteReadOnlyFiles> msbuild project parameter to 'true' in the messaging project file.", Path.GetFullPath(path2));
result.Add(new CompilerError(String.Empty, 0, 0, null, message));
base.Done(result);
}
}

public class UpdateIfChangedOutputFile : TempOutputFile
{
public UpdateIfChangedOutputFile(string filePath) : base(filePath)
{
}

public override void Done(CompilerErrorCollection result)
{
if (!FileCompare(TempFilePath, FilePath))
{
ReplaceFile(TempFilePath, FilePath);
}

base.Done(result);
}
}

Expand All @@ -71,6 +100,9 @@ public OutputFile PrepareOutputFile(string outputFilePath)
if (isReadOnly && BuildServerMode)
return new VerifyDifferenceOutputFile(outputFilePath);

if (OnlyUpdateIfChanged)
return new UpdateIfChangedOutputFile(outputFilePath);

return new OutputFile(outputFilePath);
}

Expand Down Expand Up @@ -119,7 +151,7 @@ private static bool FileCompare(string filePath1, string filePath2)
return ((file1byte - file2byte) == 0);
}

private bool IsReadOnly(string path)
private static bool IsReadOnly(string path)
{
try
{
Expand All @@ -138,11 +170,34 @@ private bool IsReadOnly(string path)
}
}

private void RemoveReadOnly(string path)
private static void RemoveReadOnly(string path)
{
FileInfo fileInfo = new FileInfo(path);
if (fileInfo.Exists && fileInfo.IsReadOnly)
fileInfo.IsReadOnly = false;
}

private static void SafeDeleteFile(string path)
{
try
{
if (IsReadOnly(path))
RemoveReadOnly(path);
File.Delete(path);

}
catch (Exception ex)
{
Debug.WriteLine(ex, "SaveDeleteFile");
}
}

private static void ReplaceFile(string sourcePath, string targetPath)
{
if (File.Exists(targetPath))
SafeDeleteFile(targetPath);

File.Move(sourcePath, targetPath);
}
}
}
13 changes: 12 additions & 1 deletion Tools/MsBuild/TaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,19 @@ protected void OutputInformation(MessageImportance importance, string message, p
private class MessageTextWriter : TextWriter
{
private TaskBase task;
MessageImportance importance = MessageImportance.Normal;
private MessageImportance importance = MessageImportance.Normal;

public MessageTextWriter(TaskBase task)
{
this.task = task;
}

public MessageTextWriter(TaskBase task, MessageImportance importance)
{
this.task = task;
this.importance = importance;
}

public override Encoding Encoding
{
get { return Encoding.Unicode; }
Expand All @@ -113,6 +119,11 @@ protected TextWriter GetMessageWriter()
return new MessageTextWriter(this);
}

protected TextWriter GetMessageWriter(MessageImportance importance)
{
return new MessageTextWriter(this, importance);
}

protected abstract void DoExecute();
}
}
58 changes: 58 additions & 0 deletions Tools/MsBuild/TechTalk.SpecFlow.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Import Project="TechTalk.SpecFlow.tasks"/>

<!-- this setting is to workaround the bug in VS (does not detect changes during the pre-build event)
see: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=423670&wa=wsignin1.0
-->
<PropertyGroup>
<UseHostCompilerIfAvailable>false</UseHostCompilerIfAvailable>
</PropertyGroup>

<PropertyGroup>
<ShowTrace Condition="'$(ShowTrace)'==''">false</ShowTrace>

<OverwriteReadOnlyFiles Condition="'$(OverwriteReadOnlyFiles)'==''">false</OverwriteReadOnlyFiles>
<ForceGeneration Condition="'$(ForceGeneration)'==''">false</ForceGeneration>
<VerboseOutput Condition="'$(VerboseOutput)'==''">false</VerboseOutput>
</PropertyGroup>

<PropertyGroup Condition="'$(BuildServerMode)' == ''">
<BuildServerMode Condition="'$(BuildingInsideVisualStudio)'=='true'">false</BuildServerMode>
<BuildServerMode Condition="'$(BuildingInsideVisualStudio)'!='true'">true</BuildServerMode>
</PropertyGroup>

<PropertyGroup>
<BuildDependsOn>
UpdateFeatureFilesInProject;
$(BuildDependsOn)
</BuildDependsOn>
<RebuildDependsOn>
SwitchToForceGenerate;
$(RebuildDependsOn)
</RebuildDependsOn>
</PropertyGroup>

<Target Name="SwitchToForceGenerate">
<PropertyGroup>
<ForceGeneration>true</ForceGeneration>
<OnlyUpdateIfChanged>true</OnlyUpdateIfChanged>
</PropertyGroup>
</Target>

<Target Name="UpdateFeatureFilesInProject">
<GeneratorTask
ShowTrace="$(ShowTrace)"

BuildServerMode="$(BuildServerMode)"
OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)"

ProjectPath="$(MSBuildProjectFullPath)"
ForceGeneration="$(ForceGeneration)"
VerboseOutput="$(VerboseOutput)"
/>

</Target>

</Project>
17 changes: 17 additions & 0 deletions Tools/MsBuild/TechTalk.SpecFlow.tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SpecFlowTasksPath Condition="'$(SpecFlowTasksPath)'==''">specflow.exe</SpecFlowTasksPath>
</PropertyGroup>

<PropertyGroup>
<!-- handle absolute / targets-relative tasks path -->
<__SpecFlowTasksFullPath>$(SpecFlowTasksPath)</__SpecFlowTasksFullPath>
<!-- handle relative tasks path -->
<__SpecFlowTasksFullPath Condition="Exists('$(MSBuildProjectDirectory)\$(SpecFlowTasksPath)')"
>$(MSBuildProjectDirectory)\$(SpecFlowTasksPath)</__SpecFlowTasksFullPath>
</PropertyGroup>

<UsingTask TaskName="TechTalk.SpecFlow.Tools.MsBuild.GeneratorTask" AssemblyFile="$(__SpecFlowTasksFullPath)" />

</Project>
14 changes: 11 additions & 3 deletions Tools/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using NConsoler;
using System;
using NConsoler;
using TechTalk.SpecFlow.Generator;
using TechTalk.SpecFlow.Generator.Configuration;
using TechTalk.SpecFlow.GeneratorTools.BatchTestGeneration;

namespace TechTalk.SpecFlow.Tools
{
Expand All @@ -21,7 +22,14 @@ public static void Generate(
{
SpecFlowProject specFlowProject = MsBuildProjectReader.LoadSpecFlowProjectFromMsBuild(projectFile);

BatchTestGenerator.ProcessProject(specFlowProject, false, false);
BatchGenerator batchGenerator = new BatchGenerator(Console.Out, verboseOutput);
batchGenerator.ProcessProject(specFlowProject, forceGeneration);
}

[Action]
public static void ToBeDefinedAction()
{

}
}
}
Loading

0 comments on commit 2bef638

Please sign in to comment.