Skip to content

Commit

Permalink
Merge branch 'generatortools'
Browse files Browse the repository at this point in the history
Conflicts:
	Generator/SpecFlowUnitTestConverter.cs
	Parser/SpecFlowGenerator.cs
	Parser/TechTalk.SpecFlow.Parser.csproj
	Tests/ParserTests/SuccessfulGenerationTest.cs
	Tests/RuntimeTests/ExecutionTestBase.cs
	VsIntegration/SpecFlowSingleFileGenerator.cs
	changelog.txt
  • Loading branch information
gasparnagy committed Nov 25, 2009
2 parents 1dfdd1c + 30d26b4 commit e370948
Show file tree
Hide file tree
Showing 44 changed files with 1,371 additions and 1,314 deletions.
97 changes: 97 additions & 0 deletions Generator/BatchGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow.Generator.Configuration;

namespace TechTalk.SpecFlow.Generator
{
public class BatchGenerator
{
private readonly TextWriter traceWriter;
private readonly bool verboseOutput;

public BatchGenerator(TextWriter traceWriter, bool verboseOutput)
{
this.traceWriter = traceWriter;
this.verboseOutput = verboseOutput;
}

public void ProcessProject(SpecFlowProject specFlowProject, bool forceGenerate)
{
if (verboseOutput)
traceWriter.WriteLine("Processing project: " + specFlowProject);

SpecFlowGenerator generator = new SpecFlowGenerator(specFlowProject);

foreach (var featureFile in specFlowProject.FeatureFiles)
{
string featureFileFullPath = featureFile.GetFullPath(specFlowProject);

string codeFileFullPath = featureFileFullPath + ".cs";

bool needsProcessing = true;

if (!forceGenerate && IsUpToDate(generator, featureFileFullPath, codeFileFullPath))
{
needsProcessing = false;
}

if (verboseOutput || needsProcessing)
{
traceWriter.WriteLine("Processing file: {0}", featureFileFullPath);
if (!needsProcessing)
traceWriter.WriteLine(" up-to-date");
}

if (needsProcessing)
{
GenerateFile(generator, featureFile, codeFileFullPath);
}
}
}

protected virtual void GenerateFile(SpecFlowGenerator generator, SpecFlowFeatureFile featureFile, string codeFileFullPath)
{
using (var writer = GetWriter(codeFileFullPath))
{
generator.GenerateCSharpTestFile(featureFile, writer);
}
}

protected virtual StreamWriter GetWriter(string codeFileFullPath)
{
return new StreamWriter(codeFileFullPath, false, Encoding.UTF8);
}

private static bool IsUpToDate(SpecFlowGenerator generator, string featureFileFullPath, string codeFileFullPath)
{
// check existance of the target file
if (!File.Exists(codeFileFullPath))
return false;

// check modification time of the target file
try
{
var featureFileModificationTime = File.GetLastWriteTime(featureFileFullPath);
var codeFileModificationTime = File.GetLastWriteTime(codeFileFullPath);

if (featureFileModificationTime > codeFileModificationTime)
return false;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}

// check tools version
var codeFileVersion = generator.GetGeneratedFileSpecFlowVersion(codeFileFullPath);
if (codeFileVersion == null || codeFileVersion != generator.GetCurrentSpecFlowVersion())
return false;

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Globalization;
using System.Linq;
using TechTalk.SpecFlow.Configuration;
using TechTalk.SpecFlow.Parser.UnitTestProvider;
using TechTalk.SpecFlow.Generator.UnitTestProvider;

namespace TechTalk.SpecFlow.Parser.Configuration
namespace TechTalk.SpecFlow.Generator.Configuration
{
public class GeneratorConfiguration
{
Expand All @@ -22,8 +22,8 @@ public GeneratorConfiguration()
{
FeatureLanguage = CultureInfo.GetCultureInfo(ConfigDefaults.FeatureLanguage);
ToolLanguage = string.IsNullOrEmpty(ConfigDefaults.ToolLanguage) ?
FeatureLanguage :
CultureInfo.GetCultureInfo(ConfigDefaults.ToolLanguage);
FeatureLanguage :
CultureInfo.GetCultureInfo(ConfigDefaults.ToolLanguage);

SetUnitTestDefaultsByName(ConfigDefaults.UnitTestProviderName);

Expand All @@ -38,8 +38,8 @@ internal void UpdateFromConfigFile(ConfigurationSectionHandler configSection)
{
FeatureLanguage = CultureInfo.GetCultureInfo(configSection.Language.Feature);
ToolLanguage = string.IsNullOrEmpty(configSection.Language.Tool) ?
FeatureLanguage :
CultureInfo.GetCultureInfo(configSection.Language.Tool);
FeatureLanguage :
CultureInfo.GetCultureInfo(configSection.Language.Tool);
}

if (configSection.UnitTestProvider != null)
Expand Down Expand Up @@ -81,4 +81,4 @@ private void SetUnitTestDefaultsByName(string name)

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Xml;
using TechTalk.SpecFlow.Configuration;

namespace TechTalk.SpecFlow.Parser.Configuration
namespace TechTalk.SpecFlow.Generator.Configuration
{
public static class GeneratorConfigurationReader
{
Expand Down
52 changes: 52 additions & 0 deletions Generator/Configuration/MsBuildProjectReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.IO;
using System.Linq;
using Microsoft.Build.BuildEngine;
using TechTalk.SpecFlow.Generator.Configuration;

namespace TechTalk.SpecFlow.Generator.Configuration
{
public static class MsBuildProjectReader
{
public static SpecFlowProject LoadSpecFlowProjectFromMsBuild(string projectFile)
{
projectFile = Path.GetFullPath(projectFile);

Project project = Engine.GlobalEngine.GetLoadedProject(projectFile);
if (project == null)
{
project = new Project();
project.Load(projectFile, ProjectLoadSettings.IgnoreMissingImports);
}

string projectFolder = Path.GetDirectoryName(projectFile);

SpecFlowProject specFlowProject = new SpecFlowProject();
specFlowProject.ProjectFolder = projectFolder;
specFlowProject.ProjectName = Path.GetFileNameWithoutExtension(projectFile);
specFlowProject.AssemblyName = project.GetEvaluatedProperty("AssemblyName");
specFlowProject.DefaultNamespace = project.GetEvaluatedProperty("RootNamespace");

var items = project.GetEvaluatedItemsByName("None").Cast<BuildItem>()
.Concat(project.GetEvaluatedItemsByName("Content").Cast<BuildItem>());
foreach (BuildItem item in items)
{
var extension = Path.GetExtension(item.FinalItemSpec);
if (extension.Equals(".feature", StringComparison.InvariantCultureIgnoreCase))
{
var featureFile = new SpecFlowFeatureFile(item.FinalItemSpec);
var ns = item.GetEvaluatedMetadata("CustomToolNamespace");
if (!String.IsNullOrEmpty(ns))
featureFile.CustomNamespace = ns;
specFlowProject.FeatureFiles.Add(featureFile);
}

if (Path.GetFileName(item.FinalItemSpec).Equals("app.config", StringComparison.InvariantCultureIgnoreCase))
{
GeneratorConfigurationReader.UpdateConfigFromFile(specFlowProject.GeneratorConfiguration, Path.Combine(projectFolder, item.FinalItemSpec));
}
}
return specFlowProject;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace TechTalk.SpecFlow.Parser.Configuration
namespace TechTalk.SpecFlow.Generator.Configuration
{
public class SpecFlowFeatureFile
{
Expand Down Expand Up @@ -32,5 +33,16 @@ public SpecFlowProject()
FeatureFiles = new List<SpecFlowFeatureFile>();
GeneratorConfiguration = new GeneratorConfiguration(); // load defaults
}

public SpecFlowFeatureFile GetOrCreateFeatureFile(string featureFileName)
{
featureFileName = Path.GetFullPath(Path.Combine(ProjectFolder, featureFileName));
var result = FeatureFiles.Find(ff => ff.GetFullPath(this).Equals(featureFileName, StringComparison.InvariantCultureIgnoreCase));
if (result == null)
{
result = new SpecFlowFeatureFile(featureFileName); //TODO: make it project relative
}
return result;
}
}
}
23 changes: 23 additions & 0 deletions Generator/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TechTalk.SpecFlow.Generator")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TechTalk.SpecFlow.Generator")]
[assembly: AssemblyCopyright("Copyright © 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("743e1609-8937-4775-9896-6e8757c6d98d")]
Loading

0 comments on commit e370948

Please sign in to comment.