Skip to content

Commit

Permalink
refactor so that when you register a target, you provide a TargetGene…
Browse files Browse the repository at this point in the history
…ratingAction instead of a System.Action
  • Loading branch information
lucasmeijer committed Apr 14, 2012
1 parent 8fcbfed commit 8dc7dd9
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 43 deletions.
4 changes: 0 additions & 4 deletions CCompilerTask.cs
Expand Up @@ -23,9 +23,5 @@ public IEnumerable<string> GetInputFiles()
foreach (var file in scanner.GetFilesIncludedBy(_sourceFile))
yield return file;
}




}
}
18 changes: 9 additions & 9 deletions DependencyGraph.cs
Expand Up @@ -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())
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
6 changes: 4 additions & 2 deletions GenerationRecord.cs
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions ITargetGeneratingAction.cs
@@ -0,0 +1,8 @@
namespace bs
{
public interface ITargetGeneratingAction
{
void Invoke(string target, TargetGenerateSettings instructions);
string GetActionHash();
}
}
10 changes: 0 additions & 10 deletions TargetBuildInstructions.cs

This file was deleted.

10 changes: 10 additions & 0 deletions TargetGenerateInstructions.cs
@@ -0,0 +1,10 @@
using System;

namespace bs
{
public class TargetGenerateInstructions
{
public ITargetGeneratingAction Action;
public TargetGenerateSettings Settings;
}
}
4 changes: 2 additions & 2 deletions TargetBuildSettings.cs → TargetGenerateSettings.cs
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Tests/DependencyGraphTests.cs
Expand Up @@ -34,6 +34,7 @@ protected void ThrowIfDepgraphGenerates()
}




}
}
16 changes: 8 additions & 8 deletions Tests/DifferentInputFiles.cs
Expand Up @@ -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");
Expand All @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions Tests/SimpleAction.cs
@@ -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;
}
}
}
7 changes: 4 additions & 3 deletions Tests/SimpleCopyDepGraph.cs
Expand Up @@ -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})}
});
}
}
Expand Down
17 changes: 14 additions & 3 deletions Tests/TargetWithoutSources.cs
Expand Up @@ -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>()}
});
}

Expand Down
6 changes: 4 additions & 2 deletions bs.csproj
Expand Up @@ -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" />
Expand All @@ -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" />
Expand Down

0 comments on commit 8dc7dd9

Please sign in to comment.