Permalink
Browse files

when the list of inputfiles changes, we should always regen.

  • Loading branch information...
1 parent 591c5e7 commit 21854cdbda33d0041bdd9ab4f662e0cf41f0e345 Lucas Meijer committed Apr 14, 2012
View
@@ -3,7 +3,7 @@
namespace bs
{
- internal class BuildHistory
+ public class BuildHistory
{
private readonly Dictionary<string, GenerationRecord> _records = new Dictionary<string, GenerationRecord>();
View
@@ -9,7 +9,16 @@ public class DependencyGraph
{
readonly Dictionary<string, TargetBuildInstructions> _graph = new Dictionary<string, TargetBuildInstructions>();
public Action<string, TargetBuildInstructions> GenerateCallback = (s, i) => { };
- private readonly BuildHistory _buildHistory = new BuildHistory();
+ private readonly BuildHistory _buildHistory;
+
+ public DependencyGraph() : this(new BuildHistory())
+ {
+ }
+
+ public DependencyGraph(BuildHistory history)
+ {
+ _buildHistory = history;
+ }
public void RequestTarget(string targetFile)
{
@@ -31,7 +40,10 @@ private bool NeedToGenerate(string targetFile, TargetBuildSettings buildSettings
if (!File.Exists(targetFile))
return true;
-
+
+ if (!buildSettings.Equals(recordOfLastBuild.Settings))
+ return true;
+
if (File.GetLastWriteTimeUtc(targetFile) < recordOfLastBuild.ModificationTimeOfTargetFile())
return true;
View
@@ -4,7 +4,7 @@
namespace bs
{
- internal class GenerationRecord
+ public class GenerationRecord
{
public string TargetFile { get; private set; }
public TargetBuildSettings Settings { get; private set; }
@@ -1,5 +1,4 @@
using System;
-using System.Collections.Generic;
namespace bs
{
@@ -8,9 +7,4 @@ public class TargetBuildInstructions
public Action<string, TargetBuildSettings> Action;
public TargetBuildSettings Settings;
}
-
- public class TargetBuildSettings
- {
- public HashSet<string> InputFiles;
- }
}
View
@@ -0,0 +1,23 @@
+using System.Collections.Generic;
+
+namespace bs
+{
+ public class TargetBuildSettings
+ {
+ 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;
+ if (other == null) return false;
+ return other.InputFiles.Equals(InputFiles);
+ }
+
+ public override int GetHashCode()
+ {
+ return (InputFiles != null ? InputFiles.GetHashCode() : 0);
+ }
+ }
+}
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.IO;
using NUnit.Framework;
@@ -17,18 +18,22 @@ public void Setup()
Directory.CreateDirectory(dirName);
Directory.SetCurrentDirectory(dirName);
- _depGraph = new DependencyGraph();
+ _buildHistory = new BuildHistory();
+ _depGraph = new DependencyGraph(_buildHistory);
}
protected const string defaultSourceFile = "input.txt";
protected const string defaulttargetFile = "output.txt";
protected DependencyGraph _depGraph;
+ protected BuildHistory _buildHistory;
protected void ThrowIfDepgraphGenerates()
{
_depGraph.GenerateCallback += (target, instructions) => { throw new InvalidOperationException(); };
}
+
+
}
}
@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using NUnit.Framework;
+
+namespace bs.Tests
+{
+ [TestFixture]
+ class DifferentInputFiles : DependencyGraphTests
+ {
+ [Test]
+ public void WhenInputFilesChangeTargetGetsRebuilt()
+ {
+ int invocationCount = 0;
+ Action<string, TargetBuildSettings> action = (t, s) =>
+ {
+ File.WriteAllText(t, "Hello");
+ invocationCount++;
+ };
+
+ File.WriteAllText("file1", "one");
+ File.WriteAllText("file2", "two");
+
+ _depGraph.RegisterTarget(defaulttargetFile, MakeInstructions(action, "file1" ));
+ _depGraph.RequestTarget(defaulttargetFile);
+ Assert.AreEqual(1, invocationCount);
+
+ _depGraph = new DependencyGraph(_buildHistory);
+ _depGraph.RegisterTarget(defaulttargetFile, MakeInstructions(action, "file1","file2"));
+ _depGraph.RequestTarget(defaulttargetFile);
+ Assert.AreEqual(2, invocationCount);
+ }
+
+ private static TargetBuildInstructions MakeInstructions(Action<string, TargetBuildSettings> action, params string[] inputFiles)
+ {
+ var targetBuildInstructions = new TargetBuildInstructions();
+
+ targetBuildInstructions.Action = action;
+ targetBuildInstructions.Settings = new TargetBuildSettings();
+ targetBuildInstructions.Settings.InputFiles = new HashSet<string>(inputFiles);
+ return targetBuildInstructions;
+ }
+ }
+}
@@ -46,5 +46,8 @@ private void SetupGraphWithOneTargetWithoutSources()
});
}
+
+
+
}
}
View
@@ -50,9 +50,11 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TargetBuildInstructions.cs" />
+ <Compile Include="TargetBuildSettings.cs" />
<Compile Include="Tests\DependencyGraphTests.cs" />
<Compile Include="DependencyGraph.cs" />
<Compile Include="MissingDependencyException.cs" />
+ <Compile Include="Tests\DifferentInputFiles.cs" />
<Compile Include="Tests\SimpleCopyDepGraph.cs" />
<Compile Include="Tests\TargetWithoutSources.cs" />
</ItemGroup>

0 comments on commit 21854cd

Please sign in to comment.