Permalink
Browse files

refactor so that when you register a target, you provide a TargetGene…

…ratingAction instead of a System.Action
  • Loading branch information...
1 parent 8fcbfed commit 8dc7dd9b47529c476cba3ec9370f46372bb0b5f4 Lucas Meijer committed Apr 14, 2012
View
@@ -23,9 +23,5 @@ public IEnumerable<string> GetInputFiles()
foreach (var file in scanner.GetFilesIncludedBy(_sourceFile))
yield return file;
}
-
-
-
-
}
}
View
@@ -7,8 +7,8 @@ namespace bs
{
public class DependencyGraph
{
- readonly Dictionary<string, TargetBuildInstructions> _graph = new Dictionary<string, TargetBuildInstructions>();
- public Action<string, TargetBuildInstructions> GenerateCallback = (s, i) => { };
+ readonly Dictionary<string, TargetGenerateInstructions> _graph = new Dictionary<string, TargetGenerateInstructions>();
+ public Action<string, TargetGenerateInstructions> GenerateCallback = (s, i) => { };
private readonly BuildHistory _buildHistory;
public DependencyGraph() : this(new BuildHistory())
@@ -31,7 +31,7 @@ public void RequestTarget(string targetFile)
Generate(targetFile, instructions);
}
- private bool NeedToGenerate(string targetFile, TargetBuildSettings buildSettings)
+ private bool NeedToGenerate(string targetFile, TargetGenerateSettings generateSettings)
{
var recordOfLastBuild = _buildHistory.FindRecordFor(targetFile);
@@ -41,25 +41,25 @@ private bool NeedToGenerate(string targetFile, TargetBuildSettings buildSettings
if (!File.Exists(targetFile))
return true;
- if (!buildSettings.Equals(recordOfLastBuild.Settings))
+ if (!generateSettings.Equals(recordOfLastBuild.Settings))
return true;
if (File.GetLastWriteTimeUtc(targetFile) < recordOfLastBuild.ModificationTimeOfTargetFile())
return true;
- return buildSettings.InputFiles.Any(sourceFile => recordOfLastBuild.ModificationTimeOf(sourceFile) != File.GetLastWriteTimeUtc(sourceFile));
+ return generateSettings.InputFiles.Any(sourceFile => recordOfLastBuild.ModificationTimeOf(sourceFile) != File.GetLastWriteTimeUtc(sourceFile));
}
- private void Generate(string targetFile, TargetBuildInstructions instructions)
+ private void Generate(string targetFile, TargetGenerateInstructions instructions)
{
GenerateCallback(targetFile, instructions);
- instructions.Action(targetFile, instructions.Settings);
+ instructions.Action.Invoke(targetFile, instructions.Settings);
- var record = new GenerationRecord(targetFile, instructions.Settings);
+ var record = new GenerationRecord(targetFile, instructions.Settings, "");
_buildHistory.AddRecord(record);
}
- public void RegisterTarget(string targetFile, TargetBuildInstructions instructions)
+ public void RegisterTarget(string targetFile, TargetGenerateInstructions instructions)
{
_graph.Add(targetFile,instructions);
}
View
@@ -7,12 +7,14 @@ namespace bs
public class GenerationRecord
{
public string TargetFile { get; private set; }
- public TargetBuildSettings Settings { get; private set; }
+ public TargetGenerateSettings Settings { get; private set; }
+ public string ActionHash { get; private set; }
private readonly Dictionary<string, DateTime> _modificationDates = new Dictionary<string, DateTime>();
- public GenerationRecord(string targetFile, TargetBuildSettings settings)
+ public GenerationRecord(string targetFile, TargetGenerateSettings settings, string actionHash)
{
+ ActionHash = actionHash;
TargetFile = targetFile;
Settings = settings;
@@ -0,0 +1,8 @@
+namespace bs
+{
+ public interface ITargetGeneratingAction
+ {
+ void Invoke(string target, TargetGenerateSettings instructions);
+ string GetActionHash();
+ }
+}
View
@@ -1,10 +0,0 @@
-using System;
-
-namespace bs
-{
- public class TargetBuildInstructions
- {
- public Action<string, TargetBuildSettings> Action;
- public TargetBuildSettings Settings;
- }
-}
@@ -0,0 +1,10 @@
+using System;
+
+namespace bs
+{
+ public class TargetGenerateInstructions
+ {
+ public ITargetGeneratingAction Action;
+ public TargetGenerateSettings Settings;
+ }
+}
@@ -2,15 +2,15 @@
namespace bs
{
- public class TargetBuildSettings
+ public class TargetGenerateSettings
{
public HashSet<string> InputFiles;
//todo: figure out how to write a sane Equals
public override bool Equals(object obj)
{
if (obj == null) return false;
- var other = obj as TargetBuildSettings;
+ var other = obj as TargetGenerateSettings;
if (other == null) return false;
return other.InputFiles.Equals(InputFiles);
}
@@ -34,6 +34,7 @@ protected void ThrowIfDepgraphGenerates()
}
+
}
}
@@ -14,11 +14,11 @@ class DifferentInputFiles : DependencyGraphTests
public void WhenInputFilesChangeTargetGetsRebuilt()
{
int invocationCount = 0;
- Action<string, TargetBuildSettings> action = (t, s) =>
- {
- File.WriteAllText(t, "Hello");
- invocationCount++;
- };
+ var action = new SimpleAction((t,s) =>
+ {
+ File.WriteAllText(t, "Hello");
+ invocationCount++;
+ }, "hash");
File.WriteAllText("file1", "one");
File.WriteAllText("file2", "two");
@@ -33,12 +33,12 @@ public void WhenInputFilesChangeTargetGetsRebuilt()
Assert.AreEqual(2, invocationCount);
}
- private static TargetBuildInstructions MakeInstructions(Action<string, TargetBuildSettings> action, params string[] inputFiles)
+ private static TargetGenerateInstructions MakeInstructions(ITargetGeneratingAction action, params string[] inputFiles)
{
- var targetBuildInstructions = new TargetBuildInstructions();
+ var targetBuildInstructions = new TargetGenerateInstructions();
targetBuildInstructions.Action = action;
- targetBuildInstructions.Settings = new TargetBuildSettings();
+ targetBuildInstructions.Settings = new TargetGenerateSettings();
targetBuildInstructions.Settings.InputFiles = new HashSet<string>(inputFiles);
return targetBuildInstructions;
}
View
@@ -0,0 +1,26 @@
+using System;
+
+namespace bs.Tests
+{
+ class SimpleAction : ITargetGeneratingAction
+ {
+ private readonly Action<string, TargetGenerateSettings> _invoke;
+ private readonly string _actionHash;
+
+ public SimpleAction(Action<string, TargetGenerateSettings> invoke, string actionHash)
+ {
+ _invoke = invoke;
+ _actionHash = actionHash;
+ }
+
+ public void Invoke(string target, TargetGenerateSettings instructions)
+ {
+ _invoke(target, instructions);
+ }
+
+ public string GetActionHash()
+ {
+ return _actionHash;
+ }
+ }
+}
@@ -63,10 +63,11 @@ public void WillNotRegeneratesWhenTargetGotManuallyUpdated()
private void SetupSimpleCopyDepGraph()
{
- _depGraph.RegisterTarget(defaulttargetFile, new TargetBuildInstructions()
+ var action = new SimpleAction((target, settings) => File.Copy(settings.InputFiles.Single(), target, true), "hash");
+ _depGraph.RegisterTarget(defaulttargetFile, new TargetGenerateInstructions()
{
- Action = (target,settings) => File.Copy(settings.InputFiles.Single(), target, true),
- Settings = new TargetBuildSettings() { InputFiles = new HashSet<string>(new[]{defaultSourceFile})}
+ Action = action,
+ Settings = new TargetGenerateSettings() { InputFiles = new HashSet<string>(new[]{defaultSourceFile})}
});
}
}
@@ -37,12 +37,23 @@ public void WillGenerateTargetIfItWasNeverBuiltBefore()
FileAssert.Contains(defaulttargetFile, "Hello");
}
+ [Test]
+ public void RegenerateWhenActionHashChanges()
+ {
+
+ }
+
+
+
+
private void SetupGraphWithOneTargetWithoutSources()
{
- _depGraph.RegisterTarget(defaulttargetFile, new TargetBuildInstructions()
+ var simpleAction = new SimpleAction((t,s) => File.WriteAllText(t, "Hello"), "hash1");
+
+ _depGraph.RegisterTarget(defaulttargetFile, new TargetGenerateInstructions()
{
- Action = (target,sources) => File.WriteAllText(target, "Hello"),
- Settings = new TargetBuildSettings() { InputFiles = new HashSet<string>()}
+ Action = simpleAction,
+ Settings = new TargetGenerateSettings() { InputFiles = new HashSet<string>()}
});
}
View
@@ -50,8 +50,8 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RecursiveIncludeScanner.cs" />
- <Compile Include="TargetBuildInstructions.cs" />
- <Compile Include="TargetBuildSettings.cs" />
+ <Compile Include="TargetGenerateInstructions.cs" />
+ <Compile Include="TargetGenerateSettings.cs" />
<Compile Include="CCompilerTask.cs" />
<Compile Include="Tests\CCompilerTests.cs" />
<Compile Include="Tests\DependencyGraphTests.cs" />
@@ -60,7 +60,9 @@
<Compile Include="Tests\DifferentInputFiles.cs" />
<Compile Include="IncludeScanner.cs" />
<Compile Include="Tests\IncludeScannerTest.cs" />
+ <Compile Include="Tests\SimpleAction.cs" />
<Compile Include="Tests\SimpleCopyDepGraph.cs" />
+ <Compile Include="ITargetGeneratingAction.cs" />
<Compile Include="Tests\TargetWithoutSources.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

0 comments on commit 8dc7dd9

Please sign in to comment.